How to stop ode45 when value reach certain value other-than zero
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I need to stop the ode when y(2) is 0.2 and here is the function I used for the solver but It doesn't work. Any thing wrong with it ?
function [val, terminate, dir]= stopevents(t,y)
val=(y(2)==0.2)-0.5;
terminate=1;
dir=0;
end
0 commentaires
Réponses (2)
Jan
le 26 Avr 2018
Modifié(e) : Jan
le 26 Avr 2018
An event occurs when value(i) is equal to zero.
function [value, terminate, direction] = stopevents(t, y)
value = y(2) - 0.2;
terminate = 1;
direction = 0;
end
The event function must be smooth. It is extremely unlikely, that the integration meets the point y(2)==0.2 exactly. Therefore your event function does not trigger. In addtion:
val = (y(2)==0.2)-0.5;
replies -0.5 or 0.5, but never 0, which would trigger the event.
By the way: Avoid using "dir" as name of a variable, because this shadows an important built-in function.
5 commentaires
Bjorn Gustavsson
le 27 Avr 2018
@jan: I guess what was meant with y(2) is y(t) for t=2, and not the second component of y
Bjorn Gustavsson
le 26 Avr 2018
You should be able to use the "events handling", look at the code for ballode.m for an example on how to handle it.
HTH
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!