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 16 entries in each of the rate and state variable arrays.
% There are a total of 21 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('t in component environment (s)');
    LEGEND_CONSTANTS(:,1) = strpad('kM2T2_on in component Model (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,2) = strpad('kM2T2_off in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,3) = strpad('kM2T2_iso in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,4) = strpad('kM2T2_negativeiso in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,5) = strpad('kM2C1_on in component Model (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('kM2C1_off in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('kM2C1_cat in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('kMT1T2_on in component Model (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('kMT1T2_off in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('kMT1T2M2pro_on in component Model (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('kMT1T2M2pro_off in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('kM2_act in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('kMT1_shedeff in component Model (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('kMT1C1_cat in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,15) = strpad('kMT1C1_on in component Model (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('kMT1C1_off in component Model (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('kMT1T2M2proMT1_on in component Model (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,18) = strpad('kMT1T2M2proMT1_off in component Model (first_order_rate_constant)');
    LEGEND_STATES(:,1) = strpad('M2T2 in component Model (M)');
    LEGEND_STATES(:,2) = strpad('M2C1 in component Model (M)');
    LEGEND_STATES(:,3) = strpad('MT1T2 in component Model (M)');
    LEGEND_STATES(:,4) = strpad('MT1T2M2proMT1 in component Model (M)');
    LEGEND_STATES(:,5) = strpad('MT1C1 in component Model (M)');
    LEGEND_STATES(:,6) = strpad('M2 in component Model (M)');
    LEGEND_STATES(:,7) = strpad('MT1 in component Model (M)');
    LEGEND_STATES(:,8) = strpad('M2_p in component Model (M)');
    LEGEND_STATES(:,9) = strpad('T2 in component Model (M)');
    LEGEND_STATES(:,10) = strpad('C1_D in component Model (M)');
    LEGEND_STATES(:,11) = strpad('C1 in component Model (M)');
    LEGEND_STATES(:,12) = strpad('MT1_cat in component Model (M)');
    LEGEND_STATES(:,13) = strpad('MT1_t in component Model (M)');
    LEGEND_STATES(:,14) = strpad('MT1T2M2pro in component Model (M)');
    LEGEND_CONSTANTS(:,19) = strpad('qMT1 in component Model (flux)');
    LEGEND_CONSTANTS(:,20) = strpad('qT2 in component Model (flux)');
    LEGEND_CONSTANTS(:,21) = strpad('qpro in component Model (flux)');
    LEGEND_STATES(:,15) = strpad('MT1T2_star in component Model (um)');
    LEGEND_STATES(:,16) = strpad('M2T2_star in component Model (um)');
    LEGEND_RATES(:,6) = strpad('d/dt M2 in component Model (M)');
    LEGEND_RATES(:,7) = strpad('d/dt MT1 in component Model (M)');
    LEGEND_RATES(:,13) = strpad('d/dt MT1_t in component Model (M)');
    LEGEND_RATES(:,5) = strpad('d/dt MT1C1 in component Model (M)');
    LEGEND_RATES(:,3) = strpad('d/dt MT1T2 in component Model (M)');
    LEGEND_RATES(:,14) = strpad('d/dt MT1T2M2pro in component Model (M)');
    LEGEND_RATES(:,4) = strpad('d/dt MT1T2M2proMT1 in component Model (M)');
    LEGEND_RATES(:,11) = strpad('d/dt C1 in component Model (M)');
    LEGEND_RATES(:,10) = strpad('d/dt C1_D in component Model (M)');
    LEGEND_RATES(:,12) = strpad('d/dt MT1_cat in component Model (M)');
    LEGEND_RATES(:,9) = strpad('d/dt T2 in component Model (M)');
    LEGEND_RATES(:,8) = strpad('d/dt M2_p in component Model (M)');
    LEGEND_RATES(:,1) = strpad('d/dt M2T2 in component Model (M)');
    LEGEND_RATES(:,16) = strpad('d/dt M2T2_star in component Model (um)');
    LEGEND_RATES(:,2) = strpad('d/dt M2C1 in component Model (M)');
    LEGEND_RATES(:,15) = strpad('d/dt MT1T2_star in component Model (um)');
    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) = 5900000;
    CONSTANTS(:,2) = 6.3;
    CONSTANTS(:,3) = 33;
    CONSTANTS(:,4) = 0.00000002;
    CONSTANTS(:,5) = 2600;
    CONSTANTS(:,6) = 0.0021;
    CONSTANTS(:,7) = 0.0045;
    CONSTANTS(:,8) = 2980000;
    CONSTANTS(:,9) = 0.202;
    CONSTANTS(:,10) = 140000;
    CONSTANTS(:,11) = 0.0047;
    CONSTANTS(:,12) = 0.02;
    CONSTANTS(:,13) = 2800;
    CONSTANTS(:,14) = 0.00197;
    CONSTANTS(:,15) = 1000;
    CONSTANTS(:,16) = 1;
    CONSTANTS(:,17) = 3000;
    CONSTANTS(:,18) = 0.0009;
    STATES(:,1) = 0.0000000072;
    STATES(:,2) = 0.0000085;
    STATES(:,3) = 0.00000000139;
    STATES(:,4) = 0.00000000056;
    STATES(:,5) = 0.0000029;
    STATES(:,6) = 0;
    STATES(:,7) = 0;
    STATES(:,8) = 0;
    STATES(:,9) = 0;
    STATES(:,10) = 0;
    STATES(:,11) = 0;
    STATES(:,12) = 0;
    STATES(:,13) = 0;
    STATES(:,14) = 0;
    CONSTANTS(:,19) = 0;
    CONSTANTS(:,20) = 0;
    CONSTANTS(:,21) = 0;
    STATES(:,15) = 0;
    STATES(:,16) = 0;
    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(:,6) = ((  - CONSTANTS(:,1).*STATES(:,6).*STATES(:,9)+ CONSTANTS(:,2).*STATES(:,1)) -  CONSTANTS(:,5).*STATES(:,6).*STATES(:,11))+ (CONSTANTS(:,6)+CONSTANTS(:,7)).*STATES(:,2)+ CONSTANTS(:,12).*STATES(:,4);
    RATES(:,7) = ((((((CONSTANTS(:,19) -  CONSTANTS(:,13).*STATES(:,7).*STATES(:,7)) -  CONSTANTS(:,8).*STATES(:,7).*STATES(:,9))+ CONSTANTS(:,9).*STATES(:,3)) -  CONSTANTS(:,15).*STATES(:,7).*STATES(:,11))+ (CONSTANTS(:,16)+CONSTANTS(:,14)).*STATES(:,5)) -  CONSTANTS(:,17).*STATES(:,7).*STATES(:,14))+ CONSTANTS(:,18).*STATES(:,4)+ CONSTANTS(:,12).*STATES(:,4);
    RATES(:,13) =  CONSTANTS(:,13).*STATES(:,7).*STATES(:,7);
    RATES(:,5) =  CONSTANTS(:,15).*STATES(:,7).*STATES(:,11) -  (CONSTANTS(:,16)+CONSTANTS(:,14)).*STATES(:,5);
    RATES(:,3) = (( CONSTANTS(:,8).*STATES(:,7).*STATES(:,9) -  CONSTANTS(:,9).*STATES(:,3)) -  CONSTANTS(:,10).*STATES(:,3).*STATES(:,8))+ CONSTANTS(:,11).*STATES(:,14);
    RATES(:,14) = (( CONSTANTS(:,10).*STATES(:,3).*STATES(:,8) -  CONSTANTS(:,11).*STATES(:,14)) -  CONSTANTS(:,17).*STATES(:,7).*STATES(:,14))+ CONSTANTS(:,18).*STATES(:,4);
    RATES(:,4) = ( CONSTANTS(:,17).*STATES(:,7).*STATES(:,14) -  CONSTANTS(:,18).*STATES(:,4)) -  CONSTANTS(:,12).*STATES(:,4);
    RATES(:,11) = ((  - CONSTANTS(:,15).*STATES(:,7).*STATES(:,11)+ CONSTANTS(:,16).*STATES(:,5)) -  CONSTANTS(:,5).*STATES(:,6).*STATES(:,11))+ CONSTANTS(:,6).*STATES(:,2);
    RATES(:,10) =  CONSTANTS(:,7).*STATES(:,2)+ CONSTANTS(:,14).*STATES(:,5);
    RATES(:,12) =  CONSTANTS(:,13).*STATES(:,7).*STATES(:,7);
    RATES(:,9) = ((  - CONSTANTS(:,1).*STATES(:,6).*STATES(:,9)+ CONSTANTS(:,2).*STATES(:,1)+CONSTANTS(:,20)) -  CONSTANTS(:,8).*STATES(:,7).*STATES(:,9))+ CONSTANTS(:,9).*STATES(:,3);
    RATES(:,8) = (CONSTANTS(:,21) -  CONSTANTS(:,10).*STATES(:,3).*STATES(:,8))+ CONSTANTS(:,11).*STATES(:,14);
    RATES(:,1) = (( CONSTANTS(:,1).*STATES(:,6).*STATES(:,9) -  CONSTANTS(:,2).*STATES(:,1)) -  CONSTANTS(:,3).*STATES(:,1))+ CONSTANTS(:,4).*STATES(:,16);
    RATES(:,16) =  CONSTANTS(:,3).*STATES(:,1) -  CONSTANTS(:,4).*STATES(:,16);
    RATES(:,2) =  CONSTANTS(:,5).*STATES(:,6).*STATES(:,11) -  (CONSTANTS(:,6)+CONSTANTS(:,7)).*STATES(:,2);
    RATES(:,15) =  CONSTANTS(:,12).*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
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