How can I stop ode45 when certain derivative is negative?

5 visualizzazioni (ultimi 30 giorni)
I have a set of differential equations which I am trying to integrate.
I have created this function in order for the integration to stop when a value is reached:
function [value, isterminal, direction] = myEventFunction(t, Y, c0_CO)
tol = 0.001;
value = Y(1) - 0.001*c0_CO;
isterminal = 1; % Stop the integration
direction = 0;
end
However, I would like to add another condition for value, and it is
value = (Y(1) - 0.001*c0_cO) || (dYdt(6) < 0)
I have been trying many things but I am failing at making dYdt an input for this function, and I do not know how to check it otherwise and stop it.
If anyone could help me with this I would really appreciate it :)

Risposte (1)

Steven Lord
Steven Lord il 30 Nov 2021
Your events function can return a vector of values and detect an event if any of the elements of that vector crosses zero. See the advanced example on this documentation page for an illustration of this technique.
edit orbitode
  2 Commenti
Juan Manuel Hussein Belda
Juan Manuel Hussein Belda il 30 Nov 2021
I have been looking at the example and I am not sure I understand it right. So, in my case, if I want it to stop when one of the derivatives of Y (integrated in ode45), then I should copy in the event function the formula for such derivative?
Juan Manuel Hussein Belda
Juan Manuel Hussein Belda il 30 Nov 2021
The following happens to me:
Thus, I would like the integration around time 3e-3 seconds, because then it starts oscillate. I do not want to remove, however, the steady state for the temperature. Thus, I wanted to stop whenever a negative slope was reached (which I think leads to the oscillatory behaviour in Figure 3). I am somehow confused because I copied the formula for dTdt and either it never stops or it never reaches a negative derivative (which is counterintuitive from Figure 3).
Thank you!

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by