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 =20;
end
% There are a total of 14 entries in each of the rate and state variable arrays.
% There are a total of 24 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('NatP in component NatP (molecules)');
    LEGEND_CONSTANTS(:,23) = strpad('v1 in component v1 (flux)');
    LEGEND_ALGEBRAIC(:,1) = strpad('v2 in component v2 (flux)');
    LEGEND_ALGEBRAIC(:,5) = strpad('v5 in component v5 (flux)');
    LEGEND_STATES(:,2) = strpad('MisP in component MisP (molecules)');
    LEGEND_ALGEBRAIC(:,2) = strpad('v3 in component v3 (flux)');
    LEGEND_ALGEBRAIC(:,4) = strpad('v4 in component v4 (flux)');
    LEGEND_ALGEBRAIC(:,6) = strpad('v6 in component v6 (flux)');
    LEGEND_ALGEBRAIC(:,7) = strpad('v7 in component v7 (flux)');
    LEGEND_STATES(:,3) = strpad('MCom in component MCom (molecules)');
    LEGEND_STATES(:,4) = strpad('Hsp90 in component Hsp90 (molecules)');
    LEGEND_ALGEBRAIC(:,9) = strpad('v9 in component v9 (flux)');
    LEGEND_ALGEBRAIC(:,17) = strpad('v16 in component v16 (flux)');
    LEGEND_ALGEBRAIC(:,8) = strpad('v8 in component v8 (flux)');
    LEGEND_ALGEBRAIC(:,18) = strpad('v17 in component v17 (flux)');
    LEGEND_STATES(:,5) = strpad('HCom in component HCom (molecules)');
    LEGEND_STATES(:,6) = strpad('HSF1 in component HSF1 (molecules)');
    LEGEND_ALGEBRAIC(:,15) = strpad('v13 in component v13 (flux)');
    LEGEND_ALGEBRAIC(:,13) = strpad('v12 in component v12 (flux)');
    LEGEND_ALGEBRAIC(:,11) = strpad('v10 in component v10 (flux)');
    LEGEND_ALGEBRAIC(:,12) = strpad('v11 in component v11 (flux)');
    LEGEND_STATES(:,7) = strpad('DiH in component DiH (molecules)');
    LEGEND_STATES(:,8) = strpad('TriH in component TriH (molecules)');
    LEGEND_ALGEBRAIC(:,14) = strpad('v14 in component v14 (flux)');
    LEGEND_ALGEBRAIC(:,16) = strpad('v15 in component v15 (flux)');
    LEGEND_STATES(:,9) = strpad('HSE_TriH in component HSE_TriH (molecules)');
    LEGEND_STATES(:,10) = strpad('HSE in component HSE (molecules)');
    LEGEND_STATES(:,11) = strpad('AggP in component AggP (molecules)');
    LEGEND_ALGEBRAIC(:,10) = strpad('v7b in component v7b (flux)');
    LEGEND_STATES(:,12) = strpad('ROS in component ROS (molecules)');
    LEGEND_CONSTANTS(:,24) = strpad('v20 in component v20 (flux)');
    LEGEND_ALGEBRAIC(:,3) = strpad('v21 in component v21 (flux)');
    LEGEND_STATES(:,13) = strpad('ATP in component ATP (molecules)');
    LEGEND_ALGEBRAIC(:,19) = strpad('v18 in component v18 (flux)');
    LEGEND_ALGEBRAIC(:,20) = strpad('v19 in component v19 (flux)');
    LEGEND_STATES(:,14) = strpad('ADP in component ADP (molecules)');
    LEGEND_CONSTANTS(:,1) = strpad('k1 in component v1 (flux)');
    LEGEND_CONSTANTS(:,2) = strpad('k2 in component v2 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,3) = strpad('k3 in component v3 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,4) = strpad('k4 in component v4 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,5) = strpad('k5 in component v5 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('k6 in component v6 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('k7 in component v7 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('k7 in component v7b (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('k8 in component v8 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('k9 in component v9 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('k10 in component v10 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('k11 in component v11 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('k12 in component v12 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('k13 in component v13 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,15) = strpad('k14 in component v14 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('k15 in component v15 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('k16 in component v16 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,18) = strpad('k17 in component v17 (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,19) = strpad('k18 in component v18 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,20) = strpad('k19 in component v19 (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,21) = strpad('k20 in component v20 (flux)');
    LEGEND_CONSTANTS(:,22) = strpad('k21 in component v21 (first_order_rate_constant)');
    LEGEND_RATES(:,1) = strpad('d/dt NatP in component NatP (molecules)');
    LEGEND_RATES(:,2) = strpad('d/dt MisP in component MisP (molecules)');
    LEGEND_RATES(:,3) = strpad('d/dt MCom in component MCom (molecules)');
    LEGEND_RATES(:,4) = strpad('d/dt Hsp90 in component Hsp90 (molecules)');
    LEGEND_RATES(:,5) = strpad('d/dt HCom in component HCom (molecules)');
    LEGEND_RATES(:,6) = strpad('d/dt HSF1 in component HSF1 (molecules)');
    LEGEND_RATES(:,7) = strpad('d/dt DiH in component DiH (molecules)');
    LEGEND_RATES(:,8) = strpad('d/dt TriH in component TriH (molecules)');
    LEGEND_RATES(:,9) = strpad('d/dt HSE_TriH in component HSE_TriH (molecules)');
    LEGEND_RATES(:,10) = strpad('d/dt HSE in component HSE (molecules)');
    LEGEND_RATES(:,11) = strpad('d/dt AggP in component AggP (molecules)');
    LEGEND_RATES(:,12) = strpad('d/dt ROS in component ROS (molecules)');
    LEGEND_RATES(:,13) = strpad('d/dt ATP in component ATP (molecules)');
    LEGEND_RATES(:,14) = strpad('d/dt ADP in component ADP (molecules)');
    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) = 6000000;
    STATES(:,2) = 0;
    STATES(:,3) = 0;
    STATES(:,4) = 300000;
    STATES(:,5) = 5900;
    STATES(:,6) = 100;
    STATES(:,7) = 0;
    STATES(:,8) = 0;
    STATES(:,9) = 0;
    STATES(:,10) = 1;
    STATES(:,11) = 0;
    STATES(:,12) = 100;
    STATES(:,13) = 10000;
    STATES(:,14) = 1000;
    CONSTANTS(:,1) = 10;
    CONSTANTS(:,2) = 0.00002;
    CONSTANTS(:,3) = 50;
    CONSTANTS(:,4) = 0.00001;
    CONSTANTS(:,5) = 4e-6;
    CONSTANTS(:,6) = 6e-7;
    CONSTANTS(:,7) = 1e-7;
    CONSTANTS(:,8) = 1e-7;
    CONSTANTS(:,9) = 500;
    CONSTANTS(:,10) = 1;
    CONSTANTS(:,11) = 0.01;
    CONSTANTS(:,12) = 100;
    CONSTANTS(:,13) = 0.5;
    CONSTANTS(:,14) = 0.5;
    CONSTANTS(:,15) = 0.05;
    CONSTANTS(:,16) = 0.08;
    CONSTANTS(:,17) = 1000;
    CONSTANTS(:,18) = 8.02e-9;
    CONSTANTS(:,19) = 12;
    CONSTANTS(:,20) = 0.02;
    CONSTANTS(:,21) = 0.1;
    CONSTANTS(:,22) = 0.00001;
    CONSTANTS(:,23) = CONSTANTS(:,1);
    CONSTANTS(:,24) = CONSTANTS(:,21);
    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
    ALGEBRAIC(:,1) =  CONSTANTS(:,2).*STATES(:,1).*STATES(:,12);
    ALGEBRAIC(:,3) =  CONSTANTS(:,22).*STATES(:,12);
    RATES(:,12) = (ALGEBRAIC(:,1)+CONSTANTS(:,24)) - (ALGEBRAIC(:,1)+ALGEBRAIC(:,3));
    ALGEBRAIC(:,5) =  CONSTANTS(:,5).*STATES(:,3).*STATES(:,13);
    RATES(:,1) = (CONSTANTS(:,23)+ALGEBRAIC(:,5)) - ALGEBRAIC(:,1);
    ALGEBRAIC(:,2) =  CONSTANTS(:,3).*STATES(:,2).*STATES(:,4);
    ALGEBRAIC(:,4) =  CONSTANTS(:,4).*STATES(:,3);
    RATES(:,3) = ALGEBRAIC(:,2) - (ALGEBRAIC(:,4)+ALGEBRAIC(:,5));
    ALGEBRAIC(:,6) =  CONSTANTS(:,6).*STATES(:,2).*STATES(:,13);
    ALGEBRAIC(:,7) =  CONSTANTS(:,7).*STATES(:,2).*STATES(:,2);
    RATES(:,2) = (ALGEBRAIC(:,1)+ALGEBRAIC(:,4)) - (ALGEBRAIC(:,2)+ALGEBRAIC(:,6)+ 2.00000.*ALGEBRAIC(:,7));
    ALGEBRAIC(:,9) =  CONSTANTS(:,10).*STATES(:,5);
    ALGEBRAIC(:,8) =  CONSTANTS(:,9).*STATES(:,6).*STATES(:,4);
    RATES(:,5) = ALGEBRAIC(:,8) - ALGEBRAIC(:,9);
    ALGEBRAIC(:,10) =  CONSTANTS(:,8).*STATES(:,2).*STATES(:,11);
    RATES(:,11) = (ALGEBRAIC(:,7)+ 2.00000.*ALGEBRAIC(:,10)) - ALGEBRAIC(:,10);
    ALGEBRAIC(:,15) =  CONSTANTS(:,14).*STATES(:,7);
    ALGEBRAIC(:,13) =  CONSTANTS(:,13).*STATES(:,8);
    ALGEBRAIC(:,11) =  CONSTANTS(:,11).*STATES(:,6).*STATES(:,6);
    ALGEBRAIC(:,12) =  CONSTANTS(:,12).*STATES(:,7).*STATES(:,6);
    RATES(:,6) = (ALGEBRAIC(:,9)+ 2.00000.*ALGEBRAIC(:,15)+ALGEBRAIC(:,13)) - (ALGEBRAIC(:,8)+ 2.00000.*ALGEBRAIC(:,11)+ALGEBRAIC(:,12));
    RATES(:,7) = (ALGEBRAIC(:,11)+ALGEBRAIC(:,13)) - (ALGEBRAIC(:,12)+ALGEBRAIC(:,15));
    ALGEBRAIC(:,14) =  CONSTANTS(:,15).*STATES(:,10).*STATES(:,8);
    ALGEBRAIC(:,16) =  CONSTANTS(:,16).*STATES(:,9);
    RATES(:,8) = (ALGEBRAIC(:,12)+ALGEBRAIC(:,16)) - (ALGEBRAIC(:,13)+ALGEBRAIC(:,14));
    RATES(:,10) = ALGEBRAIC(:,16) - ALGEBRAIC(:,14);
    ALGEBRAIC(:,17) =  CONSTANTS(:,17).*STATES(:,9);
    RATES(:,9) = (ALGEBRAIC(:,14)+ALGEBRAIC(:,17)) - (ALGEBRAIC(:,16)+ALGEBRAIC(:,17));
    ALGEBRAIC(:,18) =  CONSTANTS(:,18).*STATES(:,4).*STATES(:,13);
    RATES(:,4) = (ALGEBRAIC(:,5)+ALGEBRAIC(:,4)+ALGEBRAIC(:,9)+ALGEBRAIC(:,17)) - (ALGEBRAIC(:,2)+ALGEBRAIC(:,8)+ALGEBRAIC(:,18));
    ALGEBRAIC(:,19) =  CONSTANTS(:,19).*STATES(:,14);
    ALGEBRAIC(:,20) =  CONSTANTS(:,20).*STATES(:,13);
    RATES(:,13) = ALGEBRAIC(:,19) - (ALGEBRAIC(:,5)+ALGEBRAIC(:,6)+ALGEBRAIC(:,18)+ALGEBRAIC(:,20));
    RATES(:,14) = (ALGEBRAIC(:,5)+ALGEBRAIC(:,6)+ALGEBRAIC(:,18)+ALGEBRAIC(:,20)) - ALGEBRAIC(:,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) =  CONSTANTS(:,2).*STATES(:,1).*STATES(:,12);
    ALGEBRAIC(:,3) =  CONSTANTS(:,22).*STATES(:,12);
    ALGEBRAIC(:,5) =  CONSTANTS(:,5).*STATES(:,3).*STATES(:,13);
    ALGEBRAIC(:,2) =  CONSTANTS(:,3).*STATES(:,2).*STATES(:,4);
    ALGEBRAIC(:,4) =  CONSTANTS(:,4).*STATES(:,3);
    ALGEBRAIC(:,6) =  CONSTANTS(:,6).*STATES(:,2).*STATES(:,13);
    ALGEBRAIC(:,7) =  CONSTANTS(:,7).*STATES(:,2).*STATES(:,2);
    ALGEBRAIC(:,9) =  CONSTANTS(:,10).*STATES(:,5);
    ALGEBRAIC(:,8) =  CONSTANTS(:,9).*STATES(:,6).*STATES(:,4);
    ALGEBRAIC(:,10) =  CONSTANTS(:,8).*STATES(:,2).*STATES(:,11);
    ALGEBRAIC(:,15) =  CONSTANTS(:,14).*STATES(:,7);
    ALGEBRAIC(:,13) =  CONSTANTS(:,13).*STATES(:,8);
    ALGEBRAIC(:,11) =  CONSTANTS(:,11).*STATES(:,6).*STATES(:,6);
    ALGEBRAIC(:,12) =  CONSTANTS(:,12).*STATES(:,7).*STATES(:,6);
    ALGEBRAIC(:,14) =  CONSTANTS(:,15).*STATES(:,10).*STATES(:,8);
    ALGEBRAIC(:,16) =  CONSTANTS(:,16).*STATES(:,9);
    ALGEBRAIC(:,17) =  CONSTANTS(:,17).*STATES(:,9);
    ALGEBRAIC(:,18) =  CONSTANTS(:,18).*STATES(:,4).*STATES(:,13);
    ALGEBRAIC(:,19) =  CONSTANTS(:,19).*STATES(:,14);
    ALGEBRAIC(:,20) =  CONSTANTS(:,20).*STATES(:,13);
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