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 =13;
end
% There are a total of 5 entries in each of the rate and state variable arrays.
% There are a total of 41 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_CONSTANTS(:,1) = strpad('t_ss in component Vstim_para (second)');
    LEGEND_CONSTANTS(:,2) = strpad('V_actHolding in component Vstim_para (mV)');
    LEGEND_CONSTANTS(:,3) = strpad('t_act in component Vstim_para (second)');
    LEGEND_CONSTANTS(:,4) = strpad('V_actTest in component Vstim_para (mV)');
    LEGEND_CONSTANTS(:,5) = strpad('t_intp in component Vstim_para (second)');
    LEGEND_CONSTANTS(:,6) = strpad('np in component Vstim_para (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('Nai in component control_para (mM)');
    LEGEND_CONSTANTS(:,8) = strpad('Cai_init in component control_para (mM)');
    LEGEND_CONSTANTS(:,9) = strpad('inhPump in component control_para (dimensionless)');
    LEGEND_VOI = strpad('time in component time_s (second)');
    LEGEND_ALGEBRAIC(:,5) = strpad('V in component mPulse_protocol_s (mV)');
    LEGEND_STATES(:,1) = strpad('Cai in component Cai (mM)');
    LEGEND_ALGEBRAIC(:,13) = strpad('J_VOCC in component J_VOCC (mM_per_s)');
    LEGEND_ALGEBRAIC(:,8) = strpad('J_CaPump in component J_CaPump (mM_per_s)');
    LEGEND_ALGEBRAIC(:,11) = strpad('J_NaCa in component J_NaCa (mM_per_s)');
    LEGEND_ALGEBRAIC(:,1) = strpad('stress in component CB4HM (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('phosphorylation in component CB4HM (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('R in component constants (J_per_K_mol)');
    LEGEND_CONSTANTS(:,11) = strpad('F in component constants (C_per_mmol)');
    LEGEND_CONSTANTS(:,12) = strpad('T in component model_para (kelvin)');
    LEGEND_CONSTANTS(:,13) = strpad('Nao in component model_para (mM)');
    LEGEND_CONSTANTS(:,14) = strpad('Cao in component model_para (mM)');
    LEGEND_CONSTANTS(:,15) = strpad('V_cell in component model_para (fm3)');
    LEGEND_CONSTANTS(:,16) = strpad('V_Cahalf in component model_para (mV)');
    LEGEND_CONSTANTS(:,17) = strpad('K_Cahalf in component model_para (mV)');
    LEGEND_CONSTANTS(:,18) = strpad('g_mCa in component model_para (nS)');
    LEGEND_CONSTANTS(:,19) = strpad('V_pmax in component model_para (mM_per_s)');
    LEGEND_CONSTANTS(:,20) = strpad('n in component model_para (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('K_ph in component model_para (mM)');
    LEGEND_CONSTANTS(:,22) = strpad('K_NaCa in component model_para (mM)');
    LEGEND_CONSTANTS(:,23) = strpad('G_NaCa in component model_para (mM_per_s_mV)');
    LEGEND_CONSTANTS(:,24) = strpad('n_M in component model_para (dimensionless)');
    LEGEND_CONSTANTS(:,25) = strpad('Ca_halfMLCK in component model_para (mM)');
    LEGEND_CONSTANTS(:,26) = strpad('M_init in component initials (dimensionless)');
    LEGEND_CONSTANTS(:,27) = strpad('Mp_init in component initials (dimensionless)');
    LEGEND_CONSTANTS(:,28) = strpad('AM_init in component initials (dimensionless)');
    LEGEND_CONSTANTS(:,29) = strpad('AMp_init in component initials (dimensionless)');
    LEGEND_CONSTANTS(:,30) = strpad('K_7 in component model_para (per_s)');
    LEGEND_CONSTANTS(:,31) = strpad('K_2 in component model_para (per_s)');
    LEGEND_CONSTANTS(:,32) = strpad('K_3 in component model_para (per_s)');
    LEGEND_CONSTANTS(:,33) = strpad('K_4 in component model_para (per_s)');
    LEGEND_CONSTANTS(:,34) = strpad('K_5 in component model_para (per_s)');
    LEGEND_ALGEBRAIC(:,7) = strpad('rho_vCa in component J_VOCC (dimensionless)');
    LEGEND_CONSTANTS(:,41) = strpad('stimPeriod in component mPulse_protocol_s (second)');
    LEGEND_ALGEBRAIC(:,3) = strpad('t in component mPulse_protocol_s (second)');
    LEGEND_CONSTANTS(:,35) = strpad('Nai in component model_para (mM)');
    LEGEND_CONSTANTS(:,36) = strpad('inhPump in component model_para (dimensionless)');
    LEGEND_CONSTANTS(:,37) = strpad('Cai_init in component initials (mM)');
    LEGEND_CONSTANTS(:,38) = strpad('z_Ca in component E_Ca (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('E in component Nernst_potential (mV)');
    LEGEND_CONSTANTS(:,39) = strpad('z_Na in component E_Na (dimensionless)');
    LEGEND_CONSTANTS(:,40) = strpad('E in component Nernst_potential (mV)');
    LEGEND_ALGEBRAIC(:,12) = strpad('I in component Ionic_currents (pA)');
    LEGEND_ALGEBRAIC(:,10) = strpad('V_mNaCa in component J_NaCa (mV)');
    LEGEND_ALGEBRAIC(:,6) = strpad('K_1 in component K_1 (per_s)');
    LEGEND_ALGEBRAIC(:,4) = strpad('norm in component CB4HM (dimensionless)');
    LEGEND_STATES(:,2) = strpad('M in component CB4HM (dimensionless)');
    LEGEND_STATES(:,3) = strpad('Mp in component CB4HM (dimensionless)');
    LEGEND_STATES(:,4) = strpad('AM in component CB4HM (dimensionless)');
    LEGEND_STATES(:,5) = strpad('AMp in component CB4HM (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt Cai in component Cai (mM)');
    LEGEND_RATES(:,2) = strpad('d/dt M in component CB4HM (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt Mp in component CB4HM (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt AM in component CB4HM (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt AMp in component CB4HM (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) = 0;
    CONSTANTS(:,2) = -80;
    CONSTANTS(:,3) = 0.1;
    CONSTANTS(:,4) = 0;
    CONSTANTS(:,5) = 0.33;
    CONSTANTS(:,6) = 10;
    CONSTANTS(:,7) = 16.55;
    CONSTANTS(:,8) = 0.1e-3;
    CONSTANTS(:,9) = 1;
    CONSTANTS(:,10) = 8.314;
    CONSTANTS(:,11) = 96.48534;
    CONSTANTS(:,12) = 310;
    CONSTANTS(:,13) = 140;
    CONSTANTS(:,14) = 2;
    CONSTANTS(:,15) = 21;
    CONSTANTS(:,16) = -27;
    CONSTANTS(:,17) = 11;
    CONSTANTS(:,18) = 0.046842;
    CONSTANTS(:,19) = 5.1449e-4;
    CONSTANTS(:,20) = 1.9015;
    CONSTANTS(:,21) = 0.6e-3;
    CONSTANTS(:,22) = 7e-3;
    CONSTANTS(:,23) = 5.7297e-5;
    CONSTANTS(:,24) = 8.7613;
    CONSTANTS(:,25) = 256.98e-6;
    CONSTANTS(:,26) = 1;
    CONSTANTS(:,27) = 0;
    CONSTANTS(:,28) = 0;
    CONSTANTS(:,29) = 0;
    CONSTANTS(:,30) = 0.0378;
    CONSTANTS(:,31) = 1.2387;
    CONSTANTS(:,32) = 0.1419;
    CONSTANTS(:,33) = 0.035475;
    CONSTANTS(:,34) = 1.2387;
    CONSTANTS(:,35) = 2.9836;
    CONSTANTS(:,36) = 1;
    CONSTANTS(:,37) = 0.1e-6;
    CONSTANTS(:,38) = 2;
    CONSTANTS(:,39) = 1;
    CONSTANTS(:,40) =  (( CONSTANTS(:,10).*CONSTANTS(:,12))./( CONSTANTS(:,39).*CONSTANTS(:,11))).*log(CONSTANTS(:,13)./CONSTANTS(:,7));
    CONSTANTS(:,41) = CONSTANTS(:,3)+CONSTANTS(:,5);
    STATES(:,1) = CONSTANTS(:,8);
    STATES(:,2) = CONSTANTS(:,26);
    STATES(:,3) = CONSTANTS(:,27);
    STATES(:,4) = CONSTANTS(:,28);
    STATES(:,5) = CONSTANTS(:,29);
    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(:,6) =  (power(STATES(:,1), CONSTANTS(:,24))./(power(CONSTANTS(:,25), CONSTANTS(:,24))+power(STATES(:,1), CONSTANTS(:,24)))).*1.00000;
    ALGEBRAIC(:,4) = STATES(:,2)+STATES(:,3)+STATES(:,4)+STATES(:,5);
    RATES(:,2) = (  - ALGEBRAIC(:,6).*STATES(:,2))./ALGEBRAIC(:,4)+( CONSTANTS(:,31).*STATES(:,3))./ALGEBRAIC(:,4)+( CONSTANTS(:,30).*STATES(:,4))./ALGEBRAIC(:,4);
    RATES(:,3) = (( CONSTANTS(:,33).*STATES(:,5))./ALGEBRAIC(:,4)+( ALGEBRAIC(:,6).*STATES(:,2))./ALGEBRAIC(:,4)) - ( (CONSTANTS(:,31)+CONSTANTS(:,32)).*STATES(:,3))./ALGEBRAIC(:,4);
    RATES(:,4) = ( CONSTANTS(:,34).*STATES(:,5))./ALGEBRAIC(:,4) - ( (ALGEBRAIC(:,6)+CONSTANTS(:,30)).*STATES(:,4))./ALGEBRAIC(:,4);
    RATES(:,5) = (( CONSTANTS(:,32).*STATES(:,3))./ALGEBRAIC(:,4)+( ALGEBRAIC(:,6).*STATES(:,4))./ALGEBRAIC(:,4)) - ( (CONSTANTS(:,33)+CONSTANTS(:,34)).*STATES(:,5))./ALGEBRAIC(:,4);
    ALGEBRAIC(:,3) = (VOI - CONSTANTS(:,1)) -  CONSTANTS(:,41).*floor((VOI - CONSTANTS(:,1))./CONSTANTS(:,41));
    ALGEBRAIC(:,5) = piecewise({VOI<=CONSTANTS(:,1), CONSTANTS(:,2) , VOI>CONSTANTS(:,1)&ALGEBRAIC(:,3)<=CONSTANTS(:,3)&VOI<=CONSTANTS(:,1)+ CONSTANTS(:,41).*CONSTANTS(:,6), CONSTANTS(:,4) }, CONSTANTS(:,2));
    ALGEBRAIC(:,7) = 1.00000./(1.00000+exp((CONSTANTS(:,16) - ALGEBRAIC(:,5))./CONSTANTS(:,17)));
    ALGEBRAIC(:,9) =  (( CONSTANTS(:,10).*CONSTANTS(:,12))./( CONSTANTS(:,38).*CONSTANTS(:,11))).*log(CONSTANTS(:,14)./STATES(:,1));
    ALGEBRAIC(:,12) =  CONSTANTS(:,18).*ALGEBRAIC(:,7).*(ALGEBRAIC(:,5) - ALGEBRAIC(:,9));
    ALGEBRAIC(:,13) =  - ALGEBRAIC(:,12)./( 2.00000.*CONSTANTS(:,15).*CONSTANTS(:,11));
    ALGEBRAIC(:,8) = (  - CONSTANTS(:,19).*power(STATES(:,1), CONSTANTS(:,20)))./(power(CONSTANTS(:,21), CONSTANTS(:,20))+power(STATES(:,1), CONSTANTS(:,20)));
    ALGEBRAIC(:,10) =  3.00000.*CONSTANTS(:,40) -  2.00000.*ALGEBRAIC(:,9);
    ALGEBRAIC(:,11) =  (( CONSTANTS(:,23).*STATES(:,1))./(STATES(:,1)+CONSTANTS(:,22))).*(ALGEBRAIC(:,5) - ALGEBRAIC(:,10));
    RATES(:,1) = ALGEBRAIC(:,13)+ CONSTANTS(:,9).*ALGEBRAIC(:,8)+ALGEBRAIC(:,11);
   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(:,6) =  (power(STATES(:,1), CONSTANTS(:,24))./(power(CONSTANTS(:,25), CONSTANTS(:,24))+power(STATES(:,1), CONSTANTS(:,24)))).*1.00000;
    ALGEBRAIC(:,4) = STATES(:,2)+STATES(:,3)+STATES(:,4)+STATES(:,5);
    ALGEBRAIC(:,3) = (VOI - CONSTANTS(:,1)) -  CONSTANTS(:,41).*floor((VOI - CONSTANTS(:,1))./CONSTANTS(:,41));
    ALGEBRAIC(:,5) = piecewise({VOI<=CONSTANTS(:,1), CONSTANTS(:,2) , VOI>CONSTANTS(:,1)&ALGEBRAIC(:,3)<=CONSTANTS(:,3)&VOI<=CONSTANTS(:,1)+ CONSTANTS(:,41).*CONSTANTS(:,6), CONSTANTS(:,4) }, CONSTANTS(:,2));
    ALGEBRAIC(:,7) = 1.00000./(1.00000+exp((CONSTANTS(:,16) - ALGEBRAIC(:,5))./CONSTANTS(:,17)));
    ALGEBRAIC(:,9) =  (( CONSTANTS(:,10).*CONSTANTS(:,12))./( CONSTANTS(:,38).*CONSTANTS(:,11))).*log(CONSTANTS(:,14)./STATES(:,1));
    ALGEBRAIC(:,12) =  CONSTANTS(:,18).*ALGEBRAIC(:,7).*(ALGEBRAIC(:,5) - ALGEBRAIC(:,9));
    ALGEBRAIC(:,13) =  - ALGEBRAIC(:,12)./( 2.00000.*CONSTANTS(:,15).*CONSTANTS(:,11));
    ALGEBRAIC(:,8) = (  - CONSTANTS(:,19).*power(STATES(:,1), CONSTANTS(:,20)))./(power(CONSTANTS(:,21), CONSTANTS(:,20))+power(STATES(:,1), CONSTANTS(:,20)));
    ALGEBRAIC(:,10) =  3.00000.*CONSTANTS(:,40) -  2.00000.*ALGEBRAIC(:,9);
    ALGEBRAIC(:,11) =  (( CONSTANTS(:,23).*STATES(:,1))./(STATES(:,1)+CONSTANTS(:,22))).*(ALGEBRAIC(:,5) - ALGEBRAIC(:,10));
    ALGEBRAIC(:,1) = STATES(:,5)+STATES(:,4);
    ALGEBRAIC(:,2) = STATES(:,5)+STATES(:,3);
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