Differential equation ode45 matlab

6 ビュー (過去 30 日間)
Alex7210
Alex7210 2014 年 8 月 27 日
編集済み: Star Strider 2014 年 8 月 28 日
Hi! I have a differential equation and I try to solve them by Matlab (ODE45) For example:
function dy = f8(t,y)
dy = 3 - 3./25.*y; %(1)
tspan = [0, 2]; y0 = -3;
[Y,T] = ode45(@f8, tspan, y0);
plot(Y,T)
grid on
When the curve reaches=0, I have to go to the equation of (2):
dy = 10-3./10.*y; %(2)
Please, help me, How can I do it?

回答 (1 件)

Star Strider
Star Strider 2014 年 8 月 27 日
You need to detect the zero-crossing as an ‘event’. See the documentation on Event Location for details.
It is not difficult, but it can be a bit of a challenge to get it working the first time you do it.
  2 件のコメント
Alex7210
Alex7210 2014 年 8 月 28 日
Thanks for your answer! I could detect zero-crossing.
tspan = [0, 10]; x0 = -3;
options = odeset('Events', @kg15);
[t,x,te,xe,ie] = ode45(@f8, tspan, x0, options);
plot(t,x); grid on
%==============================
function dy = f8(t,y)
dy = 3-3./25.*y;
%==============================
function [value, isterminal, ...
direction] = kg15(t,x)
value = x-0;
isterminal = 1;
direction = 1;
But how can I continue my second equation? Help me please to understand it
Star Strider
Star Strider 2014 年 8 月 28 日
編集済み: Star Strider 2014 年 8 月 28 日
First, when the integration stops at the zero-crossing, immediately store your first set of t and x values as an array so they do not get overwritten.
Second, use the last t and x values for your new initial t and x values, call ode45 with your second function and those initial conditions and time, and continue integrating until the end of tspan.
Third, append your second set of t and x values to your first set to get the complete integration sequence. (It does not appear from your second ODE that you will have another zero-crossing, so you will only have to do this once.)

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

カテゴリ

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