Solving nonlinear DE by reduction of order on matlab

2 ビュー (過去 30 日間)
규민 권
규민 권 2021 年 8 月 11 日
コメント済み: 규민 권 2021 年 8 月 13 日
Nice of you all to answer my question.
I tried to graph the nonlinear ordinary DE in a textbook(Advanced Engineering Mathematics 6th Ed by Zill Wright).\
the DE is like this.
dy/dx = u
du/dx = x + y - y^2
Written the codes like below, but only gained 2 graphs each.
[sourcecode]
function dydt = odesys(t, y)
dydt(1) = y(1);
dydt(2) = t + y(2) - y(2)^2;
dydt = [dydt(1); dydt(2)];
end
[command]
[t, y] = ode45(@odesys, [-1 1], [-1 1])
plot(t, y);
grid
I want to get the graph as on the textbook, like the picture right below.(In case of copyright, I couldn't get original picture in the book)
What is wrong with my code?

採用された回答

Fabio Freschi
Fabio Freschi 2021 年 8 月 11 日
編集済み: Fabio Freschi 2021 年 8 月 11 日
There are some typos in your description of the problem. Here the correct version
% your ODE - note that (y(1) and y(2) are exchanged)
odesys = @(t,y)[y(2); t+y(1)-y(1)^2];
% soluiton - note that the time span is [0 10] and the initial conditions
% are set for t = 0;
[t, y] = ode45(odesys, [0 10], [-1 1]);
% plot
figure, plot(t,y(:,1));
grid on
  3 件のコメント
Fabio Freschi
Fabio Freschi 2021 年 8 月 12 日
My pleasure. Regarding your questions
  1. the return outputs are the arrays t and y. The dimensions of t are Nx1, being N the number of time steps. This number is not known a priori, since ode45 uses adaptive step size. The dimensions of y is NxM, where M is the number of variables in your ODE. In this case M = 2. I understood you wanted only y(:,1), that is the variable y in your model.
  2. your way to call ode45 was correct, because you defined your ODE as an external function. I defined the model inline with an anonymous function. The data type of the anonymous function is a function_handle, so the @ symbol was not required in the call.
규민 권
규민 권 2021 年 8 月 13 日
Thank you again! Very helpful for me to be handling MATLAB better. Have a good day!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by