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 =4;
end
% There are a total of 3 entries in each of the rate and state variable arrays.
% There are a total of 25 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 (second)');
    LEGEND_STATES(:,1) = strpad('Vm in component membrane (millivolt)');
    LEGEND_CONSTANTS(:,1) = strpad('C in component membrane (picoF)');
    LEGEND_ALGEBRAIC(:,3) = strpad('i_K in component potassium_current (picoA)');
    LEGEND_ALGEBRAIC(:,4) = strpad('i_R in component repolarising_current (picoA)');
    LEGEND_STATES(:,2) = strpad('IP3 in component IP3 (nanomolar)');
    LEGEND_CONSTANTS(:,2) = strpad('m3IP3 in component IP3 (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('m4IP3 in component IP3 (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('kIP3 in component IP3 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,5) = strpad('A in component IP3 (dimensionless)');
    LEGEND_STATES(:,3) = strpad('Ca in component Ca (nanomolar)');
    LEGEND_CONSTANTS(:,6) = strpad('m3SR in component Ca (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('m4SR in component Ca (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('m3PMCA in component Ca (dimensionless)');
    LEGEND_CONSTANTS(:,9) = strpad('m4PMCA in component Ca (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('kSR_rel in component Ca (flux)');
    LEGEND_CONSTANTS(:,11) = strpad('kPMCA in component Ca (flux)');
    LEGEND_ALGEBRAIC(:,2) = strpad('Jcat in component Jcat (flux)');
    LEGEND_CONSTANTS(:,12) = strpad('ECa in component Jcat (millivolt)');
    LEGEND_CONSTANTS(:,13) = strpad('Gcat in component Jcat (nanomolar_per_millivolt_second)');
    LEGEND_CONSTANTS(:,14) = strpad('m3cat in component Jcat (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('m4cat in component Jcat (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('Gtot in component potassium_current (picoS)');
    LEGEND_ALGEBRAIC(:,1) = strpad('PoBKCa in component potassium_current (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('PoSKCa in component potassium_current (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('E_K in component potassium_current (millivolt)');
    LEGEND_CONSTANTS(:,19) = strpad('a in component potassium_current (dimensionless)');
    LEGEND_CONSTANTS(:,20) = strpad('b in component potassium_current (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('c in component potassium_current (dimensionless)');
    LEGEND_CONSTANTS(:,22) = strpad('m3 in component potassium_current (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('m4 in component potassium_current (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('GR in component repolarising_current (picoS)');
    LEGEND_CONSTANTS(:,25) = strpad('Vrest in component repolarising_current (millivolt)');
    LEGEND_RATES(:,1) = strpad('d/dt Vm in component membrane (millivolt)');
    LEGEND_RATES(:,2) = strpad('d/dt IP3 in component IP3 (nanomolar)');
    LEGEND_RATES(:,3) = strpad('d/dt Ca in component Ca (nanomolar)');
    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 = [];
    STATES(:,1) = -31.1;
    CONSTANTS(:,1) = 1.0;
    STATES(:,2) = 1.0;
    CONSTANTS(:,2) = 4.0;
    CONSTANTS(:,3) = 55.0;
    CONSTANTS(:,4) = 0.1733;
    CONSTANTS(:,5) = 0.211;
    STATES(:,3) = 50.0;
    CONSTANTS(:,6) = 1.1;
    CONSTANTS(:,7) = 0.3;
    CONSTANTS(:,8) = -6.19;
    CONSTANTS(:,9) = 0.39;
    CONSTANTS(:,10) = 180.0;
    CONSTANTS(:,11) = 0.679;
    CONSTANTS(:,12) = 50.0;
    CONSTANTS(:,13) = 0.66;
    CONSTANTS(:,14) = -6.18;
    CONSTANTS(:,15) = 0.37;
    CONSTANTS(:,16) = 6927;
    CONSTANTS(:,17) = 0.5;
    CONSTANTS(:,18) = -80.0;
    CONSTANTS(:,19) = 53.3;
    CONSTANTS(:,20) = -80.8;
    CONSTANTS(:,21) = -6.4;
    CONSTANTS(:,22) = 1.32E-3;
    CONSTANTS(:,23) = 0.30;
    CONSTANTS(:,24) = 955.0;
    CONSTANTS(:,25) = -31.1;
    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
    RATES(:,2) =  CONSTANTS(:,5).*(1.00000+ tanh((CONSTANTS(:,2) - VOI)./CONSTANTS(:,3))) -  CONSTANTS(:,4).*STATES(:,2);
    RATES(:,3) =  (CONSTANTS(:,10)./2.00000).*(1.00000+ tanh((STATES(:,2) - CONSTANTS(:,6))./CONSTANTS(:,7))) -  (CONSTANTS(:,11)./2.00000).*(1.00000+ tanh((arbitrary_log(STATES(:,3), 10) - CONSTANTS(:,8))./CONSTANTS(:,9)));
    ALGEBRAIC(:,1) =  0.500000.*(1.00000+ tanh(( (arbitrary_log(STATES(:,3), 10) - CONSTANTS(:,21)).*(STATES(:,1) - CONSTANTS(:,20)) - CONSTANTS(:,19))./( CONSTANTS(:,22).*power((STATES(:,1)+ CONSTANTS(:,19).*(arbitrary_log(STATES(:,3), 10) - CONSTANTS(:,21))) - CONSTANTS(:,20), 2.00000)+CONSTANTS(:,23))));
    ALGEBRAIC(:,3) =  CONSTANTS(:,16).*(STATES(:,1) - CONSTANTS(:,18)).*( 0.400000.*ALGEBRAIC(:,1)+ 0.600000.*CONSTANTS(:,17));
    ALGEBRAIC(:,4) =  CONSTANTS(:,24).*(STATES(:,1) - CONSTANTS(:,25));
    RATES(:,1) =   - (1.00000./CONSTANTS(:,1)).*(ALGEBRAIC(:,3)+ALGEBRAIC(:,4));
   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) =  0.500000.*(1.00000+ tanh(( (arbitrary_log(STATES(:,3), 10) - CONSTANTS(:,21)).*(STATES(:,1) - CONSTANTS(:,20)) - CONSTANTS(:,19))./( CONSTANTS(:,22).*power((STATES(:,1)+ CONSTANTS(:,19).*(arbitrary_log(STATES(:,3), 10) - CONSTANTS(:,21))) - CONSTANTS(:,20), 2.00000)+CONSTANTS(:,23))));
    ALGEBRAIC(:,3) =  CONSTANTS(:,16).*(STATES(:,1) - CONSTANTS(:,18)).*( 0.400000.*ALGEBRAIC(:,1)+ 0.600000.*CONSTANTS(:,17));
    ALGEBRAIC(:,4) =  CONSTANTS(:,24).*(STATES(:,1) - CONSTANTS(:,25));
    ALGEBRAIC(:,2) =  ( CONSTANTS(:,13).*(CONSTANTS(:,12) - STATES(:,1))).*( 0.500000.*(1.00000+ tanh((arbitrary_log(STATES(:,3), 10) - CONSTANTS(:,14))./CONSTANTS(:,15))));
end

% Compute a logarithm to any base" +
function x = arbitrary_log(a, base)
    x = log(a) ./ log(base);
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