How to define event function in ode45 solver as the slop of variable achieve certain value?

1 visualización (últimos 30 días)
Hi! I want to solve the Van Der Pol equations using ode45 with event function as its stopping criteria. I would like to set the stopping criteria as the slope of is small enough:
where is the change of the value of at the nearest time step and its previous time step.
I am not sure can event function access the value of both at the current time and the previous time, since it seems that the events function receives both the current time and the current state vector.
In this case, how to revise the code? Thanks a lot for any suggestion!
Here is the code
function main
options = odeset('Events',@event_function);
initial_cond = [2;0;0];
[t,y] = ode45(@vanderpol,[0 20],initial_cond,options);
plot(t,y(:,1),'-o',t,y(:,2),'-o',t,y(:,3),'-o');
title('Solution of van der Pol Equation (\mu = 1) with ODE45');
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2','z');
function dydt = vanderpol(t,y)
dydt = [y(2); (1-y(1)^2)*y(2)-y(1); y(1)^2+y(2)^2];
function [value,isterminal,direction] = event_function(t,y)
value = y(1)-0.1; % when value = 0, an event is triggered
isterminal = 1; % terminate after the first event
direction = 0; % get all the zeros

Respuesta aceptada

Torsten
Torsten el 4 de Mayo de 2022
Editada: Torsten el 4 de Mayo de 2022
Maybe someone could answer if he/she knew what z is in your code. Is it y(1), y(2) or y(3) ?
Anyhow: The slopes of your variables is given by
dydt = [y(2); (1-y(1)^2)*y(2)-y(1); y(1)^2+y(2)^2];
So choose the one you need to specify your event.
  3 comentarios
Torsten
Torsten el 4 de Mayo de 2022
Editada: Torsten el 4 de Mayo de 2022
The events function only refers to current states, but you could save the value of the last call. See
for details.
H_W
H_W el 4 de Mayo de 2022
This should be really helpful to my problem! Thanks a lot for the link!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by