MATLAB: HOW CAN I USE EVENT FUNCTIONS IN ODE45 TO CHANGE THE SYSTEOM OF ODEs WHEN ONE OF THE SOLUTIONS REACHES SOME VALUE (and the final values of the solutions becomes the initial conditions for the new system)

event functionsode45 event functionsodes event function

HELLO
Is there anyone who can help me with this question ? and thanks in advance …
I have a system of ODEs with initial conditions (first order) that is given bellow, but this system changes when the value of y(2) changes.
the initial conditions of the new system are the final values of the solutions of the previous system.
when y(2)<T_b, I have this sytem
%======================================
%y(2)< T_b
dydt(1) = A – B*y(1);
dydt(2) = (-a + b*y(1))*(y(2) – c) + d;
dydt(3) = 0;
%========================================
and when y(2)>=T_b the system becomes like this
%========================================
%y(2)>= T_b
dydt(1) = -C*(y(2)-T_b);
dydt(2) = D – E*y(2);
dydt(3) = G*(y(2)-T_b);
%=========================================
and when y(2) comes back under T_b (y(2)<T_b, the system becomes like this
%=========================================
%y(2)< T_b
dydt(1) = 0;
dydt(2) = h_1 – h_2*(y(2)-T_inf) -h_3*(y(2)-T_b);
dydt(3) = -K
%=========================================

Best Answer

options1 = odeset('Events', @first_event_fcn);
options2 = odeset('Events', @second_event_fcn);
[t1, Y1] = ode45(@first_fcn, tspan, initial_conditions, options1);
[t2,Y2] = ode45(@second_fcn, [t1(end), tspan(end)], Y1(end,:), options2);
[t3,Y3] = ode45(@third_fcn, [t2(end), tspan(end)], Y2(end,:));
t = [t1; t2(2:end); t3(2:end)];
Y = [Y1; Y2(2:end,:); Y3(2:end,:)];
The 2:end is used because the first output of the second call will match the last output of the first call.