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 =5;
end
% There are a total of 7 entries in each of the rate and state variable arrays.
% There are a total of 34 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 (day)');
    LEGEND_STATES(:,1) = strpad('phi_I in component phi_I (cells_per_mm3)');
    LEGEND_ALGEBRAIC(:,4) = strpad('alpha in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,1) = strpad('k1 in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('k2 in component model_parameters (per_day)');
    LEGEND_CONSTANTS(:,3) = strpad('k3 in component model_parameters (mm3_per_cells)');
    LEGEND_CONSTANTS(:,4) = strpad('k5 in component model_parameters (mm3_per_cells)');
    LEGEND_CONSTANTS(:,5) = strpad('k6 in component model_parameters (mm3_per_microg)');
    LEGEND_CONSTANTS(:,6) = strpad('d1 in component model_parameters (per_day)');
    LEGEND_STATES(:,2) = strpad('phi_R in component phi_R (cells_per_mm3)');
    LEGEND_ALGEBRAIC(:,1) = strpad('K_T in component K_T (cells_per_mm3_per_day)');
    LEGEND_STATES(:,3) = strpad('F in component F (cells_per_mm3)');
    LEGEND_STATES(:,4) = strpad('C in component C (microg_per_mm3)');
    LEGEND_STATES(:,5) = strpad('T in component T (pg_per_mm3)');
    LEGEND_CONSTANTS(:,7) = strpad('k4 in component model_parameters (pg_per_cells_per_day)');
    LEGEND_CONSTANTS(:,8) = strpad('k7 in component model_parameters (pg_per_cells_per_day)');
    LEGEND_CONSTANTS(:,9) = strpad('d2 in component model_parameters (per_day)');
    LEGEND_STATES(:,6) = strpad('P in component P (pg_per_mm3)');
    LEGEND_CONSTANTS(:,10) = strpad('k8 in component model_parameters (pg_per_cells_per_day)');
    LEGEND_CONSTANTS(:,11) = strpad('k9 in component model_parameters (pg_per_cells_per_day)');
    LEGEND_CONSTANTS(:,12) = strpad('d3 in component model_parameters (per_day)');
    LEGEND_CONSTANTS(:,13) = strpad('k10 in component model_parameters (per_day)');
    LEGEND_CONSTANTS(:,14) = strpad('d4 in component model_parameters (per_day)');
    LEGEND_ALGEBRAIC(:,2) = strpad('M_P in component M_P (cells_per_mm3_per_day)');
    LEGEND_CONSTANTS(:,15) = strpad('k11 in component model_parameters (microg_per_cells_per_day)');
    LEGEND_ALGEBRAIC(:,3) = strpad('f_T in component f_T (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('g_C in component g_C (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('d5 in component model_parameters (mm3_per_cells_per_day)');
    LEGEND_STATES(:,7) = strpad('H in component H (microg_per_mm3)');
    LEGEND_CONSTANTS(:,17) = strpad('k12 in component model_parameters (microg_per_cells_per_day)');
    LEGEND_CONSTANTS(:,18) = strpad('d6 in component model_parameters (per_day)');
    LEGEND_CONSTANTS(:,19) = strpad('tau1 in component K_T (mm6_cells_per_pg3_day)');
    LEGEND_CONSTANTS(:,20) = strpad('tau2 in component K_T (mm3_cells_per_pg2_day)');
    LEGEND_CONSTANTS(:,21) = strpad('tau3 in component K_T (cells_per_pg_per_day)');
    LEGEND_CONSTANTS(:,22) = strpad('tau4 in component K_T (cells_per_mm3_per_day)');
    LEGEND_CONSTANTS(:,23) = strpad('tau1 in component M_P (mm6_cells_per_pg3_day)');
    LEGEND_CONSTANTS(:,24) = strpad('tau2 in component M_P (mm3_cells_per_pg2_day)');
    LEGEND_CONSTANTS(:,25) = strpad('tau3 in component M_P (cells_per_pg_per_day)');
    LEGEND_CONSTANTS(:,26) = strpad('tau4 in component M_P (cells_per_mm3_per_day)');
    LEGEND_CONSTANTS(:,27) = strpad('tau1 in component f_T (mm9_per_pg3)');
    LEGEND_CONSTANTS(:,28) = strpad('tau2 in component f_T (mm6_per_pg2)');
    LEGEND_CONSTANTS(:,29) = strpad('tau3 in component f_T (mm3_per_pg)');
    LEGEND_CONSTANTS(:,30) = strpad('tau4 in component f_T (dimensionless)');
    LEGEND_CONSTANTS(:,31) = strpad('tau1 in component g_C (mm9_per_microg3)');
    LEGEND_CONSTANTS(:,32) = strpad('tau2 in component g_C (mm6_per_microg2)');
    LEGEND_CONSTANTS(:,33) = strpad('tau3 in component g_C (mm3_per_microg)');
    LEGEND_CONSTANTS(:,34) = strpad('tau4 in component g_C (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt phi_I in component phi_I (cells_per_mm3)');
    LEGEND_RATES(:,2) = strpad('d/dt phi_R in component phi_R (cells_per_mm3)');
    LEGEND_RATES(:,5) = strpad('d/dt T in component T (pg_per_mm3)');
    LEGEND_RATES(:,6) = strpad('d/dt P in component P (pg_per_mm3)');
    LEGEND_RATES(:,3) = strpad('d/dt F in component F (cells_per_mm3)');
    LEGEND_RATES(:,4) = strpad('d/dt C in component C (microg_per_mm3)');
    LEGEND_RATES(:,7) = strpad('d/dt H in component H (microg_per_mm3)');
    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) = 200.0;
    CONSTANTS(:,1) = 0.05;
    CONSTANTS(:,2) = 0.693;
    CONSTANTS(:,3) = 0.002;
    CONSTANTS(:,4) = 0.0025;
    CONSTANTS(:,5) = 0.0004;
    CONSTANTS(:,6) = 0.2;
    STATES(:,2) = 200.0;
    STATES(:,3) = 50.0;
    STATES(:,4) = 10.0;
    STATES(:,5) = 6.0;
    CONSTANTS(:,7) = 0.07;
    CONSTANTS(:,8) = 0.004;
    CONSTANTS(:,9) = 9.1;
    STATES(:,6) = 2.0;
    CONSTANTS(:,10) = 0.015;
    CONSTANTS(:,11) = 0.0015;
    CONSTANTS(:,12) = 4.0;
    CONSTANTS(:,13) = 0.924;
    CONSTANTS(:,14) = 1.0;
    CONSTANTS(:,15) = 20.0;
    CONSTANTS(:,16) = 1.5E-5;
    STATES(:,7) = 0.01;
    CONSTANTS(:,17) = 0.01;
    CONSTANTS(:,18) = 0.7;
    CONSTANTS(:,19) = -2.47;
    CONSTANTS(:,20) = 21.94;
    CONSTANTS(:,21) = 6.41;
    CONSTANTS(:,22) = 1.75;
    CONSTANTS(:,23) = 15.333;
    CONSTANTS(:,24) = -167.21;
    CONSTANTS(:,25) = 452.38;
    CONSTANTS(:,26) = 2.6593;
    CONSTANTS(:,27) = 0.0092;
    CONSTANTS(:,28) = -0.1552;
    CONSTANTS(:,29) = 0.6279;
    CONSTANTS(:,30) = 0.2527;
    CONSTANTS(:,31) = -4.33E-10;
    CONSTANTS(:,32) = 0.0000009;
    CONSTANTS(:,33) = -0.00055;
    CONSTANTS(:,34) = 0.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
    RATES(:,5) = ( CONSTANTS(:,7).*STATES(:,1)+ CONSTANTS(:,8).*STATES(:,3)) -  CONSTANTS(:,9).*STATES(:,5);
    RATES(:,6) = ( CONSTANTS(:,10).*(STATES(:,1)+STATES(:,2))+ CONSTANTS(:,11).*STATES(:,3)) -  CONSTANTS(:,12).*STATES(:,6);
    RATES(:,7) =  CONSTANTS(:,17).*STATES(:,3) -  CONSTANTS(:,18).*STATES(:,7);
    ALGEBRAIC(:,2) =  CONSTANTS(:,23).*power(STATES(:,6), 3.00000)+ CONSTANTS(:,24).*power(STATES(:,6), 2.00000)+ CONSTANTS(:,25).*STATES(:,6)+CONSTANTS(:,26);
    RATES(:,3) = (ALGEBRAIC(:,2)+ CONSTANTS(:,13).*STATES(:,3).*(1.00000 - ( CONSTANTS(:,3).*(STATES(:,1)+STATES(:,2))+ CONSTANTS(:,4).*STATES(:,3)+ CONSTANTS(:,5).*STATES(:,4)))) -  CONSTANTS(:,14).*STATES(:,3);
    ALGEBRAIC(:,4) =  - ( 0.197000.*arbitrary_log(STATES(:,7), 10))+0.440700;
    ALGEBRAIC(:,1) =  CONSTANTS(:,19).*power(STATES(:,5), 3.00000)+ CONSTANTS(:,20).*power(STATES(:,5), 2.00000)+ CONSTANTS(:,21).*STATES(:,5)+CONSTANTS(:,22);
    RATES(:,1) = ( ALGEBRAIC(:,4).*ALGEBRAIC(:,1)+ CONSTANTS(:,1).*CONSTANTS(:,2).*STATES(:,1).*(1.00000 - ( CONSTANTS(:,3).*(STATES(:,1)+STATES(:,2))+ CONSTANTS(:,4).*STATES(:,3)+ CONSTANTS(:,5).*STATES(:,4)))) -  CONSTANTS(:,6).*STATES(:,1);
    RATES(:,2) = ( (1.00000 - ALGEBRAIC(:,4)).*ALGEBRAIC(:,1)+ CONSTANTS(:,1).*CONSTANTS(:,2).*STATES(:,2).*(1.00000 - ( CONSTANTS(:,3).*(STATES(:,1)+STATES(:,2))+ CONSTANTS(:,4).*STATES(:,3)+ CONSTANTS(:,5).*STATES(:,4)))) -  CONSTANTS(:,6).*STATES(:,2);
    ALGEBRAIC(:,3) =  CONSTANTS(:,27).*power(STATES(:,5), 3.00000)+ CONSTANTS(:,28).*power(STATES(:,5), 2.00000)+ CONSTANTS(:,29).*STATES(:,5)+CONSTANTS(:,30);
    ALGEBRAIC(:,5) =  CONSTANTS(:,31).*power(STATES(:,4), 3.00000)+ CONSTANTS(:,32).*power(STATES(:,4), 2.00000)+ CONSTANTS(:,33).*STATES(:,4)+CONSTANTS(:,34);
    RATES(:,4) =  CONSTANTS(:,15).*STATES(:,3).*ALGEBRAIC(:,3).*ALGEBRAIC(:,5) -  CONSTANTS(:,16).*STATES(:,3).*STATES(:,4);
   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(:,23).*power(STATES(:,6), 3.00000)+ CONSTANTS(:,24).*power(STATES(:,6), 2.00000)+ CONSTANTS(:,25).*STATES(:,6)+CONSTANTS(:,26);
    ALGEBRAIC(:,4) =  - ( 0.197000.*arbitrary_log(STATES(:,7), 10))+0.440700;
    ALGEBRAIC(:,1) =  CONSTANTS(:,19).*power(STATES(:,5), 3.00000)+ CONSTANTS(:,20).*power(STATES(:,5), 2.00000)+ CONSTANTS(:,21).*STATES(:,5)+CONSTANTS(:,22);
    ALGEBRAIC(:,3) =  CONSTANTS(:,27).*power(STATES(:,5), 3.00000)+ CONSTANTS(:,28).*power(STATES(:,5), 2.00000)+ CONSTANTS(:,29).*STATES(:,5)+CONSTANTS(:,30);
    ALGEBRAIC(:,5) =  CONSTANTS(:,31).*power(STATES(:,4), 3.00000)+ CONSTANTS(:,32).*power(STATES(:,4), 2.00000)+ CONSTANTS(:,33).*STATES(:,4)+CONSTANTS(:,34);
end

% Compute a logarithm to any base" +
function x = arbitrary_log(a, base)
    x = log(a) ./ log(base);
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