How to stop a function
186 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (3)
Sean de Wolski
le 13 Nov 2013
Modifié(e) : Sean de Wolski
le 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 commentaires
idris
le 17 Avr 2024
Hi, can anyone share any good matlab ebook? If yes, kindly send to: iaabdulhameed@knu.ac.kr
Walter Roberson
le 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 commentaire
Edoardo Melloni
le 14 Nov 2013
Modifié(e) : Edoardo Melloni
le 15 Nov 2013
Michael O'Connell
le 28 Juil 2014
Modifié(e) : Michael O'Connell
le 28 Juil 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 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!