Amy_is_growing
Amy_is_growing
2017-10-07 13:31

Matlab二分法,总是出现错误,各位大神请赐教!

  • matlab
  • 函数
 %二分法
function [xstar,index,it]=bisect(fun,a,b,ep)
if nargin<4 
    ep=0.00001;
end
fa=feval(fun,a);fb=feval(fun,b);
if fa*fb>0
    xstar=[fa,fb];index=0;it=0;
    return
end
k=0;
while abs(b-a)/2>=ep
    x=(a+b)/2;
    fx=feval(fun,x);
    if fx*fa<0
        b=x;fb=fx;
    else
        a=x;fa=fx;
    end
    k=k+1;
end
xstar=(a+b)/2;index=1;it=k;
end

function f=fun2(x)
f=exp(x)+10*x-2;
[xstar,index,it]=bisect('fun2',0,1,0.001);
end

出错:>> fun2(1)
Maximum recursion limit of 1000 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware
that exceeding your available stack space can crash MATLAB and/or your computer.

Error in bisect

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答