第四次上机题,留给有缘人
报告下载链接,不需要金币--->上机报告
%习题7.2
%函数y_=10.*x.*(1-y),精确函数y=1-exp(-5*x^2)
y=0; %y(0)=0 初始值
h=0.1;
fprintf('欧拉法:\n');
tic
for i=1:1/h
x=i*h; %横坐标值
accuracy=1-exp(-5*x^2);
fprintf('计算值x=%.5f\ty=%.5f\t精确值%.5f\t误差%.5f\n',x,y,accuracy,abs(y-accuracy));
k=fun(x,y); %一阶导,求的是斜率
y=y+h*k; %计算值
end
toc %与tic成对出现,计算程序运行时间
fprintf('改进欧拉法:\n');
tic
%求第一个点(x,y)的斜率k1和第二个点(x+h,y+K1*h)的斜率k2
y=0;
h=0.1;
for i=1:1/h
x=i*h;
accuracy=1-exp(-5*x^2);
fprintf('计算值x=%.5f\ty=%.5f\t精确值%.5f\t误差%.5f\n',x,y,accuracy,abs(y-accuracy));
k1=fun(x,y); %第一点斜率
k2=fun(x+h,y+k1*h); %第二点斜率
y=y+h*(k1+k2)/2;
end
toc
fprintf('龙格库塔方法:\n');
tic
%与改进欧拉法相似,只是需要四个K
h=0.1;
y=0;
for i=1:1/h
x=i*h;
accuracy=1-exp(-5*x^2);
fprintf('计算值x=%.5f\ty=%.5f\t精确值%.5f\t误差%.5f\n',x,y,accuracy,abs(y-accuracy));
k1=fun(x,y);
k2=fun(x+h/2,y+k1*(h/2));
k3=fun(x+h/2,y+k2*(h/2));
k4=fun(x+h,y+k1*h);
y=y+h*(k1+k2+k3+k4)/4;
end
toc
结果:
欧拉法:
计算值x=0.10000 y=0.00000 精确值0.04877 误差0.04877
计算值x=0.20000 y=0.10000 精确值0.18127 误差0.08127
计算值x=0.30000 y=0.28000 精确值0.36237 误差0.08237
计算值x=0.40000 y=0.49600 精确值0.55067 误差0.05467
计算值x=0.50000 y=0.69760 精确值0.71350 误差0.01590
计算值x=0.60000 y=0.84880 精确值0.83470 误差0.01410
计算值x=0.70000 y=0.93952 精确值0.91371 误差0.02581
计算值x=0.80000 y=0.98186 精确值0.95924 误差0.02262
计算值x=0.90000 y=0.99637 精确值0.98258 误差0.01379
计算值x=1.00000 y=0.99964 精确值0.99326 误差0.00638
时间已过 0.011510 秒。
改进欧拉法:
计算值x=0.10000 y=0.00000 精确值0.04877 误差0.04877
计算值x=0.20000 y=0.14000 精确值0.18127 误差0.04127
计算值x=0.30000 y=0.32920 精确值0.36237 误差0.03317
计算值x=0.40000 y=0.52373 精确值0.55067 误差0.02694
计算值x=0.50000 y=0.69043 精确值0.71350 误差0.02307
计算值x=0.60000 y=0.81426 精确值0.83470 误差0.02045
计算值x=0.70000 y=0.89598 精确值0.91371 误差0.01772
计算值x=0.80000 y=0.94487 精确值0.95924 误差0.01437
计算值x=0.90000 y=0.97188 精确值0.98258 误差0.01069
计算值x=1.00000 y=0.98594 精确值0.99326 误差0.00732
时间已过 0.005725 秒。
龙格库塔方法:
计算值x=0.10000 y=0.00000 精确值0.04877 误差0.04877
计算值x=0.20000 y=0.14045 精确值0.18127 误差0.04082
计算值x=0.30000 y=0.33103 精确值0.36237 误差0.03134
计算值x=0.40000 y=0.52761 精确值0.55067 误差0.02306
计算值x=0.50000 y=0.69637 精确值0.71350 误差0.01712
计算值x=0.60000 y=0.82155 精确值0.83470 误差0.01315
计算值x=0.70000 y=0.90351 精确值0.91371 误差0.01020
计算值x=0.80000 y=0.95163 精确值0.95924 误差0.00761
计算值x=0.90000 y=0.97730 精确值0.98258 误差0.00527
计算值x=1.00000 y=0.98992 精确值0.99326 误差0.00334
时间已过 0.008043 秒。
习题7.3
fprintf('改进欧拉法:\n');
%求第一个点(x,y)的斜率k1和第二个点(x+h,y+K1*h)的斜率k2
y=1;
h=0.2;
for i=1:1/h
x=i*h;
accuracy=exp(-x);
y=0.82*y;
fprintf('计算值x=%.5f\ty=%.5f\t精确值%.5f\t误差%.5f\n',x,y,accuracy,abs(y-accuracy));
end
结果
改进欧拉法:
计算值x=0.20000 y=0.82000 精确值0.81873 误差0.00127
计算值x=0.40000 y=0.67240 精确值0.67032 误差0.00208
计算值x=0.60000 y=0.55137 精确值0.54881 误差0.00256
计算值x=0.80000 y=0.45212 精确值0.44933 误差0.00279
计算值x=1.00000 y=0.37074 精确值0.36788 误差0.00286
习题7.6
%7_6精确函数y=exp(-x)
h=0.2;
y=1;
for i=1:1/h
x=i*h;
accuracy=exp(-x);
y=(1-h+(h^2/2)-(h^3)/6+(h^4)/24)*y;
fprintf('计算值x=%.5f\ty=%.10f\t精确值%.10f\t误差%.10f\n',x,y,accuracy,abs(y-accuracy));
end
结果
计算值x=0.20000 y=0.8187333333 精确值0.8187307531 误差0.0000025803
计算值x=0.40000 y=0.6703242711 精确值0.6703200460 误差0.0000042251
计算值x=0.60000 y=0.5488168249 精确值0.5488116361 误差0.0000051888
计算值x=0.80000 y=0.4493346284 精确值0.4493289641 误差0.0000056643
计算值x=1.00000 y=0.3678852381 精确值0.3678794412 误差0.0000057970
实习期7.1(1)
function [ z ] = fun1( x,y )
z=x^2+y^2;
end
%实习题7_1(1) y`=x^2+y^2 y(0)=0
fprintf('龙格库塔方法:\n');
h=0.1;
y=0;
for i=1:1/h
x=i*h;
fprintf('计算值x=%.5f\ty=%.5f\n',x,y);
k1=fun1(x,y);
k2=fun1(x+h/2,y+k1*(h/2));
k3=fun1(x+h/2,y+k2*(h/2));
k4=fun1(x+h,y+k1*h);
y=y+h*(k1+k2+k3+k4)/4;
end
结果
龙格库塔方法:
计算值x=0.10000 y=0.00000
计算值x=0.20000 y=0.00238
计算值x=0.30000 y=0.00875
计算值x=0.40000 y=0.02115
计算值x=0.50000 y=0.04161
计算值x=0.60000 y=0.07230
计算值x=0.70000 y=0.11552
计算值x=0.80000 y=0.17394
计算值x=0.90000 y=0.25072
计算值x=1.00000 y=0.34995