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 39 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('Nai in component control_para (mM)');
    LEGEND_CONSTANTS(:,6) = strpad('Cai_init in component control_para (mM)');
    LEGEND_CONSTANTS(:,7) = strpad('inhPump in component control_para (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('K_Cahalf in component control_para (mV)');
    LEGEND_VOI = strpad('time in component time_s (second)');
    LEGEND_ALGEBRAIC(:,3) = strpad('V in component sPulse_protocol_s (mV)');
    LEGEND_STATES(:,1) = strpad('Cai in component Cai (mM)');
    LEGEND_ALGEBRAIC(:,12) = strpad('J_VOCC in component J_VOCC (mM_per_s)');
    LEGEND_ALGEBRAIC(:,7) = strpad('J_CaPump in component J_CaPump (mM_per_s)');
    LEGEND_ALGEBRAIC(:,10) = 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(:,9) = strpad('R in component constants (J_per_K_mol)');
    LEGEND_CONSTANTS(:,10) = strpad('F in component constants (C_per_mmol)');
    LEGEND_CONSTANTS(:,11) = strpad('T in component model_para (kelvin)');
    LEGEND_CONSTANTS(:,12) = strpad('Nao in component model_para (mM)');
    LEGEND_CONSTANTS(:,13) = strpad('Cao in component model_para (mM)');
    LEGEND_CONSTANTS(:,14) = strpad('V_cell in component model_para (fm3)');
    LEGEND_CONSTANTS(:,15) = strpad('V_Cahalf in component model_para (mV)');
    LEGEND_CONSTANTS(:,16) = strpad('g_mCa in component model_para (nS)');
    LEGEND_CONSTANTS(:,17) = strpad('V_pmax in component model_para (mM_per_s)');
    LEGEND_CONSTANTS(:,18) = strpad('n in component model_para (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('K_ph in component model_para (mM)');
    LEGEND_CONSTANTS(:,20) = strpad('K_NaCa in component model_para (mM)');
    LEGEND_CONSTANTS(:,21) = strpad('G_NaCa in component model_para (mM_per_s_mV)');
    LEGEND_CONSTANTS(:,22) = strpad('n_M in component model_para (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('Ca_halfMLCK in component model_para (mM)');
    LEGEND_CONSTANTS(:,24) = strpad('M_init in component initials (dimensionless)');
    LEGEND_CONSTANTS(:,25) = strpad('Mp_init in component initials (dimensionless)');
    LEGEND_CONSTANTS(:,26) = strpad('AM_init in component initials (dimensionless)');
    LEGEND_CONSTANTS(:,27) = strpad('AMp_init in component initials (dimensionless)');
    LEGEND_CONSTANTS(:,28) = strpad('K_7 in component model_para (per_s)');
    LEGEND_CONSTANTS(:,29) = strpad('K_2 in component model_para (per_s)');
    LEGEND_CONSTANTS(:,30) = strpad('K_3 in component model_para (per_s)');
    LEGEND_CONSTANTS(:,31) = strpad('K_4 in component model_para (per_s)');
    LEGEND_CONSTANTS(:,32) = strpad('K_5 in component model_para (per_s)');
    LEGEND_ALGEBRAIC(:,5) = strpad('rho_vCa in component J_VOCC (dimensionless)');
    LEGEND_CONSTANTS(:,33) = strpad('Nai in component model_para (mM)');
    LEGEND_CONSTANTS(:,34) = strpad('K_Cahalf in component model_para (mV)');
    LEGEND_CONSTANTS(:,35) = strpad('inhPump in component model_para (dimensionless)');
    LEGEND_CONSTANTS(:,36) = strpad('Cai_init in component initials (mM)');
    LEGEND_CONSTANTS(:,37) = strpad('z_Ca in component E_Ca (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('E in component Nernst_potential (mV)');
    LEGEND_CONSTANTS(:,38) = strpad('z_Na in component E_Na (dimensionless)');
    LEGEND_CONSTANTS(:,39) = strpad('E in component Nernst_potential (mV)');
    LEGEND_ALGEBRAIC(:,11) = strpad('I in component Ionic_currents (pA)');
    LEGEND_ALGEBRAIC(:,9) = 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) = 1;
    CONSTANTS(:,4) = 0;
    CONSTANTS(:,5) = 16.55;
    CONSTANTS(:,6) = 0.1e-3;
    CONSTANTS(:,7) = 1;
    CONSTANTS(:,8) = 11;
    CONSTANTS(:,9) = 8.314;
    CONSTANTS(:,10) = 96.48534;
    CONSTANTS(:,11) = 310;
    CONSTANTS(:,12) = 140;
    CONSTANTS(:,13) = 2;
    CONSTANTS(:,14) = 21;
    CONSTANTS(:,15) = -27;
    CONSTANTS(:,16) = 0.046842;
    CONSTANTS(:,17) = 5.1449e-4;
    CONSTANTS(:,18) = 1.9015;
    CONSTANTS(:,19) = 0.6e-3;
    CONSTANTS(:,20) = 7e-3;
    CONSTANTS(:,21) = 5.7297e-5;
    CONSTANTS(:,22) = 8.7613;
    CONSTANTS(:,23) = 256.98e-6;
    CONSTANTS(:,24) = 1;
    CONSTANTS(:,25) = 0;
    CONSTANTS(:,26) = 0;
    CONSTANTS(:,27) = 0;
    CONSTANTS(:,28) = 0.0378;
    CONSTANTS(:,29) = 1.2387;
    CONSTANTS(:,30) = 0.1419;
    CONSTANTS(:,31) = 0.035475;
    CONSTANTS(:,32) = 1.2387;
    CONSTANTS(:,33) = 2.9836;
    CONSTANTS(:,34) = 11;
    CONSTANTS(:,35) = 1;
    CONSTANTS(:,36) = 0.1e-6;
    CONSTANTS(:,37) = 2;
    CONSTANTS(:,38) = 1;
    CONSTANTS(:,39) =  (( CONSTANTS(:,9).*CONSTANTS(:,11))./( CONSTANTS(:,38).*CONSTANTS(:,10))).*log(CONSTANTS(:,12)./CONSTANTS(:,5));
    STATES(:,1) = CONSTANTS(:,6);
    STATES(:,2) = CONSTANTS(:,24);
    STATES(:,3) = CONSTANTS(:,25);
    STATES(:,4) = CONSTANTS(:,26);
    STATES(:,5) = CONSTANTS(:,27);
    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(:,22))./(power(CONSTANTS(:,23), CONSTANTS(:,22))+power(STATES(:,1), CONSTANTS(:,22)))).*1.00000;
    ALGEBRAIC(:,4) = STATES(:,2)+STATES(:,3)+STATES(:,4)+STATES(:,5);
    RATES(:,2) = (  - ALGEBRAIC(:,6).*STATES(:,2))./ALGEBRAIC(:,4)+( CONSTANTS(:,29).*STATES(:,3))./ALGEBRAIC(:,4)+( CONSTANTS(:,28).*STATES(:,4))./ALGEBRAIC(:,4);
    RATES(:,3) = (( CONSTANTS(:,31).*STATES(:,5))./ALGEBRAIC(:,4)+( ALGEBRAIC(:,6).*STATES(:,2))./ALGEBRAIC(:,4)) - ( (CONSTANTS(:,29)+CONSTANTS(:,30)).*STATES(:,3))./ALGEBRAIC(:,4);
    RATES(:,4) = ( CONSTANTS(:,32).*STATES(:,5))./ALGEBRAIC(:,4) - ( (ALGEBRAIC(:,6)+CONSTANTS(:,28)).*STATES(:,4))./ALGEBRAIC(:,4);
    RATES(:,5) = (( CONSTANTS(:,30).*STATES(:,3))./ALGEBRAIC(:,4)+( ALGEBRAIC(:,6).*STATES(:,4))./ALGEBRAIC(:,4)) - ( (CONSTANTS(:,31)+CONSTANTS(:,32)).*STATES(:,5))./ALGEBRAIC(:,4);
    ALGEBRAIC(:,3) = piecewise({VOI<CONSTANTS(:,1), CONSTANTS(:,2) , VOI>=CONSTANTS(:,1)&VOI<CONSTANTS(:,3)+CONSTANTS(:,1), CONSTANTS(:,4) }, CONSTANTS(:,2));
    ALGEBRAIC(:,5) = 1.00000./(1.00000+exp((CONSTANTS(:,15) - ALGEBRAIC(:,3))./CONSTANTS(:,8)));
    ALGEBRAIC(:,8) =  (( CONSTANTS(:,9).*CONSTANTS(:,11))./( CONSTANTS(:,37).*CONSTANTS(:,10))).*log(CONSTANTS(:,13)./STATES(:,1));
    ALGEBRAIC(:,11) =  CONSTANTS(:,16).*ALGEBRAIC(:,5).*(ALGEBRAIC(:,3) - ALGEBRAIC(:,8));
    ALGEBRAIC(:,12) =  - ALGEBRAIC(:,11)./( 2.00000.*CONSTANTS(:,14).*CONSTANTS(:,10));
    ALGEBRAIC(:,7) = (  - CONSTANTS(:,17).*power(STATES(:,1), CONSTANTS(:,18)))./(power(CONSTANTS(:,19), CONSTANTS(:,18))+power(STATES(:,1), CONSTANTS(:,18)));
    ALGEBRAIC(:,9) =  3.00000.*CONSTANTS(:,39) -  2.00000.*ALGEBRAIC(:,8);
    ALGEBRAIC(:,10) =  (( CONSTANTS(:,21).*STATES(:,1))./(STATES(:,1)+CONSTANTS(:,20))).*(ALGEBRAIC(:,3) - ALGEBRAIC(:,9));
    RATES(:,1) = ALGEBRAIC(:,12)+ CONSTANTS(:,7).*ALGEBRAIC(:,7)+ALGEBRAIC(:,10);
   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(:,22))./(power(CONSTANTS(:,23), CONSTANTS(:,22))+power(STATES(:,1), CONSTANTS(:,22)))).*1.00000;
    ALGEBRAIC(:,4) = STATES(:,2)+STATES(:,3)+STATES(:,4)+STATES(:,5);
    ALGEBRAIC(:,3) = piecewise({VOI<CONSTANTS(:,1), CONSTANTS(:,2) , VOI>=CONSTANTS(:,1)&VOI<CONSTANTS(:,3)+CONSTANTS(:,1), CONSTANTS(:,4) }, CONSTANTS(:,2));
    ALGEBRAIC(:,5) = 1.00000./(1.00000+exp((CONSTANTS(:,15) - ALGEBRAIC(:,3))./CONSTANTS(:,8)));
    ALGEBRAIC(:,8) =  (( CONSTANTS(:,9).*CONSTANTS(:,11))./( CONSTANTS(:,37).*CONSTANTS(:,10))).*log(CONSTANTS(:,13)./STATES(:,1));
    ALGEBRAIC(:,11) =  CONSTANTS(:,16).*ALGEBRAIC(:,5).*(ALGEBRAIC(:,3) - ALGEBRAIC(:,8));
    ALGEBRAIC(:,12) =  - ALGEBRAIC(:,11)./( 2.00000.*CONSTANTS(:,14).*CONSTANTS(:,10));
    ALGEBRAIC(:,7) = (  - CONSTANTS(:,17).*power(STATES(:,1), CONSTANTS(:,18)))./(power(CONSTANTS(:,19), CONSTANTS(:,18))+power(STATES(:,1), CONSTANTS(:,18)));
    ALGEBRAIC(:,9) =  3.00000.*CONSTANTS(:,39) -  2.00000.*ALGEBRAIC(:,8);
    ALGEBRAIC(:,10) =  (( CONSTANTS(:,21).*STATES(:,1))./(STATES(:,1)+CONSTANTS(:,20))).*(ALGEBRAIC(:,3) - ALGEBRAIC(:,9));
    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