Using Event function to solve ODEs
Mostrar comentarios más antiguos
I am solving a set of n differential equations using ode15s. x is the n-dimensional vector.
sol = ode45(@this_model, [0,tmax], x, options);
I need to do this - whenever the value of any one of the values x(i) in the vector x falls below a threshold value th, I stop the solver, set the value of x(i) to zero and restart the solver again.
Thus,
options = odeset('NonNegative',1:n,'Events',@this_event);
and the event function is:
function [value,isterminal,direction] = this_event(~,x)
th = 1e-3;
value = min(x) > th;
isterminal = 1;
direction = 0;
end
However, this gives the error : Undefined function 'sign' for input arguments of type 'logical'.
Instead if I define my events function as:
function [value,isterminal,direction] = this_event(~,x)
th = 1e-3;
if min(x) < th
value = 0;
else
value = 1;
end
isterminal = 1;
direction = 0;
end
But when I do this, the solver does not stop even when the values of x(i) go below the threshold th.
Where am I wrong? Please help!
2 comentarios
Torsten
el 11 de Mzo. de 2019
Why don't you simply set
value = x - th
?
Aswin Krishna
el 12 de Mzo. de 2019
Editada: Aswin Krishna
el 12 de Mzo. de 2019
Respuestas (0)
Categorías
Más información sobre Ordinary Differential Equations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!