matlab二分法tol,Matlab二分法

改过来了

%   BISECTION finds function zeros

%   [ZERO,RES,NITER]=BISECTION(FUN,A,B,TOL,NITER_MAX)

%   Tries to find a zero of the continuous fuction in the interval [A,B]

%   using the bisection method.

function [zero,res,niter]=...

bisection(fun,a,b,tol,niter_max,varargin)

x=[a,(a+b)*0.5,b];

fx=feval(fun,x,varargin{:});

if fx(1)*fx(3)>0

error(['The sign of the function 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

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(3)=fx(2);

fx(2)=feval(fun,x(2),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(1)=fx(2);

fx(2)=feval(fun,x(2),varargin{:});

I=(x(3)-x(1))*0.5;

else

I=0;

end

end

if I>tol

fprintf(['\n Bisection stopped without converqing desired '...

'tolerance because the max number of iterations was reached.\n'])

end

zero=x(2);x=x(2);res=feval(fun,x);

f5c3d56501a3d0261ce0cb81cbf824a7.gif

2009-4-2 09:19 上传

点击文件名下载附件

1.26 KB, 下载次数: 3783

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值