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 20 entries in each of the rate and state variable arrays.
% There are a total of 17 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 (second)');
    LEGEND_STATES(:,1) = strpad('Py in component Py (nanomolar)');
    LEGEND_STATES(:,2) = strpad('Zn in component Zn (nanomolar)');
    LEGEND_STATES(:,3) = strpad('Py1 in component Py1 (nanomolar)');
    LEGEND_CONSTANTS(:,1) = strpad('r3 in component model_parameters (third_order_rate_constant)');
    LEGEND_CONSTANTS(:,2) = strpad('r4 in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,4) = strpad('Dw in component Dw (nanomolar)');
    LEGEND_STATES(:,5) = strpad('Qw2 in component Qw2 (nanomolar)');
    LEGEND_CONSTANTS(:,3) = strpad('k_1 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,4) = strpad('k1a in component model_parameters (second_order_rate_constant)');
    LEGEND_STATES(:,6) = strpad('Qw1 in component Qw1 (nanomolar)');
    LEGEND_STATES(:,7) = strpad('Rw in component Rw (nanomolar)');
    LEGEND_CONSTANTS(:,5) = strpad('k2 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('k_2 in component model_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,1) = strpad('k3 in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,8) = strpad('Mw in component Mw (nanomolar)');
    LEGEND_STATES(:,9) = strpad('Px in component Px (nanomolar)');
    LEGEND_STATES(:,10) = strpad('Px1 in component Px1 (nanomolar)');
    LEGEND_STATES(:,11) = strpad('Dz in component Dz (nanomolar)');
    LEGEND_STATES(:,12) = strpad('Qz4 in component Qz4 (nanomolar)');
    LEGEND_CONSTANTS(:,7) = strpad('r1 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('r2 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('k1b in component model_parameters (second_order_rate_constant)');
    LEGEND_STATES(:,13) = strpad('Qz2 in component Qz2 (nanomolar)');
    LEGEND_CONSTANTS(:,10) = strpad('k1 in component model_parameters (second_order_rate_constant)');
    LEGEND_STATES(:,14) = strpad('Tp in component Tp (nanomolar)');
    LEGEND_STATES(:,15) = strpad('Tp1 in component Tp1 (nanomolar)');
    LEGEND_CONSTANTS(:,11) = strpad('r5 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('r6 in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,16) = strpad('Qz1 in component Qz1 (nanomolar)');
    LEGEND_STATES(:,17) = strpad('Rz in component Rz (nanomolar)');
    LEGEND_CONSTANTS(:,13) = strpad('k2a in component model_parameters (second_order_rate_constant)');
    LEGEND_STATES(:,18) = strpad('Qz3 in component Qz3 (nanomolar)');
    LEGEND_STATES(:,19) = strpad('Qz5 in component Qz5 (nanomolar)');
    LEGEND_CONSTANTS(:,14) = strpad('k2b in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,15) = strpad('k2c in component model_parameters (second_order_rate_constant)');
    LEGEND_STATES(:,20) = strpad('Mz in component Mz (nanomolar)');
    LEGEND_CONSTANTS(:,16) = strpad('td0 in component model_parameters (second)');
    LEGEND_CONSTANTS(:,17) = strpad('td in component model_parameters (second)');
    LEGEND_RATES(:,1) = strpad('d/dt Py in component Py (nanomolar)');
    LEGEND_RATES(:,3) = strpad('d/dt Py1 in component Py1 (nanomolar)');
    LEGEND_RATES(:,4) = strpad('d/dt Dw in component Dw (nanomolar)');
    LEGEND_RATES(:,7) = strpad('d/dt Rw in component Rw (nanomolar)');
    LEGEND_RATES(:,6) = strpad('d/dt Qw1 in component Qw1 (nanomolar)');
    LEGEND_RATES(:,5) = strpad('d/dt Qw2 in component Qw2 (nanomolar)');
    LEGEND_RATES(:,8) = strpad('d/dt Mw in component Mw (nanomolar)');
    LEGEND_RATES(:,9) = strpad('d/dt Px in component Px (nanomolar)');
    LEGEND_RATES(:,10) = strpad('d/dt Px1 in component Px1 (nanomolar)');
    LEGEND_RATES(:,14) = strpad('d/dt Tp in component Tp (nanomolar)');
    LEGEND_RATES(:,15) = strpad('d/dt Tp1 in component Tp1 (nanomolar)');
    LEGEND_RATES(:,2) = strpad('d/dt Zn in component Zn (nanomolar)');
    LEGEND_RATES(:,11) = strpad('d/dt Dz in component Dz (nanomolar)');
    LEGEND_RATES(:,17) = strpad('d/dt Rz in component Rz (nanomolar)');
    LEGEND_RATES(:,16) = strpad('d/dt Qz1 in component Qz1 (nanomolar)');
    LEGEND_RATES(:,13) = strpad('d/dt Qz2 in component Qz2 (nanomolar)');
    LEGEND_RATES(:,18) = strpad('d/dt Qz3 in component Qz3 (nanomolar)');
    LEGEND_RATES(:,12) = strpad('d/dt Qz4 in component Qz4 (nanomolar)');
    LEGEND_RATES(:,19) = strpad('d/dt Qz5 in component Qz5 (nanomolar)');
    LEGEND_RATES(:,20) = strpad('d/dt Mz in component Mz (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) = 25.0;
    STATES(:,2) = 10000.0;
    STATES(:,3) = 0.0;
    CONSTANTS(:,1) = 4.41E10;
    CONSTANTS(:,2) = 9E-3;
    STATES(:,4) = 4.0;
    STATES(:,5) = 0.0;
    CONSTANTS(:,3) = 0.9;
    CONSTANTS(:,4) = 1.0;
    STATES(:,6) = 0.0;
    STATES(:,7) = 50.0;
    CONSTANTS(:,5) = 0.02;
    CONSTANTS(:,6) = 0.3;
    STATES(:,8) = 0.0;
    STATES(:,9) = 50.0;
    STATES(:,10) = 0.0;
    STATES(:,11) = 2.0;
    STATES(:,12) = 0.0;
    CONSTANTS(:,7) = 2.73E2;
    CONSTANTS(:,8) = 3.437E-4;
    CONSTANTS(:,9) = 1.253E-2;
    STATES(:,13) = 0.0;
    CONSTANTS(:,10) = 0.025;
    STATES(:,14) = 10000.0;
    STATES(:,15) = 0.0;
    CONSTANTS(:,11) = 3E4;
    CONSTANTS(:,12) = 1.506E-2;
    STATES(:,16) = 0.0;
    STATES(:,17) = 100.0;
    CONSTANTS(:,13) = 0.00005;
    STATES(:,18) = 0.0;
    STATES(:,19) = 0.0;
    CONSTANTS(:,14) = 0.0002;
    CONSTANTS(:,15) = 0.0037;
    STATES(:,20) = 0.0;
    CONSTANTS(:,16) = 1800.0;
    CONSTANTS(:,17) = 2700;
    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(:,2).*STATES(:,3) -  CONSTANTS(:,1).*power(STATES(:,2), 2.00000).*STATES(:,1);
    RATES(:,3) = ( CONSTANTS(:,1).*power(STATES(:,2), 2.00000).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,5)) - ( CONSTANTS(:,2).*STATES(:,3)+ CONSTANTS(:,4).*STATES(:,4).*STATES(:,3));
    RATES(:,5) =  CONSTANTS(:,4).*STATES(:,4).*STATES(:,3) -  CONSTANTS(:,3).*STATES(:,5);
    RATES(:,9) = ( CONSTANTS(:,8).*STATES(:,10)+ CONSTANTS(:,3).*STATES(:,12)) - ( CONSTANTS(:,7).*STATES(:,2).*STATES(:,9)+ CONSTANTS(:,9).*STATES(:,11).*STATES(:,9));
    RATES(:,10) = ( CONSTANTS(:,7).*STATES(:,2).*STATES(:,9)+ CONSTANTS(:,3).*STATES(:,13)) - ( CONSTANTS(:,8).*STATES(:,10)+ CONSTANTS(:,10).*STATES(:,11).*STATES(:,10));
    RATES(:,14) =  CONSTANTS(:,12).*STATES(:,15) -  CONSTANTS(:,11).*STATES(:,2).*STATES(:,14);
    RATES(:,15) =  CONSTANTS(:,11).*STATES(:,2).*STATES(:,14) -  CONSTANTS(:,12).*STATES(:,15);
    RATES(:,2) = ( CONSTANTS(:,8).*STATES(:,10)+ CONSTANTS(:,12).*STATES(:,15)) - ( CONSTANTS(:,7).*STATES(:,2).*STATES(:,9)+ CONSTANTS(:,11).*STATES(:,2).*STATES(:,14));
    ALGEBRAIC(:,1) = piecewise({VOI>=0.00000&VOI<CONSTANTS(:,16), 0.00000 , VOI>=CONSTANTS(:,16)&VOI<CONSTANTS(:,17), 0.0110000 }, NaN);
    RATES(:,4) = ( CONSTANTS(:,3).*STATES(:,5)+ ALGEBRAIC(:,1).*STATES(:,6)+ CONSTANTS(:,6).*STATES(:,6)) - ( CONSTANTS(:,4).*STATES(:,4).*STATES(:,3)+ CONSTANTS(:,5).*STATES(:,4).*STATES(:,7));
    RATES(:,7) = ( ALGEBRAIC(:,1).*STATES(:,6)+ CONSTANTS(:,6).*STATES(:,6)) -  CONSTANTS(:,5).*STATES(:,4).*STATES(:,7);
    RATES(:,6) =  CONSTANTS(:,5).*STATES(:,4).*STATES(:,7) - ( ALGEBRAIC(:,1).*STATES(:,6)+ CONSTANTS(:,6).*STATES(:,6));
    RATES(:,8) =  ALGEBRAIC(:,1).*STATES(:,6);
    RATES(:,11) = ( CONSTANTS(:,3).*STATES(:,13)+ ALGEBRAIC(:,1).*STATES(:,16)+ CONSTANTS(:,6).*STATES(:,16)+ CONSTANTS(:,3).*STATES(:,12)) - ( CONSTANTS(:,9).*STATES(:,11).*STATES(:,9)+ CONSTANTS(:,10).*STATES(:,11).*STATES(:,10)+ CONSTANTS(:,13).*STATES(:,11).*STATES(:,17));
    RATES(:,17) = ( ALGEBRAIC(:,1).*STATES(:,16)+ CONSTANTS(:,6).*STATES(:,16)+ ALGEBRAIC(:,1).*STATES(:,18)+ CONSTANTS(:,6).*STATES(:,18)+ ALGEBRAIC(:,1).*STATES(:,19)+ CONSTANTS(:,6).*STATES(:,19)) - ( CONSTANTS(:,13).*STATES(:,11).*STATES(:,17)+ CONSTANTS(:,14).*STATES(:,12).*STATES(:,17)+ CONSTANTS(:,15).*STATES(:,13).*STATES(:,17));
    RATES(:,16) =  CONSTANTS(:,13).*STATES(:,11).*STATES(:,17) - ( ALGEBRAIC(:,1).*STATES(:,16)+ CONSTANTS(:,6).*STATES(:,16));
    RATES(:,13) = ( CONSTANTS(:,10).*STATES(:,11).*STATES(:,10)+ ALGEBRAIC(:,1).*STATES(:,18)+ CONSTANTS(:,6).*STATES(:,18)) - ( CONSTANTS(:,3).*STATES(:,13)+ CONSTANTS(:,15).*STATES(:,13).*STATES(:,17));
    RATES(:,18) =  CONSTANTS(:,15).*STATES(:,13).*STATES(:,17) - ( ALGEBRAIC(:,1).*STATES(:,18)+ CONSTANTS(:,6).*STATES(:,18));
    RATES(:,12) = ( CONSTANTS(:,9).*STATES(:,11).*STATES(:,9)+ ALGEBRAIC(:,1).*STATES(:,19)+ CONSTANTS(:,6).*STATES(:,19)) - ( CONSTANTS(:,3).*STATES(:,12)+ CONSTANTS(:,14).*STATES(:,12).*STATES(:,17));
    RATES(:,19) =  CONSTANTS(:,14).*STATES(:,12).*STATES(:,17) - ( ALGEBRAIC(:,1).*STATES(:,19)+ CONSTANTS(:,6).*STATES(:,19));
    RATES(:,20) =  ALGEBRAIC(:,1).*STATES(:,16)+ ALGEBRAIC(:,1).*STATES(:,18)+ ALGEBRAIC(:,1).*STATES(:,19);
   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) = piecewise({VOI>=0.00000&VOI<CONSTANTS(:,16), 0.00000 , VOI>=CONSTANTS(:,16)&VOI<CONSTANTS(:,17), 0.0110000 }, NaN);
end

% Compute result of a piecewise function
function x = piecewise(cases, default)
    set = [0];
    for i = 1:2:length(cases)
        if (length(cases{i+1}) == 1)
            x(cases{i} & ~set,:) = cases{i+1};
        else
            x(cases{i} & ~set,:) = cases{i+1}(cases{i} & ~set);
        end
        set = set | cases{i};
        if(set), break, end
    end
    if (length(default) == 1)
        x(~set,:) = default;
    else
        x(~set,:) = default(~set);
    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