您好,欢迎访问三七文档
1.取h=0.2用Euler格式、隐式Euler格式和改进的Euler格式解微分方程1)0(/2'yyxyy(0≤x≤1),并比较精度(解析解y=x21)。1.运用欧拉法,其主程序段(M函数)如下。function[x,y]=naler(dyfun,xspan,y0,h)x=xspan(1):h:xspan(2);y(1)=y0;forn=1:length(x)-1;y(n+1)=y(n)+h*feval(dyfun,x(n),y(n));endx=x';y=y';运行过程及中间结果:clear;dyfun=inline('y-2*x/y');[x,y]=naler(dyfun,[0,1],1,0.2);p=[x,y]p=01.00000.20001.20000.40001.37330.60001.53150.80001.68111.00001.82692.运用隐式的欧拉法,其主程序段(M函数)如下。function[x,y]=yseuler(dyfun,xspan,y0,h)x=xspan(1):h:xspan(2);y(1)=y0;forn=1:length(x)-1;y(n+1)=iter(dyfun,x(n+1),y(n),h);endx=x';y=y'functiony=iter(dyfun,x,y,h)y0=y;e=1e-4;k=1e+4;y=y+h*feval(dyfun,x,y);y1=y+2*e;k=1;whileabs(y-y1)ey1=y;y=y0+h*feval(dyfun,x,y);k=k+1;ifkkerror('µü´ú·¢É¢');endend[x,y]=yseuler(dyfun,[0,1],1,0.2);[x,y]y=1.00001.16411.30141.41461.50191.5561ans=01.00000.20001.16410.40001.30140.60001.41460.80001.50191.00001.55613.运用改进的欧拉法,其主程序段(M函数)如下。function[x,y]=zseuler(dyfun,xspan,y0,h)x=xspan(1):h:xspan(2);y(1)=y0;forn=1:length(x)-1;k1=feval(dyfun,x(n),y(n));y(n+1)=y(n)+h*k1;k2=feval(dyfun,x(n+1),y(n+1));y(n+1)=y(n)+h*(k1+k2)/2;endx=x';y=y';[x,y]=zseuler(dyfun,[0,1],1,0.2);[x,y]ans=01.00000.20001.18670.40001.34830.60001.49370.80001.62791.00001.7542234num=[5100];den=[183280100];sys=tf(num,den);step(sys);axis=([01001.2]);gridon;5无干扰时有干扰时
本文标题:欧拉法的应用
链接地址:https://www.777doc.com/doc-2363826 .html