资源描述:
《欧拉法matlab程序》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、1.Euler法function[x,y]=naeuler(dyfun,xspan,yO,h)x=xspan(1):h:xspan(2);y(1)=y0;forn=l:length(x)-1y(n+1)=y(n)+h*feval(dyfun,x(n),y(n));endx=x丨;y=y1;xl=0:0.2:l;yl=(l+2*xl)•八0.5;plot(x,y,xl,yl)»dyfun=inline(,y-2*x/y,);[x,y]=naeuler(dyfun,[0,1],1,0.2);[x,y]ans=01.00000.20001.2
2、0000.40001.37330.60001.5315().80001.68111.00001.82692.隐成Euler法function[x,y]=naeulerb(dyfun,xspan,yO,h)x=xspan(1):h:xspan(2);y(1)=y0;forn=l:length(x)-1y(n+1)=iter(dyfun,x(n+1),y(n),h);endx=x丨;y=y1;xl=0:0•2:1;yl=(1十2*xl)•八0.5;plot(x,y,xl,yl)functiony=iter(dyfun,x,y,h)yO=y;
3、e=le-4;K=le+4;y=y+h*feval(dyfun,x,y);yl=y+2*e;k=l;whileabs(y-yl)>eyi=y;y=yO+h*feval(dyfun,x,y);k=k+l;ifk>Kerror('迭代发散1〉;endend»dyfun=inline(’y-2*x/y’);[x,y]=naeulerb(dyfun,[0,1],1,0.2);[x,y]ans=01.00000.20001.16410.40001.30140.60001.41460.80001.50191.00001.55613.改进Euler法
4、function[x,y]=naeuler2(dyfun,xspan,yO,h)x=xspan(1):h:xspan(2);y(l)=y0;forn=l:length(x)-1kl=feval(dyfun,x(n),y(n));y(n+1)=y(n)+h*kl;k2=feval(dyfun,x(n+1),y(n+1));y(n+1)=y(n)+h*(kl+k2)/2;endx=x1;y=y1;xl=0:0.2:l;yl=(l+2*xl)•八0.5;plot(x,y,xl,yl)>>dyfun=inline(1y-2*x/y1);[x,y
5、]=naeuler2(dyfun,[◦,1],1,0•2);[x,y]ans=01.00000.20001.18670.40001.34830.60001.49370.80001.62791.00001.7542