Is it possible for define the event function of ode45 solver in matlab in this way?

212 Views Asked by At

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 $y(3)$ is small enough: $$\frac{\Delta y(3)}{\Delta t}\bigg|<10^{-16}$$ where $\Delta y(3)$ is the change of the value of $z$ at the nearest time step and its previous time step.

I am not sure can event function access the value of $y(3)$ 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!

function main

options = odeset('Events',@event_function);
initial_cond = [2;0;0];%  [y1_0; y2_0; 0]
[t,y] = ode45(@vanderpol,[0 20],initial_cond,options);

y
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
1

There are 1 best solutions below

0
On BEST ANSWER

Use your ODE function (vanderpol) in the event function to compute the actual slope, rather than trying to find a less accurate finite difference derivative.