1、时在分题(b)中得到的结果与此时的精确值进行比较。【MATLAB相关函数】求微分方程的解析解及其数值的代入 dsolve(egn1, egn2, ) subs (expr, x,y, x1,y1, )其中egn表示第个方程,表示微分方程中的自变量,默认时自变量为。subs命令中的expr、x、y为符合型表达式,x、y分别用数值x1、x2代入。 syms x y z subs(x+y+z,x,y,z,1,2,3)ans = 6 syms xx2,x,2) 4 s=dsolve(, ans = subs(s,x,2) -0.3721右端函数的自动生成 f= inline(expr, var1, v
2、ar2,) 其中expr表示函数的表达式,var1, var2 表示函数表达式中的变量,运行该函数,生成一个新的函数表达式为f (var1, var2, )。 f=inline(x+3*y,xy)f = Inline function: f(x,y) = x+3*y f(2,3) 114,5阶龙格库塔方法求解微分方程数值解t,x=ode45(f,ts,x0,options) 其中f是由待解方程写成的m文件名;x0为函数的初值;t,x分别为输出的自变量和函数值(列向量),t的步长是程序根据误差限自动选定的。若ts=t0,t1,t2,tf,则输出在自变量指定值,等步长时用ts=t0:k:tf,输出
3、在等分点;options用于设定误差限(可以缺省,缺省时设定为相对误差,绝对误差),程序为:options=odeset(reltol,rt,abstol,at),这里rt,at分别为设定的相对误差和绝对误差。常用选项见下表。选项名功能可选值省缺值AbsTol设定绝对误差正数RelTol设定相对误差InitialStep设定初始步长自动MaxStep设定步长上界MaxOrder设定ode15s的最高阶数1,2,3,4,55Stats显示计算成本统计on,offoffBDF设定ode15s是否用反向差分例:解微分方程在命令窗口执行 = (); ans = 0 1.0000 0.0502 1.04
4、90 0.1005 1.0959 0.1507 1.1408 3.8507 2.9503 3.9005 2.9672 3.9502 2.9839 4.0000 3.0006 plot(,o-,) %解函数图形表示 %不用输出变量,则直接输出图形 1.0000 1.7321 2.0000 2.2361 3.0000 2.6458三. 操作方法与实验步骤(包括实验数据记录和处理)2-1 编程编写用向前欧拉公式和改进欧拉公式求微分方程数值解的Matlab程序,问题如下:在区间内个等距点处,逼近下列初值问题的解,并对程序的每一句添上注释语句。Euler法 y=euler(a,b,n,y0,f,f1,b
5、1)改进Euler法 y=eulerpro(a,b,n,y0,f,f1,b1)Euler法 y=euler(a,b,n,y0,f,f1,b1) y=zeros(1,n+1);y(1)=y0;h=(b-a)/n;x=a:h:b;for i=1:n; y(i+1)=y(i)+h*f(x(i),y(i); endplot(x,y)hold on % 求微分方程的精确解 x1=linspace(a,b,100); 精确解为 s=dsolve(f1,b1, syms xy1=zeros(1,100);for i=1:100 y1(i)=subs(s,x,x1(i);endplot(x1,y1,rtitl
6、e(红色代表精确解改进Euler法 y=eulerpro(a,b,n,y0,f,f1,b1) % 求微分方程的数值解 y=zeros(1,n+1); y(1)=y0; h=(b-a)/n; x=a: for i=1: T1=f(x(i),y(i); T2=f(x(i+1),y(i)+h*T1); y(i+1)=y(i)+(h/2)*(T1+T2);% 求微分方程的精确解 y1=zeros(1,100); for i=1: y1(i)=subs(s,x,x1(i);2-2分析应用题假设等分区间数,用欧拉法和改进欧拉法在区间内求解初值问题并作出解的曲线图形,同时将方程的解析解也画在同一张图上,并作
7、比较,分析这两种方法的精度。(1)向前欧拉法 euler(0,10,100,10,inline(y-20),Dy=y-20y(0)=10) 精确解为s = 20 - 10*exp(x) 1.0e+005 * Columns 1 through 8 0.0001 0.0001 0.0001 0.0001 0.0001 0.0000 0.0000 0.0000 Columns 9 through 16 -0.0000 -0.0000 -0.0001 -0.0001 -0.0001 -0.0001 -0.0002 -0.0002 Columns 17 through 24 -0.0003 -0.00
8、03 -0.0004 -0.0004 -0.0005 -0.0005 -0.0006 -0.0007 Columns 25 through 32 -0.0008 -0.0009 -0.0010 -0.0011 -0.0012 -0.0014 -0.0015 -0.0017 Columns 33 through 40 -0.0019 -0.0021 -0.0024 -0.0026 -0.0029 -0.0032 -0.0035 -0.0039 Columns 41 through 48 -0.0043 -0.0048 -0.0053 -0.0058 -0.0064 -0.0071 -0.0078
9、 -0.0086 Columns 49 through 56 -0.0095 -0.0105 -0.0115 -0.0127 -0.0140 -0.0154 -0.0170 -0.0187 Columns 57 through 64 -0.0206 -0.0227 -0.0250 -0.0275 -0.0302 -0.0333 -0.0366 -0.0403 Columns 65 through 72 -0.0444 -0.0488 -0.0537 -0.0591 -0.0651 -0.0716 -0.0788 -0.0867 Columns 73 through 80 -0.0954 -0.
10、1049 -0.1154 -0.1270 -0.1397 -0.1537 -0.1691 -0.1860 Columns 81 through 88 -0.2046 -0.2251 -0.2477 -0.2724 -0.2997 -0.3297 -0.3627 -0.3990 Columns 89 through 96 -0.4389 -0.4828 -0.5311 -0.5842 -0.6427 -0.7070 -0.7777 -0.8555 Columns 97 through 101 -0.9410 -1.0352 -1.1387 -1.2526 -1.3779(2)改进欧拉法 eule
11、rpro(0,10,100,10,inline( ans = 精确解为 s = 20 - 10*exp(x) 0.0001 0.0001 0.0001 0.0001 0.0001 0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0001 -0.0001 -0.0001 -0.0002 -0.0002 -0.0002 Columns 17 through 24 -0.0003 -0.0003 -0.0004 -0.0005 -0.0005 -0.0006 -0.0007 -0.0008 -0.0009 -0.0010 -0.0011 -0.0013 -0.001
12、4 -0.0016 -0.0018 -0.0020 -0.0022 -0.0025 -0.0028 -0.0031 -0.0034 -0.0038 -0.0042 -0.0047 -0.0052 -0.0058 -0.0064 -0.0071 -0.0079 -0.0087 -0.0097 -0.0107 -0.0119 -0.0131 -0.0145 -0.0161 -0.0178 -0.0197 -0.0218 -0.0241 -0.0266 -0.0294 -0.0325 -0.0360 -0.0398 -0.0440 -0.0486 -0.0537 -0.0594 -0.0656 -0
13、.0726 -0.0802 -0.0886 -0.0980 -0.1083 -0.1197 -0.1323 -0.1462 -0.1615 -0.1785 -0.1973 -0.2180 -0.2409 -0.2663 -0.2942 -0.3251 -0.3593 -0.3971 -0.4388 -0.4849 -0.5358 -0.5921 -0.6543 -0.7230 -0.7989 -0.8828 -0.9755 -1.0780 -1.1912 -1.3163 -1.4545 -1.6073 -1.7760 -1.9626 -2.1686改进欧拉法的精度比向前欧拉法更高。2-3分析应
14、用题用以下三种不同的方法求下述微分方程的数值解,取2-4分析应用题1)euler(0,50,50,0.01,inline(0.002-0.002*ptpDp=0.002-0.002*pp(0)=0.011 - 99/(100*exp(x/500) 0.0100 0.0120 0.0140 0.0159 0.0179 0.0199 0.0218 0.0238 0.0257 0.0277 0.0296 0.0316 0.0335 0.0354 0.0374 0.0393 0.0412 0.0431 0.0450 0.0470 0.0489 0.0508 0.0527 0.0546 0.0564 0
15、.0583 0.0602 0.0621 0.0640 0.0658 0.0677 0.0696 0.0714 0.0733 0.0751 0.0770 0.0788 0.0807 0.0825 0.0844 0.0862 0.0880 0.0898 0.0917 0.0935 0.0953 0.0971 0.0989 Columns 49 through 51 0.1007 0.1025 0.1043(2) dsolve(p(0)=0.01ans =1 - 99/(100*exp(t/500) 1 - 99/(100*exp(0.1) ans =0.1042与欧拉法求得的精确值0.1043差0,0001四. 实验结果与分析
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1