4、mumnumberofiterationsexceeded')endx=x';例如:求解 g=inline('exp(-x)');[k,p,err,P]=fixpt(g,0.5,0.01,20)x=0.5:0.01:1;y=exp(-x);plot(x,y,'o')holdonplot(x,x,'o')holdonplot(x,x,'r-')2、二分法:求解方程在区间内的一个根。前提条件是是连续的,且与的符号相反。function[c,err,yc]=bisect(f,a,b,delta)%Input-fi
5、sthefunctioninputasastring'f'%-aandbaretheleftandrightendpoint%-deltaisthetolerance%Output-cisthezero%-yc=f(c)%-erristheerrorestimateforcya=feval(f,a),yb=feval(f,b)ifya*yb>0,break,endmaxl=1+round((log(b-a)-log(delta))/log(2))fork=1:maxlc=(a+b)/2;yc=feval(f
6、,c);ifyc==0a=c;b=c;14elseifyb*yc>0b=c;yb=yc;elsea=c;ya=yc;endifb-a0x=0:0.01:0.5*pi;y=x.*sin(x)-1;plot(x,y,'r-')holdonplot(x,0,'g-')f=inline('x*sin(x)-1')[c,err,yc]=bise
7、ct(f,0,2,0.01)3、牛顿-拉夫申迭代:使用初始近似值,利用迭代式,计算函数的根的近似值。function[x0,err,k,y]=newton(f,df,x0,delta,epsilon,maxl)%Input-fistheobjectfunctioninputasastring'f'%-dfisthederivativeoffinputasastring'df'%-x0istheinitialapproximationtoazerooff%-deltaisthetoleranceforx0%-
8、epsilonisthetoleranceforthefunctionvaluesy%-maxlisthemaximumnumberofiterations%Output-xistheNewton-Raphsonapproximationtothezero%-erristheerrorestimateforx0%-kisthenumberofiterations%-yisthefunctionvaluef(x0)fork=1:maxlx1=x0-feval(f,x0)/feval(df,x0);err=ab
9、s(x1-x0);relerr=2*err/(abs(x1)+delta);x0=x1;y=feval(f,x0);if(err10、(relerr11、(abs(y)