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 =21;
end
% There are a total of 11 entries in each of the rate and state variable arrays.
% There are a total of 33 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 (minute)');
    LEGEND_CONSTANTS(:,1) = strpad('k1 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,2) = strpad('k1_ in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,3) = strpad('k1__ in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,4) = strpad('kn1 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,5) = strpad('k2 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('kn2 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('k3 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('k3_ in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('kn4 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('k5 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('kn6 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('k8 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('k9 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('k10 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,15) = strpad('k17 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('k18 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('k19 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,18) = strpad('k20 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,19) = strpad('k21 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,20) = strpad('k22 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,21) = strpad('k24 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,22) = strpad('k25 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,23) = strpad('k25_ in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,24) = strpad('k26 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,25) = strpad('k26_ in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,26) = strpad('k28 in component rate_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,27) = strpad('k29 in component rate_constants (first_order_rate_constant)');
    LEGEND_STATES(:,1) = strpad('a_CyclinE_Cdk2 in component a_CyclinE_Cdk2 (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('V_2 in component V_2 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,14) = strpad('V_10 in component V_10 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,3) = strpad('V_n2 in component V_n2 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,13) = strpad('V_9 in component V_9 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,15) = strpad('V_21 in component V_21 (first_order_rate_constant)');
    LEGEND_STATES(:,2) = strpad('i_CyclinE_Cdk2 in component i_CyclinE_Cdk2 (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('V_3 in component V_3 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,10) = strpad('V_5 in component V_5 (first_order_rate_constant)');
    LEGEND_STATES(:,3) = strpad('pRB_E2F in component pRB_E2F (dimensionless)');
    LEGEND_CONSTANTS(:,33) = strpad('V_n1 in component V_n1 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,2) = strpad('V_1 in component V_1 (first_order_rate_constant)');
    LEGEND_STATES(:,4) = strpad('E2F in component E2F (dimensionless)');
    LEGEND_CONSTANTS(:,28) = strpad('V_4 in component E2F (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,7) = strpad('V_18 in component V_18 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,4) = strpad('V_n4 in component V_n4 (first_order_rate_constant)');
    LEGEND_STATES(:,5) = strpad('pRB in component pRB (dimensionless)');
    LEGEND_CONSTANTS(:,29) = strpad('V_27 in component pRB (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,5) = strpad('V_26 in component V_26 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,11) = strpad('V_29 in component V_29 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,8) = strpad('V_28 in component V_28 (first_order_rate_constant)');
    LEGEND_STATES(:,6) = strpad('CycD_Cdk4 in component CycD_Cdk4 (dimensionless)');
    LEGEND_CONSTANTS(:,30) = strpad('V_6 in component CycD_Cdk4 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,19) = strpad('V_20 in component V_20 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,9) = strpad('V_n6 in component V_n6 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,17) = strpad('V_19 in component V_19 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,16) = strpad('V_17 in component V_17 (first_order_rate_constant)');
    LEGEND_STATES(:,7) = strpad('p27 in component p27 (dimensionless)');
    LEGEND_CONSTANTS(:,31) = strpad('V_7 in component p27 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,12) = strpad('V_8 in component V_8 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,21) = strpad('V_22 in component V_22 (first_order_rate_constant)');
    LEGEND_STATES(:,8) = strpad('CycE_Cdk2_p27 in component CycE_Cdk2_p27 (dimensionless)');
    LEGEND_STATES(:,9) = strpad('CycD_Cdk4_p27 in component CycD_Cdk4_p27 (dimensionless)');
    LEGEND_STATES(:,10) = strpad('p16 in component p16 (dimensionless)');
    LEGEND_CONSTANTS(:,32) = strpad('V_23 in component p16 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,20) = strpad('V_25 in component V_25 (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,18) = strpad('V_24 in component V_24 (first_order_rate_constant)');
    LEGEND_STATES(:,11) = strpad('pRB_P in component pRB_P (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt a_CyclinE_Cdk2 in component a_CyclinE_Cdk2 (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt i_CyclinE_Cdk2 in component i_CyclinE_Cdk2 (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt pRB_E2F in component pRB_E2F (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt E2F in component E2F (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt pRB in component pRB (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt CycD_Cdk4 in component CycD_Cdk4 (dimensionless)');
    LEGEND_RATES(:,7) = strpad('d/dt p27 in component p27 (dimensionless)');
    LEGEND_RATES(:,8) = strpad('d/dt CycE_Cdk2_p27 in component CycE_Cdk2_p27 (dimensionless)');
    LEGEND_RATES(:,9) = strpad('d/dt CycD_Cdk4_p27 in component CycD_Cdk4_p27 (dimensionless)');
    LEGEND_RATES(:,10) = strpad('d/dt p16 in component p16 (dimensionless)');
    LEGEND_RATES(:,11) = strpad('d/dt pRB_P in component pRB_P (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) = 0.1;
    CONSTANTS(:,2) = 0.5;
    CONSTANTS(:,3) = 0.5;
    CONSTANTS(:,4) = 0.001;
    CONSTANTS(:,5) = 0.1;
    CONSTANTS(:,6) = 1;
    CONSTANTS(:,7) = 1.42;
    CONSTANTS(:,8) = 0;
    CONSTANTS(:,9) = 0.016;
    CONSTANTS(:,10) = 0.02;
    CONSTANTS(:,11) = 5;
    CONSTANTS(:,12) = 2;
    CONSTANTS(:,13) = 2;
    CONSTANTS(:,14) = 0.035;
    CONSTANTS(:,15) = 3.5;
    CONSTANTS(:,16) = 0.0001;
    CONSTANTS(:,17) = 0.05;
    CONSTANTS(:,18) = 0.01;
    CONSTANTS(:,19) = 0.1;
    CONSTANTS(:,20) = 0.001;
    CONSTANTS(:,21) = 0.1;
    CONSTANTS(:,22) = 0.01;
    CONSTANTS(:,23) = 0.02;
    CONSTANTS(:,24) = 0.01;
    CONSTANTS(:,25) = 0.1;
    CONSTANTS(:,26) = 0.01;
    CONSTANTS(:,27) = 0.001;
    STATES(:,1) = 0;
    STATES(:,2) = 0.01;
    STATES(:,3) = 1.95;
    STATES(:,4) = 0;
    CONSTANTS(:,28) = 0.000001;
    STATES(:,5) = 0.05;
    CONSTANTS(:,29) = 0.01;
    STATES(:,6) = 0;
    CONSTANTS(:,30) = 0.018;
    STATES(:,7) = 5;
    CONSTANTS(:,31) = 0.0001;
    STATES(:,8) = 1;
    STATES(:,9) = 0;
    STATES(:,10) = 5;
    CONSTANTS(:,32) = 0.2;
    STATES(:,11) = 0.01;
    CONSTANTS(:,33) = CONSTANTS(:,4);
    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(:,2) =  CONSTANTS(:,2).*STATES(:,6).*STATES(:,3)+ CONSTANTS(:,3).*STATES(:,9).*STATES(:,3)+ CONSTANTS(:,1).*STATES(:,1).*STATES(:,3);
    RATES(:,3) = CONSTANTS(:,33) - ALGEBRAIC(:,2);
    ALGEBRAIC(:,7) =  CONSTANTS(:,16).*STATES(:,4);
    ALGEBRAIC(:,4) =  CONSTANTS(:,9).*STATES(:,4);
    RATES(:,4) = (ALGEBRAIC(:,2)+CONSTANTS(:,28)+ALGEBRAIC(:,7)) - (CONSTANTS(:,33)+ALGEBRAIC(:,4));
    ALGEBRAIC(:,1) =  CONSTANTS(:,5).*STATES(:,1).*STATES(:,2);
    ALGEBRAIC(:,3) =  CONSTANTS(:,6).*STATES(:,1);
    ALGEBRAIC(:,6) =  CONSTANTS(:,7).*STATES(:,4)+CONSTANTS(:,8);
    ALGEBRAIC(:,10) =  CONSTANTS(:,10).*STATES(:,2);
    RATES(:,2) = (ALGEBRAIC(:,6)+ALGEBRAIC(:,3)) - (ALGEBRAIC(:,1)+ALGEBRAIC(:,10));
    ALGEBRAIC(:,5) = CONSTANTS(:,24)./( (1.00000+ CONSTANTS(:,25).*STATES(:,10)).*1.00000);
    ALGEBRAIC(:,11) =  CONSTANTS(:,27).*STATES(:,11);
    ALGEBRAIC(:,8) =  CONSTANTS(:,26).*STATES(:,5);
    RATES(:,5) = (CONSTANTS(:,29)+ALGEBRAIC(:,5)+ALGEBRAIC(:,11)) - (CONSTANTS(:,33)+ALGEBRAIC(:,8));
    RATES(:,11) = ALGEBRAIC(:,2) - ALGEBRAIC(:,11);
    ALGEBRAIC(:,14) =  CONSTANTS(:,14).*STATES(:,8);
    ALGEBRAIC(:,13) =  CONSTANTS(:,13).*STATES(:,7).*STATES(:,1);
    RATES(:,8) = ALGEBRAIC(:,13) - ALGEBRAIC(:,14);
    ALGEBRAIC(:,15) =  CONSTANTS(:,19).*power(STATES(:,1), 2.00000);
    RATES(:,1) = (ALGEBRAIC(:,1)+ALGEBRAIC(:,14)) - (ALGEBRAIC(:,3)+ALGEBRAIC(:,13)+ALGEBRAIC(:,15));
    ALGEBRAIC(:,19) =  CONSTANTS(:,18).*STATES(:,9);
    ALGEBRAIC(:,9) =  CONSTANTS(:,11).*STATES(:,6);
    ALGEBRAIC(:,17) =  CONSTANTS(:,17).*STATES(:,7).*STATES(:,6);
    ALGEBRAIC(:,16) =  CONSTANTS(:,15).*STATES(:,10).*STATES(:,6);
    RATES(:,6) = (CONSTANTS(:,30)+ALGEBRAIC(:,19)) - (ALGEBRAIC(:,9)+ALGEBRAIC(:,17)+ALGEBRAIC(:,16));
    RATES(:,9) = ALGEBRAIC(:,17) - ALGEBRAIC(:,19);
    ALGEBRAIC(:,20) = CONSTANTS(:,22)./( (1.00000+ CONSTANTS(:,23).*STATES(:,5)).*1.00000);
    ALGEBRAIC(:,18) =  CONSTANTS(:,21).*STATES(:,10);
    RATES(:,10) = (CONSTANTS(:,32)+ALGEBRAIC(:,20)) - (ALGEBRAIC(:,16)+ALGEBRAIC(:,18));
    ALGEBRAIC(:,12) =  CONSTANTS(:,12).*STATES(:,7).*STATES(:,1);
    ALGEBRAIC(:,21) =  CONSTANTS(:,20).*STATES(:,7);
    RATES(:,7) = (CONSTANTS(:,31)+ALGEBRAIC(:,14)+ALGEBRAIC(:,19)) - (ALGEBRAIC(:,12)+ALGEBRAIC(:,13)+ALGEBRAIC(:,17)+ALGEBRAIC(:,21));
   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(:,2) =  CONSTANTS(:,2).*STATES(:,6).*STATES(:,3)+ CONSTANTS(:,3).*STATES(:,9).*STATES(:,3)+ CONSTANTS(:,1).*STATES(:,1).*STATES(:,3);
    ALGEBRAIC(:,7) =  CONSTANTS(:,16).*STATES(:,4);
    ALGEBRAIC(:,4) =  CONSTANTS(:,9).*STATES(:,4);
    ALGEBRAIC(:,1) =  CONSTANTS(:,5).*STATES(:,1).*STATES(:,2);
    ALGEBRAIC(:,3) =  CONSTANTS(:,6).*STATES(:,1);
    ALGEBRAIC(:,6) =  CONSTANTS(:,7).*STATES(:,4)+CONSTANTS(:,8);
    ALGEBRAIC(:,10) =  CONSTANTS(:,10).*STATES(:,2);
    ALGEBRAIC(:,5) = CONSTANTS(:,24)./( (1.00000+ CONSTANTS(:,25).*STATES(:,10)).*1.00000);
    ALGEBRAIC(:,11) =  CONSTANTS(:,27).*STATES(:,11);
    ALGEBRAIC(:,8) =  CONSTANTS(:,26).*STATES(:,5);
    ALGEBRAIC(:,14) =  CONSTANTS(:,14).*STATES(:,8);
    ALGEBRAIC(:,13) =  CONSTANTS(:,13).*STATES(:,7).*STATES(:,1);
    ALGEBRAIC(:,15) =  CONSTANTS(:,19).*power(STATES(:,1), 2.00000);
    ALGEBRAIC(:,19) =  CONSTANTS(:,18).*STATES(:,9);
    ALGEBRAIC(:,9) =  CONSTANTS(:,11).*STATES(:,6);
    ALGEBRAIC(:,17) =  CONSTANTS(:,17).*STATES(:,7).*STATES(:,6);
    ALGEBRAIC(:,16) =  CONSTANTS(:,15).*STATES(:,10).*STATES(:,6);
    ALGEBRAIC(:,20) = CONSTANTS(:,22)./( (1.00000+ CONSTANTS(:,23).*STATES(:,5)).*1.00000);
    ALGEBRAIC(:,18) =  CONSTANTS(:,21).*STATES(:,10);
    ALGEBRAIC(:,12) =  CONSTANTS(:,12).*STATES(:,7).*STATES(:,1);
    ALGEBRAIC(:,21) =  CONSTANTS(:,20).*STATES(:,7);
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