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 =3;
end
% There are a total of 9 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_CONSTANTS(:,1) = strpad('EGF in component EGF (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('pro_EGFR in component pro_EGFR (dimensionless)');
    LEGEND_STATES(:,1) = strpad('EGFR in component EGFR (dimensionless)');
    LEGEND_STATES(:,2) = strpad('EGF_EGFR in component EGF_EGFR (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('k2_EGF_EGFR in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,4) = strpad('k1_EGF_EGFR in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,5) = strpad('EGFR_turnover in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('k1_EGFR_phosphorylation in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,3) = strpad('pEGFR in component pEGFR (dimensionless)');
    LEGEND_STATES(:,4) = strpad('pEGFR_Akt in component pEGFR_Akt (dimensionless)');
    LEGEND_STATES(:,5) = strpad('Akt in component Akt (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('k1_pEGFR_degradation in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('k1_Akt_phosphorylation in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('k1_pEGFR_Akt in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('k2_pEGFR_Akt in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,6) = strpad('pAkt in component pAkt (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('k1_pAkt_dephosphorylation in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,7) = strpad('S6 in component S6 (dimensionless)');
    LEGEND_STATES(:,8) = strpad('pAkt_S6 in component pAkt_S6 (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('k1_pAkt_S6 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('k2_pAkt_S6 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('k1_S6_phosphorylation in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,9) = strpad('pS6 in component pS6 (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('k1_pS6_dephosphorylation in component model_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,1) = strpad('pEGFR_total in component pEGFR_total (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('pEGFR_scalefactor in component pEGFR_total (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('pAkt_total in component pAkt_total (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('pAkt_scalefactor in component pAkt_total (dimensionless)');
    LEGEND_ALGEBRAIC(:,3) = strpad('pS6_total in component pS6_total (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('pS6_scalefactor in component pS6_total (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt EGFR in component EGFR (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt EGF_EGFR in component EGF_EGFR (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt pEGFR in component pEGFR (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt pEGFR_Akt in component pEGFR_Akt (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt Akt in component Akt (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt pAkt in component pAkt (dimensionless)');
    LEGEND_RATES(:,8) = strpad('d/dt pAkt_S6 in component pAkt_S6 (dimensionless)');
    LEGEND_RATES(:,7) = strpad('d/dt S6 in component S6 (dimensionless)');
    LEGEND_RATES(:,9) = strpad('d/dt pS6 in component pS6 (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) = 30.0;
    CONSTANTS(:,2) = 6.81902e4;
    STATES(:,1) = 6.81902e4;
    STATES(:,2) = 0.0;
    CONSTANTS(:,3) = 4.07490e-2;
    CONSTANTS(:,4) = 6.73816e-3;
    CONSTANTS(:,5) = 1.06386129269658e-4;
    CONSTANTS(:,6) = 1.92391e-2;
    STATES(:,3) = 0.0;
    STATES(:,4) = 0.0;
    STATES(:,5) = 4.33090e-2;
    CONSTANTS(:,7) = 9.97194e-2;
    CONSTANTS(:,8) = 3.05684e-2;
    CONSTANTS(:,9) = 1.55430e-5;
    CONSTANTS(:,10) = 5.17473e-3;
    STATES(:,6) = 0.0;
    CONSTANTS(:,11) = 3.27962e-2;
    STATES(:,7) = 3.54317e0;
    STATES(:,8) = 0.0;
    CONSTANTS(:,12) = 2.10189e-6;
    CONSTANTS(:,13) = 5.17940e-15;
    CONSTANTS(:,14) = 1.21498e-3;
    STATES(:,9) = 0.0;
    CONSTANTS(:,15) = 1.13102e-3;
    CONSTANTS(:,16) = 1.81735e-4;
    CONSTANTS(:,17) = 6.00588e1;
    CONSTANTS(:,18) = 4.98862e4;
    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(:,1) = ( CONSTANTS(:,3).*STATES(:,2)+ CONSTANTS(:,5).*(CONSTANTS(:,2) - STATES(:,1))) -  CONSTANTS(:,4).*CONSTANTS(:,1).*STATES(:,1);
    RATES(:,2) =  CONSTANTS(:,4).*CONSTANTS(:,1).*STATES(:,1) - ( CONSTANTS(:,3).*STATES(:,2)+ CONSTANTS(:,6).*STATES(:,2));
    RATES(:,3) = ( CONSTANTS(:,6).*STATES(:,2)+ CONSTANTS(:,8).*STATES(:,4)+ CONSTANTS(:,10).*STATES(:,4)) - ( CONSTANTS(:,7).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,3).*STATES(:,5));
    RATES(:,4) =  CONSTANTS(:,9).*STATES(:,3).*STATES(:,5) - ( CONSTANTS(:,10).*STATES(:,4)+ CONSTANTS(:,8).*STATES(:,4));
    RATES(:,5) = ( CONSTANTS(:,10).*STATES(:,4)+ CONSTANTS(:,11).*STATES(:,6)) -  CONSTANTS(:,9).*STATES(:,3).*STATES(:,5);
    RATES(:,6) = ( CONSTANTS(:,8).*STATES(:,4)+ CONSTANTS(:,13).*STATES(:,8)+ CONSTANTS(:,14).*STATES(:,8)) - ( CONSTANTS(:,11).*STATES(:,6)+ CONSTANTS(:,12).*STATES(:,6).*STATES(:,7));
    RATES(:,8) =  CONSTANTS(:,12).*STATES(:,6).*STATES(:,7) - ( CONSTANTS(:,13).*STATES(:,8)+ CONSTANTS(:,14).*STATES(:,8));
    RATES(:,7) = ( CONSTANTS(:,13).*STATES(:,8)+ CONSTANTS(:,15).*STATES(:,9)) -  CONSTANTS(:,12).*STATES(:,6).*STATES(:,7);
    RATES(:,9) =  CONSTANTS(:,14).*STATES(:,8) -  CONSTANTS(:,15).*STATES(:,9);
   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) =  (STATES(:,3)+STATES(:,4)).*CONSTANTS(:,16);
    ALGEBRAIC(:,2) =  (STATES(:,6)+STATES(:,8)).*CONSTANTS(:,17);
    ALGEBRAIC(:,3) =  STATES(:,9).*CONSTANTS(:,18);
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