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 =21;
end
% There are a total of 20 entries in each of the rate and state variable arrays.
% There are a total of 50 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_ALGEBRAIC(:,6) = strpad('LRG in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_ALGEBRAIC(:,8) = strpad('RG in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_ALGEBRAIC(:,1) = strpad('LR in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_STATES(:,1) = strpad('beta1_AR in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_STATES(:,2) = strpad('Gs in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_STATES(:,3) = strpad('beta1_ARact in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_STATES(:,4) = strpad('beta1_AR_S464 in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_STATES(:,5) = strpad('beta1_AR_S301 in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_STATES(:,6) = strpad('L in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_CONSTANTS(:,1) = strpad('Ltotmax in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_CONSTANTS(:,2) = strpad('KL in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_CONSTANTS(:,3) = strpad('KR in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_CONSTANTS(:,4) = strpad('KC in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_CONSTANTS(:,5) = strpad('Gstot in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_CONSTANTS(:,6) = strpad('k_betaARK_plus in component beta_adrenergic_receptor_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('k_betaARK_minus in component beta_adrenergic_receptor_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('k_PKA_plus in component beta_adrenergic_receptor_module (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('k_PKA_minus in component beta_adrenergic_receptor_module (first_order_rate_constant)');
    LEGEND_STATES(:,7) = strpad('Gs_beta_gamma in component Gs_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,10) = strpad('PKACI in component PKA_activation_module (micromolar)');
    LEGEND_STATES(:,8) = strpad('Gs_alpha_GTPtot in component Gs_activation_module (micromolar)');
    LEGEND_STATES(:,9) = strpad('Gs_alpha_GDP in component Gs_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,10) = strpad('k_gact in component Gs_activation_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('k_hyd in component Gs_activation_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('k_reassoc in component Gs_activation_module (second_order_rate_constant)');
    LEGEND_STATES(:,10) = strpad('cAMPtot in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_STATES(:,11) = strpad('AC in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_STATES(:,12) = strpad('Gs_alpha_GTP in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_ALGEBRAIC(:,3) = strpad('Gs_alpha_GTP_AC in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_STATES(:,13) = strpad('PDE in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,13) = strpad('ACtot in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,14) = strpad('ATP in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,15) = strpad('PDEtot in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_ALGEBRAIC(:,2) = strpad('PDEinhib in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,16) = strpad('IBMXtot in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_STATES(:,14) = strpad('IBMX in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,17) = strpad('fsktot in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_STATES(:,15) = strpad('fsk in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_ALGEBRAIC(:,7) = strpad('fsk_AC in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,18) = strpad('kAC_basal in component cyclic_AMP_metabolism_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,19) = strpad('kAC_fsk in component cyclic_AMP_metabolism_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,20) = strpad('k_PDE in component cyclic_AMP_metabolism_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,21) = strpad('kAC_Gs_alpha in component cyclic_AMP_metabolism_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,22) = strpad('Km_basal in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,23) = strpad('Km_PDE in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,24) = strpad('Km_fsk in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,25) = strpad('K_fsk in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,26) = strpad('Km_Gs_alpha_GTP in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,27) = strpad('K_Gs_alpha in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_CONSTANTS(:,28) = strpad('K_IBMX in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_ALGEBRAIC(:,11) = strpad('cAMP in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,12) = strpad('PKACII in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,13) = strpad('PKAtemp in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,14) = strpad('ARCI in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,15) = strpad('A2RCI in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,16) = strpad('A2RI in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,17) = strpad('A2RCII in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,18) = strpad('A2RII in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,19) = strpad('ARCII in component PKA_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,29) = strpad('Ki_pki in component PKA_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,30) = strpad('PKAItot in component PKA_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,31) = strpad('PKAIItot in component PKA_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,32) = strpad('PKItot in component PKA_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,33) = strpad('KA in component PKA_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,34) = strpad('KB in component PKA_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,35) = strpad('KD in component PKA_activation_module (micromolar)');
    LEGEND_CONSTANTS(:,36) = strpad('KPKI in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,20) = strpad('PKI in component PKA_activation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,4) = strpad('fracPLBp in component phospholamban_regulation_module (dimensionless)');
    LEGEND_ALGEBRAIC(:,21) = strpad('fracPLB in component phospholamban_regulation_module (dimensionless)');
    LEGEND_CONSTANTS(:,37) = strpad('fracPLBo in component phospholamban_regulation_module (dimensionless)');
    LEGEND_STATES(:,16) = strpad('PLBp in component phospholamban_regulation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,9) = strpad('PLB in component phospholamban_regulation_module (micromolar)');
    LEGEND_STATES(:,17) = strpad('Inhib1ptot in component phospholamban_regulation_module (micromolar)');
    LEGEND_STATES(:,18) = strpad('Inhib1 in component phospholamban_regulation_module (micromolar)');
    LEGEND_STATES(:,19) = strpad('Inhib1p in component phospholamban_regulation_module (micromolar)');
    LEGEND_ALGEBRAIC(:,5) = strpad('PP1_Inhib1p in component phospholamban_regulation_module (micromolar)');
    LEGEND_STATES(:,20) = strpad('PP1 in component phospholamban_regulation_module (micromolar)');
    LEGEND_CONSTANTS(:,38) = strpad('PP1tot in component phospholamban_regulation_module (micromolar)');
    LEGEND_CONSTANTS(:,39) = strpad('PLBtot in component phospholamban_regulation_module (micromolar)');
    LEGEND_CONSTANTS(:,40) = strpad('Inhib1tot in component phospholamban_regulation_module (micromolar)');
    LEGEND_CONSTANTS(:,41) = strpad('epsilon in component phospholamban_regulation_module (dimensionless)');
    LEGEND_CONSTANTS(:,42) = strpad('kPKA_PLB in component phospholamban_regulation_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,43) = strpad('KmPKA_PLB in component phospholamban_regulation_module (micromolar)');
    LEGEND_CONSTANTS(:,44) = strpad('kPKA_Inhib1 in component phospholamban_regulation_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,45) = strpad('kPP1_PLB in component phospholamban_regulation_module (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,46) = strpad('KmPP1_PLB in component phospholamban_regulation_module (micromolar)');
    LEGEND_CONSTANTS(:,47) = strpad('KmPKA_Inhib1 in component phospholamban_regulation_module (micromolar)');
    LEGEND_CONSTANTS(:,48) = strpad('VmaxPP2A_Inhib1 in component phospholamban_regulation_module (flux)');
    LEGEND_CONSTANTS(:,49) = strpad('KmPP2A_Inhib1 in component phospholamban_regulation_module (micromolar)');
    LEGEND_CONSTANTS(:,50) = strpad('KInhib1 in component phospholamban_regulation_module (micromolar)');
    LEGEND_RATES(:,6) = strpad('d/dt L in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_RATES(:,2) = strpad('d/dt Gs in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_RATES(:,1) = strpad('d/dt beta1_AR in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_RATES(:,3) = strpad('d/dt beta1_ARact in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_RATES(:,4) = strpad('d/dt beta1_AR_S464 in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_RATES(:,5) = strpad('d/dt beta1_AR_S301 in component beta_adrenergic_receptor_module (micromolar)');
    LEGEND_RATES(:,8) = strpad('d/dt Gs_alpha_GTPtot in component Gs_activation_module (micromolar)');
    LEGEND_RATES(:,7) = strpad('d/dt Gs_beta_gamma in component Gs_activation_module (micromolar)');
    LEGEND_RATES(:,9) = strpad('d/dt Gs_alpha_GDP in component Gs_activation_module (micromolar)');
    LEGEND_RATES(:,12) = strpad('d/dt Gs_alpha_GTP in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_RATES(:,15) = strpad('d/dt fsk in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_RATES(:,11) = strpad('d/dt AC in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_RATES(:,13) = strpad('d/dt PDE in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_RATES(:,14) = strpad('d/dt IBMX in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_RATES(:,10) = strpad('d/dt cAMPtot in component cyclic_AMP_metabolism_module (micromolar)');
    LEGEND_RATES(:,20) = strpad('d/dt PP1 in component phospholamban_regulation_module (micromolar)');
    LEGEND_RATES(:,19) = strpad('d/dt Inhib1p in component phospholamban_regulation_module (micromolar)');
    LEGEND_RATES(:,16) = strpad('d/dt PLBp in component phospholamban_regulation_module (micromolar)');
    LEGEND_RATES(:,17) = strpad('d/dt Inhib1ptot in component phospholamban_regulation_module (micromolar)');
    LEGEND_RATES(:,18) = strpad('d/dt Inhib1 in component phospholamban_regulation_module (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.0001;
    STATES(:,2) = 3.182;
    STATES(:,3) = 0.01205;
    STATES(:,4) = 0;
    STATES(:,5) = 1.154e-3;
    STATES(:,6) = 0.988;
    CONSTANTS(:,1) = 1;
    CONSTANTS(:,2) = 0.285;
    CONSTANTS(:,3) = 0.062;
    CONSTANTS(:,4) = 33;
    CONSTANTS(:,5) = 3.83;
    CONSTANTS(:,6) = 1.1e-3;
    CONSTANTS(:,7) = 2.2e-3;
    CONSTANTS(:,8) = 3.6e-3;
    CONSTANTS(:,9) = 2.2e-3;
    STATES(:,7) = 0.02569;
    STATES(:,8) = 0.02505;
    STATES(:,9) = 6.44e-4;
    CONSTANTS(:,10) = 16;
    CONSTANTS(:,11) = 0.8;
    CONSTANTS(:,12) = 1.21e3;
    STATES(:,10) = 0.8453;
    STATES(:,11) = 0.04706295;
    STATES(:,12) = 0.02241295;
    STATES(:,13) = 0.0389;
    CONSTANTS(:,13) = 49.7e-3;
    CONSTANTS(:,14) = 5e3;
    CONSTANTS(:,15) = 38.9e-3;
    CONSTANTS(:,16) = 0;
    STATES(:,14) = 0;
    CONSTANTS(:,17) = 0;
    STATES(:,15) = 0;
    CONSTANTS(:,18) = 0.2;
    CONSTANTS(:,19) = 7.3;
    CONSTANTS(:,20) = 5;
    CONSTANTS(:,21) = 8.5;
    CONSTANTS(:,22) = 1.03e3;
    CONSTANTS(:,23) = 1.3;
    CONSTANTS(:,24) = 860;
    CONSTANTS(:,25) = 44;
    CONSTANTS(:,26) = 0.4;
    CONSTANTS(:,27) = 0.4;
    CONSTANTS(:,28) = 30;
    CONSTANTS(:,29) = 0.2e-3;
    CONSTANTS(:,30) = 0.59;
    CONSTANTS(:,31) = 0.025;
    CONSTANTS(:,32) = 0.18;
    CONSTANTS(:,33) = 9.14e-3;
    CONSTANTS(:,34) = 1.64e-3;
    CONSTANTS(:,35) = 4.375e-3;
    CONSTANTS(:,36) = 2e-4;
    CONSTANTS(:,37) = 0.9613;
    STATES(:,16) = 4.105;
    STATES(:,17) = 0.0526;
    STATES(:,18) = 0.2474;
    STATES(:,19) = 6.27339e-5;
    STATES(:,20) = 0.8374627;
    CONSTANTS(:,38) = 0.89;
    CONSTANTS(:,39) = 106;
    CONSTANTS(:,40) = 0.3;
    CONSTANTS(:,41) = 10;
    CONSTANTS(:,42) = 54;
    CONSTANTS(:,43) = 21;
    CONSTANTS(:,44) = 60;
    CONSTANTS(:,45) = 8.5;
    CONSTANTS(:,46) = 7;
    CONSTANTS(:,47) = 1;
    CONSTANTS(:,48) = 14;
    CONSTANTS(:,49) = 1;
    CONSTANTS(:,50) = 1e-3;
    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(:,9) =  CONSTANTS(:,11).*STATES(:,8) -  CONSTANTS(:,12).*STATES(:,9).*STATES(:,7);
    ALGEBRAIC(:,3) = ( STATES(:,12).*STATES(:,11))./CONSTANTS(:,27);
    RATES(:,12) = STATES(:,8) - (ALGEBRAIC(:,3)+STATES(:,12));
    RATES(:,11) = CONSTANTS(:,13) - (ALGEBRAIC(:,3)+STATES(:,11));
    ALGEBRAIC(:,2) = ( STATES(:,13).*STATES(:,14))./CONSTANTS(:,28);
    RATES(:,13) = CONSTANTS(:,15) - (ALGEBRAIC(:,2)+STATES(:,13));
    RATES(:,14) = CONSTANTS(:,16) - (ALGEBRAIC(:,2)+STATES(:,14));
    ALGEBRAIC(:,5) = ( STATES(:,20).*STATES(:,19))./CONSTANTS(:,50);
    RATES(:,20) = CONSTANTS(:,38) - (ALGEBRAIC(:,5)+STATES(:,20));
    RATES(:,19) = STATES(:,17) - (ALGEBRAIC(:,5)+STATES(:,19));
    ALGEBRAIC(:,6) = ( STATES(:,6).*STATES(:,1).*STATES(:,2))./( CONSTANTS(:,2).*CONSTANTS(:,3));
    ALGEBRAIC(:,1) = ( STATES(:,6).*STATES(:,1))./CONSTANTS(:,2);
    RATES(:,6) = CONSTANTS(:,1) - (ALGEBRAIC(:,1)+ALGEBRAIC(:,6)+STATES(:,6));
    RATES(:,4) =  CONSTANTS(:,6).*(ALGEBRAIC(:,1)+ALGEBRAIC(:,6)) -  CONSTANTS(:,7).*STATES(:,4);
    ALGEBRAIC(:,7) = ( STATES(:,15).*STATES(:,11))./CONSTANTS(:,25);
    RATES(:,15) = CONSTANTS(:,17) - (ALGEBRAIC(:,7)+STATES(:,15));
    RATES(:,10) = (( CONSTANTS(:,18).*STATES(:,11).*CONSTANTS(:,14))./(CONSTANTS(:,22)+CONSTANTS(:,14))+( CONSTANTS(:,21).*ALGEBRAIC(:,3).*CONSTANTS(:,14))./(CONSTANTS(:,26)+CONSTANTS(:,14))+( CONSTANTS(:,19).*ALGEBRAIC(:,7).*CONSTANTS(:,14))./(CONSTANTS(:,24)+CONSTANTS(:,14))) - ( CONSTANTS(:,20).*STATES(:,13).*STATES(:,10))./(CONSTANTS(:,23)+STATES(:,10));
    ALGEBRAIC(:,8) = ( STATES(:,1).*STATES(:,2))./CONSTANTS(:,4);
    RATES(:,2) = CONSTANTS(:,5) - (ALGEBRAIC(:,8)+ALGEBRAIC(:,6)+STATES(:,2));
    RATES(:,1) = STATES(:,3) - (ALGEBRAIC(:,1)+ALGEBRAIC(:,6)+ALGEBRAIC(:,8)+STATES(:,1));
    RATES(:,8) =  CONSTANTS(:,10).*(ALGEBRAIC(:,8)+ALGEBRAIC(:,6)) -  CONSTANTS(:,11).*STATES(:,8);
    RATES(:,7) =  CONSTANTS(:,10).*(ALGEBRAIC(:,8)+ALGEBRAIC(:,6)) -  CONSTANTS(:,12).*STATES(:,9).*STATES(:,7);
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
    RATES(:,3) = ( CONSTANTS(:,7).*STATES(:,4) -  CONSTANTS(:,6).*(ALGEBRAIC(:,1)+ALGEBRAIC(:,6)))+( CONSTANTS(:,9).*STATES(:,5) -  CONSTANTS(:,8).*ALGEBRAIC(:,10).*STATES(:,3));
    RATES(:,5) =  CONSTANTS(:,8).*ALGEBRAIC(:,10).*STATES(:,3) -  CONSTANTS(:,9).*STATES(:,5);
    ALGEBRAIC(:,9) = CONSTANTS(:,39) - STATES(:,16);
    RATES(:,16) = ( CONSTANTS(:,42).*ALGEBRAIC(:,10).*ALGEBRAIC(:,9))./(CONSTANTS(:,43)+ALGEBRAIC(:,9)) - ( CONSTANTS(:,45).*STATES(:,20).*STATES(:,16))./(CONSTANTS(:,46)+STATES(:,16));
    RATES(:,17) = ( CONSTANTS(:,44).*ALGEBRAIC(:,10).*STATES(:,17))./(CONSTANTS(:,47)+STATES(:,17)) - ( CONSTANTS(:,48).*STATES(:,18))./(CONSTANTS(:,49)+STATES(:,18));
    RATES(:,18) = ( CONSTANTS(:,48).*STATES(:,18))./(CONSTANTS(:,49)+STATES(:,18)) - ( CONSTANTS(:,44).*ALGEBRAIC(:,10).*STATES(:,17))./(CONSTANTS(:,47)+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
    ALGEBRAIC(:,3) = ( STATES(:,12).*STATES(:,11))./CONSTANTS(:,27);
    ALGEBRAIC(:,2) = ( STATES(:,13).*STATES(:,14))./CONSTANTS(:,28);
    ALGEBRAIC(:,5) = ( STATES(:,20).*STATES(:,19))./CONSTANTS(:,50);
    ALGEBRAIC(:,6) = ( STATES(:,6).*STATES(:,1).*STATES(:,2))./( CONSTANTS(:,2).*CONSTANTS(:,3));
    ALGEBRAIC(:,1) = ( STATES(:,6).*STATES(:,1))./CONSTANTS(:,2);
    ALGEBRAIC(:,7) = ( STATES(:,15).*STATES(:,11))./CONSTANTS(:,25);
    ALGEBRAIC(:,8) = ( STATES(:,1).*STATES(:,2))./CONSTANTS(:,4);
    ALGEBRAIC(:,9) = CONSTANTS(:,39) - STATES(:,16);
    ALGEBRAIC(:,4) = STATES(:,16)./CONSTANTS(:,39);
    ALGEBRAIC(:,21) = ALGEBRAIC(:,9)./CONSTANTS(:,39);
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
    ALGEBRAIC = ALGEBRAIC_IN;
    CONSTANTS = CONSTANTS_IN;
    STATES = STATES_IN;
    global initialGuess_0;
    if (length(initialGuess_0) ~= 11), initialGuess_0 = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1];, end
    options = optimset('Display', 'off', 'TolX', 1E-6);
    if length(VOI) == 1
        residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
        soln = fsolve(residualfn, initialGuess_0, options);
        initialGuess_0 = soln;
        ALGEBRAIC(:,10) = soln(1);
        ALGEBRAIC(:,11) = soln(2);
        ALGEBRAIC(:,12) = soln(3);
        ALGEBRAIC(:,13) = soln(4);
        ALGEBRAIC(:,14) = soln(5);
        ALGEBRAIC(:,15) = soln(6);
        ALGEBRAIC(:,16) = soln(7);
        ALGEBRAIC(:,17) = soln(8);
        ALGEBRAIC(:,18) = soln(9);
        ALGEBRAIC(:,19) = soln(10);
        ALGEBRAIC(:,20) = soln(11);
    else
        SET_ALGEBRAIC(:,10) = logical(1);
        SET_ALGEBRAIC(:,11) = logical(1);
        SET_ALGEBRAIC(:,12) = logical(1);
        SET_ALGEBRAIC(:,13) = logical(1);
        SET_ALGEBRAIC(:,14) = logical(1);
        SET_ALGEBRAIC(:,15) = logical(1);
        SET_ALGEBRAIC(:,16) = logical(1);
        SET_ALGEBRAIC(:,17) = logical(1);
        SET_ALGEBRAIC(:,18) = logical(1);
        SET_ALGEBRAIC(:,19) = logical(1);
        SET_ALGEBRAIC(:,20) = logical(1);
        for i=1:length(VOI)
            residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
            soln = fsolve(residualfn, initialGuess_0, options);
            initialGuess_0 = soln;
            TEMP_ALGEBRAIC(:,10) = soln(1);
            TEMP_ALGEBRAIC(:,11) = soln(2);
            TEMP_ALGEBRAIC(:,12) = soln(3);
            TEMP_ALGEBRAIC(:,13) = soln(4);
            TEMP_ALGEBRAIC(:,14) = soln(5);
            TEMP_ALGEBRAIC(:,15) = soln(6);
            TEMP_ALGEBRAIC(:,16) = soln(7);
            TEMP_ALGEBRAIC(:,17) = soln(8);
            TEMP_ALGEBRAIC(:,18) = soln(9);
            TEMP_ALGEBRAIC(:,19) = soln(10);
            TEMP_ALGEBRAIC(:,20) = soln(11);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    ALGEBRAIC(:,10) = algebraicCandidate(1);
    ALGEBRAIC(:,11) = algebraicCandidate(2);
    ALGEBRAIC(:,12) = algebraicCandidate(3);
    ALGEBRAIC(:,13) = algebraicCandidate(4);
    ALGEBRAIC(:,14) = algebraicCandidate(5);
    ALGEBRAIC(:,15) = algebraicCandidate(6);
    ALGEBRAIC(:,16) = algebraicCandidate(7);
    ALGEBRAIC(:,17) = algebraicCandidate(8);
    ALGEBRAIC(:,18) = algebraicCandidate(9);
    ALGEBRAIC(:,19) = algebraicCandidate(10);
    ALGEBRAIC(:,20) = algebraicCandidate(11);
    resid(1) = ALGEBRAIC(:,20) - ( CONSTANTS(:,32).*CONSTANTS(:,29))./(CONSTANTS(:,29)+ALGEBRAIC(:,10)+ALGEBRAIC(:,12));
    resid(2) = ALGEBRAIC(:,15) -  (ALGEBRAIC(:,10)./CONSTANTS(:,35)).*ALGEBRAIC(:,10).*(1.00000+ALGEBRAIC(:,20)./CONSTANTS(:,29));
    resid(3) = ALGEBRAIC(:,16) -  ALGEBRAIC(:,10).*(1.00000+ALGEBRAIC(:,20)./CONSTANTS(:,29));
    resid(4) = ALGEBRAIC(:,17) -  (ALGEBRAIC(:,12)./CONSTANTS(:,35)).*ALGEBRAIC(:,12).*(1.00000+ALGEBRAIC(:,20)./CONSTANTS(:,29));
    resid(5) = ALGEBRAIC(:,18) -  ALGEBRAIC(:,12).*(1.00000+ALGEBRAIC(:,20)./CONSTANTS(:,29));
    resid(6) = ALGEBRAIC(:,14) -  (CONSTANTS(:,33)./ALGEBRAIC(:,11)).*ALGEBRAIC(:,15);
    resid(7) = ALGEBRAIC(:,19) -  (CONSTANTS(:,33)./ALGEBRAIC(:,11)).*ALGEBRAIC(:,17);
    resid(8) = ALGEBRAIC(:,11) - ((STATES(:,10) - (ALGEBRAIC(:,14)+ 2.00000.*ALGEBRAIC(:,15)+ 2.00000.*ALGEBRAIC(:,16))) - (ALGEBRAIC(:,19)+ 2.00000.*ALGEBRAIC(:,17)+ 2.00000.*ALGEBRAIC(:,18)));
    resid(9) = ALGEBRAIC(:,13) - (( CONSTANTS(:,33).*CONSTANTS(:,34))./CONSTANTS(:,35)+( CONSTANTS(:,33).*ALGEBRAIC(:,11))./CONSTANTS(:,35)+( ALGEBRAIC(:,11).*ALGEBRAIC(:,11))./CONSTANTS(:,35));
    resid(10) = ALGEBRAIC(:,10) - ( 2.00000.*CONSTANTS(:,30).*ALGEBRAIC(:,11).*ALGEBRAIC(:,11) -  ALGEBRAIC(:,10).*(1.00000+ALGEBRAIC(:,20)./CONSTANTS(:,29)).*( ALGEBRAIC(:,13).*ALGEBRAIC(:,10)+ ALGEBRAIC(:,11).*ALGEBRAIC(:,11)));
    resid(11) = ALGEBRAIC(:,12) - ( 2.00000.*CONSTANTS(:,31).*ALGEBRAIC(:,11).*ALGEBRAIC(:,11) -  ALGEBRAIC(:,12).*(1.00000+ALGEBRAIC(:,20)./CONSTANTS(:,29)).*( ALGEBRAIC(:,13).*ALGEBRAIC(:,12)+ ALGEBRAIC(:,11).*ALGEBRAIC(:,11)));
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