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 6 entries in each of the rate and state variable arrays.
% There are a total of 21 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 environment (second)');
    LEGEND_CONSTANTS(:,1) = strpad('C_m in component environment (fF)');
    LEGEND_ALGEBRAIC(:,8) = strpad('v_RyR in component RyR (fmol_per_sec)');
    LEGEND_CONSTANTS(:,2) = strpad('F in component constants (C_per_mol)');
    LEGEND_STATES(:,1) = strpad('q_Ca_SR in component environment (fmol)');
    LEGEND_STATES(:,2) = strpad('q_Ca_D in component environment (fmol)');
    LEGEND_STATES(:,3) = strpad('q_C_RyR in component environment (fmol)');
    LEGEND_STATES(:,4) = strpad('q_CI_RyR in component environment (fmol)');
    LEGEND_STATES(:,5) = strpad('q_I_RyR in component environment (fmol)');
    LEGEND_STATES(:,6) = strpad('q_O_RyR in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,13) = strpad('v_RyRgate_Ca_D in component RyR (fmol_per_sec)');
    LEGEND_CONSTANTS(:,3) = strpad('kappa_RyR in component RyR_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,4) = strpad('kappa_OC in component RyR_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,5) = strpad('kappa_CCI in component RyR_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,6) = strpad('kappa_CII in component RyR_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,7) = strpad('kappa_IO in component RyR_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,8) = strpad('K_Ca_SR in component RyR_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,9) = strpad('K_Ca_D in component RyR_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,10) = strpad('K_C_RyR in component RyR_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,11) = strpad('K_CI_RyR in component RyR_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,12) = strpad('K_I_RyR in component RyR_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,13) = strpad('K_O_RyR in component RyR_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,14) = strpad('R in component constants (J_per_K_per_mol)');
    LEGEND_CONSTANTS(:,15) = strpad('T in component constants (kelvin)');
    LEGEND_CONSTANTS(:,16) = strpad('nCa_1 in component RyR (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('nCa_2 in component RyR (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('mu_Ca_SR in component RyR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,3) = strpad('mu_Ca_D in component RyR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,2) = strpad('q_Ca_gate_complexes in component RyR (fmol)');
    LEGEND_ALGEBRAIC(:,4) = strpad('mu_C_RyR in component RyR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,5) = strpad('mu_CI_RyR in component RyR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,6) = strpad('mu_I_RyR in component RyR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,7) = strpad('mu_O_RyR in component RyR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,9) = strpad('v_OC in component RyR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,10) = strpad('v_CCI in component RyR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,11) = strpad('v_CII in component RyR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,12) = strpad('v_IO in component RyR (fmol_per_sec)');
    LEGEND_CONSTANTS(:,18) = strpad('zNa in component ion_valences (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('zCl in component ion_valences (dimensionless)');
    LEGEND_CONSTANTS(:,20) = strpad('zK in component ion_valences (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('zCa in component ion_valences (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt q_Ca_SR in component environment (fmol)');
    LEGEND_RATES(:,2) = strpad('d/dt q_Ca_D in component environment (fmol)');
    LEGEND_RATES(:,6) = strpad('d/dt q_O_RyR in component environment (fmol)');
    LEGEND_RATES(:,3) = strpad('d/dt q_C_RyR in component environment (fmol)');
    LEGEND_RATES(:,4) = strpad('d/dt q_CI_RyR in component environment (fmol)');
    LEGEND_RATES(:,5) = strpad('d/dt q_I_RyR in component environment (fmol)');
    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) = 1.381e5;
    CONSTANTS(:,2) = 96485;
    STATES(:,1) = 6.41e-1;
    STATES(:,2) = 3.68876E-07;
    STATES(:,3) = 2.9682830E-06;
    STATES(:,4) = 2.9682830E-06;
    STATES(:,5) = 2.9682830E-06;
    STATES(:,6) = 2.9682830E-06;
    CONSTANTS(:,3) = 498020;
    CONSTANTS(:,4) = 5.01701;
    CONSTANTS(:,5) = 6.61466;
    CONSTANTS(:,6) = 5.43605;
    CONSTANTS(:,7) = 0.453004;
    CONSTANTS(:,8) = 102.047;
    CONSTANTS(:,9) = 66.2643;
    CONSTANTS(:,10) = 0.819033;
    CONSTANTS(:,11) = 0.755897;
    CONSTANTS(:,12) = 11.0374;
    CONSTANTS(:,13) = 11.9593;
    CONSTANTS(:,14) = 8.31;
    CONSTANTS(:,15) = 310;
    CONSTANTS(:,16) = 1;
    CONSTANTS(:,17) = 2;
    CONSTANTS(:,18) = 1;
    CONSTANTS(:,19) = -1;
    CONSTANTS(:,20) = 1;
    CONSTANTS(:,21) = 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
    ALGEBRAIC(:,1) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,8).*STATES(:,1));
    ALGEBRAIC(:,3) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,9).*STATES(:,2));
    ALGEBRAIC(:,7) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,13).*STATES(:,6));
    ALGEBRAIC(:,8) =  CONSTANTS(:,3).*exp(ALGEBRAIC(:,7)./( CONSTANTS(:,14).*CONSTANTS(:,15))).*(exp(ALGEBRAIC(:,1)./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp(ALGEBRAIC(:,3)./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    RATES(:,1) =  - ALGEBRAIC(:,8);
    ALGEBRAIC(:,4) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,10).*STATES(:,3));
    ALGEBRAIC(:,9) =  CONSTANTS(:,4).*(exp(ALGEBRAIC(:,7)./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp((ALGEBRAIC(:,4)+ CONSTANTS(:,17).*ALGEBRAIC(:,3))./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    ALGEBRAIC(:,5) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,11).*STATES(:,4));
    ALGEBRAIC(:,10) =  CONSTANTS(:,5).*(exp((ALGEBRAIC(:,4)+ CONSTANTS(:,16).*ALGEBRAIC(:,3))./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp(ALGEBRAIC(:,5)./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    RATES(:,3) = ALGEBRAIC(:,9) - ALGEBRAIC(:,10);
    ALGEBRAIC(:,6) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,12).*STATES(:,5));
    ALGEBRAIC(:,11) =  CONSTANTS(:,6).*(exp((ALGEBRAIC(:,5)+ CONSTANTS(:,17).*ALGEBRAIC(:,3))./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp(ALGEBRAIC(:,6)./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    RATES(:,4) = ALGEBRAIC(:,10) - ALGEBRAIC(:,11);
    ALGEBRAIC(:,12) =  CONSTANTS(:,7).*(exp(ALGEBRAIC(:,6)./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp((ALGEBRAIC(:,7)+ CONSTANTS(:,16).*ALGEBRAIC(:,3))./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    RATES(:,6) = ALGEBRAIC(:,12) - ALGEBRAIC(:,9);
    RATES(:,5) = ALGEBRAIC(:,11) - ALGEBRAIC(:,12);
    ALGEBRAIC(:,13) = (( CONSTANTS(:,17).*ALGEBRAIC(:,9) -  CONSTANTS(:,16).*ALGEBRAIC(:,10)) -  CONSTANTS(:,17).*ALGEBRAIC(:,11))+ CONSTANTS(:,16).*ALGEBRAIC(:,12);
    RATES(:,2) = ALGEBRAIC(:,8)+ALGEBRAIC(:,13);
   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(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,8).*STATES(:,1));
    ALGEBRAIC(:,3) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,9).*STATES(:,2));
    ALGEBRAIC(:,7) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,13).*STATES(:,6));
    ALGEBRAIC(:,8) =  CONSTANTS(:,3).*exp(ALGEBRAIC(:,7)./( CONSTANTS(:,14).*CONSTANTS(:,15))).*(exp(ALGEBRAIC(:,1)./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp(ALGEBRAIC(:,3)./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    ALGEBRAIC(:,4) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,10).*STATES(:,3));
    ALGEBRAIC(:,9) =  CONSTANTS(:,4).*(exp(ALGEBRAIC(:,7)./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp((ALGEBRAIC(:,4)+ CONSTANTS(:,17).*ALGEBRAIC(:,3))./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    ALGEBRAIC(:,5) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,11).*STATES(:,4));
    ALGEBRAIC(:,10) =  CONSTANTS(:,5).*(exp((ALGEBRAIC(:,4)+ CONSTANTS(:,16).*ALGEBRAIC(:,3))./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp(ALGEBRAIC(:,5)./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    ALGEBRAIC(:,6) =  CONSTANTS(:,14).*CONSTANTS(:,15).*log( CONSTANTS(:,12).*STATES(:,5));
    ALGEBRAIC(:,11) =  CONSTANTS(:,6).*(exp((ALGEBRAIC(:,5)+ CONSTANTS(:,17).*ALGEBRAIC(:,3))./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp(ALGEBRAIC(:,6)./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    ALGEBRAIC(:,12) =  CONSTANTS(:,7).*(exp(ALGEBRAIC(:,6)./( CONSTANTS(:,14).*CONSTANTS(:,15))) - exp((ALGEBRAIC(:,7)+ CONSTANTS(:,16).*ALGEBRAIC(:,3))./( CONSTANTS(:,14).*CONSTANTS(:,15))));
    ALGEBRAIC(:,13) = (( CONSTANTS(:,17).*ALGEBRAIC(:,9) -  CONSTANTS(:,16).*ALGEBRAIC(:,10)) -  CONSTANTS(:,17).*ALGEBRAIC(:,11))+ CONSTANTS(:,16).*ALGEBRAIC(:,12);
    ALGEBRAIC(:,2) =  CONSTANTS(:,17).*STATES(:,6)+ CONSTANTS(:,16).*STATES(:,4)+ (CONSTANTS(:,16)+CONSTANTS(:,17)).*STATES(:,5);
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