How to stop an ode-solver if integration takes too long?

55 views (last 30 days)
Hi all,
I am working with implicit ode15i solver.
I receive results within seconds for more than 80 parameters to solve. However, sometimes it seems it gets stuck - no progress after hours. I can reproduce that, so I am sure I have chosen bad initial conditions. Is there a way to interrupt the integration after certain time to start new with different initial conditions? Something like a timing option?
I know about the event-function but this is for events given by the system only as far as I understood.
Thanks a lot in advance. Best, Franziska

Accepted Answer

Mahdi
Mahdi on 3 Apr 2013
At the start of your code:
StartTime=clock;
At the end of each loop/the point you might want to exit
TimeElapsed=clock-StartTime;
if TimeElapsed(end)>10 %Set it to a value that you want (I chose 10 seconds)
return
end
Look at the clock function to understand this more.

More Answers (2)

Jared Barber
Jared Barber on 15 Jul 2019
I know it's been a while since this was posted but since I have implemented something that does something similar to what the poster asks about, I thought I'd report it.
This may not give the level of control that you are seeking, but you can, in fact, feed the start time of your integration to your events function and use that to monitor how long the ode solver has been integrating for. The following code integrates y' = sin(t^2)*y and stops prematurely because the integration has been running for 1.2 seconds:
myevent function
function [values,isterminal,direction] = myevent(t,y,yp,tstart)
% Don't let t cross zero...a dummy "event" to illustrate how
% one might handle other events in conjunction with the time
% constraint. Not necessary, but I put it in just in case.
values(1) = t;
% Don't let integration go for more than 1.2 seconds.
values(2) = toc(tstart) < 1.2;
isterminal = true(size(values));
direction = zeros(size(values));
end
corresponding function definition and function call:
myf = @(t,y,yp) yp-sin(t^2)*y;
tstart = tic;
[t,y,te,ye,ie] = ode15i(myf,[0,1e15],1,0,odeset('Events',@(t,y,yp) myevent(t,y,yp,tstart)));
plot(t,y);
This was also in Matlab 2019a. It may not have been possible in Matlab 2013 or earlier, I'm not sure.

Franziska
Franziska on 3 Apr 2013
Thanks Mahdi for your fast answer. I think we misunderstood. My problem is the following:
I want to solve a system of equations as long as certain condition is full filled. Here a > 0. The system of equations are solved and last values serve as initial conditions for the new situation (system of equations may change). That works fine. However, sometimes the ode-solver itself takes unfortunate long so I would like to interrupt and start with another set of initial conditions again. So, I want to interrupt the solver during its integration process.
while a > 0
% build system called set_of_equations with sprintf -> y = ...
eval(set_of_equations)
...
options = odeset('Events',@events_zero,'MaxStep',0.001,'RelTol',1e-3);
t = ode15i(y,tspan,y0,yd0,options);
% -> if interrupted by timing conditions choose another y0 yd0 and continue
end
Thanks. Franziska
  2 Comments
Mahdi
Mahdi on 3 Apr 2013
Please reply as a comment to keep it all within the same thread for future reference.
Unless you go into the loops for ode15i, this is almost impossible to do. The feature hasn't been implemented to my knowledge. You can try to do it with while statements (and the way I suggested) into the ode15i function.
This link discusses why MATLAB can't have this feature, and I don't think they've implemented it since then.
Franziska
Franziska on 4 Apr 2013
Thanks for your reply. Thus, I have to find another solution.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!