Borrar filtros
Borrar filtros

How to stop a function

125 visualizaciones (últimos 30 días)
Edoardo Melloni
Edoardo Melloni el 13 de Nov. de 2013
Comentada: idris el 17 de Abr. de 2024
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?

Respuestas (3)

Sean de Wolski
Sean de Wolski el 13 de Nov. de 2013
Editada: Sean de Wolski el 13 de Nov. de 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 comentarios
Edoardo Melloni
Edoardo Melloni el 14 de Nov. de 2013
Thanks, see the my answer to Walter Roberson
idris
idris el 17 de Abr. de 2024
Hi, can anyone share any good matlab ebook? If yes, kindly send to: iaabdulhameed@knu.ac.kr

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 13 de Nov. de 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 comentario
Edoardo Melloni
Edoardo Melloni el 14 de Nov. de 2013
Editada: Edoardo Melloni el 15 de Nov. de 2013
Thanks for you answer, I have seen the example for the events inside an ode function but I'm not able to implement the event in my function. I think the problem is that the example only integrates one thing, the ball's height, while I'm integrating +1000 equations and I need to stop the ode15s when, let's say, the fourth element reaches a certain value. Now, when I write
function [value,isterminal,direction] = events(t,y)
value = 0;
isterminal = 1;
direction = 0;
end
How can I tell to the function that the 4th element of the yODE vector must be 0?
Other thing, this event only occurs when the function reaches a certain value, how can I make the event happen also if the function derivative reaches a certain value?
Edit :
With respect to the fourth element I solved as it follows
value=y(4)-0;
Does someone have any idea on how to fill the condition on a derivative?

Iniciar sesión para comentar.


Michael O'Connell
Michael O'Connell el 28 de Jul. de 2014
Editada: Michael O'Connell el 28 de Jul. de 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.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by