How to programatically stop ode solver execution after fixed amount of computation time?

30 ビュー (過去 30 日間)
I have an initial value problem whose parameters I want to tune to measurement data. I found that my problem seems to become stiff if one or more parameters have very high values, at least what I observe is that in those cases, the ode45 solver takes a very long time and in most cases I end up terminating the calculation before it finishes. I tried to use instead the ode15s solver, which came to a solution in very short time.
I would like to automate this process somehow, and the easiest solution I can think of is to run the ode45 solver and after a given amount of time, terminate the execution and make a second attempt using the ode15s solver. I am aware of the existence of event triggers, but never worked with them and can't really figure out how I could use them in the context of my problem.
Can someone please draft how to achieve this? Thank you very much!
  4 件のコメント
Torsten
Torsten 2022 年 4 月 12 日
編集済み: Torsten 2022 年 4 月 12 日
No drawbacks concerning precision.
For higher-dimensional problems which are not stiff, using a stiff solver might increase the runtime and the required RAM because of the Jacobian matrix that has to be built in each integration step.
But changing the solver during integration is nonsense in my opinion.
Niklas Barthel
Niklas Barthel 2022 年 4 月 20 日
Thanks for pointing this out. I have accepted @Jan|s answer because it precisely answers my question, but I think I will in fact adapt your suggestion and use ode15s instead. Again, thanks!

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

採用された回答

Jan
Jan 2022 年 4 月 11 日
The Event function is a bad idea: If the specified time has come, ode45 tries to find the simulated t, when the event value has changed. This wastes computing time.
The OutputFcn is thought for triggering an external event:
stopTime = now + 2 / 86400; % 2 seconds
options = odeset('OutputFcn', @(t, y, flag) myOutputFcn(t, y, flag, stopTime));
Sol = ode45(@fcn, [0, 10], 0, options);
Sol.x(end) < 10 % Stopped by timer?
function dy = fcn(t, y)
dy = sin(t);
v = exp(exp(sqrt(rand(1000))))^3; % Waste time to slow down processing
end
function status = myOutputFcn(t, y, flag, stopTime)
status = double(now > stopTime);
end
This stops the integration after 2 seconds. This a kind of crash driven programming: instead of analysing the stiffness of the function to be intergrated, the runtime of the integrator is used to determine problems. Running this on a very slow or very fast machine can influence the method, so this is a bad idea. E.g. if Windows decides to install an update in the background, your algorithm might detect a wrong stiffness limit. Then repeated computations can cause different results.
  3 件のコメント
Jan
Jan 2022 年 4 月 12 日
The output function is called after an successfully accepted step. Then you can use the provided t,y to calculate the sensitivity matrix: The cumulative product of the matrix, which measures then quotient of the output of the function to be integrated to a variation of the input. In other words this measures the sensitivity of the solution to noise. This can be used to determine a bad choice of tolerances and you can detect stiffness also.
As long, as you know about the "dirtiness" and document this exhaustively, everything is fine.
Niklas Barthel
Niklas Barthel 2022 年 4 月 20 日
Hey, I was able to adapt your solution and everything works fine, thanks a lot for your time!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by