ODE45 function time step

12 ビュー (過去 30 日間)
Anis Frej
Anis Frej 2021 年 5 月 11 日
コメント済み: Anis Frej 2021 年 5 月 11 日
Hi,
How can i increment a global variable in every time step of the ode45 function or in other words each sampling time of the ode45 function
thank you

回答 (1 件)

Jan
Jan 2021 年 5 月 11 日
Avoid using global variables, because they are a shot in your knee.
Use a persistent variable instead and reply it to the caller for specific input arguments.
You cannot implement this in the function to be integrated, because the solver might reject some steps. So use the OutputFcn, which is called after successful steps only.
  3 件のコメント
Jan
Jan 2021 年 5 月 11 日
options = odeset('OutputFcn', @YourOutput);
[t, y] = ode45(@YourODE, tSpan, y0, options);
function status = myOutputFcn(t,y,flag)
persistent Data
if isempty(Data)
Data.t = [];
Data.y = [];
end
status = 0;
switch flag
case 'init'
Data = [];
case 'done'
case []
Data.t(end + 1) = t;
Data.y(end + 1, :) = y;
case 'flush'
status = Data;
otherwise
error('Bad flag');
end
end
Now run the integration and obtain the current status by:
Data = myOutputFcn([], [], 'flush')
Anis Frej
Anis Frej 2021 年 5 月 11 日
Thank you very much

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

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by