How to stop a function
178 views (last 30 days)
Show older comments
Hi everybody!
I'm having a problem: during the execution of a ode15s function I want to stop the function if, let's say, the first differential dy(1) is less than 10^-6.
[tODE,yODE]=ode15s(@function1,tSpan,y0,[]);
When the function1 stops I want to save all the yODE calculated until the "stop" moment, return to my main file and call another function with an ode45
Then I want to solve this ode45 whose initial values are the output values of the first ode15s, this is why I need to save the yODE generated by the function1.
In the function1 I wrote at the end of the function the following code:
if dy(1)<=10^-6
disp('verified')
return
end
When I run the program, in the command window the word "verified" appears so I know that the condition is true, but the function continues to run and the word "verified" is repeated unitil the end of the ode15s execution, as if the "return" command was not effective.
Why this is happening?
Other (or equivalent) question:
Is it possible to have an output from the ode15s function if I interrupt its execution?
0 Comments
Answers (3)
Sean de Wolski
on 13 Nov 2013
Edited: Sean de Wolski
on 13 Nov 2013
return only returns out of the innermost function. Thus is it jumps out of your ode function, function1, but not out of the solver. The easiest way to jump all of the way out is to throw an exception:
error('Breaking out of function1');
More per comment
You can use an OutputFcn to store the intermediate results. The OutputFcn is set using odeset
doc odeset %documentation
Inside of your OutputFcn, you can store/save/print/whatever the intermediate steps.
4 Comments
idris
on 17 Apr 2024
Hi, can anyone share any good matlab ebook? If yes, kindly send to: iaabdulhameed@knu.ac.kr
Walter Roberson
on 13 Nov 2013
Use odeset to create an options structure in which you create an event function, in which you specify an events function. Use the value and isterminal outputs to control the stop. See the example at http://www.mathworks.com/help/matlab/math/ordinary-differential-equations.html#f1-669698
1 Comment
Michael O'Connell
on 28 Jul 2014
Edited: Michael O'Connell
on 28 Jul 2014
I get around this problem by causing an error inside the function1 script (as was suggested above), but it requires putting the call to ode15s within a try/catch statement. That way, when the error is caused in function1, it exits function1 AND ode15s but doesn't kill your whole code. You probably just need to make sure your output variables get some default definition in the catch statement. Like so:
try
[t,y] = ode15s(@function1, etc...)
catch
t = []; y = []; %just so they aren't undefined
% If I want to mark this iteration as bad, I will put something like
% y = NaN;
end
I'm not sure if this is considered good practice, but it works.
0 Comments
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!