How to programatically stop ode solver execution after fixed amount of computation time?
31 views (last 30 days)
Show older comments
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 Comments
Torsten
on 12 Apr 2022
Edited: Torsten
on 12 Apr 2022
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.
Accepted Answer
Jan
on 11 Apr 2022
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 Comments
Jan
on 12 Apr 2022
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.
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!