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 20 entries in each of the rate and state variable arrays.
% There are a total of 26 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_STATES(:,1) = strpad('S_0 in component S_0 (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('R_0 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('u_s in component kinetic_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,1) = strpad('phi in component phi (first_order_rate_constant)');
    LEGEND_STATES(:,2) = strpad('S_1 in component S_1 (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('R_1 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,1) = strpad('epsilon_s in component kinetic_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,2) = strpad('alpha in component kinetic_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('u in component kinetic_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,3) = strpad('S_2 in component S_2 (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('R_2 in component R (first_order_rate_constant)');
    LEGEND_STATES(:,4) = strpad('S_3 in component S_3 (dimensionless)');
    LEGEND_CONSTANTS(:,20) = strpad('R_3 in component R (first_order_rate_constant)');
    LEGEND_STATES(:,5) = strpad('S_4 in component S_4 (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('R_4 in component R (first_order_rate_constant)');
    LEGEND_STATES(:,6) = strpad('S_5 in component S_5 (dimensionless)');
    LEGEND_CONSTANTS(:,22) = strpad('R_5 in component R (first_order_rate_constant)');
    LEGEND_STATES(:,7) = strpad('S_6 in component S_6 (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('R_6 in component R (first_order_rate_constant)');
    LEGEND_STATES(:,8) = strpad('S_7 in component S_7 (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('R_7 in component R (first_order_rate_constant)');
    LEGEND_STATES(:,9) = strpad('S_8 in component S_8 (dimensionless)');
    LEGEND_CONSTANTS(:,25) = strpad('R_8 in component R (first_order_rate_constant)');
    LEGEND_STATES(:,10) = strpad('M_0 in component M_0 (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('epsilon_m in component kinetic_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,26) = strpad('u_m in component kinetic_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,11) = strpad('M_1 in component M_1 (dimensionless)');
    LEGEND_STATES(:,12) = strpad('M_1_2 in component M_1 (dimensionless)');
    LEGEND_STATES(:,13) = strpad('M_2 in component M_2 (dimensionless)');
    LEGEND_STATES(:,14) = strpad('M_3 in component M_3 (dimensionless)');
    LEGEND_STATES(:,15) = strpad('M_4 in component M_4 (dimensionless)');
    LEGEND_STATES(:,16) = strpad('M_5 in component M_5 (dimensionless)');
    LEGEND_STATES(:,17) = strpad('M_6 in component M_6 (dimensionless)');
    LEGEND_STATES(:,18) = strpad('M_7 in component M_7 (dimensionless)');
    LEGEND_STATES(:,19) = strpad('M_8 in component M_8 (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('r_0 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('r_1 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('r_2 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('r_3 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('r_4 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('r_5 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('r_6 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('r_7 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('r_8 in component R (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('a in component kinetic_parameters (dimensionless)');
    LEGEND_STATES(:,20) = strpad('w in component w (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('total_cells in component total_cells (dimensionless)');
    LEGEND_ALGEBRAIC(:,3) = strpad('stable_total in component total_cells (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('mutant_total in component total_cells (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('beta in component kinetic_parameters (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt S_0 in component S_0 (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt S_1 in component S_1 (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt S_2 in component S_2 (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt S_3 in component S_3 (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt S_4 in component S_4 (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt S_5 in component S_5 (dimensionless)');
    LEGEND_RATES(:,7) = strpad('d/dt S_6 in component S_6 (dimensionless)');
    LEGEND_RATES(:,8) = strpad('d/dt S_7 in component S_7 (dimensionless)');
    LEGEND_RATES(:,9) = strpad('d/dt S_8 in component S_8 (dimensionless)');
    LEGEND_RATES(:,10) = strpad('d/dt M_0 in component M_0 (dimensionless)');
    LEGEND_RATES(:,11) = strpad('d/dt M_1 in component M_1 (dimensionless)');
    LEGEND_RATES(:,12) = strpad('d/dt M_1_2 in component M_1 (dimensionless)');
    LEGEND_RATES(:,13) = strpad('d/dt M_2 in component M_2 (dimensionless)');
    LEGEND_RATES(:,14) = strpad('d/dt M_3 in component M_3 (dimensionless)');
    LEGEND_RATES(:,15) = strpad('d/dt M_4 in component M_4 (dimensionless)');
    LEGEND_RATES(:,16) = strpad('d/dt M_5 in component M_5 (dimensionless)');
    LEGEND_RATES(:,17) = strpad('d/dt M_6 in component M_6 (dimensionless)');
    LEGEND_RATES(:,18) = strpad('d/dt M_7 in component M_7 (dimensionless)');
    LEGEND_RATES(:,19) = strpad('d/dt M_8 in component M_8 (dimensionless)');
    LEGEND_RATES(:,20) = strpad('d/dt w in component w (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 = [];
    STATES(:,1) = 0.5;
    STATES(:,2) = 0;
    CONSTANTS(:,1) = 0.99;
    CONSTANTS(:,2) = 0.6;
    CONSTANTS(:,3) = 0.07;
    STATES(:,3) = 0;
    STATES(:,4) = 0;
    STATES(:,5) = 0;
    STATES(:,6) = 0;
    STATES(:,7) = 0;
    STATES(:,8) = 0;
    STATES(:,9) = 0;
    STATES(:,10) = 0.5;
    CONSTANTS(:,4) = 0.1;
    STATES(:,11) = 0;
    STATES(:,12) = 0;
    STATES(:,13) = 0;
    STATES(:,14) = 0;
    STATES(:,15) = 0;
    STATES(:,16) = 0;
    STATES(:,17) = 0;
    STATES(:,18) = 0;
    STATES(:,19) = 0;
    CONSTANTS(:,5) = 0.5;
    CONSTANTS(:,6) = 0.6;
    CONSTANTS(:,7) = 0.7;
    CONSTANTS(:,8) = 0.8;
    CONSTANTS(:,9) = 0.9;
    CONSTANTS(:,10) = 1;
    CONSTANTS(:,11) = 1.1;
    CONSTANTS(:,12) = 1.2;
    CONSTANTS(:,13) = 1.3;
    CONSTANTS(:,14) = 0.5;
    STATES(:,20) = 0;
    CONSTANTS(:,15) = 0.2;
    CONSTANTS(:,16) = CONSTANTS(:,5);
    CONSTANTS(:,17) =  CONSTANTS(:,3).*(1.00000 - ( CONSTANTS(:,15).*CONSTANTS(:,1))./1.00000);
    CONSTANTS(:,18) =  CONSTANTS(:,6).*(1.00000 - CONSTANTS(:,14));
    CONSTANTS(:,19) =  CONSTANTS(:,7).*(1.00000 - CONSTANTS(:,14));
    CONSTANTS(:,20) =  CONSTANTS(:,8).*(1.00000 - CONSTANTS(:,14));
    CONSTANTS(:,21) =  CONSTANTS(:,9).*(1.00000 - CONSTANTS(:,14));
    CONSTANTS(:,22) =  CONSTANTS(:,10).*(1.00000 - CONSTANTS(:,14));
    CONSTANTS(:,23) =  CONSTANTS(:,11).*(1.00000 - CONSTANTS(:,14));
    CONSTANTS(:,24) =  CONSTANTS(:,12).*(1.00000 - CONSTANTS(:,14));
    CONSTANTS(:,25) =  CONSTANTS(:,13).*(1.00000 - CONSTANTS(:,14));
    CONSTANTS(:,26) =  CONSTANTS(:,3).*(1.00000 - ( CONSTANTS(:,15).*CONSTANTS(:,4))./1.00000);
    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(:,11) =  STATES(:,10).*1.00000;
    ALGEBRAIC(:,1) =  ((1.00000 - CONSTANTS(:,17))./1.00000).*( CONSTANTS(:,16).*STATES(:,1)+ CONSTANTS(:,18).*STATES(:,2)+ CONSTANTS(:,19).*STATES(:,3)+ CONSTANTS(:,20).*STATES(:,4)+ CONSTANTS(:,21).*STATES(:,5))+ ((1.00000 - CONSTANTS(:,26))./1.00000).*( CONSTANTS(:,18).*STATES(:,11)+ CONSTANTS(:,19).*STATES(:,13)+ CONSTANTS(:,20).*STATES(:,14)+ CONSTANTS(:,21).*STATES(:,15));
    RATES(:,1) =  (( CONSTANTS(:,16).*STATES(:,1))./1.00000).*(1.00000 - CONSTANTS(:,17)) -  ALGEBRAIC(:,1).*STATES(:,1);
    RATES(:,2) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,16).*STATES(:,1))./1.00000).*(1.00000 - CONSTANTS(:,1))+ (( CONSTANTS(:,18).*STATES(:,2))./1.00000).*(1.00000 - CONSTANTS(:,17))) -  ALGEBRAIC(:,1).*STATES(:,2);
    RATES(:,3) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,18).*STATES(:,2))./1.00000).*(1.00000 - CONSTANTS(:,1))+ (( CONSTANTS(:,19).*STATES(:,3))./1.00000).*(1.00000 - CONSTANTS(:,17))) -  ALGEBRAIC(:,1).*STATES(:,3);
    RATES(:,4) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,19).*STATES(:,3))./1.00000).*(1.00000 - CONSTANTS(:,1))+ (( CONSTANTS(:,20).*STATES(:,4))./1.00000).*(1.00000 - CONSTANTS(:,17))) -  ALGEBRAIC(:,1).*STATES(:,4);
    RATES(:,5) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,20).*STATES(:,4))./1.00000).*(1.00000 - CONSTANTS(:,1))+ (( CONSTANTS(:,21).*STATES(:,5))./1.00000).*(1.00000 - CONSTANTS(:,17))) -  ALGEBRAIC(:,1).*STATES(:,5);
    RATES(:,6) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,21).*STATES(:,5))./1.00000).*(1.00000 - CONSTANTS(:,1))+ (( CONSTANTS(:,22).*STATES(:,6))./1.00000).*(1.00000 - CONSTANTS(:,17))) -  ALGEBRAIC(:,1).*STATES(:,6);
    RATES(:,7) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,22).*STATES(:,6))./1.00000).*(1.00000 - CONSTANTS(:,1))+ (( CONSTANTS(:,23).*STATES(:,7))./1.00000).*(1.00000 - CONSTANTS(:,17))) -  ALGEBRAIC(:,1).*STATES(:,7);
    RATES(:,8) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,23).*STATES(:,7))./1.00000).*(1.00000 - CONSTANTS(:,1))+ (( CONSTANTS(:,24).*STATES(:,8))./1.00000).*(1.00000 - CONSTANTS(:,17))) -  ALGEBRAIC(:,1).*STATES(:,8);
    RATES(:,9) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,24).*STATES(:,8))./1.00000).*(1.00000 - CONSTANTS(:,1))+ (( CONSTANTS(:,25).*STATES(:,9))./1.00000).*(1.00000 - CONSTANTS(:,17))) -  ALGEBRAIC(:,1).*STATES(:,9);
    RATES(:,10) =  (( CONSTANTS(:,16).*STATES(:,10))./1.00000).*(1.00000 - CONSTANTS(:,26)) -  ALGEBRAIC(:,1).*STATES(:,10);
    RATES(:,12) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,16).*STATES(:,10))./1.00000).*(1.00000 - CONSTANTS(:,4))+ (( CONSTANTS(:,18).*STATES(:,11))./1.00000).*(1.00000 - CONSTANTS(:,26))) -  ALGEBRAIC(:,1).*STATES(:,11);
    RATES(:,13) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,18).*STATES(:,11))./1.00000).*(1.00000 - CONSTANTS(:,4))+ (( CONSTANTS(:,19).*STATES(:,13))./1.00000).*(1.00000 - CONSTANTS(:,26))) -  ALGEBRAIC(:,1).*STATES(:,13);
    RATES(:,14) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,19).*STATES(:,13))./1.00000).*(1.00000 - CONSTANTS(:,4))+ (( CONSTANTS(:,20).*STATES(:,14))./1.00000).*(1.00000 - CONSTANTS(:,26))) -  ALGEBRAIC(:,1).*STATES(:,14);
    RATES(:,15) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,20).*STATES(:,15))./1.00000).*(1.00000 - CONSTANTS(:,4))+ (( CONSTANTS(:,21).*STATES(:,15))./1.00000).*(1.00000 - CONSTANTS(:,26))) -  ALGEBRAIC(:,1).*STATES(:,15);
    RATES(:,16) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,21).*STATES(:,15))./1.00000).*(1.00000 - CONSTANTS(:,4))+ (( CONSTANTS(:,22).*STATES(:,16))./1.00000).*(1.00000 - CONSTANTS(:,26))) -  ALGEBRAIC(:,1).*STATES(:,16);
    RATES(:,17) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,22).*STATES(:,16))./1.00000).*(1.00000 - CONSTANTS(:,4))+ (( CONSTANTS(:,23).*STATES(:,17))./1.00000).*(1.00000 - CONSTANTS(:,26))) -  ALGEBRAIC(:,1).*STATES(:,17);
    RATES(:,18) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,23).*STATES(:,17))./1.00000).*(1.00000 - CONSTANTS(:,4))+ (( CONSTANTS(:,24).*STATES(:,18))./1.00000).*(1.00000 - CONSTANTS(:,26))) -  ALGEBRAIC(:,1).*STATES(:,18);
    RATES(:,19) = ( (( CONSTANTS(:,2).*CONSTANTS(:,3).*CONSTANTS(:,24).*STATES(:,18))./1.00000).*(1.00000 - CONSTANTS(:,4))+ (( CONSTANTS(:,25).*STATES(:,19))./1.00000).*((1.00000 - CONSTANTS(:,26))+ (( CONSTANTS(:,2).*CONSTANTS(:,3))./1.00000).*(1.00000 - CONSTANTS(:,4)))) -  ALGEBRAIC(:,1).*STATES(:,9);
    RATES(:,20) =  (( (1.00000 - CONSTANTS(:,2)).*CONSTANTS(:,3))./1.00000).*( (1.00000 - CONSTANTS(:,1)).*( CONSTANTS(:,16).*STATES(:,1)+ CONSTANTS(:,18).*STATES(:,2)+ CONSTANTS(:,19).*STATES(:,3)+ CONSTANTS(:,20).*STATES(:,4)+ CONSTANTS(:,21).*STATES(:,5)+ CONSTANTS(:,22).*STATES(:,6)+ CONSTANTS(:,23).*STATES(:,7)+ CONSTANTS(:,24).*STATES(:,8)+ CONSTANTS(:,25).*STATES(:,9))+ (1.00000 - CONSTANTS(:,4)).*( CONSTANTS(:,16).*STATES(:,10)+ CONSTANTS(:,18).*STATES(:,11)+ CONSTANTS(:,19).*STATES(:,13)+ CONSTANTS(:,20).*STATES(:,14)+ CONSTANTS(:,21).*STATES(:,15)+ CONSTANTS(:,22).*STATES(:,16)+ CONSTANTS(:,23).*STATES(:,17)+ CONSTANTS(:,24).*STATES(:,18)+ CONSTANTS(:,25).*STATES(:,19))) -  ALGEBRAIC(:,1).*STATES(:,20);
   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) =  ((1.00000 - CONSTANTS(:,17))./1.00000).*( CONSTANTS(:,16).*STATES(:,1)+ CONSTANTS(:,18).*STATES(:,2)+ CONSTANTS(:,19).*STATES(:,3)+ CONSTANTS(:,20).*STATES(:,4)+ CONSTANTS(:,21).*STATES(:,5))+ ((1.00000 - CONSTANTS(:,26))./1.00000).*( CONSTANTS(:,18).*STATES(:,11)+ CONSTANTS(:,19).*STATES(:,13)+ CONSTANTS(:,20).*STATES(:,14)+ CONSTANTS(:,21).*STATES(:,15));
    ALGEBRAIC(:,2) = STATES(:,1)+STATES(:,2)+STATES(:,3)+STATES(:,4)+STATES(:,5)+STATES(:,6)+STATES(:,7)+STATES(:,8)+STATES(:,9)+STATES(:,10)+STATES(:,13)+STATES(:,14)+STATES(:,15)+STATES(:,16)+STATES(:,17)+STATES(:,18)+STATES(:,19);
    ALGEBRAIC(:,3) = STATES(:,1)+STATES(:,2)+STATES(:,3)+STATES(:,4)+STATES(:,5)+STATES(:,6)+STATES(:,7)+STATES(:,8)+STATES(:,9);
    ALGEBRAIC(:,4) = STATES(:,10)+STATES(:,13)+STATES(:,14)+STATES(:,15)+STATES(:,16)+STATES(:,17)+STATES(:,18)+STATES(:,19);
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