MATLAB: When using ODE45, can I specify a variable to assume two different values during the timespan

currentintegrationMATLABodeode45

I have tried in different ways to see what happens to voltage V and gating conductances m, n and h when, at time step x, current I switched from 0 to 0.1, and then at time step x + n it gets back to 0. However, it looks like regardless of what I do, ODE45 still assumes I is either 0 or 0.1 for the whole time span.
This is the function. In this case the current I is 0.1 for the whole time span. Is there a way to make it 0 everywhere except for 10 ms in the middle, for example?
function ODE_Hodgkin_Huxley (varargin)
t=0:0.1:25; %Time Array ms
V=-60; % Initial Membrane voltage
m=alpham(V)/(alpham(V)+betam(V)); % Initial m-value
n=alphan(V)/(alphan(V)+betan(V)); % Initial n-value
h=alphah(V)/(alphah(V)+betah(V)); % Initial h-value
y0=[V;n;m;h];
tspan = [0,max(t)];
%Matlab's ode45 function
[time,V] = ode45(@ODEMAT,tspan,y0);
OD=V(:,1);
ODn=V(:,2);
ODm=V(:,3);
ODh=V(:,4);
[r,c] = size(time);
I = ones (r,c) ./ 10; %Current
figure
subplot(3,1,1)
plot(time,OD);
legend('ODE45 solver');
xlabel('Time (ms)');
ylabel('Voltage (mV)');
title('Voltage Change for Hodgkin-Huxley Model');
subplot(3,1,2)
plot(time,I);
legend('Current injected')
xlabel('Time (ms)')
ylabel('Ampere')
title('Current')
subplot(3,1,3)
plot(time,[ODn,ODm,ODh]);
legend('ODn','ODm','ODh');
xlabel('Time (ms)')
ylabel('Value')
title('Gating variables'
end
function [dydt] = ODEMAT(t,y)
%Constants
ENa=55; % mv Na reversal potential
EK=-72; % mv K reversal potential
El=-49; % mv Leakage reversal potential
%Values of conductances
gbarl=0.003; % mS/cm^2 Leakage conductance
gbarNa=1.2; % mS/cm^2 Na conductance
gbarK=0.36; % mS/cm^2 K conductancence
I = 0.1; %Applied constant
Cm = 0.01; % Capacitance
% Values set to equal input values
V = y(1);
n = y(2);
m = y(3);
h = y(4);
gNa = gbarNa*m^3*h;
gK = gbarK*n^4;
gL = gbarl;
INa=gNa*(V-ENa);
IK=gK*(V-EK);
Il=gL*(V-El);
dydt = [((1/Cm)*(I-(INa+IK+Il))); % Normal case
alphan(V)*(1-n)-betan(V)*n;
alpham(V)*(1-m)-betam(V)*m;
alphah(V)*(1-h)-betah(V)*h];
end
I attach the other functions here.
Thank you!

Best Answer

Solve the system with V = 0 up until the time when it should change. If you're not sure of the exact time when it should change, use an events function to determine when it should change. See the ballode example for how to use events functions with the ODE solvers.
Use the final result from the first call to the ODE solver to generate initial conditions for the second call to the ODE solver, which solves the ODE with V = 0.1 over the interval when it has that value.
Use the final result from the second call to the ODE solver to generate initial conditions for the third call to the ODE solver, which solves the ODE with V = 0 over the remaining interval.
Related Question