Octave Matlab二分法

Octave Matlab二分法,octave,gnu,Octave,Gnu,我正试图写一个程序来寻找GNU八度音阶的根 %this is program to calculate root of Non linear differential eqn display('you can change function in bsfun.m') a=input("enter maximum value\t"); b=input("entr minimum value\t"); e=input("enter tolerenc

我正试图写一个程序来寻找GNU八度音阶的根

%this is program to calculate root of Non linear differential eqn

display('you can change function in bsfun.m')
a=input("enter maximum value\t");
b=input("entr minimum value\t");
e=input("enter tolerence\t");
if (bsfun(a)*bsfun(b)>0)
  display("you have not assumed right values")
endif
 
while ((b-a)>e)
  c=(b+a)/2;    %find middile point
  disp c
  if (bsfun(c)==0)    %check middile point is the root 
    break
  elseif (bsfun(a)*bsfun(c)<0)    % Decide the side to repeat the steps
    b=c;
  else 
    a=c;
  endif
end
fprintf("The root is %f\n",c);
但它不起作用 我是编程新手
也可以在第一个程序上定义用户定义的函数(不需要像c中那样制作单独的文件)

问题解决了将abs(b-a)设置为
a>b
b-a
始终是负数,所以您应该使用
a-b
。您还应该添加一个真正
a>b
yes的控件,这是一个愚蠢的错误
%bisection Function
function out = bsfun(x) 
  out = (x.^2)+(2.1*x)-8.82;