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 =21;
end
% There are a total of 11 entries in each of the rate and state variable arrays.
% There are a total of 22 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('time in component environment (ms)');
    LEGEND_CONSTANTS(:,1) = strpad('R_gas_const in component parameters (millijoule_per_mole_kelvin)');
    LEGEND_CONSTANTS(:,2) = strpad('Temp in component parameters (kelvin)');
    LEGEND_CONSTANTS(:,3) = strpad('F in component parameters (coulomb_per_mole)');
    LEGEND_STATES(:,1) = strpad('V in component membrane (mV)');
    LEGEND_CONSTANTS(:,4) = strpad('Cm in component membrane (uF_per_cm2)');
    LEGEND_ALGEBRAIC(:,1) = strpad('i_app in component stimulus_protocol (uA_per_cm2)');
    LEGEND_ALGEBRAIC(:,13) = strpad('i_Na in component sodium_current (uA_per_cm2)');
    LEGEND_ALGEBRAIC(:,15) = strpad('i_K in component potassium_current (uA_per_cm2)');
    LEGEND_ALGEBRAIC(:,17) = strpad('i_leak in component leak_current (uA_per_cm2)');
    LEGEND_CONSTANTS(:,5) = strpad('IstimStart in component stimulus_protocol (ms)');
    LEGEND_CONSTANTS(:,6) = strpad('IstimEnd in component stimulus_protocol (ms)');
    LEGEND_CONSTANTS(:,7) = strpad('IstimAmplitude in component stimulus_protocol (uA_per_cm2)');
    LEGEND_CONSTANTS(:,8) = strpad('IstimPeriod in component stimulus_protocol (ms)');
    LEGEND_CONSTANTS(:,9) = strpad('IstimPulseDuration in component stimulus_protocol (ms)');
    LEGEND_ALGEBRAIC(:,11) = strpad('x_infinity in component sodium_current (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('alpha_x in component sodium_current (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('beta_x in component sodium_current (dimensionless)');
    LEGEND_STATES(:,2) = strpad('n in component potassium_current_n_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('alpha_n in component potassium_current_n_gate (per_ms)');
    LEGEND_ALGEBRAIC(:,7) = strpad('beta_n in component potassium_current_n_gate (per_ms)');
    LEGEND_ALGEBRAIC(:,3) = strpad('T in component transmitter_release (uM)');
    LEGEND_CONSTANTS(:,10) = strpad('T_bar in component transmitter_release (uM)');
    LEGEND_STATES(:,3) = strpad('R in component transmitter_release (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('kr_plus in component transmitter_release (per_uM_per_ms)');
    LEGEND_CONSTANTS(:,12) = strpad('kr_minus in component transmitter_release (per_ms)');
    LEGEND_ALGEBRAIC(:,20) = strpad('Ca in component calcium_concentration (uM)');
    LEGEND_CONSTANTS(:,13) = strpad('Ca_ex in component calcium_concentration (uM)');
    LEGEND_ALGEBRAIC(:,10) = strpad('Ca_open in component calcium_concentration (uM)');
    LEGEND_CONSTANTS(:,14) = strpad('Dc in component calcium_concentration (um2_per_second)');
    LEGEND_CONSTANTS(:,15) = strpad('r in component calcium_concentration (nm)');
    LEGEND_ALGEBRAIC(:,8) = strpad('sigma in component calcium_concentration (uM_per_ms)');
    LEGEND_ALGEBRAIC(:,4) = strpad('i_V in component calcium_concentration (uA)');
    LEGEND_CONSTANTS(:,16) = strpad('g_Ca in component calcium_concentration (pS)');
    LEGEND_CONSTANTS(:,17) = strpad('P in component calcium_concentration (mV_per_uM)');
    LEGEND_ALGEBRAIC(:,18) = strpad('O in component O (dimensionless)');
    LEGEND_ALGEBRAIC(:,12) = strpad('alpha in component rate_constants (per_ms)');
    LEGEND_ALGEBRAIC(:,14) = strpad('alpha_ in component rate_constants (per_ms)');
    LEGEND_ALGEBRAIC(:,16) = strpad('beta in component rate_constants (per_ms)');
    LEGEND_ALGEBRAIC(:,19) = strpad('beta_ in component rate_constants (per_ms)');
    LEGEND_ALGEBRAIC(:,21) = strpad('kG_plus in component rate_constants (per_ms)');
    LEGEND_CONSTANTS(:,18) = strpad('kG_minus in component rate_constants (per_ms)');
    LEGEND_CONSTANTS(:,19) = strpad('kG2_minus in component rate_constants (per_ms)');
    LEGEND_CONSTANTS(:,20) = strpad('kG3_minus in component rate_constants (per_ms)');
    LEGEND_STATES(:,4) = strpad('a in component rate_constants (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('ka_plus in component rate_constants (per_uM_per_ms)');
    LEGEND_CONSTANTS(:,22) = strpad('ka_minus in component rate_constants (per_ms)');
    LEGEND_STATES(:,5) = strpad('C1 in component C1 (dimensionless)');
    LEGEND_STATES(:,6) = strpad('C2 in component C2 (dimensionless)');
    LEGEND_STATES(:,7) = strpad('C_G1 in component C_G1 (dimensionless)');
    LEGEND_STATES(:,8) = strpad('C3 in component C3 (dimensionless)');
    LEGEND_STATES(:,9) = strpad('C_G2 in component C_G2 (dimensionless)');
    LEGEND_STATES(:,10) = strpad('C4 in component C4 (dimensionless)');
    LEGEND_STATES(:,11) = strpad('C_G3 in component C_G3 (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('C_G in component O (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt V in component membrane (mV)');
    LEGEND_RATES(:,2) = strpad('d/dt n in component potassium_current_n_gate (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt R in component transmitter_release (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt a in component rate_constants (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt C1 in component C1 (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt C2 in component C2 (dimensionless)');
    LEGEND_RATES(:,8) = strpad('d/dt C3 in component C3 (dimensionless)');
    LEGEND_RATES(:,10) = strpad('d/dt C4 in component C4 (dimensionless)');
    LEGEND_RATES(:,7) = strpad('d/dt C_G1 in component C_G1 (dimensionless)');
    LEGEND_RATES(:,9) = strpad('d/dt C_G2 in component C_G2 (dimensionless)');
    LEGEND_RATES(:,11) = strpad('d/dt C_G3 in component C_G3 (dimensionless)');
    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) = 8314.41;
    CONSTANTS(:,2) = 310;
    CONSTANTS(:,3) = 96485;
    STATES(:,1) = -65;
    CONSTANTS(:,4) = 1;
    CONSTANTS(:,5) = 10;
    CONSTANTS(:,6) = 50000;
    CONSTANTS(:,7) = 40.0;
    CONSTANTS(:,8) = 100;
    CONSTANTS(:,9) = 1;
    STATES(:,2) = 0;
    CONSTANTS(:,10) = 4000.0;
    STATES(:,3) = 0;
    CONSTANTS(:,11) = 0.15;
    CONSTANTS(:,12) = 2.5;
    CONSTANTS(:,13) = 2000.0;
    CONSTANTS(:,14) = 220;
    CONSTANTS(:,15) = 10;
    CONSTANTS(:,16) = 1.2;
    CONSTANTS(:,17) = 0.006;
    CONSTANTS(:,18) = 0.00025;
    CONSTANTS(:,19) = 0.016;
    CONSTANTS(:,20) = 1.024;
    STATES(:,4) = 0;
    CONSTANTS(:,21) = 200.0;
    CONSTANTS(:,22) = 0.0015;
    STATES(:,5) = 1;
    STATES(:,6) = 0;
    STATES(:,7) = 0;
    STATES(:,8) = 0;
    STATES(:,9) = 0;
    STATES(:,10) = 0;
    STATES(:,11) = 0;
    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
    ALGEBRAIC(:,3) =  CONSTANTS(:,10).*STATES(:,3);
    RATES(:,4) =  CONSTANTS(:,21).*ALGEBRAIC(:,3).*(1.00000 - STATES(:,4)) -  CONSTANTS(:,22).*STATES(:,4);
    ALGEBRAIC(:,2) = ( 0.0200000.*(STATES(:,1)+55.0000))./(1.00000 - exp( - (STATES(:,1)+55.0000)./10.0000));
    ALGEBRAIC(:,7) =  0.250000.*exp( - (STATES(:,1)+65.0000)./80.0000);
    RATES(:,2) =  ALGEBRAIC(:,2).*(1.00000 - STATES(:,2)) -  ALGEBRAIC(:,7).*STATES(:,2);
    ALGEBRAIC(:,1) = piecewise({VOI>=CONSTANTS(:,5)&VOI<=CONSTANTS(:,6)&(VOI - CONSTANTS(:,5)) -  floor((VOI - CONSTANTS(:,5))./CONSTANTS(:,8)).*CONSTANTS(:,8)<=CONSTANTS(:,9), CONSTANTS(:,7) }, 0.00000);
    ALGEBRAIC(:,6) = ( 0.200000.*(STATES(:,1)+40.0000))./(1.00000 -  1.00000.*exp( - (STATES(:,1)+40.0000)./10.0000));
    ALGEBRAIC(:,9) =  8.00000.*exp(1.00000./ - (STATES(:,1)+65.0000./18.0000));
    ALGEBRAIC(:,11) = ALGEBRAIC(:,6)./(ALGEBRAIC(:,6)+ALGEBRAIC(:,9));
    ALGEBRAIC(:,13) =  120.000.*power(ALGEBRAIC(:,11), 3.00000).*(1.00000 - STATES(:,2)).*(STATES(:,1) - 120.000);
    ALGEBRAIC(:,15) = ( 36.0000.*power(STATES(:,2), 4.00000).*(STATES(:,1)+77.0000))./1.00000;
    ALGEBRAIC(:,17) =  0.300000.*(STATES(:,1)+54.0000);
    RATES(:,1) =  - ((ALGEBRAIC(:,13)+ALGEBRAIC(:,15)+ALGEBRAIC(:,17)) - ALGEBRAIC(:,1))./CONSTANTS(:,4);
    ALGEBRAIC(:,18) = ((((((1.00000 - STATES(:,5)) - STATES(:,6)) - STATES(:,8)) - STATES(:,10)) - STATES(:,7)) - STATES(:,9)) - STATES(:,11);
    ALGEBRAIC(:,12) =  0.450000.*exp(STATES(:,1)./22.0000);
    ALGEBRAIC(:,16) =  0.0150000.*exp( - STATES(:,1)./14.0000);
    RATES(:,10) = ( 2.00000.*ALGEBRAIC(:,12).*STATES(:,8)+ 4.00000.*ALGEBRAIC(:,16).*ALGEBRAIC(:,18)) -  STATES(:,10).*( 3.00000.*ALGEBRAIC(:,16)+ALGEBRAIC(:,12));
    ALGEBRAIC(:,4) = ( (( CONSTANTS(:,16).*CONSTANTS(:,17).*2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2))).*CONSTANTS(:,13))./(1.00000 - exp(( 2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,8) =   - 5.18200.*ALGEBRAIC(:,4);
    ALGEBRAIC(:,10) = ALGEBRAIC(:,8)./( 2.00000.*CONSTANTS(:,14).*CONSTANTS(:,15).* pi);
    ALGEBRAIC(:,20) =  ALGEBRAIC(:,18).*ALGEBRAIC(:,10)+0.100000;
    RATES(:,3) =  CONSTANTS(:,11).*ALGEBRAIC(:,20).*(1.00000 - STATES(:,3)) -  CONSTANTS(:,12).*STATES(:,3);
    ALGEBRAIC(:,21) = ( 3.00000.*STATES(:,4))./(680.000+ 320.000.*STATES(:,4));
    RATES(:,5) = ( ALGEBRAIC(:,16).*STATES(:,6)+ CONSTANTS(:,18).*STATES(:,7)) -  STATES(:,5).*( 4.00000.*ALGEBRAIC(:,12)+ALGEBRAIC(:,21));
    RATES(:,6) = ( 4.00000.*ALGEBRAIC(:,12).*STATES(:,5)+ 2.00000.*ALGEBRAIC(:,16).*STATES(:,8)+ CONSTANTS(:,19).*STATES(:,9)) -  STATES(:,6).*(ALGEBRAIC(:,16)+ 3.00000.*ALGEBRAIC(:,12)+ALGEBRAIC(:,21));
    RATES(:,8) = ( 3.00000.*ALGEBRAIC(:,12).*STATES(:,6)+ 3.00000.*ALGEBRAIC(:,16).*STATES(:,10)+ CONSTANTS(:,20).*STATES(:,11)) -  STATES(:,8).*( 2.00000.*ALGEBRAIC(:,16)+ 2.00000.*ALGEBRAIC(:,12)+ALGEBRAIC(:,21));
    ALGEBRAIC(:,14) = ALGEBRAIC(:,12)./8.00000;
    ALGEBRAIC(:,19) =  ALGEBRAIC(:,16).*8.00000;
    RATES(:,7) = ( ALGEBRAIC(:,19).*STATES(:,9)+ ALGEBRAIC(:,21).*STATES(:,5)) -  STATES(:,7).*( 4.00000.*ALGEBRAIC(:,14)+CONSTANTS(:,18));
    RATES(:,9) = ( 4.00000.*ALGEBRAIC(:,14).*STATES(:,7)+ 2.00000.*ALGEBRAIC(:,19).*STATES(:,11)+ ALGEBRAIC(:,21).*STATES(:,6)) -  STATES(:,9).*(ALGEBRAIC(:,19)+ 3.00000.*ALGEBRAIC(:,14)+CONSTANTS(:,19));
    RATES(:,11) = ( 3.00000.*ALGEBRAIC(:,14).*STATES(:,9)+ ALGEBRAIC(:,21).*STATES(:,8)) -  STATES(:,11).*( 2.00000.*ALGEBRAIC(:,19)+CONSTANTS(:,20));
   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(:,3) =  CONSTANTS(:,10).*STATES(:,3);
    ALGEBRAIC(:,2) = ( 0.0200000.*(STATES(:,1)+55.0000))./(1.00000 - exp( - (STATES(:,1)+55.0000)./10.0000));
    ALGEBRAIC(:,7) =  0.250000.*exp( - (STATES(:,1)+65.0000)./80.0000);
    ALGEBRAIC(:,1) = piecewise({VOI>=CONSTANTS(:,5)&VOI<=CONSTANTS(:,6)&(VOI - CONSTANTS(:,5)) -  floor((VOI - CONSTANTS(:,5))./CONSTANTS(:,8)).*CONSTANTS(:,8)<=CONSTANTS(:,9), CONSTANTS(:,7) }, 0.00000);
    ALGEBRAIC(:,6) = ( 0.200000.*(STATES(:,1)+40.0000))./(1.00000 -  1.00000.*exp( - (STATES(:,1)+40.0000)./10.0000));
    ALGEBRAIC(:,9) =  8.00000.*exp(1.00000./ - (STATES(:,1)+65.0000./18.0000));
    ALGEBRAIC(:,11) = ALGEBRAIC(:,6)./(ALGEBRAIC(:,6)+ALGEBRAIC(:,9));
    ALGEBRAIC(:,13) =  120.000.*power(ALGEBRAIC(:,11), 3.00000).*(1.00000 - STATES(:,2)).*(STATES(:,1) - 120.000);
    ALGEBRAIC(:,15) = ( 36.0000.*power(STATES(:,2), 4.00000).*(STATES(:,1)+77.0000))./1.00000;
    ALGEBRAIC(:,17) =  0.300000.*(STATES(:,1)+54.0000);
    ALGEBRAIC(:,18) = ((((((1.00000 - STATES(:,5)) - STATES(:,6)) - STATES(:,8)) - STATES(:,10)) - STATES(:,7)) - STATES(:,9)) - STATES(:,11);
    ALGEBRAIC(:,12) =  0.450000.*exp(STATES(:,1)./22.0000);
    ALGEBRAIC(:,16) =  0.0150000.*exp( - STATES(:,1)./14.0000);
    ALGEBRAIC(:,4) = ( (( CONSTANTS(:,16).*CONSTANTS(:,17).*2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2))).*CONSTANTS(:,13))./(1.00000 - exp(( 2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,8) =   - 5.18200.*ALGEBRAIC(:,4);
    ALGEBRAIC(:,10) = ALGEBRAIC(:,8)./( 2.00000.*CONSTANTS(:,14).*CONSTANTS(:,15).* pi);
    ALGEBRAIC(:,20) =  ALGEBRAIC(:,18).*ALGEBRAIC(:,10)+0.100000;
    ALGEBRAIC(:,21) = ( 3.00000.*STATES(:,4))./(680.000+ 320.000.*STATES(:,4));
    ALGEBRAIC(:,14) = ALGEBRAIC(:,12)./8.00000;
    ALGEBRAIC(:,19) =  ALGEBRAIC(:,16).*8.00000;
    ALGEBRAIC(:,5) = STATES(:,7)+STATES(:,9)+STATES(:,11);
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
function strout = strpad(strin)
    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