How to stop a function

115 次查看(过去 30 天)
Edoardo Melloni
Edoardo Melloni 2013-11-13
评论: idris 2024-4-17
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?

回答(3 个)

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

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2013-11-13
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 个评论
Edoardo Melloni
Edoardo Melloni 2013-11-14
编辑:Edoardo Melloni 2013-11-15
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?

请先登录,再进行评论。


Michael O'Connell
Michael O'Connell 2014-7-28
编辑:Michael O'Connell 2014-7-28
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.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by