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

16 ビュー (過去 30 日間)
Franziska
Franziska 2013 年 4 月 3 日
コメント済み: Karim Shawki 2019 年 7 月 17 日
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

採用された回答

Mahdi
Mahdi 2013 年 4 月 3 日
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.

その他の回答 (2 件)

Jared Barber
Jared Barber 2019 年 7 月 15 日
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.
  1 件のコメント
Karim Shawki
Karim Shawki 2019 年 7 月 17 日
Works perfectly, thanks Jared!

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


Franziska
Franziska 2013 年 4 月 3 日
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 件のコメント
Mahdi
Mahdi 2013 年 4 月 3 日
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 2013 年 4 月 4 日
Thanks for your reply. Thus, I have to find another solution.

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by