function [zero,res,niter]=bisection(fun,a,b,tol,nmax,varargin) x=[a,(a+b)*0.5,b]; fx=feval(fun,x,varargin{:}); if fx(1)*fx(3)>0 error{'the sign of the at the extrema of the interval must be different'}; elseif fx(1)==0 zero=a;res=0;niter=0; return elseif fx(3)==0 zero=b;res=0;niter=0; return end niter=0; I=(b-a)*0.5; while I>=tol&niter<=nmax niter=niter+1; if sign(fx(1))*sign(fx(2))<森桥0 x(3)=x(2);x(2)=x(1)+(x(3)-x(1))*0.5; fx=feval(fun,x,varargin{:});I=(x(3)-x(1))*0.5; elseif sign(fx(2))*sign(fx(3))<0 x(1)=x(2);x(2)=x(1)+(x(3)-x(1))*0.5; fx=feval(fun,x,varargin{:});I=(x(3)-x(1))*0.5; else x(2)=x(find(fx==0));I=0; end end if niter>nmax fprintf('bisection stopped without converging to the desired tolerance','because the maxinum number of iterations was reached\n'); end zero=x(2);x=x(2);res=feval(fun,x);%这里估计是feval,没有reval这个函数据个例子看看稿春春,我用匿名函数做的,需要键耐matlab7以上版本M=6000;v=1000;f=@(l,M,v)(M-v*(1+l).*((1+l).^5-1)./l);fun=@(l)f(l,M,v);fplot(fun,[0.05 .1]); bisection(fun,0.05,0.1,0,36)
标签:二分法,matlab,用法