Subtracting the two resulting graghs of my ode program

1 回表示 (過去 30 日間)
MA
MA 2016 年 7 月 6 日
コメント済み: MA 2016 年 7 月 7 日
I have two second order differential equations. I solved them with Ode45, but I want to subtract the two resulting answers from each other and plot the subtraction of the one answer from another one. How can I do that???
function dy=function1(t,y)
dy=zeros(2,1);
dy(1)=y(2);
dy(2)=sin(t)-y(1);
end
function dy=function2(t,y)
dy=zeros(2,1);
dy(1)=y(2);
dy(2)=(t^2)-2*y(1)-y(2);
end
[T,Y]=ode45(@function1,[0 10],[0 0]);
[M,X]=ode45(@function2,[0 10],[0 0]);
plot(T,Y(:,1),M,X(:,1))
I want to plot X(:,1)-Y(:,1)???

採用された回答

Kelly Kearney
Kelly Kearney 2016 年 7 月 6 日
You can either change the second input of both ode45 calls to specify specific time values to output:
tspan = linspace(0,10,100);
[T,Y]=ode45(@function1,tspan,[0 0]);
[M,X]=ode45(@function2,tspan,[0 0]);
(Here, T, M, and tspan will be equal).
Or, you could keep your original input but interpolate both output timeseries to the same grid:
y2 = interp1(T, Y, tspan);
x2 = interp1(M, X, tspan);
plot(tspan, y2(:,1)-x2(:,1));
  1 件のコメント
MA
MA 2016 年 7 月 7 日
Thank you very much :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by