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 14 entries in each of the rate and state variable arrays.
% There are a total of 18 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('O2 in component O2 (molar)');
    LEGEND_ALGEBRAIC(:,1) = strpad('v1 in component v1 (flux)');
    LEGEND_ALGEBRAIC(:,5) = strpad('v5 in component v5 (flux)');
    LEGEND_ALGEBRAIC(:,7) = strpad('v7 in component v7 (flux)');
    LEGEND_ALGEBRAIC(:,11) = strpad('v11 in component v11 (flux)');
    LEGEND_ALGEBRAIC(:,13) = strpad('v13_back in component v13_back (flux)');
    LEGEND_CONSTANTS(:,18) = strpad('v13 in component v13 (flux)');
    LEGEND_STATES(:,2) = strpad('O2_radical in component O2_radical (molar)');
    LEGEND_ALGEBRAIC(:,6) = strpad('v6 in component v6 (flux)');
    LEGEND_STATES(:,3) = strpad('NADH in component NADH (molar)');
    LEGEND_CONSTANTS(:,17) = strpad('v12 in component v12 (flux)');
    LEGEND_ALGEBRAIC(:,12) = strpad('v14 in component v14 (flux)');
    LEGEND_STATES(:,4) = strpad('NAD in component NAD (molar)');
    LEGEND_ALGEBRAIC(:,8) = strpad('v8 in component v8 (flux)');
    LEGEND_ALGEBRAIC(:,10) = strpad('v10 in component v10 (flux)');
    LEGEND_STATES(:,5) = strpad('NAD_radical in component NAD_radical (molar)');
    LEGEND_ALGEBRAIC(:,9) = strpad('v9 in component v9 (flux)');
    LEGEND_STATES(:,6) = strpad('NAD2 in component NAD2 (molar)');
    LEGEND_STATES(:,7) = strpad('H2O2 in component H2O2 (molar)');
    LEGEND_ALGEBRAIC(:,2) = strpad('v2 in component v2 (flux)');
    LEGEND_STATES(:,8) = strpad('Per3 in component Per3 (molar)');
    LEGEND_ALGEBRAIC(:,4) = strpad('v4 in component v4 (flux)');
    LEGEND_STATES(:,9) = strpad('Per2 in component Per2 (molar)');
    LEGEND_STATES(:,10) = strpad('coI in component coI (molar)');
    LEGEND_ALGEBRAIC(:,3) = strpad('v3 in component v3 (flux)');
    LEGEND_STATES(:,11) = strpad('coII in component coII (molar)');
    LEGEND_STATES(:,12) = strpad('coIII in component coIII (molar)');
    LEGEND_STATES(:,13) = strpad('Ar_radical in component Ar_radical (molar)');
    LEGEND_STATES(:,14) = strpad('ArH in component ArH (molar)');
    LEGEND_CONSTANTS(:,1) = strpad('k1 in component v1 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,2) = strpad('k2 in component v2 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,3) = strpad('k3 in component v3 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,4) = strpad('k4 in component v4 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,5) = strpad('k5 in component v5 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('k6 in component v6 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('k7 in component v7 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('k8 in component v8 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('k9 in component v9 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('k10 in component v10 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('k11 in component v11 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('k12 in component v12 (flux)');
    LEGEND_CONSTANTS(:,13) = strpad('O2eq in component v13 (molar)');
    LEGEND_CONSTANTS(:,14) = strpad('k13 in component v13 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,15) = strpad('k13_ in component v13_back (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('k14 in component v14 (second_order_rate_constant)');
    LEGEND_RATES(:,1) = strpad('d/dt O2 in component O2 (molar)');
    LEGEND_RATES(:,2) = strpad('d/dt O2_radical in component O2_radical (molar)');
    LEGEND_RATES(:,3) = strpad('d/dt NADH in component NADH (molar)');
    LEGEND_RATES(:,4) = strpad('d/dt NAD in component NAD (molar)');
    LEGEND_RATES(:,5) = strpad('d/dt NAD_radical in component NAD_radical (molar)');
    LEGEND_RATES(:,6) = strpad('d/dt NAD2 in component NAD2 (molar)');
    LEGEND_RATES(:,7) = strpad('d/dt H2O2 in component H2O2 (molar)');
    LEGEND_RATES(:,8) = strpad('d/dt Per3 in component Per3 (molar)');
    LEGEND_RATES(:,9) = strpad('d/dt Per2 in component Per2 (molar)');
    LEGEND_RATES(:,10) = strpad('d/dt coI in component coI (molar)');
    LEGEND_RATES(:,11) = strpad('d/dt coII in component coII (molar)');
    LEGEND_RATES(:,12) = strpad('d/dt coIII in component coIII (molar)');
    LEGEND_RATES(:,13) = strpad('d/dt Ar_radical in component Ar_radical (molar)');
    LEGEND_RATES(:,14) = strpad('d/dt ArH in component ArH (molar)');
    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) = 5.33e-6;
    STATES(:,2) = 0.026e-6;
    STATES(:,3) = 0.95e-6;
    STATES(:,4) = 558e-6;
    STATES(:,5) = 5e-10;
    STATES(:,6) = 0.12e-6;
    STATES(:,7) = 0.013e-6;
    STATES(:,8) = 0.059e-6;
    STATES(:,9) = 1e-10;
    STATES(:,10) = 8.9e-10;
    STATES(:,11) = 0.026e-6;
    STATES(:,12) = 1.31e-6;
    STATES(:,13) = 0.12e-6;
    STATES(:,14) = 299.88e-6;
    CONSTANTS(:,1) = 3;
    CONSTANTS(:,2) = 1.8e7;
    CONSTANTS(:,3) = 1.5e5;
    CONSTANTS(:,4) = 5.2e3;
    CONSTANTS(:,5) = 2e7;
    CONSTANTS(:,6) = 1.7e7;
    CONSTANTS(:,7) = 2e7;
    CONSTANTS(:,8) = 4e7;
    CONSTANTS(:,9) = 6e7;
    CONSTANTS(:,10) = 1.8e6;
    CONSTANTS(:,11) = 1e5;
    CONSTANTS(:,12) = 0.08e-6;
    CONSTANTS(:,13) = 1.2e-5;
    CONSTANTS(:,14) = 6e-3;
    CONSTANTS(:,15) = 6e-3;
    CONSTANTS(:,16) = 7e5;
    CONSTANTS(:,17) = CONSTANTS(:,12);
    CONSTANTS(:,18) =  CONSTANTS(:,14).*CONSTANTS(:,13);
    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(:,4) =  CONSTANTS(:,4).*STATES(:,11).*STATES(:,14);
    ALGEBRAIC(:,3) =  CONSTANTS(:,3).*STATES(:,10).*STATES(:,14);
    RATES(:,11) = ALGEBRAIC(:,3) - ALGEBRAIC(:,4);
    ALGEBRAIC(:,5) =  CONSTANTS(:,5).*STATES(:,5).*STATES(:,1);
    ALGEBRAIC(:,7) =  CONSTANTS(:,7).*power(STATES(:,2), 2.00000);
    ALGEBRAIC(:,6) =  CONSTANTS(:,6).*STATES(:,2).*STATES(:,8);
    RATES(:,2) = (ALGEBRAIC(:,5) - ALGEBRAIC(:,6)) -  2.00000.*ALGEBRAIC(:,7);
    ALGEBRAIC(:,1) =  CONSTANTS(:,1).*STATES(:,3).*STATES(:,1);
    ALGEBRAIC(:,2) =  CONSTANTS(:,2).*STATES(:,7).*STATES(:,8);
    RATES(:,7) = (ALGEBRAIC(:,1) - ALGEBRAIC(:,2)) - ALGEBRAIC(:,7);
    ALGEBRAIC(:,8) =  CONSTANTS(:,8).*STATES(:,12).*STATES(:,5);
    RATES(:,10) = (ALGEBRAIC(:,2) - ALGEBRAIC(:,3))+ALGEBRAIC(:,8);
    ALGEBRAIC(:,9) =  CONSTANTS(:,9).*power(STATES(:,5), 2.00000);
    RATES(:,6) = ALGEBRAIC(:,9);
    ALGEBRAIC(:,10) =  CONSTANTS(:,10).*STATES(:,8).*STATES(:,5);
    RATES(:,4) = ALGEBRAIC(:,1)+ALGEBRAIC(:,5)+ALGEBRAIC(:,8)+ALGEBRAIC(:,10);
    RATES(:,8) = (( - ALGEBRAIC(:,2)+ALGEBRAIC(:,4)) - ALGEBRAIC(:,6)) - ALGEBRAIC(:,10);
    ALGEBRAIC(:,12) =  CONSTANTS(:,16).*STATES(:,13).*STATES(:,3);
    RATES(:,3) = ( - ALGEBRAIC(:,1)+CONSTANTS(:,17)) - ALGEBRAIC(:,12);
    RATES(:,5) = (((  - 2.00000.*ALGEBRAIC(:,9) - ALGEBRAIC(:,5)) - ALGEBRAIC(:,8)) - ALGEBRAIC(:,10))+ALGEBRAIC(:,12);
    ALGEBRAIC(:,11) =  CONSTANTS(:,11).*STATES(:,9).*STATES(:,1);
    RATES(:,9) =  - ALGEBRAIC(:,11)+ALGEBRAIC(:,10);
    RATES(:,12) = (ALGEBRAIC(:,6) - ALGEBRAIC(:,8))+ALGEBRAIC(:,11);
    RATES(:,13) = (ALGEBRAIC(:,3)+ALGEBRAIC(:,4)) - ALGEBRAIC(:,12);
    RATES(:,14) = ( - ALGEBRAIC(:,3) - ALGEBRAIC(:,4))+ALGEBRAIC(:,12);
    ALGEBRAIC(:,13) =  CONSTANTS(:,15).*STATES(:,1);
    RATES(:,1) = (((( - ALGEBRAIC(:,1) - ALGEBRAIC(:,5))+ALGEBRAIC(:,7)) - ALGEBRAIC(:,11))+CONSTANTS(:,18)) - 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(:,4) =  CONSTANTS(:,4).*STATES(:,11).*STATES(:,14);
    ALGEBRAIC(:,3) =  CONSTANTS(:,3).*STATES(:,10).*STATES(:,14);
    ALGEBRAIC(:,5) =  CONSTANTS(:,5).*STATES(:,5).*STATES(:,1);
    ALGEBRAIC(:,7) =  CONSTANTS(:,7).*power(STATES(:,2), 2.00000);
    ALGEBRAIC(:,6) =  CONSTANTS(:,6).*STATES(:,2).*STATES(:,8);
    ALGEBRAIC(:,1) =  CONSTANTS(:,1).*STATES(:,3).*STATES(:,1);
    ALGEBRAIC(:,2) =  CONSTANTS(:,2).*STATES(:,7).*STATES(:,8);
    ALGEBRAIC(:,8) =  CONSTANTS(:,8).*STATES(:,12).*STATES(:,5);
    ALGEBRAIC(:,9) =  CONSTANTS(:,9).*power(STATES(:,5), 2.00000);
    ALGEBRAIC(:,10) =  CONSTANTS(:,10).*STATES(:,8).*STATES(:,5);
    ALGEBRAIC(:,12) =  CONSTANTS(:,16).*STATES(:,13).*STATES(:,3);
    ALGEBRAIC(:,11) =  CONSTANTS(:,11).*STATES(:,9).*STATES(:,1);
    ALGEBRAIC(:,13) =  CONSTANTS(:,15).*STATES(:,1);
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