Confusion on event function
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
Vipin Padinjarath
el 30 de Oct. de 2018
1 voto
12 comentarios
Walter Roberson
el 30 de Oct. de 2018
No, just use the last element of your current tspan variable, the point where you would want to end anyhow.
Vipin Padinjarath
el 30 de Oct. de 2018
Vipin Padinjarath
el 14 de Nov. de 2018
Vipin Padinjarath
el 3 de Dic. de 2018
Editada: Vipin Padinjarath
el 3 de Dic. de 2018
Walter Roberson
el 3 de Dic. de 2018
Editada: Walter Roberson
el 3 de Dic. de 2018
why loop to N and have the if? Why not loop to N-1 ?
You would not even need to loop just vectorize.
Walter Roberson
el 3 de Dic. de 2018
value = y - 1;
would appear to provide testing for all of the systems .
Vipin Padinjarath
el 3 de Dic. de 2018
Vipin Padinjarath
el 3 de Dic. de 2018
Walter Roberson
el 3 de Dic. de 2018
no loop just
value = y - 1;
Vipin Padinjarath
el 7 de Dic. de 2018
Walter Roberson
el 8 de Dic. de 2018
Editada: Walter Roberson
el 12 de Dic. de 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
el 12 de Dic. de 2018
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!