ploting differential equation 2 order

3 次查看(过去 30 天)
hello, i'm new and i can't solve this second order deffirential equation : -y'=y''+3y+cos(wt) where w=0.1,1,10 and t=[0;100] and after that plot the equation with 3 different plot on the same graph
y0=[0 0]
w=0.1
tspan=[0,100]
odefun=@(t,y) [y(2);cos(w*t)+y(2)+3*y(1)];
[t,y]=ode45(odefun,tspan,y0);
plot(t,y)
I do this but i'm not sure if it's true or false. Thank you for your help.

回答(1 个)

Jim Riggs
Jim Riggs 2018-5-24
编辑:Jim Riggs 2018-5-24
Here is how to do this:
y0=[0 0]
tspan=[0,100]
w=0.1
odefun=@(t,y) [y(2);cos(w*t)+y(2)+3*y(1)];
[t1,y1]=ode45(odefun,tspan,y0);
w=1.0
odefun=@(t,y) [y(2);cos(w*t)+y(2)+3*y(1)];
[t2,y2]=ode45(odefun,tspan,y0);
w=10.0
odefun=@(t,y) [y(2);cos(w*t)+y(2)+3*y(1)];
[t3,y3]=ode45(odefun,tspan,y0);
Now you have three solutions, t1,y1, t2,y2, and t3,y3 for values of w=0.1, 1.0 and 10.0.
When you define an anonymous function using a constant (in this case w) the constant becomes embedded in the function. That's why you have to re-define the function when you change the value of w.
Note that y1, y2, and y3 contain two columns each. You can plot these using:
figure;
plot(t1,y1(:,1),'r');
hold on;
grid on;
plot(t2,y2(:,1),'b');
plot(t3,y3(:,1),'g');
legend('w=0.1','w=1.0','w=10.0');
The notation y1(:,1) y2(:,1) y3(:,1) indicates the first column. If you want to plot the second column, change the 1 to a 2.

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by