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.
0 件のコメント
回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File 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!