Generated Code

The following is matlab code generated by the CellML API from this CellML file. (Back to language selection)

The raw code is available.

```function [VOI, STATES, ALGEBRAIC, CONSTANTS] = mainFunction()
% This is the "main function".  In Matlab, things work best if you rename this function to match the filename.
[VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel();
end

function [algebraicVariableCount] = getAlgebraicVariableCount()
% Used later when setting a global variable with the number of algebraic variables.
% Note: This is not the "main method".
algebraicVariableCount =12;
end
% There are a total of 5 entries in each of the rate and state variable arrays.
% There are a total of 31 entries in the constant variable array.
%

function [VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel()
% Create ALGEBRAIC of correct size
global algebraicVariableCount;  algebraicVariableCount = getAlgebraicVariableCount();
% Initialise constants and state variables
[INIT_STATES, CONSTANTS] = initConsts;

% Set timespan to solve over
tspan = [0, 10];

% Set numerical accuracy options for ODE solver
options = odeset('RelTol', 1e-06, 'AbsTol', 1e-06, 'MaxStep', 1);

% Solve model with ODE solver
[VOI, STATES] = ode15s(@(VOI, STATES)computeRates(VOI, STATES, CONSTANTS), tspan, INIT_STATES, options);

% Compute algebraic variables
[RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS);
ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI);

% Plot state variables against variable of integration
[LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends();
figure();
plot(VOI, STATES);
xlabel(LEGEND_VOI);
l = legend(LEGEND_STATES);
set(l,'Interpreter','none');
end

function [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends()
LEGEND_STATES = ''; LEGEND_ALGEBRAIC = ''; LEGEND_VOI = ''; LEGEND_CONSTANTS = '';
LEGEND_VOI = strpad('t in component interface (ms)');
LEGEND_CONSTANTS(:,1) = strpad('Cm in component interface (uFpmmsq)');
LEGEND_CONSTANTS(:,2) = strpad('Am in component interface (pmm)');
LEGEND_ALGEBRAIC(:,1) = strpad('Istim in component interface (uApmmcu)');
LEGEND_STATES(:,1) = strpad('Vm in component membrane (mV)');
LEGEND_STATES(:,2) = strpad('Vt in component Ttubular_current_Vt_var (mV)');
LEGEND_STATES(:,3) = strpad('m in component sodium_current_m_gate (dimensionless)');
LEGEND_STATES(:,4) = strpad('h in component sodium_current_h_gate (dimensionless)');
LEGEND_STATES(:,5) = strpad('n in component potassium_current_n_gate (dimensionless)');
LEGEND_ALGEBRAIC(:,5) = strpad('INa in component sodium_current (uApmmsq)');
LEGEND_ALGEBRAIC(:,10) = strpad('IK in component potassium_current (uApmmsq)');
LEGEND_ALGEBRAIC(:,11) = strpad('IL in component leak_current (uApmmsq)');
LEGEND_ALGEBRAIC(:,12) = strpad('IT in component Ttubular_current (uApmmsq)');
LEGEND_ALGEBRAIC(:,6) = strpad('IStimC in component interface (uApmmcu)');
LEGEND_CONSTANTS(:,31) = strpad('AmC in component interface (pmm)');
LEGEND_CONSTANTS(:,3) = strpad('IstimStart in component interface (ms)');
LEGEND_CONSTANTS(:,4) = strpad('IstimEnd in component interface (ms)');
LEGEND_CONSTANTS(:,5) = strpad('IstimAmplitude in component interface (uApmmcu)');
LEGEND_CONSTANTS(:,6) = strpad('IstimPeriod in component interface (ms)');
LEGEND_CONSTANTS(:,7) = strpad('IstimPulseDuration in component interface (ms)');
LEGEND_CONSTANTS(:,8) = strpad('gNa_max in component sodium_current (mSpmmsq)');
LEGEND_CONSTANTS(:,9) = strpad('ENa in component sodium_current (mV)');
LEGEND_ALGEBRAIC(:,2) = strpad('alpha_m in component sodium_current_m_gate (pms)');
LEGEND_ALGEBRAIC(:,7) = strpad('beta_m in component sodium_current_m_gate (pms)');
LEGEND_CONSTANTS(:,10) = strpad('alpha_m_max in component sodium_current_m_gate (pms)');
LEGEND_CONSTANTS(:,11) = strpad('beta_m_max in component sodium_current_m_gate (pms)');
LEGEND_CONSTANTS(:,12) = strpad('Em in component sodium_current_m_gate (mV)');
LEGEND_CONSTANTS(:,13) = strpad('v_alpha_m in component sodium_current_m_gate (dimensionless)');
LEGEND_CONSTANTS(:,14) = strpad('v_beta_m in component sodium_current_m_gate (mV)');
LEGEND_ALGEBRAIC(:,3) = strpad('alpha_h in component sodium_current_h_gate (pms)');
LEGEND_ALGEBRAIC(:,8) = strpad('beta_h in component sodium_current_h_gate (pms)');
LEGEND_CONSTANTS(:,15) = strpad('alpha_h_max in component sodium_current_h_gate (pms)');
LEGEND_CONSTANTS(:,16) = strpad('beta_h_max in component sodium_current_h_gate (pms)');
LEGEND_CONSTANTS(:,17) = strpad('Eh in component sodium_current_h_gate (mV)');
LEGEND_CONSTANTS(:,18) = strpad('v_alpha_h in component sodium_current_h_gate (mV)');
LEGEND_CONSTANTS(:,19) = strpad('v_beta_h in component sodium_current_h_gate (mV)');
LEGEND_CONSTANTS(:,20) = strpad('gK_max in component potassium_current (mSpmmsq)');
LEGEND_CONSTANTS(:,21) = strpad('EK in component potassium_current (mV)');
LEGEND_ALGEBRAIC(:,4) = strpad('alpha_n in component potassium_current_n_gate (pms)');
LEGEND_ALGEBRAIC(:,9) = strpad('beta_n in component potassium_current_n_gate (pms)');
LEGEND_CONSTANTS(:,22) = strpad('alpha_n_max in component potassium_current_n_gate (pms)');
LEGEND_CONSTANTS(:,23) = strpad('beta_n_max in component potassium_current_n_gate (pms)');
LEGEND_CONSTANTS(:,24) = strpad('En in component potassium_current_n_gate (mV)');
LEGEND_CONSTANTS(:,25) = strpad('v_alpha_n in component potassium_current_n_gate (dimensionless)');
LEGEND_CONSTANTS(:,26) = strpad('v_beta_n in component potassium_current_n_gate (mV)');
LEGEND_CONSTANTS(:,27) = strpad('EL in component leak_current (mV)');
LEGEND_CONSTANTS(:,28) = strpad('gL_max in component leak_current (mSpmmsq)');
LEGEND_CONSTANTS(:,29) = strpad('Rs in component Ttubular_current (mmsqpmS)');
LEGEND_CONSTANTS(:,30) = strpad('Ct in component Ttubular_current_Vt_var (uFpmmsq)');
LEGEND_RATES(:,1) = strpad('d/dt Vm in component membrane (mV)');
LEGEND_RATES(:,3) = strpad('d/dt m in component sodium_current_m_gate (dimensionless)');
LEGEND_RATES(:,4) = strpad('d/dt h in component sodium_current_h_gate (dimensionless)');
LEGEND_RATES(:,5) = strpad('d/dt n in component potassium_current_n_gate (dimensionless)');
LEGEND_RATES(:,2) = strpad('d/dt Vt in component Ttubular_current_Vt_var (mV)');
LEGEND_STATES  = LEGEND_STATES';
LEGEND_ALGEBRAIC = LEGEND_ALGEBRAIC';
LEGEND_RATES = LEGEND_RATES';
LEGEND_CONSTANTS = LEGEND_CONSTANTS';
end

function [STATES, CONSTANTS] = initConsts()
VOI = 0; CONSTANTS = []; STATES = []; ALGEBRAIC = [];
CONSTANTS(:,1) = 0.009;
CONSTANTS(:,2) = 200.0;
STATES(:,1) = -95.0;
STATES(:,2) = -95.0;
STATES(:,3) = 0.0;
STATES(:,4) = 1.0;
STATES(:,5) = 0.0;
CONSTANTS(:,3) = 10;
CONSTANTS(:,4) = 50000;
CONSTANTS(:,5) = 0.5;
CONSTANTS(:,6) = 1000;
CONSTANTS(:,7) = 1;
CONSTANTS(:,8) = 1.8;
CONSTANTS(:,9) = 50.0;
CONSTANTS(:,10) = 0.208;
CONSTANTS(:,11) = 2.081;
CONSTANTS(:,12) = -42.0;
CONSTANTS(:,13) = 10.0;
CONSTANTS(:,14) = 18.0;
CONSTANTS(:,15) = 0.0156;
CONSTANTS(:,16) = 3.382;
CONSTANTS(:,17) = -41.0;
CONSTANTS(:,18) = 14.7;
CONSTANTS(:,19) = 7.6;
CONSTANTS(:,20) = 0.415;
CONSTANTS(:,21) = -70.0;
CONSTANTS(:,22) = 0.0229;
CONSTANTS(:,23) = 0.09616;
CONSTANTS(:,24) = -40.0;
CONSTANTS(:,25) = 7.0;
CONSTANTS(:,26) = 40.0;
CONSTANTS(:,27) = -95.0;
CONSTANTS(:,28) = 0.0024;
CONSTANTS(:,29) = 15.0;
CONSTANTS(:,30) = 0.04;
CONSTANTS(:,31) = CONSTANTS(:,2);
if (isempty(STATES)), warning('Initial values for states not set');, end
end

function [RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS)
global algebraicVariableCount;
statesSize = size(STATES);
statesColumnCount = statesSize(2);
if ( statesColumnCount == 1)
STATES = STATES';
ALGEBRAIC = zeros(1, algebraicVariableCount);
utilOnes = 1;
else
statesRowCount = statesSize(1);
ALGEBRAIC = zeros(statesRowCount, algebraicVariableCount);
RATES = zeros(statesRowCount, statesColumnCount);
utilOnes = ones(statesRowCount, 1);
end
RATES(:,2) = (STATES(:,1) - STATES(:,2))./( CONSTANTS(:,29).*CONSTANTS(:,30));
ALGEBRAIC(:,2) = ( CONSTANTS(:,10).*(STATES(:,1) - CONSTANTS(:,12)))./(1.00000 - exp((CONSTANTS(:,12) - STATES(:,1))./CONSTANTS(:,13)));
ALGEBRAIC(:,7) =  CONSTANTS(:,11).*exp((CONSTANTS(:,12) - STATES(:,1))./CONSTANTS(:,14));
RATES(:,3) =  ALGEBRAIC(:,2).*(1.00000 - STATES(:,3)) -  ALGEBRAIC(:,7).*STATES(:,3);
ALGEBRAIC(:,3) =  CONSTANTS(:,15).*exp((CONSTANTS(:,17) - STATES(:,1))./CONSTANTS(:,18));
ALGEBRAIC(:,8) = CONSTANTS(:,16)./(1.00000+exp((CONSTANTS(:,17) - STATES(:,1))./CONSTANTS(:,19)));
RATES(:,4) =  ALGEBRAIC(:,3).*(1.00000 - STATES(:,4)) -  ALGEBRAIC(:,8).*STATES(:,4);
ALGEBRAIC(:,4) = ( CONSTANTS(:,22).*(STATES(:,1) - CONSTANTS(:,24)))./(1.00000 - exp((CONSTANTS(:,24) - STATES(:,1))./CONSTANTS(:,25)));
ALGEBRAIC(:,9) =  CONSTANTS(:,23).*exp((CONSTANTS(:,24) - STATES(:,1))./CONSTANTS(:,26));
RATES(:,5) =  ALGEBRAIC(:,4).*(1.00000 - STATES(:,5)) -  ALGEBRAIC(:,9).*STATES(:,5);
ALGEBRAIC(:,1) = piecewise({VOI>=CONSTANTS(:,3)&VOI<=CONSTANTS(:,4)&(VOI - CONSTANTS(:,3)) -  floor((VOI - CONSTANTS(:,3))./CONSTANTS(:,6)).*CONSTANTS(:,6)<=CONSTANTS(:,7), CONSTANTS(:,5) }, 0.00000);
ALGEBRAIC(:,5) =  CONSTANTS(:,8).*STATES(:,3).*STATES(:,3).*STATES(:,3).*STATES(:,4).*(STATES(:,1) - CONSTANTS(:,9));
ALGEBRAIC(:,10) =  CONSTANTS(:,20).*STATES(:,5).*STATES(:,5).*STATES(:,5).*STATES(:,5).*(STATES(:,1) - CONSTANTS(:,21));
ALGEBRAIC(:,11) =  CONSTANTS(:,28).*(STATES(:,1) - CONSTANTS(:,27));
ALGEBRAIC(:,12) = (STATES(:,1) - STATES(:,2))./CONSTANTS(:,29);
RATES(:,1) = (ALGEBRAIC(:,1) - (ALGEBRAIC(:,5)+ALGEBRAIC(:,10)+ALGEBRAIC(:,11)+ALGEBRAIC(:,12)))./CONSTANTS(:,1);
RATES = RATES';
end

% Calculate algebraic variables
function ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI)
statesSize = size(STATES);
statesColumnCount = statesSize(2);
if ( statesColumnCount == 1)
STATES = STATES';
utilOnes = 1;
else
statesRowCount = statesSize(1);
utilOnes = ones(statesRowCount, 1);
end
ALGEBRAIC(:,2) = ( CONSTANTS(:,10).*(STATES(:,1) - CONSTANTS(:,12)))./(1.00000 - exp((CONSTANTS(:,12) - STATES(:,1))./CONSTANTS(:,13)));
ALGEBRAIC(:,7) =  CONSTANTS(:,11).*exp((CONSTANTS(:,12) - STATES(:,1))./CONSTANTS(:,14));
ALGEBRAIC(:,3) =  CONSTANTS(:,15).*exp((CONSTANTS(:,17) - STATES(:,1))./CONSTANTS(:,18));
ALGEBRAIC(:,8) = CONSTANTS(:,16)./(1.00000+exp((CONSTANTS(:,17) - STATES(:,1))./CONSTANTS(:,19)));
ALGEBRAIC(:,4) = ( CONSTANTS(:,22).*(STATES(:,1) - CONSTANTS(:,24)))./(1.00000 - exp((CONSTANTS(:,24) - STATES(:,1))./CONSTANTS(:,25)));
ALGEBRAIC(:,9) =  CONSTANTS(:,23).*exp((CONSTANTS(:,24) - STATES(:,1))./CONSTANTS(:,26));
ALGEBRAIC(:,1) = piecewise({VOI>=CONSTANTS(:,3)&VOI<=CONSTANTS(:,4)&(VOI - CONSTANTS(:,3)) -  floor((VOI - CONSTANTS(:,3))./CONSTANTS(:,6)).*CONSTANTS(:,6)<=CONSTANTS(:,7), CONSTANTS(:,5) }, 0.00000);
ALGEBRAIC(:,5) =  CONSTANTS(:,8).*STATES(:,3).*STATES(:,3).*STATES(:,3).*STATES(:,4).*(STATES(:,1) - CONSTANTS(:,9));
ALGEBRAIC(:,10) =  CONSTANTS(:,20).*STATES(:,5).*STATES(:,5).*STATES(:,5).*STATES(:,5).*(STATES(:,1) - CONSTANTS(:,21));
ALGEBRAIC(:,11) =  CONSTANTS(:,28).*(STATES(:,1) - CONSTANTS(:,27));
ALGEBRAIC(:,12) = (STATES(:,1) - STATES(:,2))./CONSTANTS(:,29);
ALGEBRAIC(:,6) = ALGEBRAIC(:,1);
end

% Compute result of a piecewise function
function x = piecewise(cases, default)
set = [0];
for i = 1:2:length(cases)
if (length(cases{i+1}) == 1)
x(cases{i} & ~set,:) = cases{i+1};
else
x(cases{i} & ~set,:) = cases{i+1}(cases{i} & ~set);
end
set = set | cases{i};
if(set), break, end
end
if (length(default) == 1)
x(~set,:) = default;
else
x(~set,:) = default(~set);
end
end

% Pad out or shorten strings to a set length
req_length = 160;
insize = size(strin,2);
if insize > req_length
strout = strin(1:req_length);
else
strout = [strin, blanks(req_length - insize)];
end
end

```
Source
Derived from workspace Adrian, Chandler, Hodgkin, 1970 at changeset cdf8973b740d.
Collaboration
To begin collaborating on this work, please use your git client and issue this command: