How to stop an iteration inside a for loop when its execution exceeds some period of time
6 ビュー (過去 30 日間)
古いコメントを表示
Khalil Messaoudi
2022 年 5 月 31 日
コメント済み: Khalil Messaoudi
2022 年 6 月 1 日
Hello,
I have the following problem. I'm iterativing in a for loop. Sometimes an iteration takes a lot of time, therefore I want to skip it and move to the next iteration. I tried with tic toc but it does not seem to work.
Here is the code :
for i=1:9
[alpha_w2u,C_w2u,m_w2u] = FL_WSGP1(xu,yu,d,eps1,sigma,K,c,b,0.5 *10^(i-5)) ;
pFeLi.f = @(xk) pred1(alpha_w2u,C_w2u,m_w2u,xk,K,0) ;
ctrl = @(t,xu) ctrlFeLi(t,xu,pFeLi,reffun);
tic
while (toc<2)
[t4u,x4u] = ode45 (@(t4u,x4u) dynAffine(t4u,x4u,ctrl,pdyn),[0 50],xin);
end
x4uop(i) = x4u(end) ;
end
How can I stop the execution of :
[t4u,x4u] = = ode45 (@(t4u,x4u) dynAffine(t4u,x4u,ctrl,pdyn),[0 50],xin);
if it takes more than 2 seconds and then move to the next iteration.
Thanks in advance
0 件のコメント
採用された回答
Walter Roberson
2022 年 5 月 31 日
use an ode event function
@(t,y)my_event(t,y,tic)
have the function test whether toc() of the input tic exceeds the limit and if so signal termination
9 件のコメント
Torsten
2022 年 5 月 31 日
編集済み: Torsten
2022 年 5 月 31 日
Take inittime as
inittime = tic
before you call ode45.
Pass "inittime" to "interrupt". Taking "toc(inittime)" therein gives you the elapsed time since you called ode45.
So ode45 will stop if the elapsed time since you called it is > 2 seconds.
Or do I miss something ?
その他の回答 (1 件)
Image Analyst
2022 年 5 月 31 日
編集済み: Image Analyst
2022 年 5 月 31 日
I don't think you can programmatically stop a canned function once it's been called. But for other situations...
Try this:
for k = 1 : 100000
startTime = tic; % Time at start of this iteration.
% some code that takes time...then, whenever you want to check on the time:
elapsedSeconds = toc(startTime);
if elapsedSeconds > 2
continue;
end
end
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!