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 6 entries in each of the rate and state variable arrays.
% There are a total of 25 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 (minute)');
    LEGEND_CONSTANTS(:,1) = strpad('j_1 in component parameters (flux)');
    LEGEND_CONSTANTS(:,2) = strpad('j_2 in component parameters (flux)');
    LEGEND_CONSTANTS(:,3) = strpad('j_3 in component parameters (flux)');
    LEGEND_CONSTANTS(:,4) = strpad('v_d1 in component parameters (flux)');
    LEGEND_CONSTANTS(:,5) = strpad('v_d2 in component parameters (flux)');
    LEGEND_CONSTANTS(:,6) = strpad('v_d3 in component parameters (flux)');
    LEGEND_CONSTANTS(:,7) = strpad('k_d1 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('k_d2 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('k_d3 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('k_c1 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('k_c2 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('k_c3 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('k_m1 in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,14) = strpad('k_m2 in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,15) = strpad('k_m3 in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,16) = strpad('v_12 in component parameters (flux)');
    LEGEND_CONSTANTS(:,17) = strpad('v_11 in component parameters (flux)');
    LEGEND_CONSTANTS(:,18) = strpad('v_10 in component parameters (flux)');
    LEGEND_CONSTANTS(:,19) = strpad('k_120 in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,20) = strpad('k_110 in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,21) = strpad('k_100 in component parameters (nanomolar)');
    LEGEND_CONSTANTS(:,22) = strpad('k_d4 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,23) = strpad('k_d5 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,24) = strpad('k_d6 in component parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,25) = strpad('n in component parameters (dimensionless)');
    LEGEND_STATES(:,1) = strpad('C_1 in component C_1 (nanomolar)');
    LEGEND_STATES(:,2) = strpad('C_2 in component C_2 (nanomolar)');
    LEGEND_STATES(:,3) = strpad('T_1 in component T_1 (nanomolar)');
    LEGEND_STATES(:,4) = strpad('C_3 in component C_3 (nanomolar)');
    LEGEND_STATES(:,5) = strpad('T_2 in component T_2 (nanomolar)');
    LEGEND_STATES(:,6) = strpad('T_3 in component T_3 (nanomolar)');
    LEGEND_RATES(:,1) = strpad('d/dt C_1 in component C_1 (nanomolar)');
    LEGEND_RATES(:,2) = strpad('d/dt C_2 in component C_2 (nanomolar)');
    LEGEND_RATES(:,4) = strpad('d/dt C_3 in component C_3 (nanomolar)');
    LEGEND_RATES(:,3) = strpad('d/dt T_1 in component T_1 (nanomolar)');
    LEGEND_RATES(:,5) = strpad('d/dt T_2 in component T_2 (nanomolar)');
    LEGEND_RATES(:,6) = strpad('d/dt T_3 in component T_3 (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 = [];
    CONSTANTS(:,1) = 0.9;
    CONSTANTS(:,2) = 0.5;
    CONSTANTS(:,3) = 0.6;
    CONSTANTS(:,4) = 6;
    CONSTANTS(:,5) = 1.052;
    CONSTANTS(:,6) = 3;
    CONSTANTS(:,7) = 0.8;
    CONSTANTS(:,8) = 0.9;
    CONSTANTS(:,9) = 0.8;
    CONSTANTS(:,10) = 0.2;
    CONSTANTS(:,11) = 0.22;
    CONSTANTS(:,12) = 0.6;
    CONSTANTS(:,13) = 5;
    CONSTANTS(:,14) = 5;
    CONSTANTS(:,15) = 5;
    CONSTANTS(:,16) = 15;
    CONSTANTS(:,17) = 15;
    CONSTANTS(:,18) = 15;
    CONSTANTS(:,19) = 10;
    CONSTANTS(:,20) = 10;
    CONSTANTS(:,21) = 10;
    CONSTANTS(:,22) = 0.16;
    CONSTANTS(:,23) = 0.16;
    CONSTANTS(:,24) = 0.16;
    CONSTANTS(:,25) = 2;
    STATES(:,1) = 0;
    STATES(:,2) = 0;
    STATES(:,3) = 6;
    STATES(:,4) = 0;
    STATES(:,5) = 5;
    STATES(:,6) = 1;
    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(:,16).*power(STATES(:,3), CONSTANTS(:,25)))./(power(CONSTANTS(:,19), CONSTANTS(:,25))+power(STATES(:,3), CONSTANTS(:,25))+power(STATES(:,2), CONSTANTS(:,25))) -  CONSTANTS(:,22).*STATES(:,1);
    RATES(:,2) = ( CONSTANTS(:,17).*power(STATES(:,5), CONSTANTS(:,25)))./(power(CONSTANTS(:,20), CONSTANTS(:,25))+power(STATES(:,5), CONSTANTS(:,25))+power(STATES(:,4), CONSTANTS(:,25))) -  CONSTANTS(:,23).*STATES(:,2);
    RATES(:,4) = ( CONSTANTS(:,18).*power(STATES(:,6), CONSTANTS(:,25)))./(power(CONSTANTS(:,21), CONSTANTS(:,25))+power(STATES(:,6), CONSTANTS(:,25))+power(STATES(:,1), CONSTANTS(:,25))) -  CONSTANTS(:,24).*STATES(:,4);
    RATES(:,3) = (CONSTANTS(:,1)+( CONSTANTS(:,4).*power(STATES(:,6), CONSTANTS(:,25)))./(power(CONSTANTS(:,13), CONSTANTS(:,25))+power(STATES(:,6), CONSTANTS(:,25)))+ CONSTANTS(:,10).*STATES(:,1)) -  CONSTANTS(:,7).*STATES(:,3);
    RATES(:,5) = (CONSTANTS(:,2)+( CONSTANTS(:,5).*power(STATES(:,3), CONSTANTS(:,25)))./(power(CONSTANTS(:,14), CONSTANTS(:,25))+power(STATES(:,3), CONSTANTS(:,25)))+ CONSTANTS(:,11).*STATES(:,2)) -  CONSTANTS(:,8).*STATES(:,5);
    RATES(:,6) = (CONSTANTS(:,3)+( CONSTANTS(:,6).*power(STATES(:,5), CONSTANTS(:,25)))./(power(CONSTANTS(:,15), CONSTANTS(:,25))+power(STATES(:,5), CONSTANTS(:,25)))+ CONSTANTS(:,12).*STATES(:,4)) -  CONSTANTS(:,9).*STATES(:,6);
   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