Event functions with ode15s not halting integration
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MT
le 31 Août 2021
Réponse apportée : Walter Roberson
le 1 Sep 2021
I want to halt integration when the global variable "phase" switches from 2 to 3 or from 3 to 2 (it is always either 2 or 3). Here (below) is how I set this up in my event function, but it does not work even when I output value2 and see that it crosses zero many times. Does the value that crosses zero have to be one that is solved for by the ode solver? i.e. does it have to be part of the y vector?
function [value,isterminal,direction] = stopSolver(y)
global phase
value1 = y(1);
if phase == 3
value2 = 1;
elseif phase == 2
value2= -1;
end
value = [value1; value2];
isterminal = [1; 1]; % Stop the integration
direction = [0; 0]; % detect all zeros (default)
0 commentaires
Réponse acceptée
Walter Roberson
le 1 Sep 2021
Event function will have two parameters passed in; I am surprised that it did not complain about too many input arguments. Or did you configure the event function like @(t,y) stopSolver(y) ?
To answer your question: the vector of values you return does not have to have anything to do with the t or y.
However... event functions are not to be used to terminate immediately .
The ode*() solvers make trial proposals. They then check whether the Runge Kutta methods predict that the location is within integration tolerances. If the proposal seems successful, the proposed location is passed to the event function, which returns a verdict. If it fails, then the ode*() solvers start a phase of narrowing the steps, trying to find the boundary, under the assumption that it has overstepped the boundary; the solvers look for a narrow interval where the position fails on one side and passes on the other side, and when it has narrowed the interval to its satisfaction, it emits the values at that boundary and exits.
But... you are setting a global variable, and the global variable probably does not get reset, so the solvers are not going to be able to back up to the boundary.
Whatever code is used to detect the phase change should be in the event function instead of being managed as a global variable.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!