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 =1;
end
% There are a total of 10 entries in each of the rate and state variable arrays.
% There are a total of 38 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('MP in component MP (nanomolar)');
    LEGEND_CONSTANTS(:,1) = strpad('vsP in component MP (flux)');
    LEGEND_CONSTANTS(:,2) = strpad('vmP in component MP (flux)');
    LEGEND_CONSTANTS(:,3) = strpad('KmP in component MP (nanomolar)');
    LEGEND_CONSTANTS(:,4) = strpad('KIP in component MP (nanomolar)');
    LEGEND_CONSTANTS(:,5) = strpad('kd in component parameters (first_order_rate_constant)');
    LEGEND_STATES(:,2) = strpad('CN in component CN (nanomolar)');
    LEGEND_CONSTANTS(:,6) = strpad('n in component parameters (dimensionless)');
    LEGEND_STATES(:,3) = strpad('P0 in component P0 (nanomolar)');
    LEGEND_CONSTANTS(:,7) = strpad('ksP in component P0 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('V1P in component parameters (flux)');
    LEGEND_CONSTANTS(:,9) = strpad('V2P in component parameters (flux)');
    LEGEND_CONSTANTS(:,10) = strpad('K1P in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,11) = strpad('K2P in component parameters (nanomolar)');
    LEGEND_STATES(:,4) = strpad('P1 in component P1 (nanomolar)');
    LEGEND_CONSTANTS(:,12) = strpad('V3P in component parameters (flux)');
    LEGEND_CONSTANTS(:,13) = strpad('V4P in component parameters (flux)');
    LEGEND_CONSTANTS(:,14) = strpad('K3P in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,15) = strpad('K4P in component parameters (nanomolar)');
    LEGEND_STATES(:,5) = strpad('P2 in component P2 (nanomolar)');
    LEGEND_CONSTANTS(:,16) = strpad('vdP in component P2 (flux)');
    LEGEND_CONSTANTS(:,17) = strpad('KdP in component P2 (nanomolar)');
    LEGEND_ALGEBRAIC(:,1) = strpad('Pt in component P2 (nanomolar)');
    LEGEND_CONSTANTS(:,18) = strpad('k3 in component parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,19) = strpad('k4 in component parameters (first_order_rate_constant)');
    LEGEND_STATES(:,6) = strpad('T2 in component T2 (nanomolar)');
    LEGEND_STATES(:,7) = strpad('C in component C (nanomolar)');
    LEGEND_STATES(:,8) = strpad('MT in component MT (nanomolar)');
    LEGEND_CONSTANTS(:,20) = strpad('vsT in component MT (flux)');
    LEGEND_CONSTANTS(:,21) = strpad('vmT in component MT (flux)');
    LEGEND_CONSTANTS(:,22) = strpad('KmT in component MT (nanomolar)');
    LEGEND_CONSTANTS(:,23) = strpad('KIT in component MT (nanomolar)');
    LEGEND_STATES(:,9) = strpad('T0 in component T0 (nanomolar)');
    LEGEND_CONSTANTS(:,24) = strpad('ksT in component T0 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,25) = strpad('V1T in component parameters (flux)');
    LEGEND_CONSTANTS(:,26) = strpad('V2T in component parameters (flux)');
    LEGEND_CONSTANTS(:,27) = strpad('K1T in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,28) = strpad('K2T in component parameters (nanomolar)');
    LEGEND_STATES(:,10) = strpad('T1 in component T1 (nanomolar)');
    LEGEND_CONSTANTS(:,29) = strpad('V3T in component parameters (flux)');
    LEGEND_CONSTANTS(:,30) = strpad('V4T in component parameters (flux)');
    LEGEND_CONSTANTS(:,31) = strpad('K3T in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,32) = strpad('K4T in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,33) = strpad('vdT in component T2 (flux)');
    LEGEND_CONSTANTS(:,34) = strpad('KdT in component T2 (nanomolar)');
    LEGEND_CONSTANTS(:,35) = strpad('kdC in component C (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,36) = strpad('k1 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,37) = strpad('k2 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,38) = strpad('kdN in component CN (first_order_rate_constant)');
    LEGEND_RATES(:,1) = strpad('d/dt MP in component MP (nanomolar)');
    LEGEND_RATES(:,3) = strpad('d/dt P0 in component P0 (nanomolar)');
    LEGEND_RATES(:,4) = strpad('d/dt P1 in component P1 (nanomolar)');
    LEGEND_RATES(:,5) = strpad('d/dt P2 in component P2 (nanomolar)');
    LEGEND_RATES(:,8) = strpad('d/dt MT in component MT (nanomolar)');
    LEGEND_RATES(:,9) = strpad('d/dt T0 in component T0 (nanomolar)');
    LEGEND_RATES(:,10) = strpad('d/dt T1 in component T1 (nanomolar)');
    LEGEND_RATES(:,6) = strpad('d/dt T2 in component T2 (nanomolar)');
    LEGEND_RATES(:,7) = strpad('d/dt C in component C (nanomolar)');
    LEGEND_RATES(:,2) = strpad('d/dt CN in component CN (nanomolar)');
    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.1;
    CONSTANTS(:,1) = 1;
    CONSTANTS(:,2) = 0.7;
    CONSTANTS(:,3) = 0.2;
    CONSTANTS(:,4) = 1.0;
    CONSTANTS(:,5) = 0.01;
    STATES(:,2) = 1.25;
    CONSTANTS(:,6) = 4.0;
    STATES(:,3) = 0.1;
    CONSTANTS(:,7) = 0.9;
    CONSTANTS(:,8) = 8.0;
    CONSTANTS(:,9) = 1.0;
    CONSTANTS(:,10) = 2.0;
    CONSTANTS(:,11) = 2.0;
    STATES(:,4) = 0.1;
    CONSTANTS(:,12) = 8.0;
    CONSTANTS(:,13) = 1.0;
    CONSTANTS(:,14) = 2.0;
    CONSTANTS(:,15) = 1;
    STATES(:,5) = 0.1;
    CONSTANTS(:,16) = 2;
    CONSTANTS(:,17) = 0.2;
    CONSTANTS(:,18) = 1.2;
    CONSTANTS(:,19) = 0.6;
    STATES(:,6) = 0.1;
    STATES(:,7) = 0.1;
    STATES(:,8) = 1.6;
    CONSTANTS(:,20) = 1.0;
    CONSTANTS(:,21) = 0.7;
    CONSTANTS(:,22) = 0.2;
    CONSTANTS(:,23) = 1.0;
    STATES(:,9) = 0.1;
    CONSTANTS(:,24) = 0.9;
    CONSTANTS(:,25) = 8.0;
    CONSTANTS(:,26) = 1.0;
    CONSTANTS(:,27) = 2.0;
    CONSTANTS(:,28) = 2.0;
    STATES(:,10) = 0.1;
    CONSTANTS(:,29) = 8.0;
    CONSTANTS(:,30) = 1.0;
    CONSTANTS(:,31) = 2.0;
    CONSTANTS(:,32) = 1;
    CONSTANTS(:,33) = 2;
    CONSTANTS(:,34) = 0.2;
    CONSTANTS(:,35) = 0.01;
    CONSTANTS(:,36) = 0.6;
    CONSTANTS(:,37) = 0.2;
    CONSTANTS(:,38) = 0.01;
    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).*(power(CONSTANTS(:,4), CONSTANTS(:,6))./(power(CONSTANTS(:,4), CONSTANTS(:,6))+power(STATES(:,2), CONSTANTS(:,6)))) - ( CONSTANTS(:,2).*(STATES(:,1)./(CONSTANTS(:,3)+STATES(:,1)))+ CONSTANTS(:,5).*STATES(:,1));
    RATES(:,3) = ( CONSTANTS(:,7).*STATES(:,1)+ CONSTANTS(:,9).*(STATES(:,4)./(CONSTANTS(:,11)+STATES(:,4)))) - ( CONSTANTS(:,8).*(STATES(:,3)./(CONSTANTS(:,10)+STATES(:,3)))+ CONSTANTS(:,5).*STATES(:,3));
    RATES(:,4) = ( CONSTANTS(:,8).*(STATES(:,3)./(CONSTANTS(:,10)+STATES(:,3)))+ CONSTANTS(:,13).*(STATES(:,5)./(CONSTANTS(:,15)+STATES(:,5)))) - ( CONSTANTS(:,9).*(STATES(:,4)./(CONSTANTS(:,11)+STATES(:,4)))+ CONSTANTS(:,12).*(STATES(:,4)./(CONSTANTS(:,14)+STATES(:,4)))+ CONSTANTS(:,5).*STATES(:,4));
    RATES(:,5) = ( CONSTANTS(:,12).*(STATES(:,4)./(CONSTANTS(:,14)+STATES(:,4)))+ CONSTANTS(:,19).*STATES(:,7)) - ( CONSTANTS(:,13).*(STATES(:,5)./(CONSTANTS(:,15)+STATES(:,5)))+ CONSTANTS(:,18).*STATES(:,5).*STATES(:,6)+ CONSTANTS(:,16).*(STATES(:,5)./(CONSTANTS(:,17)+STATES(:,5)))+ CONSTANTS(:,5).*STATES(:,5));
    RATES(:,8) =  CONSTANTS(:,20).*(power(CONSTANTS(:,23), CONSTANTS(:,6))./(power(CONSTANTS(:,23), CONSTANTS(:,6))+power(STATES(:,2), CONSTANTS(:,6)))) - ( CONSTANTS(:,21).*(STATES(:,8)./(CONSTANTS(:,22)+STATES(:,8)))+ CONSTANTS(:,5).*STATES(:,8));
    RATES(:,9) = ( CONSTANTS(:,24).*STATES(:,8)+ CONSTANTS(:,26).*(STATES(:,10)./(CONSTANTS(:,28)+STATES(:,10)))) - ( CONSTANTS(:,25).*(STATES(:,9)./(CONSTANTS(:,27)+STATES(:,9)))+ CONSTANTS(:,5).*STATES(:,9));
    RATES(:,10) = ( CONSTANTS(:,25).*(STATES(:,9)./(CONSTANTS(:,27)+STATES(:,9)))+ CONSTANTS(:,30).*(STATES(:,6)./(CONSTANTS(:,32)+STATES(:,6)))) - ( CONSTANTS(:,26).*(STATES(:,10)./(CONSTANTS(:,28)+STATES(:,10)))+ CONSTANTS(:,29).*(STATES(:,10)./(CONSTANTS(:,31)+STATES(:,10)))+ CONSTANTS(:,5).*STATES(:,10));
    RATES(:,6) = ( CONSTANTS(:,29).*(STATES(:,10)./(CONSTANTS(:,31)+STATES(:,10)))+ CONSTANTS(:,19).*STATES(:,7)) - ( CONSTANTS(:,30).*(STATES(:,6)./(CONSTANTS(:,32)+STATES(:,6)))+ CONSTANTS(:,18).*STATES(:,5).*STATES(:,6)+ CONSTANTS(:,33).*(STATES(:,6)./(CONSTANTS(:,34)+STATES(:,6)))+ CONSTANTS(:,5).*STATES(:,6));
    RATES(:,7) = ( CONSTANTS(:,18).*STATES(:,5).*STATES(:,6)+ CONSTANTS(:,37).*STATES(:,2)) - ( CONSTANTS(:,19).*STATES(:,7)+ CONSTANTS(:,36).*STATES(:,7)+ CONSTANTS(:,35).*STATES(:,7));
    RATES(:,2) =  CONSTANTS(:,36).*STATES(:,7) - ( CONSTANTS(:,37).*STATES(:,2)+ CONSTANTS(:,38).*STATES(:,2));
   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)+STATES(:,5)+STATES(:,7)+STATES(:,2);
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