欢迎来到天天文库
浏览记录
ID:42016195
大小:277.97 KB
页数:14页
时间:2019-09-06
《计算方法上机--非线性方程数值方法》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、非线性方程数值方法I、固定点迭代:求解方程X=g(x)的近似值,初始值为无0,迭代式为£+1二g(£)function[k,xl,err,x]=fixpt(g,xO,tol,maxl)%Input-gistheiterationfunctioninputasastring1g1%-xOistheinitialguessforthefixedpoint%-tolisthetolerance%-maxiisthemaximumnumberofiterations%Output-kisthenumberofiterationthatwereca
2、rriedout%-xlistheapproximationtothefixedpoint%-erristheerrorintheapproximation%-xcontainsthesequence{xn}x(1)=x0;fork=2:maxix(k)=feval(g,x(k-1));err=abs(x(k)-x(k-1));relerr=err/(abs(x(k))+eps);xl=x(k);if(err3、(relerr4、tionsexceeded1)endx=x1;例如:求解x=e~xg=inline(1exp(_x)1);[k,pzerr,P]=fixpt(gz0.5,0.01,20)x=0.5:0.01:1;y=exp(-x);plot(x’y,O)holdonplot(x,x,1o1)holdonplot(x,x,1r-1)2、二分法:求解方程/(x)=0在区间[a,b]内的一个根。前提条件是/(兀)是连续的,月./⑺)与/(/?)的符号相反。function[c,err,yc]=bisect(fza,b,delta)%Input-fisthefu5、nctioninputasastring1f1%-aandbaretheleftandrightendpoint%-deltaisthetolerance%Output-cisthezero%-yc=f(c)%-erristheerrorestimateforcya=feval(fza),yb=feval(f,b)ifya*yb>0zbreak,endmaxl=l+round((log(b-a)-log(delta))/log(2))fork=l:maxic=(a+b)/2;yc=feval(f,c);ifyc==0a=c;b=c;els6、eifyb*yc>0b=c;yb=yc;elsea=c;ya=yc;endifb-a022x=0:0.01:0.5*pi;y=x.*sin(x)-1;plot(x,yJr—)holdonplot(x,0,*g-')f=inline(1x*sin(x)-11)[c,err,yc]=bisect(f,0,2,0.01)3、牛顿一拉夫申迭代:使用初始近似值兀o,7、利用迭代式耳/(无-Jk广(Z函数/(%)=0的根的近似值。function[x0,errzk,y]=newton(f,dfzx0zdelta,epsilon,maxi)%Input-fis%-dfis%-xOistheobjectfunctioninputthederivativeoffinputtheinitialapproximationasastring1f1asastring1dftoazerooff-deltaisthetoleranceforxO%-epsilonisthetoleranceforthefunctionval8、uesy%-maxiisthemaximumnumberofiterations%Output-xistheNewton-Raphsonapproximationtothezero%-erristheerrorestimateforxO%-kisthenumberofiterations%-yisthefunctionvaluef(xO)fork=l:maxixl=x0-feval(fzxO)/feval(df,xO);err=abs(xl-xO);relerr=2*err/(abs(xl)+delta);x0=xl;y=feval(f,9、xO);if(err10、(relerr11、(abs(y)
3、(relerr4、tionsexceeded1)endx=x1;例如:求解x=e~xg=inline(1exp(_x)1);[k,pzerr,P]=fixpt(gz0.5,0.01,20)x=0.5:0.01:1;y=exp(-x);plot(x’y,O)holdonplot(x,x,1o1)holdonplot(x,x,1r-1)2、二分法:求解方程/(x)=0在区间[a,b]内的一个根。前提条件是/(兀)是连续的,月./⑺)与/(/?)的符号相反。function[c,err,yc]=bisect(fza,b,delta)%Input-fisthefu5、nctioninputasastring1f1%-aandbaretheleftandrightendpoint%-deltaisthetolerance%Output-cisthezero%-yc=f(c)%-erristheerrorestimateforcya=feval(fza),yb=feval(f,b)ifya*yb>0zbreak,endmaxl=l+round((log(b-a)-log(delta))/log(2))fork=l:maxic=(a+b)/2;yc=feval(f,c);ifyc==0a=c;b=c;els6、eifyb*yc>0b=c;yb=yc;elsea=c;ya=yc;endifb-a022x=0:0.01:0.5*pi;y=x.*sin(x)-1;plot(x,yJr—)holdonplot(x,0,*g-')f=inline(1x*sin(x)-11)[c,err,yc]=bisect(f,0,2,0.01)3、牛顿一拉夫申迭代:使用初始近似值兀o,7、利用迭代式耳/(无-Jk广(Z函数/(%)=0的根的近似值。function[x0,errzk,y]=newton(f,dfzx0zdelta,epsilon,maxi)%Input-fis%-dfis%-xOistheobjectfunctioninputthederivativeoffinputtheinitialapproximationasastring1f1asastring1dftoazerooff-deltaisthetoleranceforxO%-epsilonisthetoleranceforthefunctionval8、uesy%-maxiisthemaximumnumberofiterations%Output-xistheNewton-Raphsonapproximationtothezero%-erristheerrorestimateforxO%-kisthenumberofiterations%-yisthefunctionvaluef(xO)fork=l:maxixl=x0-feval(fzxO)/feval(df,xO);err=abs(xl-xO);relerr=2*err/(abs(xl)+delta);x0=xl;y=feval(f,9、xO);if(err10、(relerr11、(abs(y)
4、tionsexceeded1)endx=x1;例如:求解x=e~xg=inline(1exp(_x)1);[k,pzerr,P]=fixpt(gz0.5,0.01,20)x=0.5:0.01:1;y=exp(-x);plot(x’y,O)holdonplot(x,x,1o1)holdonplot(x,x,1r-1)2、二分法:求解方程/(x)=0在区间[a,b]内的一个根。前提条件是/(兀)是连续的,月./⑺)与/(/?)的符号相反。function[c,err,yc]=bisect(fza,b,delta)%Input-fisthefu
5、nctioninputasastring1f1%-aandbaretheleftandrightendpoint%-deltaisthetolerance%Output-cisthezero%-yc=f(c)%-erristheerrorestimateforcya=feval(fza),yb=feval(f,b)ifya*yb>0zbreak,endmaxl=l+round((log(b-a)-log(delta))/log(2))fork=l:maxic=(a+b)/2;yc=feval(f,c);ifyc==0a=c;b=c;els
6、eifyb*yc>0b=c;yb=yc;elsea=c;ya=yc;endifb-a022x=0:0.01:0.5*pi;y=x.*sin(x)-1;plot(x,yJr—)holdonplot(x,0,*g-')f=inline(1x*sin(x)-11)[c,err,yc]=bisect(f,0,2,0.01)3、牛顿一拉夫申迭代:使用初始近似值兀o,
7、利用迭代式耳/(无-Jk广(Z函数/(%)=0的根的近似值。function[x0,errzk,y]=newton(f,dfzx0zdelta,epsilon,maxi)%Input-fis%-dfis%-xOistheobjectfunctioninputthederivativeoffinputtheinitialapproximationasastring1f1asastring1dftoazerooff-deltaisthetoleranceforxO%-epsilonisthetoleranceforthefunctionval
8、uesy%-maxiisthemaximumnumberofiterations%Output-xistheNewton-Raphsonapproximationtothezero%-erristheerrorestimateforxO%-kisthenumberofiterations%-yisthefunctionvaluef(xO)fork=l:maxixl=x0-feval(fzxO)/feval(df,xO);err=abs(xl-xO);relerr=2*err/(abs(xl)+delta);x0=xl;y=feval(f,
9、xO);if(err10、(relerr11、(abs(y)
10、(relerr11、(abs(y)
11、(abs(y)
此文档下载收益归作者所有