您好,欢迎访问三七文档
数学实验报告一【实验目的】掌握用Matlab求微分方程数值解,比较各种解法与不同步长的优缺点,掌握Matlab绘图的基本知识。二【实验任务】1、用前进Euler公式求解并和精确解比较:(1)2dyxydx,[0,2]x(0)1y(2)sindyyxdx,[0,1]x(0)1y2、分析人口增长模型:1mdxxrxdtx,[0,]xn0(0)yx其中,6600.31,3.910,19710mrxx,分别取10,100,1000n进行分析。三【实验程序及运行结果】第一题(1):①定义前进欧拉函数QJEuleraaa1:functiony=QJEuleraaa1(h)a=0;b=2;y0=1;N=(b-a)/h;y=zeros(N+1,1);x=a:h:b;y(1)=y0;fori=2:N+1y(i)=y(i-1)+h*2*x(i-1)*y(i-1);end②取步长h=0.1,0.01,0.001计算近似值:x=0:0.1:2;z=zeros(21,1);fori=1:21z(i)=exp(x(i).^2);endy1=QJEuleraaa1(0.1);y2=QJEuleraaa1(0.01);y3=QJEuleraaa1(0.001);y22=zeros(21,1);fori=1:21y22(i)=y2(10*(i-1)+1);endy33=zeros(21,1);fori=1:21y33(i)=y3(100*(i-1)+1);endplot(x,y1,'b-o',x,y22,'g-x',x,y33,'k-+',x,z,'r');legend('h=0.1','h=0.01','h=0.001','exact');axis([0,2,-inf,inf])xlabel('x')ylabel('exactsolutionandapproximatesolutions')注:该MATLAB程序是是描述三个不同步长(h=0.1;0.01;0.001)的前进欧拉公式所求的解与精确值并以图像的形式给出三个步长的直观效果。如图1图1结果说明:由图1可以直观的了解到,随着步长h的减小,由前进欧拉公式算得的数值近似值与精确值的误差会越来越小。第一题(1)当h=0.1时a=0;b=2;h=0.1;y0=1;N=(b-a)/h;y=zeros(N+1,1);x=a:h:b;y(1)=y0;fori=2:N+1y(i)=y(i-1)+h*2*x(i-1)*y(i-1);endz=zeros(N+1,1);fori=1:N+1z(i)=exp(x(i).^2);endyzerr=abs(y-z);e=y-z;subplot(2,1,1)plot(x,y,'b',x,z,'r');axis([0,2,-inf,inf])legend('QJEuler','exact');xlabel('x')ylabel('exactsolutionandapproximatesolution')subplot(2,1,2)bar(x,e)xlabel('x')ylabel('error')figure(2);plot(x,err,'b');axis([0,2,-inf,inf])xlabel('x')ylabel('absoluteerror')注:该程序描述了h=0.1时前进欧拉公式所求的解与精确解的函数图象以及残差图和近似解相对于精确解的绝对误差的曲线图像。同理只需将该程序中的h依次改为0.01;0.001即可得到步长为0.01;0.001时前进欧拉公式所求的解与精确解的函数图象以及残差图。h=0.1(残差图)h=0.1(绝对误差图)h=0.01(残差图)h=0.01(绝对误差图)h=0.001(残差图)h=0.001(绝对误差图)根据以上程序运行结果得出不同步长的近似值与精确解的图表自变量值近似值(h=0.1)近似值(h=0.01)近似值(h=0.001)精确值011110.111.0090348761.0099485041.0100501670.21.021.0386800211.0405971261.0408107740.31.06081.0907105921.0938264961.0941742840.41.1244481.1683480341.1729917191.1735108710.51.214403841.2765938521.2832769941.2840254170.61.3358442241.4227677511.4322640531.4333294150.71.4961455311.6173314281.6308021051.632316220.81.7056059051.8751277251.8943193251.8964808790.91.978502852.2172396462.2447973412.24790798712.3346333632.673791682.7137596482.7182818281.12.8015600363.2882052493.3468340843.3534846531.23.4179032434.1237280754.210792274.2206958171.34.2382000225.2735645785.404537735.4194807051.45.3401320286.8767799517.0764698837.0993270651.56.8353689959.1435769259.4522765199.4877358361.68.88597969412.3959793712.8800067912.935817321.711.729493217.1341651117.9041603317.99330961.815.7175208824.1460529125.3891575525.533721751.921.375828434.6907928936.7280071636.96605281229.4986431950.8102141154.2000210354.59815003表1结果说明:通过几个步长的残差图、绝对误差图和表一可以看出,随着步长的减小,前进欧拉公式得到的近似值与精确值将会越来越接近。对于同一步长,其绝对误差会随着自变量x值的增大而逐渐累积加大。第一题(2)①定义前进欧拉函数QJEulerbbb1:functiony=QJEulerbbb1(h)a=0;b=1;y0=1;N=(b-a)/h;y=zeros(N+1,1);x=a:h:b;y(1)=y0;fori=2:N+1y(i)=y(i-1)+h*(-y(i-1)-sin(x(i-1)));end②取步长h=0.1.0.01.0.001:x=0:0.1:1;z=zeros(11,1);fori=1:11z(i)=1/2*cos(x(i))-1/2*sin(x(i))+1/2*exp(-x(i));endy1=QJEulerbbb1(0.1);y2=QJEulerbbb1(0.01);y3=QJEulerbbb1(0.001);y22=zeros(11,1);fori=1:11y22(i)=y2(10*(i-1)+1);endy33=zeros(11,1);fori=1:11y33(i)=y3(100*(i-1)+1);endy1y22y33plot(x,y1,'b-o',x,y22,'g-x',x,y33,'k-+',x,z,'r');legend('h=0.1','h=0.01','h=0.001','exact');axis([0,1,-inf,inf])xlabel('x')ylabel('exactsolutionandapproximatesolutions')注:该MATLAB程序是是描述三个不同步长(h=0.1;0.01;0.001)的前进欧拉公式所求的解与精确值并以图像的形式给出三个步长直观效果,如图2图2结果说明:由图2可以直观的了解到,随着步长h的减小,由前进欧拉公式算得的数值近似值与精确值的误差会越来越小。第一题(2)当h=0.1时a=0;b=1;h=0.1;y0=1;N=(b-a)/h;y=zeros(N+1,1);x=a:h:b;y(1)=y0;fori=2:N+1y(i)=y(i-1)+h*(-y(i-1)-sin(x(i-1)));endz=zeros(N+1,1);fori=1:N+1z(i)=1/2*cos(x(i))-1/2*sin(x(i))+1/2*exp(-x(i));enderr=abs(y-z);e=y-z;subplot(2,1,1)plot(x,y,'b',x,z,'r');axis([0,1,-inf,inf])legend('QJEuler','exact');xlabel('x')ylabel('exactsolutionandapproximatesolution')subplot(2,1,2)xlabel('x')ylabel('absoluteerror')bar(x,e)figure(2);plot(x,err,'b');axis([0,1,-inf,inf])xlabel('x')ylabel('absoluteerror')注:该程序描述了h=0.1时前进欧拉公式所求的解与精确解的函数图象以及残差图和近似解相对于精确解的绝对误差的曲线图像。同理只需将该程序中的h依次改为0.01;0.001即可得到步长为0.01;0.001时前进欧拉公式所求的解与精确解的函数图象以及残差图。h=0.1(残差图)h=0.1(绝对误差图)h=0.01(残差图)h=0.01(绝对误差图)h=0.001(残差图)h=0.001(绝对误差图)根据以上程序运行结果得出不同步长的近似值与精确解的图表自变量值近似值(h=0.1)近似值(h=0.01)近似值(h=0.001)精确解011110.10.90.9000033260.9000040040.9000040830.20.8000166580.8000580920.8000633990.8000640.30.7001480590.7002981520.7003153220.7003172520.40.6005812330.6009381940.6009770050.6009813490.50.5015812750.5022636920.5023357920.5023438410.60.4034805940.4046209020.4047392020.4047523890.70.3066682870.3084069330.3085850680.3086049020.80.211579690.2140599350.2143117730.2143397910.90.1186861120.1220494310.1223886440.12242635910.028484810.0328668370.0333065180.033355381表2结果说明:通过几个步长的残差图、绝对误差图和表2可以看出,随着步长的减小,前进欧拉公式得到的近似值与精确值将会越来越接近。对于同一步长,其绝对误差会随着自变量x值的增大而逐渐累积加大。第二题取n=10就算10年内人口数量近似值:a=0;h=1;n=10;y0=3.9*10^6;r=0.31;m=197*10^6;N=(n-a)/h;y=zeros(N+1,1);x=a:h:n;y(1)=y0;fori=2:N+1y(i)=y(i-1)+h*r*(1-y(i-1)/m)*y(i-1);endz=zeros(N+1,1);fori=1:N+1z(i)=m/(1+1/3900000*exp(-r*x(i))*(m-3900000));enderr=abs(y-z);e=y-z;subplot(2,1,1)plot(x,y,'b',x,z,'r');legend('Q
本文标题:数学实验报告
链接地址:https://www.777doc.com/doc-4156456 .html