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 18 entries in each of the rate and state variable arrays.
% There are a total of 37 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_CONSTANTS(:,1) = strpad('mostot in component total_concs (micromolar)');
    LEGEND_CONSTANTS(:,2) = strpad('e1tot in component total_concs (micromolar)');
    LEGEND_CONSTANTS(:,3) = strpad('e2tot in component total_concs (micromolar)');
    LEGEND_CONSTANTS(:,4) = strpad('mektot in component total_concs (micromolar)');
    LEGEND_CONSTANTS(:,5) = strpad('mekpasetot in component total_concs (micromolar)');
    LEGEND_CONSTANTS(:,6) = strpad('mapktot in component total_concs (micromolar)');
    LEGEND_CONSTANTS(:,7) = strpad('mapkpasetot in component total_concs (micromolar)');
    LEGEND_CONSTANTS(:,8) = strpad('a1 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,9) = strpad('a2 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,10) = strpad('a3 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,11) = strpad('a4 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,12) = strpad('a5 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,13) = strpad('a6 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,14) = strpad('a7 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,15) = strpad('a8 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,16) = strpad('a9 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,17) = strpad('a10 in component rate_constants (second_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,18) = strpad('d1 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,19) = strpad('d2 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,20) = strpad('d3 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,21) = strpad('d4 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,22) = strpad('d5 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,23) = strpad('d6 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,24) = strpad('d7 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,25) = strpad('d8 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,26) = strpad('d9 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,27) = strpad('d10 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,28) = strpad('k1 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,29) = strpad('k2 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,30) = strpad('k3 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,31) = strpad('k4 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,32) = strpad('k5 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,33) = strpad('k6 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,34) = strpad('k7 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,35) = strpad('k8 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,36) = strpad('k9 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_CONSTANTS(:,37) = strpad('k10 in component rate_constants (first_order_rate_constant_units)');
    LEGEND_STATES(:,1) = strpad('mos in component mos (micromolar)');
    LEGEND_STATES(:,2) = strpad('mosstar in component mosstar (micromolar)');
    LEGEND_STATES(:,3) = strpad('mose1 in component mose1 (micromolar)');
    LEGEND_STATES(:,4) = strpad('mosstare2 in component mosstare2 (micromolar)');
    LEGEND_STATES(:,5) = strpad('mekstarmosstar in component mekstarmosstar (micromolar)');
    LEGEND_STATES(:,6) = strpad('mekmosstar in component mekmosstar (micromolar)');
    LEGEND_STATES(:,7) = strpad('mekstar in component mekstar (micromolar)');
    LEGEND_STATES(:,8) = strpad('mekstarstar in component mekstarstar (micromolar)');
    LEGEND_STATES(:,9) = strpad('mekstarmekpase in component mekstarmekpase (micromolar)');
    LEGEND_STATES(:,10) = strpad('mekstarstarmekpase in component mekstarstarmekpase (micromolar)');
    LEGEND_STATES(:,11) = strpad('mapkmekstarstar in component mapkmekstarstar (micromolar)');
    LEGEND_STATES(:,12) = strpad('mapkstarmekstarstar in component mapkstarmekstarstar (micromolar)');
    LEGEND_STATES(:,13) = strpad('mek in component mek (micromolar)');
    LEGEND_STATES(:,14) = strpad('mapkstar in component mapkstar (micromolar)');
    LEGEND_STATES(:,15) = strpad('mapkstarstar in component mapkstarstar (micromolar)');
    LEGEND_STATES(:,16) = strpad('mapkstarmapkpase in component mapkstarmapkpase (micromolar)');
    LEGEND_STATES(:,17) = strpad('mapkstarstarmapkpase in component mapkstarstarmapkpase (micromolar)');
    LEGEND_STATES(:,18) = strpad('mapk in component mapk (micromolar)');
    LEGEND_RATES(:,1) = strpad('d/dt mos in component mos (micromolar)');
    LEGEND_RATES(:,3) = strpad('d/dt mose1 in component mose1 (micromolar)');
    LEGEND_RATES(:,2) = strpad('d/dt mosstar in component mosstar (micromolar)');
    LEGEND_RATES(:,4) = strpad('d/dt mosstare2 in component mosstare2 (micromolar)');
    LEGEND_RATES(:,13) = strpad('d/dt mek in component mek (micromolar)');
    LEGEND_RATES(:,6) = strpad('d/dt mekmosstar in component mekmosstar (micromolar)');
    LEGEND_RATES(:,7) = strpad('d/dt mekstar in component mekstar (micromolar)');
    LEGEND_RATES(:,9) = strpad('d/dt mekstarmekpase in component mekstarmekpase (micromolar)');
    LEGEND_RATES(:,5) = strpad('d/dt mekstarmosstar in component mekstarmosstar (micromolar)');
    LEGEND_RATES(:,8) = strpad('d/dt mekstarstar in component mekstarstar (micromolar)');
    LEGEND_RATES(:,10) = strpad('d/dt mekstarstarmekpase in component mekstarstarmekpase (micromolar)');
    LEGEND_RATES(:,18) = strpad('d/dt mapk in component mapk (micromolar)');
    LEGEND_RATES(:,11) = strpad('d/dt mapkmekstarstar in component mapkmekstarstar (micromolar)');
    LEGEND_RATES(:,14) = strpad('d/dt mapkstar in component mapkstar (micromolar)');
    LEGEND_RATES(:,12) = strpad('d/dt mapkstarmekstarstar in component mapkstarmekstarstar (micromolar)');
    LEGEND_RATES(:,15) = strpad('d/dt mapkstarstar in component mapkstarstar (micromolar)');
    LEGEND_RATES(:,16) = strpad('d/dt mapkstarmapkpase in component mapkstarmapkpase (micromolar)');
    LEGEND_RATES(:,17) = strpad('d/dt mapkstarstarmapkpase in component mapkstarstarmapkpase (micromolar)');
    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.003;
    CONSTANTS(:,2) = 0.0003;
    CONSTANTS(:,3) = 0.0003;
    CONSTANTS(:,4) = 1.2;
    CONSTANTS(:,5) = 0.0003;
    CONSTANTS(:,6) = 1.2;
    CONSTANTS(:,7) = 0.12;
    CONSTANTS(:,8) = 1000;
    CONSTANTS(:,9) = 1000;
    CONSTANTS(:,10) = 1000;
    CONSTANTS(:,11) = 1000;
    CONSTANTS(:,12) = 1000;
    CONSTANTS(:,13) = 1000;
    CONSTANTS(:,14) = 1000;
    CONSTANTS(:,15) = 1000;
    CONSTANTS(:,16) = 1000;
    CONSTANTS(:,17) = 1000;
    CONSTANTS(:,18) = 150;
    CONSTANTS(:,19) = 150;
    CONSTANTS(:,20) = 150;
    CONSTANTS(:,21) = 150;
    CONSTANTS(:,22) = 150;
    CONSTANTS(:,23) = 150;
    CONSTANTS(:,24) = 150;
    CONSTANTS(:,25) = 150;
    CONSTANTS(:,26) = 150;
    CONSTANTS(:,27) = 150;
    CONSTANTS(:,28) = 150;
    CONSTANTS(:,29) = 150;
    CONSTANTS(:,30) = 150;
    CONSTANTS(:,31) = 150;
    CONSTANTS(:,32) = 150;
    CONSTANTS(:,33) = 150;
    CONSTANTS(:,34) = 150;
    CONSTANTS(:,35) = 150;
    CONSTANTS(:,36) = 150;
    CONSTANTS(:,37) = 150;
    STATES(:,1) = 0.003;
    STATES(:,2) = 0;
    STATES(:,3) = 0;
    STATES(:,4) = 0;
    STATES(:,5) = 0;
    STATES(:,6) = 0;
    STATES(:,7) = 0;
    STATES(:,8) = 0;
    STATES(:,9) = 0;
    STATES(:,10) = 0;
    STATES(:,11) = 0;
    STATES(:,12) = 0;
    STATES(:,13) = 1.2;
    STATES(:,14) = 0;
    STATES(:,15) = 0;
    STATES(:,16) = 0;
    STATES(:,17) = 0;
    STATES(:,18) = 1.2;
    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(:,8).*(((((CONSTANTS(:,1) - STATES(:,2)) - STATES(:,3)) - STATES(:,4)) - STATES(:,6)) - STATES(:,5)).*(CONSTANTS(:,2) - STATES(:,3))+ CONSTANTS(:,18).*STATES(:,3)+ CONSTANTS(:,29).*STATES(:,4);
    RATES(:,3) =  CONSTANTS(:,8).*(((((CONSTANTS(:,1) - STATES(:,2)) - STATES(:,3)) - STATES(:,4)) - STATES(:,6)) - STATES(:,5)).*(CONSTANTS(:,2) - STATES(:,3)) -  (CONSTANTS(:,18)+CONSTANTS(:,28)).*STATES(:,3);
    RATES(:,2) = (((  - CONSTANTS(:,9).*STATES(:,2).*(CONSTANTS(:,3) - STATES(:,4))+ CONSTANTS(:,19).*STATES(:,4)+ CONSTANTS(:,28).*STATES(:,3)+ (CONSTANTS(:,30)+CONSTANTS(:,20)).*STATES(:,6)) -  CONSTANTS(:,10).*STATES(:,2).*((((((((CONSTANTS(:,4) - STATES(:,7)) - STATES(:,8)) - STATES(:,9)) - STATES(:,10)) - STATES(:,6)) - STATES(:,5)) - STATES(:,11)) - STATES(:,12)))+ (CONSTANTS(:,32)+CONSTANTS(:,22)).*STATES(:,5)) -  CONSTANTS(:,12).*STATES(:,7).*STATES(:,2);
    RATES(:,4) =  CONSTANTS(:,9).*STATES(:,2).*(CONSTANTS(:,3) - STATES(:,4)) -  (CONSTANTS(:,19)+CONSTANTS(:,29)).*STATES(:,4);
    RATES(:,13) =   - CONSTANTS(:,10).*((((((((CONSTANTS(:,4) - STATES(:,7)) - STATES(:,8)) - STATES(:,9)) - STATES(:,10)) - STATES(:,6)) - STATES(:,5)) - STATES(:,11)) - STATES(:,12)).*STATES(:,2)+ CONSTANTS(:,20).*STATES(:,6)+ CONSTANTS(:,31).*STATES(:,9);
    RATES(:,6) =  CONSTANTS(:,10).*((((((((CONSTANTS(:,4) - STATES(:,7)) - STATES(:,8)) - STATES(:,9)) - STATES(:,10)) - STATES(:,6)) - STATES(:,5)) - STATES(:,11)) - STATES(:,12)).*STATES(:,2) -  (CONSTANTS(:,20)+CONSTANTS(:,30)).*STATES(:,6);
    RATES(:,7) = (  - CONSTANTS(:,11).*STATES(:,7).*((CONSTANTS(:,5) - STATES(:,9)) - STATES(:,10))+ CONSTANTS(:,21).*STATES(:,9)+ CONSTANTS(:,30).*STATES(:,6)+ CONSTANTS(:,33).*STATES(:,10)+ CONSTANTS(:,22).*STATES(:,5)) -  CONSTANTS(:,12).*STATES(:,7).*STATES(:,2);
    RATES(:,9) =  CONSTANTS(:,11).*STATES(:,7).*((CONSTANTS(:,5) - STATES(:,9)) - STATES(:,10)) -  (CONSTANTS(:,21)+CONSTANTS(:,31)).*STATES(:,9);
    RATES(:,5) =  CONSTANTS(:,12).*STATES(:,7).*STATES(:,2) -  (CONSTANTS(:,22)+CONSTANTS(:,32)).*STATES(:,5);
    RATES(:,8) = (((( CONSTANTS(:,32).*STATES(:,5) -  CONSTANTS(:,13).*STATES(:,8).*((CONSTANTS(:,5) - STATES(:,9)) - STATES(:,10)))+ CONSTANTS(:,23).*STATES(:,10)) -  CONSTANTS(:,14).*STATES(:,8).*((((((CONSTANTS(:,6) - STATES(:,14)) - STATES(:,15)) - STATES(:,16)) - STATES(:,17)) - STATES(:,11)) - STATES(:,12)))+ (CONSTANTS(:,24)+CONSTANTS(:,34)).*STATES(:,11)+ (CONSTANTS(:,26)+CONSTANTS(:,36)).*STATES(:,12)) -  CONSTANTS(:,16).*STATES(:,14).*STATES(:,8);
    RATES(:,10) =  CONSTANTS(:,13).*STATES(:,8).*((CONSTANTS(:,5) - STATES(:,9)) - STATES(:,10)) -  (CONSTANTS(:,23)+CONSTANTS(:,33)).*STATES(:,10);
    RATES(:,18) =   - CONSTANTS(:,14).*((((((CONSTANTS(:,6) - STATES(:,14)) - STATES(:,15)) - STATES(:,16)) - STATES(:,17)) - STATES(:,11)) - STATES(:,12)).*STATES(:,8)+ CONSTANTS(:,24).*STATES(:,11)+ CONSTANTS(:,35).*STATES(:,16);
    RATES(:,11) =  CONSTANTS(:,14).*((((((CONSTANTS(:,6) - STATES(:,14)) - STATES(:,15)) - STATES(:,16)) - STATES(:,17)) - STATES(:,11)) - STATES(:,12)).*STATES(:,8) -  (CONSTANTS(:,24)+CONSTANTS(:,34)).*STATES(:,11);
    RATES(:,14) = ((( CONSTANTS(:,34).*STATES(:,11) -  CONSTANTS(:,15).*STATES(:,14).*((CONSTANTS(:,7) - STATES(:,16)) - STATES(:,17)))+ CONSTANTS(:,25).*STATES(:,16)) -  CONSTANTS(:,16).*STATES(:,14).*STATES(:,8))+ CONSTANTS(:,26).*STATES(:,12)+ CONSTANTS(:,37).*STATES(:,17);
    RATES(:,12) =  CONSTANTS(:,16).*STATES(:,14).*STATES(:,8) -  (CONSTANTS(:,26)+CONSTANTS(:,36)).*STATES(:,12);
    RATES(:,15) =   - CONSTANTS(:,17).*STATES(:,15).*((CONSTANTS(:,7) - STATES(:,16)) - STATES(:,17))+ CONSTANTS(:,27).*STATES(:,17)+ CONSTANTS(:,36).*STATES(:,12);
    RATES(:,16) =  CONSTANTS(:,15).*STATES(:,14).*((CONSTANTS(:,7) - STATES(:,16)) - STATES(:,17)) -  (CONSTANTS(:,25)+CONSTANTS(:,35)).*STATES(:,16);
    RATES(:,17) =  CONSTANTS(:,17).*STATES(:,15).*((CONSTANTS(:,7) - STATES(:,16)) - STATES(:,17)) -  (CONSTANTS(:,27)+CONSTANTS(:,37)).*STATES(:,17);
   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