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 =9;
end
% There are a total of 22 entries in each of the rate and state variable arrays.
% There are a total of 45 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('T2 in component T2 (micromolar)');
    LEGEND_ALGEBRAIC(:,1) = strpad('Vmax in component T2 (flux)');
    LEGEND_CONSTANTS(:,38) = strpad('KR in component T2 (molar)');
    LEGEND_STATES(:,2) = strpad('LT2 in component LT2 (micromolar)');
    LEGEND_STATES(:,3) = strpad('T2R in component T2R (micromolar)');
    LEGEND_CONSTANTS(:,1) = strpad('R in component R (micromolar)');
    LEGEND_STATES(:,4) = strpad('T3 in component T3 (micromolar)');
    LEGEND_CONSTANTS(:,2) = strpad('L in component L (micromolar)');
    LEGEND_STATES(:,5) = strpad('Bp in component Bp (micromolar)');
    LEGEND_STATES(:,6) = strpad('T2p in component T2p (micromolar)');
    LEGEND_CONSTANTS(:,3) = strpad('Y0 in component Y0 (micromolar)');
    LEGEND_STATES(:,7) = strpad('Yp in component Yp (micromolar)');
    LEGEND_CONSTANTS(:,4) = strpad('B0 in component B0 (micromolar)');
    LEGEND_CONSTANTS(:,5) = strpad('k5 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('k_5 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('k8 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('k_1 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('ky in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('kb in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('k1a in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('k1b in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('k1c in component model_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,2) = strpad('Vmax in component T3 (flux)');
    LEGEND_CONSTANTS(:,39) = strpad('KR in component T3 (molar)');
    LEGEND_STATES(:,8) = strpad('LT3 in component LT3 (micromolar)');
    LEGEND_STATES(:,9) = strpad('T3R in component T3R (micromolar)');
    LEGEND_STATES(:,10) = strpad('T4 in component T4 (micromolar)');
    LEGEND_STATES(:,11) = strpad('T3p in component T3p (micromolar)');
    LEGEND_CONSTANTS(:,14) = strpad('k6 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,15) = strpad('k_6 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('k9 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('k_2 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,18) = strpad('k2a in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,19) = strpad('k2b in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,20) = strpad('k2c in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,12) = strpad('LT4 in component LT4 (micromolar)');
    LEGEND_STATES(:,13) = strpad('T4p in component T4p (micromolar)');
    LEGEND_CONSTANTS(:,21) = strpad('k7 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,22) = strpad('k_7 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,23) = strpad('k10 in component model_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,3) = strpad('Vmax in component T2p (flux)');
    LEGEND_CONSTANTS(:,40) = strpad('KR in component T2p (molar)');
    LEGEND_STATES(:,14) = strpad('LT2p in component LT2p (micromolar)');
    LEGEND_STATES(:,15) = strpad('T2pR in component T2pR (micromolar)');
    LEGEND_ALGEBRAIC(:,4) = strpad('Vmax in component T3p (flux)');
    LEGEND_CONSTANTS(:,41) = strpad('KR in component T3p (molar)');
    LEGEND_STATES(:,16) = strpad('LT3p in component LT3p (micromolar)');
    LEGEND_STATES(:,17) = strpad('T3pR in component T3pR (micromolar)');
    LEGEND_STATES(:,18) = strpad('LT4p in component LT4p (micromolar)');
    LEGEND_ALGEBRAIC(:,5) = strpad('Vmax in component LT2 (flux)');
    LEGEND_CONSTANTS(:,42) = strpad('KR in component LT2 (molar)');
    LEGEND_STATES(:,19) = strpad('LT2R in component LT2R (micromolar)');
    LEGEND_CONSTANTS(:,24) = strpad('k11 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,25) = strpad('k_3 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,26) = strpad('k3a in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,27) = strpad('k3b in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,28) = strpad('k3c in component model_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,6) = strpad('Vmax in component LT3 (flux)');
    LEGEND_CONSTANTS(:,43) = strpad('KR in component LT3 (molar)');
    LEGEND_STATES(:,20) = strpad('LT3R in component LT3R (micromolar)');
    LEGEND_CONSTANTS(:,29) = strpad('k12 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,30) = strpad('k_4 in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,31) = strpad('k4a in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,32) = strpad('k4b in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,33) = strpad('k4c in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,34) = strpad('k13 in component model_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,7) = strpad('Vmax in component LT2p (flux)');
    LEGEND_CONSTANTS(:,44) = strpad('KR in component LT2p (molar)');
    LEGEND_STATES(:,21) = strpad('LT2pR in component LT2pR (micromolar)');
    LEGEND_ALGEBRAIC(:,8) = strpad('Vmax in component LT3p (flux)');
    LEGEND_CONSTANTS(:,45) = strpad('KR in component LT3p (molar)');
    LEGEND_STATES(:,22) = strpad('LT3pR in component LT3pR (micromolar)');
    LEGEND_ALGEBRAIC(:,9) = strpad('P in component P (micromolar)');
    LEGEND_CONSTANTS(:,35) = strpad('Z in component Z (micromolar)');
    LEGEND_CONSTANTS(:,36) = strpad('k_y in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,37) = strpad('k_b in component model_parameters (first_order_rate_constant)');
    LEGEND_RATES(:,1) = strpad('d/dt T2 in component T2 (micromolar)');
    LEGEND_RATES(:,4) = strpad('d/dt T3 in component T3 (micromolar)');
    LEGEND_RATES(:,10) = strpad('d/dt T4 in component T4 (micromolar)');
    LEGEND_RATES(:,6) = strpad('d/dt T2p in component T2p (micromolar)');
    LEGEND_RATES(:,11) = strpad('d/dt T3p in component T3p (micromolar)');
    LEGEND_RATES(:,13) = strpad('d/dt T4p in component T4p (micromolar)');
    LEGEND_RATES(:,2) = strpad('d/dt LT2 in component LT2 (micromolar)');
    LEGEND_RATES(:,8) = strpad('d/dt LT3 in component LT3 (micromolar)');
    LEGEND_RATES(:,12) = strpad('d/dt LT4 in component LT4 (micromolar)');
    LEGEND_RATES(:,14) = strpad('d/dt LT2p in component LT2p (micromolar)');
    LEGEND_RATES(:,16) = strpad('d/dt LT3p in component LT3p (micromolar)');
    LEGEND_RATES(:,18) = strpad('d/dt LT4p in component LT4p (micromolar)');
    LEGEND_RATES(:,7) = strpad('d/dt Yp in component Yp (micromolar)');
    LEGEND_RATES(:,5) = strpad('d/dt Bp in component Bp (micromolar)');
    LEGEND_RATES(:,3) = strpad('d/dt T2R in component T2R (micromolar)');
    LEGEND_RATES(:,9) = strpad('d/dt T3R in component T3R (micromolar)');
    LEGEND_RATES(:,15) = strpad('d/dt T2pR in component T2pR (micromolar)');
    LEGEND_RATES(:,17) = strpad('d/dt T3pR in component T3pR (micromolar)');
    LEGEND_RATES(:,19) = strpad('d/dt LT2R in component LT2R (micromolar)');
    LEGEND_RATES(:,20) = strpad('d/dt LT3R in component LT3R (micromolar)');
    LEGEND_RATES(:,21) = strpad('d/dt LT2pR in component LT2pR (micromolar)');
    LEGEND_RATES(:,22) = strpad('d/dt LT3pR in component LT3pR (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 = [];
    STATES(:,1) = 0.01;
    STATES(:,2) = 0.01;
    STATES(:,3) = 0.01;
    CONSTANTS(:,1) = 0.3;
    STATES(:,4) = 0.01;
    CONSTANTS(:,2) = 3.0;
    STATES(:,5) = 0.01;
    STATES(:,6) = 0.01;
    CONSTANTS(:,3) = 20.0;
    STATES(:,7) = 0.01;
    CONSTANTS(:,4) = 1.7;
    CONSTANTS(:,5) = 7E7;
    CONSTANTS(:,6) = 70.0;
    CONSTANTS(:,7) = 15.0;
    CONSTANTS(:,8) = 4E5;
    CONSTANTS(:,9) = 3E7;
    CONSTANTS(:,10) = 8E5;
    CONSTANTS(:,11) = 10;
    CONSTANTS(:,12) = 17;
    CONSTANTS(:,13) = 0.17;
    STATES(:,8) = 0.01;
    STATES(:,9) = 0.01;
    STATES(:,10) = 0.01;
    STATES(:,11) = 0.01;
    CONSTANTS(:,14) = 7E7;
    CONSTANTS(:,15) = 70.0;
    CONSTANTS(:,16) = 45.0;
    CONSTANTS(:,17) = 3E4;
    CONSTANTS(:,18) = 10;
    CONSTANTS(:,19) = 17;
    CONSTANTS(:,20) = 0.017;
    STATES(:,12) = 0.01;
    STATES(:,13) = 0.01;
    CONSTANTS(:,21) = 7E7;
    CONSTANTS(:,22) = 70.0;
    CONSTANTS(:,23) = 48.0;
    STATES(:,14) = 0.01;
    STATES(:,15) = 0.01;
    STATES(:,16) = 0.01;
    STATES(:,17) = 0.01;
    STATES(:,18) = 0.01;
    STATES(:,19) = 0.01;
    CONSTANTS(:,24) = 0.0;
    CONSTANTS(:,25) = 4E5;
    CONSTANTS(:,26) = 10;
    CONSTANTS(:,27) = 17;
    CONSTANTS(:,28) = 5.1;
    STATES(:,20) = 0.01;
    CONSTANTS(:,29) = 16.5;
    CONSTANTS(:,30) = 3E4;
    CONSTANTS(:,31) = 10;
    CONSTANTS(:,32) = 17;
    CONSTANTS(:,33) = 0.51;
    CONSTANTS(:,34) = 34.56;
    STATES(:,21) = 0.01;
    STATES(:,22) = 0.01;
    CONSTANTS(:,35) = 40.0;
    CONSTANTS(:,36) = 5E5;
    CONSTANTS(:,37) = 0.35;
    CONSTANTS(:,38) = (CONSTANTS(:,11)+CONSTANTS(:,13))./CONSTANTS(:,12);
    CONSTANTS(:,39) = (CONSTANTS(:,18)+CONSTANTS(:,20))./CONSTANTS(:,19);
    CONSTANTS(:,40) = (CONSTANTS(:,11)+CONSTANTS(:,13))./CONSTANTS(:,12);
    CONSTANTS(:,41) = (CONSTANTS(:,18)+CONSTANTS(:,20))./CONSTANTS(:,19);
    CONSTANTS(:,42) = (CONSTANTS(:,26)+CONSTANTS(:,28))./CONSTANTS(:,27);
    CONSTANTS(:,43) = (CONSTANTS(:,31)+CONSTANTS(:,33))./CONSTANTS(:,32);
    CONSTANTS(:,44) = (CONSTANTS(:,26)+CONSTANTS(:,28))./CONSTANTS(:,27);
    CONSTANTS(:,45) = (CONSTANTS(:,31)+CONSTANTS(:,33))./CONSTANTS(:,32);
    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(:,10) = ( CONSTANTS(:,22).*STATES(:,12)+ CONSTANTS(:,9).*STATES(:,13).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,13).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,21).*CONSTANTS(:,2).*STATES(:,10)+ CONSTANTS(:,23).*STATES(:,10));
    RATES(:,13) = ( CONSTANTS(:,22).*STATES(:,18)+ CONSTANTS(:,9).*STATES(:,10).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,10).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,21).*CONSTANTS(:,2).*STATES(:,13)+ CONSTANTS(:,23).*STATES(:,13));
    RATES(:,12) = ( CONSTANTS(:,22).*STATES(:,10)+ CONSTANTS(:,9).*STATES(:,18).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,18).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,21).*CONSTANTS(:,2).*STATES(:,12)+ CONSTANTS(:,34).*STATES(:,12));
    RATES(:,18) = ( CONSTANTS(:,22).*STATES(:,13)+ CONSTANTS(:,9).*STATES(:,18).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,18).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,21).*CONSTANTS(:,2).*STATES(:,18)+ CONSTANTS(:,34).*STATES(:,18));
    RATES(:,3) =  CONSTANTS(:,12).*STATES(:,1).*CONSTANTS(:,1) - ( CONSTANTS(:,11).*STATES(:,3)+ CONSTANTS(:,13).*STATES(:,3));
    RATES(:,9) =  CONSTANTS(:,19).*STATES(:,4).*CONSTANTS(:,1) - ( CONSTANTS(:,18).*STATES(:,9)+ CONSTANTS(:,20).*STATES(:,9));
    RATES(:,15) =  CONSTANTS(:,12).*STATES(:,6).*CONSTANTS(:,1) - ( CONSTANTS(:,11).*STATES(:,15)+ CONSTANTS(:,13).*STATES(:,15));
    RATES(:,17) =  CONSTANTS(:,19).*STATES(:,11).*CONSTANTS(:,1) - ( CONSTANTS(:,18).*STATES(:,17)+ CONSTANTS(:,20).*STATES(:,17));
    RATES(:,19) =  CONSTANTS(:,27).*STATES(:,2).*CONSTANTS(:,1) - ( CONSTANTS(:,26).*STATES(:,19)+ CONSTANTS(:,28).*STATES(:,19));
    RATES(:,20) =  CONSTANTS(:,32).*STATES(:,8).*CONSTANTS(:,1) - ( CONSTANTS(:,31).*STATES(:,20)+ CONSTANTS(:,33).*STATES(:,20));
    RATES(:,21) =  CONSTANTS(:,27).*STATES(:,14).*CONSTANTS(:,1) - ( CONSTANTS(:,26).*STATES(:,21)+ CONSTANTS(:,28).*STATES(:,21));
    RATES(:,22) =  CONSTANTS(:,32).*STATES(:,16).*CONSTANTS(:,1) - ( CONSTANTS(:,31).*STATES(:,22)+ CONSTANTS(:,33).*STATES(:,22));
    ALGEBRAIC(:,1) =  CONSTANTS(:,13).*(CONSTANTS(:,1)+STATES(:,3));
    RATES(:,1) = ( CONSTANTS(:,6).*STATES(:,2)+ CONSTANTS(:,8).*STATES(:,5).*STATES(:,4)+ CONSTANTS(:,9).*STATES(:,6).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,6).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,5).*CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,7).*STATES(:,1)+ ALGEBRAIC(:,1).*(STATES(:,1)./(CONSTANTS(:,38)+STATES(:,1))));
    ALGEBRAIC(:,2) =  CONSTANTS(:,20).*(CONSTANTS(:,1)+STATES(:,9));
    RATES(:,4) = ( CONSTANTS(:,15).*STATES(:,8)+ CONSTANTS(:,17).*STATES(:,5).*STATES(:,10)+ CONSTANTS(:,9).*STATES(:,11).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,11).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,14).*CONSTANTS(:,2).*STATES(:,4)+ CONSTANTS(:,16).*STATES(:,4)+ ALGEBRAIC(:,2).*(STATES(:,4)./(CONSTANTS(:,39)+STATES(:,4))));
    ALGEBRAIC(:,3) =  CONSTANTS(:,13).*(CONSTANTS(:,1)+STATES(:,15));
    RATES(:,6) = ( CONSTANTS(:,6).*STATES(:,14)+ CONSTANTS(:,8).*STATES(:,5).*STATES(:,11)+ CONSTANTS(:,9).*STATES(:,1).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,1).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,5).*CONSTANTS(:,2).*STATES(:,6)+ CONSTANTS(:,7).*STATES(:,6)+ ALGEBRAIC(:,3).*(STATES(:,6)./(CONSTANTS(:,40)+STATES(:,6))));
    ALGEBRAIC(:,4) =  CONSTANTS(:,20).*(CONSTANTS(:,1)+STATES(:,17));
    RATES(:,11) = ( CONSTANTS(:,15).*STATES(:,16)+ CONSTANTS(:,17).*STATES(:,5).*STATES(:,13)+ CONSTANTS(:,9).*STATES(:,4).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,4).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,14).*CONSTANTS(:,2).*STATES(:,11)+ CONSTANTS(:,16).*STATES(:,11)+ ALGEBRAIC(:,4).*(STATES(:,11)./(CONSTANTS(:,41)+STATES(:,11))));
    ALGEBRAIC(:,5) =  CONSTANTS(:,28).*(CONSTANTS(:,1)+STATES(:,19));
    RATES(:,2) = ( CONSTANTS(:,6).*STATES(:,1)+ CONSTANTS(:,25).*STATES(:,5).*STATES(:,8)+ CONSTANTS(:,9).*STATES(:,14).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,14).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,5).*CONSTANTS(:,2).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,2)+ ALGEBRAIC(:,5).*(STATES(:,2)./(CONSTANTS(:,42)+STATES(:,2))));
    ALGEBRAIC(:,6) =  CONSTANTS(:,33).*(CONSTANTS(:,1)+STATES(:,20));
    RATES(:,8) = ( CONSTANTS(:,15).*STATES(:,4)+ CONSTANTS(:,30).*STATES(:,5).*STATES(:,12)+ CONSTANTS(:,9).*STATES(:,16).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,16).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,14).*CONSTANTS(:,2).*STATES(:,8)+ CONSTANTS(:,29).*STATES(:,8)+ ALGEBRAIC(:,6).*(STATES(:,8)./(CONSTANTS(:,43)+STATES(:,8))));
    ALGEBRAIC(:,7) =  CONSTANTS(:,28).*(CONSTANTS(:,1)+STATES(:,21));
    RATES(:,14) = ( CONSTANTS(:,6).*STATES(:,6)+ CONSTANTS(:,25).*STATES(:,5).*STATES(:,16)+ CONSTANTS(:,9).*STATES(:,2).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,2).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,5).*CONSTANTS(:,2).*STATES(:,14)+ CONSTANTS(:,24).*STATES(:,14)+ ALGEBRAIC(:,7).*(STATES(:,14)./(CONSTANTS(:,44)+STATES(:,14))));
    ALGEBRAIC(:,8) =  CONSTANTS(:,33).*(CONSTANTS(:,1)+STATES(:,22));
    RATES(:,16) = ( CONSTANTS(:,15).*STATES(:,11)+ CONSTANTS(:,30).*STATES(:,5).*STATES(:,18)+ CONSTANTS(:,9).*STATES(:,8).*(CONSTANTS(:,3) - STATES(:,7))+ CONSTANTS(:,10).*STATES(:,8).*(CONSTANTS(:,4) - STATES(:,5))) - ( CONSTANTS(:,14).*CONSTANTS(:,2).*STATES(:,16)+ CONSTANTS(:,29).*STATES(:,16)+ ALGEBRAIC(:,8).*(STATES(:,16)./(CONSTANTS(:,45)+STATES(:,16))));
    ALGEBRAIC(:,9) = STATES(:,6)+STATES(:,14)+STATES(:,11)+STATES(:,16)+STATES(:,13)+STATES(:,18);
    RATES(:,7) =  CONSTANTS(:,9).*ALGEBRAIC(:,9).*(CONSTANTS(:,3) - STATES(:,7)) -  CONSTANTS(:,36).*CONSTANTS(:,35).*STATES(:,7);
    RATES(:,5) =  CONSTANTS(:,10).*ALGEBRAIC(:,9).*(CONSTANTS(:,4) - STATES(:,5)) -  CONSTANTS(:,37).*STATES(:,5);
   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(:,13).*(CONSTANTS(:,1)+STATES(:,3));
    ALGEBRAIC(:,2) =  CONSTANTS(:,20).*(CONSTANTS(:,1)+STATES(:,9));
    ALGEBRAIC(:,3) =  CONSTANTS(:,13).*(CONSTANTS(:,1)+STATES(:,15));
    ALGEBRAIC(:,4) =  CONSTANTS(:,20).*(CONSTANTS(:,1)+STATES(:,17));
    ALGEBRAIC(:,5) =  CONSTANTS(:,28).*(CONSTANTS(:,1)+STATES(:,19));
    ALGEBRAIC(:,6) =  CONSTANTS(:,33).*(CONSTANTS(:,1)+STATES(:,20));
    ALGEBRAIC(:,7) =  CONSTANTS(:,28).*(CONSTANTS(:,1)+STATES(:,21));
    ALGEBRAIC(:,8) =  CONSTANTS(:,33).*(CONSTANTS(:,1)+STATES(:,22));
    ALGEBRAIC(:,9) = STATES(:,6)+STATES(:,14)+STATES(:,11)+STATES(:,16)+STATES(:,13)+STATES(:,18);
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