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 =19;
end
% There are a total of 9 entries in each of the rate and state variable arrays.
% There are a total of 29 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 (hour)');
    LEGEND_ALGEBRAIC(:,2) = strpad('v_1 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_CONSTANTS(:,26) = strpad('v_2 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_CONSTANTS(:,27) = strpad('v_3 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,3) = strpad('v_4 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_CONSTANTS(:,28) = strpad('v_5 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,4) = strpad('v_6 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_CONSTANTS(:,29) = strpad('v_7 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,5) = strpad('v_8 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,6) = strpad('v_9 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,7) = strpad('v_10 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,8) = strpad('v_11 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,9) = strpad('v_12 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,10) = strpad('v_13 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,11) = strpad('v_14 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,12) = strpad('v_15 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,13) = strpad('v_16 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,14) = strpad('v_17 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,15) = strpad('v_18 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,16) = strpad('v_19 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,18) = strpad('v_20 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,17) = strpad('v_21 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_ALGEBRAIC(:,19) = strpad('v_22 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_CONSTANTS(:,1) = strpad('k_1 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_CONSTANTS(:,2) = strpad('k_2 in component main (pmol_per_ug_DNA_per_hour)');
    LEGEND_CONSTANTS(:,3) = strpad('k_3 in component main (per_hour)');
    LEGEND_CONSTANTS(:,4) = strpad('k_4 in component main (per_hour)');
    LEGEND_CONSTANTS(:,5) = strpad('k_5 in component main (ug_DNA_per_pmol_per_hour)');
    LEGEND_CONSTANTS(:,6) = strpad('k_6 in component main (per_hour)');
    LEGEND_CONSTANTS(:,7) = strpad('k_7 in component main (per_hour)');
    LEGEND_CONSTANTS(:,8) = strpad('k_8 in component main (per_hour)');
    LEGEND_CONSTANTS(:,9) = strpad('k_9 in component main (per_hour)');
    LEGEND_CONSTANTS(:,10) = strpad('k_10 in component main (ug_DNA_per_pmol_per_hour)');
    LEGEND_CONSTANTS(:,11) = strpad('k_11 in component main (per_hour)');
    LEGEND_CONSTANTS(:,12) = strpad('k_12 in component main (per_hour)');
    LEGEND_CONSTANTS(:,13) = strpad('k_13 in component main (per_hour)');
    LEGEND_CONSTANTS(:,14) = strpad('k_14 in component main (per_hour)');
    LEGEND_CONSTANTS(:,15) = strpad('k_15 in component main (per_hour)');
    LEGEND_CONSTANTS(:,16) = strpad('k_16 in component main (per_hour)');
    LEGEND_CONSTANTS(:,17) = strpad('k_17 in component main (per_hour)');
    LEGEND_CONSTANTS(:,18) = strpad('k_18 in component main (per_hour)');
    LEGEND_CONSTANTS(:,19) = strpad('k_19 in component main (per_hour)');
    LEGEND_CONSTANTS(:,20) = strpad('k_20 in component main (per_hour)');
    LEGEND_CONSTANTS(:,21) = strpad('k_21 in component main (per_hour)');
    LEGEND_CONSTANTS(:,22) = strpad('k_22 in component main (per_hour)');
    LEGEND_ALGEBRAIC(:,1) = strpad('LPS in component main (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('PIP_2 in component main (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('DG in component main (pmol_per_ug_DNA)');
    LEGEND_CONSTANTS(:,25) = strpad('GPCho in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,1) = strpad('AA in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,2) = strpad('HETE in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,3) = strpad('PGH_2 in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,4) = strpad('PGE_2 in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,5) = strpad('PGF_2a in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,6) = strpad('PGD_2 in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,7) = strpad('PGJ_2 in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,8) = strpad('dPGD_2 in component main (pmol_per_ug_DNA)');
    LEGEND_STATES(:,9) = strpad('dPGJ_2 in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,1) = strpad('d/dt AA in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,2) = strpad('d/dt HETE in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,3) = strpad('d/dt PGH_2 in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,4) = strpad('d/dt PGE_2 in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,5) = strpad('d/dt PGF_2a in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,6) = strpad('d/dt PGD_2 in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,7) = strpad('d/dt PGJ_2 in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,8) = strpad('d/dt dPGD_2 in component main (pmol_per_ug_DNA)');
    LEGEND_RATES(:,9) = strpad('d/dt dPGJ_2 in component main (pmol_per_ug_DNA)');
    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) = 355.637;
    CONSTANTS(:,2) = 1e-15;
    CONSTANTS(:,3) = 1e-15;
    CONSTANTS(:,4) = 1e-15;
    CONSTANTS(:,5) = 1e-15;
    CONSTANTS(:,6) = 0.33;
    CONSTANTS(:,7) = 1e-15;
    CONSTANTS(:,8) = 0.007;
    CONSTANTS(:,9) = 0.187;
    CONSTANTS(:,10) = 0.024;
    CONSTANTS(:,11) = 0.111;
    CONSTANTS(:,12) = 0.098;
    CONSTANTS(:,13) = 0.204;
    CONSTANTS(:,14) = 1e-15;
    CONSTANTS(:,15) = 0.061;
    CONSTANTS(:,16) = 1e-15;
    CONSTANTS(:,17) = 3.116;
    CONSTANTS(:,18) = 0.054;
    CONSTANTS(:,19) = 0.029;
    CONSTANTS(:,20) = 0.014;
    CONSTANTS(:,21) = 0.034;
    CONSTANTS(:,22) = 0.116;
    CONSTANTS(:,23) = 1;
    CONSTANTS(:,24) = 0;
    CONSTANTS(:,25) = 1;
    STATES(:,1) = 25;
    STATES(:,2) = 0;
    STATES(:,3) = 0;
    STATES(:,4) = 0;
    STATES(:,5) = 0;
    STATES(:,6) = 0;
    STATES(:,7) = 0;
    STATES(:,8) = 0;
    STATES(:,9) = 0;
    CONSTANTS(:,26) =  CONSTANTS(:,23).*CONSTANTS(:,2);
    CONSTANTS(:,27) =  CONSTANTS(:,24).*CONSTANTS(:,3);
    CONSTANTS(:,28) =  CONSTANTS(:,24).*CONSTANTS(:,25).*CONSTANTS(:,5);
    CONSTANTS(:,29) =  CONSTANTS(:,25).*CONSTANTS(:,7);
    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(:,5) =  STATES(:,1).*CONSTANTS(:,8);
    ALGEBRAIC(:,6) =  STATES(:,2).*CONSTANTS(:,9);
    RATES(:,2) = ALGEBRAIC(:,5) - ALGEBRAIC(:,6);
    ALGEBRAIC(:,1) = piecewise({VOI<=0.500000,  VOI.*2.00000 , VOI>0.500000&VOI<2.00000, 1.00000 - (VOI - 0.500000)./1.50000 }, 0.00000);
    ALGEBRAIC(:,2) =  ALGEBRAIC(:,1).*CONSTANTS(:,23).*CONSTANTS(:,1);
    ALGEBRAIC(:,3) =  STATES(:,1).*CONSTANTS(:,4);
    ALGEBRAIC(:,4) =  ALGEBRAIC(:,1).*CONSTANTS(:,25).*CONSTANTS(:,6);
    ALGEBRAIC(:,7) =  CONSTANTS(:,24).*STATES(:,1).*CONSTANTS(:,10);
    ALGEBRAIC(:,8) =  ALGEBRAIC(:,1).*STATES(:,1).*CONSTANTS(:,11);
    ALGEBRAIC(:,9) =  STATES(:,1).*CONSTANTS(:,12);
    RATES(:,1) = ((((((ALGEBRAIC(:,2)+CONSTANTS(:,26)+CONSTANTS(:,27)) - ALGEBRAIC(:,3))+CONSTANTS(:,28)+ALGEBRAIC(:,4)+CONSTANTS(:,29)) - ALGEBRAIC(:,5)) - ALGEBRAIC(:,7)) - ALGEBRAIC(:,8)) - ALGEBRAIC(:,9);
    ALGEBRAIC(:,10) =  STATES(:,3).*CONSTANTS(:,13);
    ALGEBRAIC(:,11) =  STATES(:,4).*CONSTANTS(:,14);
    RATES(:,4) = ALGEBRAIC(:,10) - ALGEBRAIC(:,11);
    ALGEBRAIC(:,12) =  STATES(:,3).*CONSTANTS(:,15);
    ALGEBRAIC(:,14) =  STATES(:,3).*CONSTANTS(:,17);
    RATES(:,3) = (((ALGEBRAIC(:,7)+ALGEBRAIC(:,8)+ALGEBRAIC(:,9)) - ALGEBRAIC(:,10)) - ALGEBRAIC(:,12)) - ALGEBRAIC(:,14);
    ALGEBRAIC(:,13) =  STATES(:,5).*CONSTANTS(:,16);
    RATES(:,5) = ALGEBRAIC(:,12) - ALGEBRAIC(:,13);
    ALGEBRAIC(:,15) =  STATES(:,6).*CONSTANTS(:,18);
    ALGEBRAIC(:,16) =  STATES(:,6).*CONSTANTS(:,19);
    RATES(:,6) = (ALGEBRAIC(:,14) - ALGEBRAIC(:,15)) - ALGEBRAIC(:,16);
    ALGEBRAIC(:,17) =  STATES(:,7).*CONSTANTS(:,21);
    RATES(:,7) = ALGEBRAIC(:,15) - ALGEBRAIC(:,17);
    ALGEBRAIC(:,18) =  STATES(:,8).*CONSTANTS(:,20);
    RATES(:,8) = ALGEBRAIC(:,16) - ALGEBRAIC(:,18);
    ALGEBRAIC(:,19) =  STATES(:,9).*CONSTANTS(:,22);
    RATES(:,9) = ALGEBRAIC(:,17) - ALGEBRAIC(:,19);
   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(:,5) =  STATES(:,1).*CONSTANTS(:,8);
    ALGEBRAIC(:,6) =  STATES(:,2).*CONSTANTS(:,9);
    ALGEBRAIC(:,1) = piecewise({VOI<=0.500000,  VOI.*2.00000 , VOI>0.500000&VOI<2.00000, 1.00000 - (VOI - 0.500000)./1.50000 }, 0.00000);
    ALGEBRAIC(:,2) =  ALGEBRAIC(:,1).*CONSTANTS(:,23).*CONSTANTS(:,1);
    ALGEBRAIC(:,3) =  STATES(:,1).*CONSTANTS(:,4);
    ALGEBRAIC(:,4) =  ALGEBRAIC(:,1).*CONSTANTS(:,25).*CONSTANTS(:,6);
    ALGEBRAIC(:,7) =  CONSTANTS(:,24).*STATES(:,1).*CONSTANTS(:,10);
    ALGEBRAIC(:,8) =  ALGEBRAIC(:,1).*STATES(:,1).*CONSTANTS(:,11);
    ALGEBRAIC(:,9) =  STATES(:,1).*CONSTANTS(:,12);
    ALGEBRAIC(:,10) =  STATES(:,3).*CONSTANTS(:,13);
    ALGEBRAIC(:,11) =  STATES(:,4).*CONSTANTS(:,14);
    ALGEBRAIC(:,12) =  STATES(:,3).*CONSTANTS(:,15);
    ALGEBRAIC(:,14) =  STATES(:,3).*CONSTANTS(:,17);
    ALGEBRAIC(:,13) =  STATES(:,5).*CONSTANTS(:,16);
    ALGEBRAIC(:,15) =  STATES(:,6).*CONSTANTS(:,18);
    ALGEBRAIC(:,16) =  STATES(:,6).*CONSTANTS(:,19);
    ALGEBRAIC(:,17) =  STATES(:,7).*CONSTANTS(:,21);
    ALGEBRAIC(:,18) =  STATES(:,8).*CONSTANTS(:,20);
    ALGEBRAIC(:,19) =  STATES(:,9).*CONSTANTS(:,22);
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