Matlab一阶微分方程

4.在Matlab中,一阶微分方程能够和方便的得到数值解,试求解一阶微分方程y'(t)= -3 cos(2t) +9sin(t)+t的数值解,并和解析解进行比较,初值 y(0)=0, 0 ≤t ≤ 5。(可用龙格库塔法ode23或ode45)

clear all
clc

f=@(t,y)(-3*cos(2*t)+9*sin(t)+t);
tspan=0:.05:5;
[t,y1]=ode45(f,tspan,0);
y2=tspan.^2/2 - 9*cos(tspan) - (3*sin(2*tspan))/2 + 9
plot(tspan,y1,tspan,y2,'r+'),legend('数值解','解析解')

 

基本是一样的

温馨提示:答案为网友推荐,仅供参考