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 19 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('R in component transmitter_release (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('kr_plus in component transmitter_release (per_uM_per_ms)');
    LEGEND_CONSTANTS(:,5) = strpad('kr_minus in component transmitter_release (per_ms)');
    LEGEND_ALGEBRAIC(:,20) = strpad('Ca in component calcium_concentration (uM)');
    LEGEND_ALGEBRAIC(:,1) = strpad('T in component transmitter_release (uM)');
    LEGEND_CONSTANTS(:,6) = strpad('T_bar in component transmitter_release (uM)');
    LEGEND_CONSTANTS(:,7) = strpad('Ca_ex in component calcium_concentration (uM)');
    LEGEND_ALGEBRAIC(:,9) = strpad('Ca_open in component calcium_concentration (uM)');
    LEGEND_CONSTANTS(:,8) = strpad('Dc in component calcium_concentration (micrometre2_per_second)');
    LEGEND_CONSTANTS(:,9) = strpad('r in component calcium_concentration (nanometre)');
    LEGEND_ALGEBRAIC(:,6) = strpad('sigma in component calcium_concentration (uM_per_ms)');
    LEGEND_ALGEBRAIC(:,2) = strpad('i_V in component calcium_concentration (uA)');
    LEGEND_CONSTANTS(:,10) = strpad('g_Ca in component calcium_concentration (pS)');
    LEGEND_CONSTANTS(:,11) = strpad('P in component calcium_concentration (mV_per_uM)');
    LEGEND_STATES(:,2) = strpad('V_post in component membrane_post (mV)');
    LEGEND_ALGEBRAIC(:,17) = strpad('O in component O (dimensionless)');
    LEGEND_ALGEBRAIC(:,11) = strpad('alpha in component rate_constants (per_ms)');
    LEGEND_ALGEBRAIC(:,13) = strpad('alpha_ in component rate_constants (per_ms)');
    LEGEND_ALGEBRAIC(:,15) = strpad('beta in component rate_constants (per_ms)');
    LEGEND_ALGEBRAIC(:,18) = strpad('beta_ in component rate_constants (per_ms)');
    LEGEND_ALGEBRAIC(:,21) = strpad('kG_plus in component rate_constants (per_ms)');
    LEGEND_STATES(:,3) = strpad('b in component rate_constants (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('kb_plus in component rate_constants (per_uM_per_ms)');
    LEGEND_CONSTANTS(:,13) = strpad('kb_minus in component rate_constants (per_ms)');
    LEGEND_CONSTANTS(:,14) = strpad('kG_minus in component rate_constants (per_ms)');
    LEGEND_CONSTANTS(:,15) = strpad('kG2_minus in component rate_constants (per_ms)');
    LEGEND_CONSTANTS(:,16) = strpad('kG3_minus in component rate_constants (per_ms)');
    LEGEND_STATES(:,4) = strpad('C1 in component C1 (dimensionless)');
    LEGEND_STATES(:,5) = strpad('C2 in component C2 (dimensionless)');
    LEGEND_STATES(:,6) = strpad('C_G1 in component C_G1 (dimensionless)');
    LEGEND_STATES(:,7) = strpad('C3 in component C3 (dimensionless)');
    LEGEND_STATES(:,8) = strpad('C_G2 in component C_G2 (dimensionless)');
    LEGEND_STATES(:,9) = strpad('C4 in component C4 (dimensionless)');
    LEGEND_STATES(:,10) = strpad('C_G3 in component C_G3 (dimensionless)');
    LEGEND_ALGEBRAIC(:,3) = strpad('C_G in component O (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('Cm in component membrane_post (uF_per_cm2)');
    LEGEND_ALGEBRAIC(:,4) = strpad('i_syn in component synaptic_current (uA_per_cm2)');
    LEGEND_ALGEBRAIC(:,16) = strpad('i_Na_post in component sodium_current_post (uA_per_cm2)');
    LEGEND_ALGEBRAIC(:,19) = strpad('i_K_post in component potassium_current_post (uA_per_cm2)');
    LEGEND_ALGEBRAIC(:,7) = strpad('i_leak_post in component leak_current_post (uA_per_cm2)');
    LEGEND_CONSTANTS(:,18) = strpad('g_syn in component synaptic_current (mS_per_cm2)');
    LEGEND_CONSTANTS(:,19) = strpad('V_syn in component synaptic_current (mV)');
    LEGEND_ALGEBRAIC(:,14) = strpad('x_infinity in component sodium_current_post (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('alpha_x in component sodium_current_post (dimensionless)');
    LEGEND_ALGEBRAIC(:,12) = strpad('beta_x in component sodium_current_post (dimensionless)');
    LEGEND_STATES(:,11) = strpad('n_post in component potassium_current_n_gate_post (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('alpha_n in component potassium_current_n_gate_post (per_ms)');
    LEGEND_ALGEBRAIC(:,8) = strpad('beta_n in component potassium_current_n_gate_post (per_ms)');
    LEGEND_RATES(:,1) = strpad('d/dt R in component transmitter_release (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt b in component rate_constants (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt C1 in component C1 (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt C2 in component C2 (dimensionless)');
    LEGEND_RATES(:,7) = strpad('d/dt C3 in component C3 (dimensionless)');
    LEGEND_RATES(:,9) = strpad('d/dt C4 in component C4 (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt C_G1 in component C_G1 (dimensionless)');
    LEGEND_RATES(:,8) = strpad('d/dt C_G2 in component C_G2 (dimensionless)');
    LEGEND_RATES(:,10) = strpad('d/dt C_G3 in component C_G3 (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt V_post in component membrane_post (mV)');
    LEGEND_RATES(:,11) = strpad('d/dt n_post in component potassium_current_n_gate_post (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) = 0;
    CONSTANTS(:,4) = 0.15;
    CONSTANTS(:,5) = 2.5;
    CONSTANTS(:,6) = 4000.0;
    CONSTANTS(:,7) = 2000.0;
    CONSTANTS(:,8) = 220;
    CONSTANTS(:,9) = 10;
    CONSTANTS(:,10) = 1.2;
    CONSTANTS(:,11) = 0.006;
    STATES(:,2) = -65;
    STATES(:,3) = 0;
    CONSTANTS(:,12) = 2000.0;
    CONSTANTS(:,13) = 1.0;
    CONSTANTS(:,14) = 0.00025;
    CONSTANTS(:,15) = 0.016;
    CONSTANTS(:,16) = 1.024;
    STATES(:,4) = 1;
    STATES(:,5) = 0;
    STATES(:,6) = 0;
    STATES(:,7) = 0;
    STATES(:,8) = 0;
    STATES(:,9) = 0;
    STATES(:,10) = 0;
    CONSTANTS(:,17) = 1.0;
    CONSTANTS(:,18) = 0.2;
    CONSTANTS(:,19) = 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(:,1) =  CONSTANTS(:,6).*STATES(:,1);
    RATES(:,3) =  CONSTANTS(:,12).*ALGEBRAIC(:,1).*(1.00000 - STATES(:,3)) -  CONSTANTS(:,13).*STATES(:,3);
    ALGEBRAIC(:,5) = ( 0.0200000.*(STATES(:,2)+55.0000))./(1.00000 - exp( - (STATES(:,2)+55.0000)./10.0000));
    ALGEBRAIC(:,8) =  0.250000.*exp( - (STATES(:,2)+65.0000)./80.0000);
    RATES(:,11) =  ALGEBRAIC(:,5).*(1.00000 - STATES(:,11)) -  ALGEBRAIC(:,8).*STATES(:,11);
    ALGEBRAIC(:,17) = ((((((1.00000 - STATES(:,4)) - STATES(:,5)) - STATES(:,7)) - STATES(:,9)) - STATES(:,6)) - STATES(:,8)) - STATES(:,10);
    ALGEBRAIC(:,11) =  0.450000.*exp(STATES(:,2)./22.0000);
    ALGEBRAIC(:,15) =  0.0150000.*exp( - STATES(:,2)./14.0000);
    RATES(:,9) = ( 2.00000.*ALGEBRAIC(:,11).*STATES(:,7)+ 4.00000.*ALGEBRAIC(:,15).*ALGEBRAIC(:,17)) -  STATES(:,9).*( 3.00000.*ALGEBRAIC(:,15)+ALGEBRAIC(:,11));
    ALGEBRAIC(:,4) =  CONSTANTS(:,18).*STATES(:,3).*(STATES(:,2) - CONSTANTS(:,19));
    ALGEBRAIC(:,10) = ( 0.200000.*(STATES(:,2)+40.0000))./(1.00000 -  1.00000.*exp( - (STATES(:,2)+40.0000)./10.0000));
    ALGEBRAIC(:,12) =  8.00000.*exp(1.00000./ - (STATES(:,2)+65.0000./18.0000));
    ALGEBRAIC(:,14) = ALGEBRAIC(:,10)./(ALGEBRAIC(:,10)+ALGEBRAIC(:,12));
    ALGEBRAIC(:,16) =  120.000.*power(ALGEBRAIC(:,14), 3.00000).*(1.00000 - STATES(:,11)).*(STATES(:,2) - 120.000);
    ALGEBRAIC(:,19) =  36.0000.*power(STATES(:,11), 4.00000).*(STATES(:,2)+77.0000);
    ALGEBRAIC(:,7) =  0.300000.*(STATES(:,2)+54.0000);
    RATES(:,2) =  - (ALGEBRAIC(:,16)+ALGEBRAIC(:,19)+ALGEBRAIC(:,7)+ALGEBRAIC(:,4))./CONSTANTS(:,17);
    ALGEBRAIC(:,2) = ( (( CONSTANTS(:,10).*CONSTANTS(:,11).*2.00000.*CONSTANTS(:,3).*STATES(:,2))./( CONSTANTS(:,1).*CONSTANTS(:,2))).*CONSTANTS(:,7))./(1.00000 - exp(( 2.00000.*CONSTANTS(:,3).*STATES(:,2))./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,6) =   - 5.18200.*ALGEBRAIC(:,2);
    ALGEBRAIC(:,9) = ALGEBRAIC(:,6)./( 2.00000.*CONSTANTS(:,8).*CONSTANTS(:,9).* pi);
    ALGEBRAIC(:,20) =  ALGEBRAIC(:,17).*ALGEBRAIC(:,9)+0.100000;
    RATES(:,1) =  CONSTANTS(:,4).*ALGEBRAIC(:,20).*(1.00000 - STATES(:,1)) -  CONSTANTS(:,5).*STATES(:,1);
    ALGEBRAIC(:,21) = ( 3.00000.*STATES(:,3))./(680.000+ 320.000.*STATES(:,3));
    RATES(:,4) = ( ALGEBRAIC(:,15).*STATES(:,5)+ CONSTANTS(:,14).*STATES(:,6)) -  STATES(:,4).*( 4.00000.*ALGEBRAIC(:,11)+ALGEBRAIC(:,21));
    RATES(:,5) = ( 4.00000.*ALGEBRAIC(:,11).*STATES(:,4)+ 2.00000.*ALGEBRAIC(:,15).*STATES(:,7)+ CONSTANTS(:,15).*STATES(:,8)) -  STATES(:,5).*(ALGEBRAIC(:,15)+ 3.00000.*ALGEBRAIC(:,11)+ALGEBRAIC(:,21));
    RATES(:,7) = ( 3.00000.*ALGEBRAIC(:,11).*STATES(:,5)+ 3.00000.*ALGEBRAIC(:,15).*STATES(:,9)+ CONSTANTS(:,16).*STATES(:,10)) -  STATES(:,7).*( 2.00000.*ALGEBRAIC(:,15)+ 2.00000.*ALGEBRAIC(:,11)+ALGEBRAIC(:,21));
    ALGEBRAIC(:,13) = ALGEBRAIC(:,11)./8.00000;
    ALGEBRAIC(:,18) =  ALGEBRAIC(:,15).*8.00000;
    RATES(:,6) = ( ALGEBRAIC(:,18).*STATES(:,8)+ ALGEBRAIC(:,21).*STATES(:,4)) -  STATES(:,6).*( 4.00000.*ALGEBRAIC(:,13)+CONSTANTS(:,14));
    RATES(:,8) = ( 4.00000.*ALGEBRAIC(:,13).*STATES(:,6)+ 2.00000.*ALGEBRAIC(:,18).*STATES(:,10)+ ALGEBRAIC(:,21).*STATES(:,5)) -  STATES(:,8).*(ALGEBRAIC(:,18)+ 3.00000.*ALGEBRAIC(:,13)+CONSTANTS(:,15));
    RATES(:,10) = ( 3.00000.*ALGEBRAIC(:,13).*STATES(:,8)+ ALGEBRAIC(:,21).*STATES(:,7)) -  STATES(:,10).*( 2.00000.*ALGEBRAIC(:,18)+CONSTANTS(:,16));
   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(:,1) =  CONSTANTS(:,6).*STATES(:,1);
    ALGEBRAIC(:,5) = ( 0.0200000.*(STATES(:,2)+55.0000))./(1.00000 - exp( - (STATES(:,2)+55.0000)./10.0000));
    ALGEBRAIC(:,8) =  0.250000.*exp( - (STATES(:,2)+65.0000)./80.0000);
    ALGEBRAIC(:,17) = ((((((1.00000 - STATES(:,4)) - STATES(:,5)) - STATES(:,7)) - STATES(:,9)) - STATES(:,6)) - STATES(:,8)) - STATES(:,10);
    ALGEBRAIC(:,11) =  0.450000.*exp(STATES(:,2)./22.0000);
    ALGEBRAIC(:,15) =  0.0150000.*exp( - STATES(:,2)./14.0000);
    ALGEBRAIC(:,4) =  CONSTANTS(:,18).*STATES(:,3).*(STATES(:,2) - CONSTANTS(:,19));
    ALGEBRAIC(:,10) = ( 0.200000.*(STATES(:,2)+40.0000))./(1.00000 -  1.00000.*exp( - (STATES(:,2)+40.0000)./10.0000));
    ALGEBRAIC(:,12) =  8.00000.*exp(1.00000./ - (STATES(:,2)+65.0000./18.0000));
    ALGEBRAIC(:,14) = ALGEBRAIC(:,10)./(ALGEBRAIC(:,10)+ALGEBRAIC(:,12));
    ALGEBRAIC(:,16) =  120.000.*power(ALGEBRAIC(:,14), 3.00000).*(1.00000 - STATES(:,11)).*(STATES(:,2) - 120.000);
    ALGEBRAIC(:,19) =  36.0000.*power(STATES(:,11), 4.00000).*(STATES(:,2)+77.0000);
    ALGEBRAIC(:,7) =  0.300000.*(STATES(:,2)+54.0000);
    ALGEBRAIC(:,2) = ( (( CONSTANTS(:,10).*CONSTANTS(:,11).*2.00000.*CONSTANTS(:,3).*STATES(:,2))./( CONSTANTS(:,1).*CONSTANTS(:,2))).*CONSTANTS(:,7))./(1.00000 - exp(( 2.00000.*CONSTANTS(:,3).*STATES(:,2))./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,6) =   - 5.18200.*ALGEBRAIC(:,2);
    ALGEBRAIC(:,9) = ALGEBRAIC(:,6)./( 2.00000.*CONSTANTS(:,8).*CONSTANTS(:,9).* pi);
    ALGEBRAIC(:,20) =  ALGEBRAIC(:,17).*ALGEBRAIC(:,9)+0.100000;
    ALGEBRAIC(:,21) = ( 3.00000.*STATES(:,3))./(680.000+ 320.000.*STATES(:,3));
    ALGEBRAIC(:,13) = ALGEBRAIC(:,11)./8.00000;
    ALGEBRAIC(:,18) =  ALGEBRAIC(:,15).*8.00000;
    ALGEBRAIC(:,3) = STATES(:,6)+STATES(:,8)+STATES(:,10);
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