Confusion on event function

3 views (last 30 days)
Hi all, I want to incorporate an event into my ode45 code. The event is that every time the value y reaches 1 (initial condition 0) it should stop integrating and must restart from zero. I will share the code I have written below. Looks fine, but I am not able to understand how it works. Actually where did I set the event in this code? (I was just imitating a code given in the documentation). If it works for N=1, I need to generalise it for large number of systems. Will that be possible?
N=1;
m=0.3;
%Initial values
ystart=zeros(N,1);
options = odeset('Events',@SpikeEvents,'RelTol',1e-5,'AbsTol',1e-4);
%ODE solver
[t,y,te,ye,ie]=ode45(@ELIF,tspan,ystart,options);
%Plotting
plot(t,y)
xlabel('time')
ylabel('voltage')
title('Leaky IF with spike reset')
%Defining event
%Defining event
function [value,isterminal,direction] = SpikeEvents(t,y)
value = y(1);
isterminal = 1; % Stop the integration
direction = 1;
end
%Defining the system of equations
function dydt=ELIF(t,y,m)
N=1;
m=0.3;
dydt=zeros(N,1);
for i=1:N
dydt(i)=-y(i)+m; %The model equation
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 26 Oct 2018
Event functions cannot reset the integration by themselves. Instead what you need to do is configure the event function to signal termination, and then you need to loop, something like
K = 1;
start_time = tspan(1);
end_time = tspan(2);
time_tolerance = 1e-3;
y0 = ystart;
while true
[t{K}, y{K}, te{K}, ye{K}, ie{K}]=ode45(@ELIF, [start_time end_time], ystart, options);
ended_at = t{K}(end);
if end_time - ended_at <= time_tolerance
break;
end
start_time = ended_at;
y0 = y{K}(end,:);
y0(1) = 0; %restart integration from 0
K = K + 1;
end
Then the overall path would be found be concatenating together all of the t{:} and y{:}, except omitting the overlap time point.
  6 Comments
Steven Lord
Steven Lord on 26 Oct 2018
The standard simple example I point people to when trying to explain ODE solver events is the ballode example. It's similar to the one Walter posted, though it grows the arrays of output in its for loop rather than using cell arrays.
Vipin  Padinjarath
Vipin Padinjarath on 26 Oct 2018
I have gone through the ballode code. Yet I could not fully understand how it works. Thanks for the input. These are great helps.

Sign in to comment.

More Answers (1)

Vipin  Padinjarath
Vipin Padinjarath on 30 Oct 2018
In Walter's code, we need to guess the end time? I mean do we need to guess the time when the first event would occur?
  12 Comments
Walter Roberson
Walter Roberson on 8 Dec 2018
Edited: Walter Roberson on 12 Dec 2018
y-1
if you want to test all of the y values at the same time which is what I have interpreted your question about simultaneous systems to be .
Vipin  Padinjarath
Vipin Padinjarath on 12 Dec 2018
Okay. Thank you very much.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!