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

13 views (last 30 days)
Samuele Bolotta on 7 Mar 2021
Edited: Samuele Bolotta on 9 Mar 2021
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!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Steven Lord on 7 Mar 2021
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.
##### 1 CommentShowHide None
Samuele Bolotta on 9 Mar 2021
Thank you!
What are the advantages of using this approach rather than the "ODE with Time-Dependent Terms" example that is mentioned in the ode45 documentation?
Best,
Samuele

Sign in to comment.

### More Answers (1)

Jan on 7 Mar 2021
ODE45 is designe to integrate smooth functions only. To change a parameter you have to integrate in chunks:
tSwitch1 = 10.0
tSwtich2 = 10.1;
t0 = 0;
...
I = 0.1;
[time1, V1] = ode45(@(t,y), @ODEMAT(t, y, I), [t0, tSwitch1], y0);
I = 0.0;
y0 = V1(end, :);
[time2, V2] = ode45(@(t,y), @ODEMAT(t, y, I), [tSwitch1, tSwitch2], y0);
I = 0.1;
y0 = V2(end, :);
[time3, V3] = ode45(@(t,y), @ODEMAT(t, y, I), [tSwitch1, tEnd], y0);
time = cat(1, time1, time2, time3);
V = cat(1, V1, V2, V3);
...
function [dydt] = ODEMAT(t, y, I)
% Omit the definition of I inside this function.
...
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!