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 =0;
end
% There are a total of 10 entries in each of the rate and state variable arrays.
% There are a total of 28 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('T in component uninfected_CD4 (cells_per_mm3)');
    LEGEND_CONSTANTS(:,1) = strpad('s in component uninfected_CD4 (cells_per_mm3_day)');
    LEGEND_CONSTANTS(:,2) = strpad('lambda in component uninfected_CD4 (per_day)');
    LEGEND_STATES(:,2) = strpad('T1_star in component productively_infected_CD4 (cells_per_mm3)');
    LEGEND_STATES(:,3) = strpad('T2_star in component productively_infected_CD4 (cells_per_mm3)');
    LEGEND_CONSTANTS(:,3) = strpad('Tmax in component uninfected_CD4 (cells_per_mm3)');
    LEGEND_CONSTANTS(:,4) = strpad('mu in component uninfected_CD4 (per_day)');
    LEGEND_CONSTANTS(:,5) = strpad('k1 in component productively_infected_CD4 (ml_per_virons_day)');
    LEGEND_CONSTANTS(:,6) = strpad('r1 in component drug_parameters (dimensionless)');
    LEGEND_STATES(:,4) = strpad('V1_I in component virus_strain1 (virons_per_ml)');
    LEGEND_CONSTANTS(:,7) = strpad('k2 in component productively_infected_CD4 (ml_per_virons_day)');
    LEGEND_CONSTANTS(:,8) = strpad('r2 in component drug_parameters (dimensionless)');
    LEGEND_STATES(:,5) = strpad('V2_I in component virus_strain2 (virons_per_ml)');
    LEGEND_STATES(:,6) = strpad('M in component uninfected_long_lived_cells (cells_per_mm3)');
    LEGEND_CONSTANTS(:,9) = strpad('lambda_M in component uninfected_long_lived_cells (cells_per_mm3_day)');
    LEGEND_CONSTANTS(:,10) = strpad('mu_M in component uninfected_long_lived_cells (per_day)');
    LEGEND_CONSTANTS(:,11) = strpad('k1_M in component productively_infected_long_lived_cells (ml_per_virons_day)');
    LEGEND_CONSTANTS(:,12) = strpad('k2_M in component productively_infected_long_lived_cells (ml_per_virons_day)');
    LEGEND_CONSTANTS(:,13) = strpad('m11 in component productively_infected_CD4 (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('m21 in component productively_infected_CD4 (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('delta1 in component productively_infected_CD4 (per_day)');
    LEGEND_CONSTANTS(:,16) = strpad('m22 in component productively_infected_CD4 (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('m12 in component productively_infected_CD4 (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('delta2 in component productively_infected_CD4 (per_day)');
    LEGEND_STATES(:,7) = strpad('M1_star in component productively_infected_long_lived_cells (cells_per_mm3)');
    LEGEND_CONSTANTS(:,19) = strpad('delta1_M in component productively_infected_long_lived_cells (per_day)');
    LEGEND_STATES(:,8) = strpad('M2_star in component productively_infected_long_lived_cells (cells_per_mm3)');
    LEGEND_CONSTANTS(:,20) = strpad('delta2_M in component productively_infected_long_lived_cells (per_day)');
    LEGEND_CONSTANTS(:,21) = strpad('p1 in component drug_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,22) = strpad('N1 in component virus_strain1 (virons_per_cell)');
    LEGEND_CONSTANTS(:,23) = strpad('N1_M in component virus_strain1 (virons_per_cell)');
    LEGEND_STATES(:,9) = strpad('V1 in component virus_strain1 (virons_per_ml)');
    LEGEND_CONSTANTS(:,24) = strpad('c1 in component virus_strain1 (per_day)');
    LEGEND_CONSTANTS(:,25) = strpad('p2 in component drug_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,26) = strpad('N2 in component virus_strain2 (virons_per_cell)');
    LEGEND_CONSTANTS(:,27) = strpad('N2_M in component virus_strain2 (virons_per_cell)');
    LEGEND_CONSTANTS(:,28) = strpad('c2 in component virus_strain2 (per_day)');
    LEGEND_STATES(:,10) = strpad('V2 in component virus_strain2 (virons_per_ml)');
    LEGEND_RATES(:,1) = strpad('d/dt T in component uninfected_CD4 (cells_per_mm3)');
    LEGEND_RATES(:,6) = strpad('d/dt M in component uninfected_long_lived_cells (cells_per_mm3)');
    LEGEND_RATES(:,2) = strpad('d/dt T1_star in component productively_infected_CD4 (cells_per_mm3)');
    LEGEND_RATES(:,3) = strpad('d/dt T2_star in component productively_infected_CD4 (cells_per_mm3)');
    LEGEND_RATES(:,7) = strpad('d/dt M1_star in component productively_infected_long_lived_cells (cells_per_mm3)');
    LEGEND_RATES(:,8) = strpad('d/dt M2_star in component productively_infected_long_lived_cells (cells_per_mm3)');
    LEGEND_RATES(:,4) = strpad('d/dt V1_I in component virus_strain1 (virons_per_ml)');
    LEGEND_RATES(:,9) = strpad('d/dt V1 in component virus_strain1 (virons_per_ml)');
    LEGEND_RATES(:,5) = strpad('d/dt V2_I in component virus_strain2 (virons_per_ml)');
    LEGEND_RATES(:,10) = strpad('d/dt V2 in component virus_strain2 (virons_per_ml)');
    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) = 178.81;
    CONSTANTS(:,1) = 0;
    CONSTANTS(:,2) = 0.01;
    STATES(:,2) = 1.19;
    STATES(:,3) = 0.004;
    CONSTANTS(:,3) = 450;
    CONSTANTS(:,4) = 0.0014;
    CONSTANTS(:,5) = 3.43E-8;
    CONSTANTS(:,6) = 0.9;
    STATES(:,4) = 133500;
    CONSTANTS(:,7) = 3.43E-8;
    CONSTANTS(:,8) = 0.25;
    STATES(:,5) = 450;
    STATES(:,6) = 49.2;
    CONSTANTS(:,9) = 2.0;
    CONSTANTS(:,10) = 0.04;
    CONSTANTS(:,11) = 4.67E-9;
    CONSTANTS(:,12) = 4.67E-9;
    CONSTANTS(:,13) = 1;
    CONSTANTS(:,14) = 3.4E-5;
    CONSTANTS(:,15) = 0.69;
    CONSTANTS(:,16) = 1;
    CONSTANTS(:,17) = 3.4E-5;
    CONSTANTS(:,18) = 0.69;
    STATES(:,7) = 0.49;
    CONSTANTS(:,19) = 0.062;
    STATES(:,8) = 1.7E-3;
    CONSTANTS(:,20) = 0.062;
    CONSTANTS(:,21) = 0.99;
    CONSTANTS(:,22) = 480.1;
    CONSTANTS(:,23) = 534.4;
    STATES(:,9) = 133500;
    CONSTANTS(:,24) = 3.07;
    CONSTANTS(:,25) = 0.25;
    CONSTANTS(:,26) = 475.3;
    CONSTANTS(:,27) = 529.0;
    CONSTANTS(:,28) = 3.07;
    STATES(:,10) = 450;
    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(:,1)+ CONSTANTS(:,2).*STATES(:,1).*(1.00000 - (STATES(:,1)+STATES(:,2)+STATES(:,3))./CONSTANTS(:,3))) -  CONSTANTS(:,4).*STATES(:,1)) -  CONSTANTS(:,5).*(1.00000 - CONSTANTS(:,6)).*STATES(:,1).*STATES(:,4)) -  CONSTANTS(:,7).*(1.00000 - CONSTANTS(:,8)).*STATES(:,1).*STATES(:,5);
    RATES(:,6) = ((CONSTANTS(:,9) -  CONSTANTS(:,10).*STATES(:,6)) -  CONSTANTS(:,11).*(1.00000 - CONSTANTS(:,6)).*STATES(:,6).*STATES(:,4)) -  CONSTANTS(:,12).*(1.00000 - CONSTANTS(:,8)).*STATES(:,6).*STATES(:,5);
    RATES(:,2) = ( CONSTANTS(:,13).*CONSTANTS(:,5).*(1.00000 - CONSTANTS(:,6)).*STATES(:,1).*STATES(:,4)+ CONSTANTS(:,14).*CONSTANTS(:,7).*(1.00000 - CONSTANTS(:,8)).*STATES(:,1).*STATES(:,5)) -  CONSTANTS(:,15).*STATES(:,2);
    RATES(:,3) = ( CONSTANTS(:,16).*CONSTANTS(:,7).*(1.00000 - CONSTANTS(:,8)).*STATES(:,1).*STATES(:,5)+ CONSTANTS(:,17).*CONSTANTS(:,5).*(1.00000 - CONSTANTS(:,6)).*STATES(:,1).*STATES(:,4)) -  CONSTANTS(:,18).*STATES(:,3);
    RATES(:,7) = ( CONSTANTS(:,13).*CONSTANTS(:,11).*(1.00000 - CONSTANTS(:,6)).*STATES(:,6).*STATES(:,4)+ CONSTANTS(:,14).*CONSTANTS(:,12).*(1.00000 - CONSTANTS(:,8)).*STATES(:,6).*STATES(:,5)) -  CONSTANTS(:,19).*STATES(:,7);
    RATES(:,8) = ( CONSTANTS(:,16).*CONSTANTS(:,12).*(1.00000 - CONSTANTS(:,8)).*STATES(:,6).*STATES(:,5)+ CONSTANTS(:,17).*CONSTANTS(:,11).*(1.00000 - CONSTANTS(:,6)).*STATES(:,6).*STATES(:,4)) -  CONSTANTS(:,20).*STATES(:,8);
    RATES(:,4) = ( (1.00000 - CONSTANTS(:,21)).*CONSTANTS(:,22).*CONSTANTS(:,15).*STATES(:,2)+ (1.00000 - CONSTANTS(:,21)).*CONSTANTS(:,23).*CONSTANTS(:,19).*STATES(:,7)) -  CONSTANTS(:,24).*STATES(:,4);
    RATES(:,9) = ( CONSTANTS(:,22).*CONSTANTS(:,15).*STATES(:,2)+ CONSTANTS(:,23).*CONSTANTS(:,19).*STATES(:,7)) -  CONSTANTS(:,24).*STATES(:,9);
    RATES(:,5) = ( (1.00000 - CONSTANTS(:,25)).*CONSTANTS(:,26).*CONSTANTS(:,18).*STATES(:,3)+ (1.00000 - CONSTANTS(:,25)).*CONSTANTS(:,27).*CONSTANTS(:,20).*STATES(:,8)) -  CONSTANTS(:,28).*STATES(:,5);
    RATES(:,10) = ( CONSTANTS(:,26).*CONSTANTS(:,18).*STATES(:,3)+ CONSTANTS(:,27).*CONSTANTS(:,20).*STATES(:,8)) -  CONSTANTS(:,28).*STATES(:,10);
   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
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