MATLAB求解微分方程.pptx
211xdtdx例MATLAB命令:dsolve(Dx=1+x*x)结果:x=tan(t+C1)tteyxdtydeyxxdtxd43422222例MATLAB命令:x,y=dsolve(D2x+2*Dx=x+2*y-exp(-t),.Dy=4*x+3*y+4*exp(-t); 结果:x=-6*t*exp(-t)+C1*exp(-t)+C2*exp(1+6(1/2)*t)+ C3*exp(1-6(1/2)*t) y=6*t*exp(-t)-C1*exp(-t)+4*C2*exp(1+6(1/2)*t)+2*C2*exp(1+6(1/2)*t)*6(1/2)+4*C3*exp(1-6(1/2)*t)-2*C3*exp(1-6(1/2)*t)*6(1/2)+1/2*exp(-t) 用用Matlab求微分方程的数值解求微分方程的数值解t,x=solver(f,ts,x0,options)ode45 ode23 ode113ode15sode23s由待解方程写成的m-文件名ts=t0,tf,t0、tf为自变量的初值和终值函数的初值自变量值函数值用于设定误差限(缺省时设定相对误差10-3, 绝对误差10-6),命令为:options=odeset(reltol,rt,abstol,at), rt,at:分别为设定的相对误差和绝对误差.0)0( ; 2)0(0)1 (10003222xxxdtdxxdtxd例STEP1 令令 y1=x,y2=y1 STEP2 建立M文件 function dy=vdp(t,y) dy=y(2); 1000*(1-y(1)2)*y(2)-y(1);STEP3 调用MATLAB 函数ODE15ST,Y=ode15s(vdp,0 3000,2 0); plot(T,Y(:,1)图形结果050010001500200025003000-2.5-2-1.5-1-0.500.511.52使用匿名函数,STEP2 和STEP3 可以合并为 vdp2=(t,y)y(2),1000*(1-y(1)2)*y(2)-y(1); % vdp2为匿名函数T,Y=ode15s(vdp2,0,3000,2 ,0); plot(T,Y(:,1)zcxbdtdzayxdtdyzydtdxRossler)(4方程)(例选定a=0.3,b=2,c=3 初值x(0)=0,y(0)=0,z(0)=0 a=0.3;b=2;c=3; rossler=(t,y)-y(2)-y(3),y(1)+a*y(2),b+(y(1)-c)*y(3); ts=0 100;x0=0 0 0; t,y=ode45(rossler,ts,x0); plot(t,y(:,1),r,t,y(:,2),b,t,y(:,3),g); figure plot3(y(:,1),y(:,2),y(:,3) 0102030405060708090100-2.5-2-1.5-1-0.500.511.52-2-1012-3-2-10100.511.5微分方程求解时参数传递方法一 假设上例中参数a,b,c的值在运行时确定,我们可以这样建立函数文件rosslersolve.m function rosslersolve(a,b,c) rossler1=(t,y,a,b,c)-y(2)-y(3),y(1)+a*y(2),b+(y(1)-c)*y(3); rossler2=(t,y)rossler1(t,y,a,b,c); ts=0 100;x0=0 0 0; t,y=ode45(rossler2,ts,x0); plot3(y(:,1),y(:,2),y(:,3) Matlab命令行输入rosslersolve(0.2,2,3) 求解参数a,b,c的值为0.2,2,3时的rossler方程参数传递方法二 Lorenz=(t,y,beta,rho,sigma). -beta*y(1)+y(2)*y(3) -rho*(y(2)-y(3) -y(1)*y(2)+sigma*y(2)-y(3); ts=0,100;x0=0 0 1e-10; t,y=ode45(Lorenz,ts,x0,8/3,10,28); plot(y(:,1),y(:,2);figure;plot(y(:,1),y(:,3); figure;plot(y(:,2),y(:,3);-1001020304050-20-15-10-505101520-1001020304050-30-20-100102030-20-15-10-505101520-30-20-100102030