资源描述:
《数值分析作业8》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、9-1使用四阶Adams预测校正算法和经典RK算法。RK算法functionyrk4=rk4(fxy,a,b,y0,n)formatlong;h=(b-a)/n;x=a:h:b;yrk4=zeros(n,1);yrk4(1)=y0;fori=1:n-1k1=fxy(x(i),yrk4(i));k2=fxy(x(i)+h/2,yrk4(i)+h*k1/2);k3=fxy(x(i)+h/2,yrk4(i)+h*k2/2);k4=fxy(x(i)+h,yrk4(i)+h*k3);yrk4(i+1)=yrk4(i)+h*(k1+2*k2+2*k3+k4)/
2、6;endfunctionz=dm(x,y)z=-y+2*cos(x);>>yrk4=rk4(@dm,0,pi,1,10)yrk4=1.0000000000000001.2600370910600211.3967362675431841.3967155733483761.2599763853528780.9999032140839190.6419534874237690.221165565757292-0.221271082024303-0.642047809096457精确值x=0:pi/10:pi;y=cos(x)+sin(x)>>deey=C
3、olumns1through41.0000000000000001.2600735106701011.3968022466674211.396802246667421Columns5through81.2600735106701011.0000000000000000.6420395219202060.221231742082474Columns9through11-0.221231742082474-0.642039521920206-1.0000000000000009-2.functionre(a,b,c,d,max)f1=@(t,x,y,z
4、)-y-z;f2=@(t,x,y,z)x+a*y;f3=@(t,x,y,z)(x-c)*z+b;x(1)=d;y(1)=d;z(1)=d;h=0.1;i=1;fort=0:h:max-hk1(1)=f1(t,x(i),y(i),z(i));k1(2)=f2(t,x(i),y(i),z(i));k1(3)=f3(t,x(i),y(i),z(i));k2(1)=f1(t+h,x(i)+h*k1(1),y(i)+h*k1(1),z(i)+h*k1(1));k2(2)=f2(t+h,x(i)+h*k1(2),y(i)+h*k1(2),z(i)+h*k1(2
5、));k2(3)=f3(t+h,x(i)+h*k1(3),y(i)+h*k1(3),z(i)+h*k1(3));x(i+1)=x(i)+h/2*(k1(1)+k2(1));y(i+1)=y(i)+h/2*(k1(2)+k2(2));z(i+1)=z(i)+h/2*(k1(3)+k2(3));i=i+1;endfigure(1)plot3(0:h:max,x,y,'-');figure(2)plot(x,y);endre(0.2,0.8,4.5,1,1000)9-3functionde(h,max)f1=@(t,x,y,z,m,n,p)m;f2=@(
6、t,x,y,z,m,n,p)n;f3=@(t,x,y,z,m,n,p)p;f4=@(t,x,y,z,m,n,p)2*x*z*m+3*x^2*y*t^2;f5=@(t,x,y,z,m,n,p)exp(y)*n+4*x*z*t^2;f6=@(t,x,y,z,m,n,p)2*t*p+2*t*exp(x*y);x(1)=3;y(1)=3;z(1)=3;m(1)=2;n(1)=2;p(1)=2;i=1;fort=1:h:max-hk1(1)=f1(t,x(i),y(i),z(i),m(i),n(i),p(i));k1(2)=f2(t,x(i),y(i),z(
7、i),m(i),n(i),p(i));k1(3)=f3(t,x(i),y(i),z(i),m(i),n(i),p(i));k1(4)=f4(t,x(i),y(i),z(i),m(i),n(i),p(i));k1(5)=f5(t,x(i),y(i),z(i),m(i),n(i),p(i));k1(6)=f6(t,x(i),y(i),z(i),m(i),n(i),p(i));k2(1)=f1(t+h,x(i)+h*k1(1),y(i)+h*k1(1),z(i)+h*k1(1),m(i)+h*k1(1),n(i)+h*k1(1),p(i)+h*k1(1)
8、);k2(2)=f2(t+h,x(i)+h*k1(2),y(i)+h*k1(2),z(i)+h*k1(2),m(i)+h*