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 =56;
end
% There are a total of 4 entries in each of the rate and state variable arrays.
% There are a total of 72 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 (minute)');
    LEGEND_CONSTANTS(:,1) = strpad('ADHMK in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('AMK in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('AMNA in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('ANM in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('ANPX in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('AUM in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('CKE in component kidney (monovalent_mEq_per_litre)');
    LEGEND_CONSTANTS(:,8) = strpad('CNA in component kidney (monovalent_mEq_per_litre)');
    LEGEND_CONSTANTS(:,9) = strpad('HM1 in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('MYOGRS in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('PA in component kidney (mmHg)');
    LEGEND_CONSTANTS(:,12) = strpad('PAMKRN in component kidney (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('PPC in component kidney (mmHg)');
    LEGEND_CONSTANTS(:,14) = strpad('VTW in component kidney (litre)');
    LEGEND_ALGEBRAIC(:,1) = strpad('PAR in component perfusion_pressure (mmHg)');
    LEGEND_CONSTANTS(:,15) = strpad('GBL in component parameter_values (mmHg)');
    LEGEND_CONSTANTS(:,16) = strpad('RAPRSP in component parameter_values (mmHg)');
    LEGEND_CONSTANTS(:,17) = strpad('RFCDFT in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('RCDFPC in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('RCDFDP in component parameter_values (minute)');
    LEGEND_STATES(:,1) = strpad('PAR1 in component perfusion_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,3) = strpad('MDFLW in component proximal_tubular_and_macula_densa_flow (L_per_minute)');
    LEGEND_ALGEBRAIC(:,4) = strpad('RNAUG2 in component renal_autoregulatory_feedback_factor (dimensionless)');
    LEGEND_CONSTANTS(:,20) = strpad('RNAUGN in component parameter_values (minute_per_L)');
    LEGEND_CONSTANTS(:,21) = strpad('RNAULL in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,22) = strpad('RNAUUL in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('RNAUAD in component parameter_values (per_minute)');
    LEGEND_ALGEBRAIC(:,5) = strpad('RNAUG1 in component renal_autoregulatory_feedback_factor (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('RNAUG1T in component renal_autoregulatory_feedback_factor (dimensionless)');
    LEGEND_STATES(:,2) = strpad('RNAUG3 in component renal_autoregulatory_feedback_factor (dimensionless)');
    LEGEND_CONSTANTS(:,64) = strpad('AUMK in component autonomic_effect_on_AAR (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('ARF in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,63) = strpad('AUMKT in component autonomic_effect_on_AAR (dimensionless)');
    LEGEND_CONSTANTS(:,66) = strpad('ANMAR in component angiotensin_effect_on_AAR (dimensionless)');
    LEGEND_CONSTANTS(:,25) = strpad('ANMAM in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,26) = strpad('ANMARL in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,65) = strpad('ANMAR1 in component angiotensin_effect_on_AAR (dimensionless)');
    LEGEND_ALGEBRAIC(:,7) = strpad('AAR1 in component AAR_calculation (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,27) = strpad('AARK in component parameter_values (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,8) = strpad('AAR in component atrial_natriuretic_peptide_effect_on_AAR (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,28) = strpad('ANPXAF in component parameter_values (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,29) = strpad('AARLL in component parameter_values (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,9) = strpad('AART in component atrial_natriuretic_peptide_effect_on_AAR (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,67) = strpad('AUMK2 in component autonomic_effect_on_EAR (dimensionless)');
    LEGEND_CONSTANTS(:,30) = strpad('AUMK1 in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,68) = strpad('ANMER in component angiotensin_effect_on_EAR (dimensionless)');
    LEGEND_CONSTANTS(:,31) = strpad('ANMEM in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('RNAUG4 in component effect_of_renal_autoregulatory_feedback_on_EAR (dimensionless)');
    LEGEND_CONSTANTS(:,32) = strpad('EFAFR in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,11) = strpad('EAR in component EAR_calculation (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,33) = strpad('EARK in component parameter_values (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,34) = strpad('EARLL in component parameter_values (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,12) = strpad('EAR1 in component EAR_calculation (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,13) = strpad('RR in component total_renal_resistance (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,14) = strpad('RFN in component normal_renal_blood_flow (L_per_minute)');
    LEGEND_ALGEBRAIC(:,25) = strpad('RBF in component actual_renal_blood_flow (L_per_minute)');
    LEGEND_CONSTANTS(:,35) = strpad('REK in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,15) = strpad('GFN in component glomerular_filtration_rate (L_per_minute)');
    LEGEND_ALGEBRAIC(:,16) = strpad('GLPC in component glomerular_colloid_osmotic_pressure (mmHg)');
    LEGEND_CONSTANTS(:,36) = strpad('GPPD in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,37) = strpad('GLPCA in component parameter_values (mmHg)');
    LEGEND_ALGEBRAIC(:,17) = strpad('EFAFPR in component glomerular_colloid_osmotic_pressure (dimensionless)');
    LEGEND_ALGEBRAIC(:,18) = strpad('EFAFPR1 in component glomerular_colloid_osmotic_pressure (dimensionless)');
    LEGEND_ALGEBRAIC(:,19) = strpad('GLP in component glomerular_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,20) = strpad('APD in component glomerular_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,26) = strpad('GFR in component glomerular_filtration_rate (L_per_minute)');
    LEGEND_CONSTANTS(:,38) = strpad('PXTP in component parameter_values (mmHg)');
    LEGEND_CONSTANTS(:,39) = strpad('GFLC in component parameter_values (L_per_minute_per_mmHg)');
    LEGEND_CONSTANTS(:,40) = strpad('GFNLL in component parameter_values (L_per_minute)');
    LEGEND_ALGEBRAIC(:,21) = strpad('PFL in component glomerular_filtration_rate (mmHg)');
    LEGEND_ALGEBRAIC(:,22) = strpad('GFN1 in component glomerular_filtration_rate (L_per_minute)');
    LEGEND_CONSTANTS(:,41) = strpad('MDFL1 in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,23) = strpad('PTFL in component proximal_tubular_and_macula_densa_flow (L_per_minute)');
    LEGEND_ALGEBRAIC(:,24) = strpad('MDFLWT in component proximal_tubular_and_macula_densa_flow (L_per_minute)');
    LEGEND_ALGEBRAIC(:,28) = strpad('RTSPPC in component renal_tissue_osmotic_pressure (mmHg)');
    LEGEND_CONSTANTS(:,42) = strpad('RTPPR in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,43) = strpad('RTPPRS in component parameter_values (mmHg)');
    LEGEND_ALGEBRAIC(:,27) = strpad('RTSPPC1 in component renal_tissue_osmotic_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,50) = strpad('UROD in component actual_urea_excretion_rate (mOsm_per_minute)');
    LEGEND_STATES(:,3) = strpad('PLUR in component glomerular_urea_concentration (mOsm)');
    LEGEND_CONSTANTS(:,44) = strpad('URFORM in component parameter_values (mOsm_per_minute)');
    LEGEND_ALGEBRAIC(:,2) = strpad('PLURC in component plasma_urea_concentration (mOsm_per_litre)');
    LEGEND_ALGEBRAIC(:,29) = strpad('RCPRS in component peritubular_capillary_pressure (mmHg)');
    LEGEND_CONSTANTS(:,45) = strpad('RFABX in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,46) = strpad('RVRS in component parameter_values (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,34) = strpad('RFABD in component peritubular_capillary_reabsorption_factor (dimensionless)');
    LEGEND_CONSTANTS(:,47) = strpad('RTSPRS in component parameter_values (mmHg)');
    LEGEND_CONSTANTS(:,48) = strpad('RABSC in component parameter_values (per_mmHg)');
    LEGEND_CONSTANTS(:,49) = strpad('RFABDP in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,50) = strpad('RFABDM in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,30) = strpad('RABSPR in component peritubular_capillary_reabsorption_factor (mmHg)');
    LEGEND_ALGEBRAIC(:,31) = strpad('RFAB1 in component peritubular_capillary_reabsorption_factor (dimensionless)');
    LEGEND_ALGEBRAIC(:,32) = strpad('RFAB in component peritubular_capillary_reabsorption_factor (dimensionless)');
    LEGEND_ALGEBRAIC(:,33) = strpad('RFABD1 in component peritubular_capillary_reabsorption_factor (dimensionless)');
    LEGEND_ALGEBRAIC(:,35) = strpad('DTNAI in component distal_tubular_Na_delivery (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,37) = strpad('DTNARA in component Na_reabsorption_into_distal_tubules (monovalent_mEq_per_minute)');
    LEGEND_CONSTANTS(:,51) = strpad('DTNAR in component parameter_values (monovalent_mEq_per_minute)');
    LEGEND_CONSTANTS(:,52) = strpad('DIURET in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,53) = strpad('AHMNAR in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,54) = strpad('DTNARL in component parameter_values (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,36) = strpad('DTNARA1 in component Na_reabsorption_into_distal_tubules (monovalent_mEq_per_minute)');
    LEGEND_CONSTANTS(:,70) = strpad('DTNANG in component angiotensin_induced_Na_reabsorption_into_distal_tubules (monovalent_mEq_per_minute)');
    LEGEND_CONSTANTS(:,55) = strpad('ANMNAM in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,69) = strpad('DTNANG1 in component angiotensin_induced_Na_reabsorption_into_distal_tubules (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,38) = strpad('DTKI in component distal_tubular_K_delivery (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,39) = strpad('RFABK in component effect_of_physical_forces_on_distal_K_reabsorption (monovalent_mEq_per_minute)');
    LEGEND_CONSTANTS(:,56) = strpad('RFABKM in component parameter_values (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,41) = strpad('MDFLK in component effect_of_fluid_flow_on_distal_K_reabsorption (monovalent_mEq_per_minute)');
    LEGEND_CONSTANTS(:,57) = strpad('MDFLKM in component parameter_values (monovalent_mEq_per_litre)');
    LEGEND_ALGEBRAIC(:,40) = strpad('MDFLK1 in component effect_of_fluid_flow_on_distal_K_reabsorption (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,47) = strpad('KODN in component normal_K_excretion (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,55) = strpad('VUDN in component normal_urine_volume (L_per_minute)');
    LEGEND_STATES(:,4) = strpad('DTKA in component K_reabsorption_into_distal_tubules (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,42) = strpad('DTKSC in component K_secretion_from_distal_tubules (monovalent_mEq_per_minute)');
    LEGEND_CONSTANTS(:,58) = strpad('ANMKEM in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,59) = strpad('ANMKEL in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,60) = strpad('CKEEX in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,71) = strpad('ANMKE1 in component K_secretion_from_distal_tubules (dimensionless)');
    LEGEND_CONSTANTS(:,72) = strpad('ANMKE in component K_secretion_from_distal_tubules (dimensionless)');
    LEGEND_ALGEBRAIC(:,44) = strpad('NODN in component normal_Na_excretion (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,43) = strpad('NODN1 in component normal_Na_excretion (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,45) = strpad('KODN1 in component normal_K_excretion (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,48) = strpad('DTURI in component normal_urea_excretion (mOsm_per_minute)');
    LEGEND_ALGEBRAIC(:,51) = strpad('OSMOPN1 in component normal_osmolar_and_water_excretion (mOsm_per_minute)');
    LEGEND_ALGEBRAIC(:,52) = strpad('OSMOPN in component normal_osmolar_and_water_excretion (mOsm_per_minute)');
    LEGEND_ALGEBRAIC(:,53) = strpad('OSMOP1T in component normal_urine_volume (mOsm_per_minute)');
    LEGEND_ALGEBRAIC(:,54) = strpad('OSMOP1 in component normal_urine_volume (mOsm_per_minute)');
    LEGEND_ALGEBRAIC(:,46) = strpad('NOD in component actual_Na_excretion_rate (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,49) = strpad('KOD in component actual_K_excretion_rate (monovalent_mEq_per_minute)');
    LEGEND_ALGEBRAIC(:,56) = strpad('VUD in component actual_urine_volume (L_per_minute)');
    LEGEND_CONSTANTS(:,61) = strpad('RNAGTC in component parameter_values (minute)');
    LEGEND_CONSTANTS(:,62) = strpad('GFNDMP in component parameter_values (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt PAR1 in component perfusion_pressure (mmHg)');
    LEGEND_RATES(:,2) = strpad('d/dt RNAUG3 in component renal_autoregulatory_feedback_factor (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt PLUR in component glomerular_urea_concentration (mOsm)');
    LEGEND_RATES(:,4) = strpad('d/dt DTKA in component K_reabsorption_into_distal_tubules (monovalent_mEq_per_minute)');
    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) = 1.0;
    CONSTANTS(:,2) = 1.037;
    CONSTANTS(:,3) = 1.0;
    CONSTANTS(:,4) = 0.987545;
    CONSTANTS(:,5) = 1.0;
    CONSTANTS(:,6) = 1.00066;
    CONSTANTS(:,7) = 4.44092;
    CONSTANTS(:,8) = 142.035;
    CONSTANTS(:,9) = 0.39984739;
    CONSTANTS(:,10) = 1.0;
    CONSTANTS(:,11) = 103.525;
    CONSTANTS(:,12) = 1.0;
    CONSTANTS(:,13) = 29.9941;
    CONSTANTS(:,14) = 39.8952;
    CONSTANTS(:,15) = 0;
    CONSTANTS(:,16) = 0;
    CONSTANTS(:,17) = 0;
    CONSTANTS(:,18) = 0;
    CONSTANTS(:,19) = 2000;
    STATES(:,1) = 103.525;
    CONSTANTS(:,20) = 0.6;
    CONSTANTS(:,21) = 0.3;
    CONSTANTS(:,22) = 10;
    CONSTANTS(:,23) = 0;
    STATES(:,2) = 0.0;
    CONSTANTS(:,24) = 0.5;
    CONSTANTS(:,25) = 1.4;
    CONSTANTS(:,26) = 0.86;
    CONSTANTS(:,27) = 1;
    CONSTANTS(:,28) = 1.5;
    CONSTANTS(:,29) = 4;
    CONSTANTS(:,30) = 0.3;
    CONSTANTS(:,31) = 1.6;
    CONSTANTS(:,32) = 0;
    CONSTANTS(:,33) = 1;
    CONSTANTS(:,34) = 24;
    CONSTANTS(:,35) = 1;
    CONSTANTS(:,36) = 1.0;
    CONSTANTS(:,37) = 1.0;
    CONSTANTS(:,38) = 8;
    CONSTANTS(:,39) = 0.0208333;
    CONSTANTS(:,40) = 0.001;
    CONSTANTS(:,41) = 10;
    CONSTANTS(:,42) = 0.9;
    CONSTANTS(:,43) = 15.2;
    STATES(:,3) = 159.549;
    CONSTANTS(:,44) = 0.24;
    CONSTANTS(:,45) = 0.8;
    CONSTANTS(:,46) = 19.167;
    CONSTANTS(:,47) = 6;
    CONSTANTS(:,48) = 0.5;
    CONSTANTS(:,49) = 1;
    CONSTANTS(:,50) = 0.3;
    CONSTANTS(:,51) = 0.675;
    CONSTANTS(:,52) = 1;
    CONSTANTS(:,53) = 0.3;
    CONSTANTS(:,54) = 1e-06;
    CONSTANTS(:,55) = 1;
    CONSTANTS(:,56) = 0.03;
    CONSTANTS(:,57) = 0.667;
    STATES(:,4) = 0.0367573;
    CONSTANTS(:,58) = 2;
    CONSTANTS(:,59) = 0.3;
    CONSTANTS(:,60) = 4;
    CONSTANTS(:,61) = 15;
    CONSTANTS(:,62) = 3;
    CONSTANTS(:,63) =  (CONSTANTS(:,6) - 1.00000).*CONSTANTS(:,24)+1.00000;
    CONSTANTS(:,64) = piecewise({CONSTANTS(:,63)<0.800000, 0.800000 }, CONSTANTS(:,63));
    CONSTANTS(:,65) =  (CONSTANTS(:,4) - 1.00000).*CONSTANTS(:,25)+1.00000;
    CONSTANTS(:,66) = piecewise({CONSTANTS(:,65)<CONSTANTS(:,26), CONSTANTS(:,26) }, CONSTANTS(:,65));
    CONSTANTS(:,67) =  (CONSTANTS(:,64) - 1.00000).*CONSTANTS(:,30)+1.00000;
    CONSTANTS(:,68) =  (CONSTANTS(:,4) - 1.00000).*CONSTANTS(:,31)+1.00000;
    CONSTANTS(:,69) =  ( (CONSTANTS(:,4) - 1.00000).*CONSTANTS(:,55)+1.00000).*0.100000;
    CONSTANTS(:,70) = piecewise({CONSTANTS(:,69)<0.00000, 0.00000 }, CONSTANTS(:,69));
    CONSTANTS(:,71) =  (CONSTANTS(:,4) - 1.00000).*CONSTANTS(:,58)+1.00000;
    CONSTANTS(:,72) = piecewise({CONSTANTS(:,71)<CONSTANTS(:,59), CONSTANTS(:,59) }, CONSTANTS(:,71));
    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) = ((100.000+ (CONSTANTS(:,11) - 100.000).*CONSTANTS(:,18)) - STATES(:,1))./CONSTANTS(:,19);
    ALGEBRAIC(:,1) = piecewise({CONSTANTS(:,16)>0.00000&CONSTANTS(:,17)<=0.00000, CONSTANTS(:,16) , CONSTANTS(:,17)>0.00000, STATES(:,1) }, CONSTANTS(:,11) - CONSTANTS(:,15));
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
    RATES(:,2) =  (ALGEBRAIC(:,4) - 1.00000).*CONSTANTS(:,23);
    ALGEBRAIC(:,2) = STATES(:,3)./CONSTANTS(:,14);
    ALGEBRAIC(:,48) =  power(ALGEBRAIC(:,15), 2.00000).*ALGEBRAIC(:,2).*3.84000;
    ALGEBRAIC(:,50) =  ALGEBRAIC(:,48).*CONSTANTS(:,35);
    RATES(:,3) = CONSTANTS(:,44) - ALGEBRAIC(:,50);
    ALGEBRAIC(:,35) =  ALGEBRAIC(:,3).*CONSTANTS(:,8).*0.00616190;
    ALGEBRAIC(:,38) = ( ALGEBRAIC(:,35).*CONSTANTS(:,7))./CONSTANTS(:,8);
    ALGEBRAIC(:,27) =  ALGEBRAIC(:,16).*CONSTANTS(:,42) - CONSTANTS(:,43);
    ALGEBRAIC(:,28) = piecewise({ALGEBRAIC(:,27)<1.00000, 1.00000 }, ALGEBRAIC(:,27));
    ALGEBRAIC(:,29) =  ( (ALGEBRAIC(:,14) - 1.20000).*CONSTANTS(:,45)+1.20000).*CONSTANTS(:,46);
    ALGEBRAIC(:,30) = ((ALGEBRAIC(:,16)+CONSTANTS(:,47)) - ALGEBRAIC(:,29)) - ALGEBRAIC(:,28);
    ALGEBRAIC(:,31) =  ALGEBRAIC(:,30).*CONSTANTS(:,48);
    ALGEBRAIC(:,32) = ALGEBRAIC(:,31);
    ALGEBRAIC(:,33) =  (ALGEBRAIC(:,32) - 1.00000).*CONSTANTS(:,50)+1.00000;
    ALGEBRAIC(:,34) = piecewise({ALGEBRAIC(:,33)<0.000100000, 0.000100000 }, ALGEBRAIC(:,33));
    ALGEBRAIC(:,39) =  (ALGEBRAIC(:,34) - 1.00000).*CONSTANTS(:,56);
    ALGEBRAIC(:,40) =  (ALGEBRAIC(:,3) - 1.00000).*CONSTANTS(:,57)+1.00000;
    ALGEBRAIC(:,41) = piecewise({ALGEBRAIC(:,40)<0.100000, 0.100000 }, ALGEBRAIC(:,40));
    ALGEBRAIC(:,42) = ( power(CONSTANTS(:,7)./4.40000, CONSTANTS(:,60)).*CONSTANTS(:,2).*0.0800000.*ALGEBRAIC(:,41))./CONSTANTS(:,72);
    ALGEBRAIC(:,45) = ((ALGEBRAIC(:,38)+ALGEBRAIC(:,42)) - STATES(:,4)) - ALGEBRAIC(:,39);
    ALGEBRAIC(:,47) = piecewise({ALGEBRAIC(:,45)<0.00000, 0.00000 }, ALGEBRAIC(:,45));
    ALGEBRAIC(:,36) =  (( CONSTANTS(:,3).*ALGEBRAIC(:,34).*CONSTANTS(:,51))./CONSTANTS(:,52)).*( (CONSTANTS(:,1) - 1.00000).*CONSTANTS(:,53)+1.00000);
    ALGEBRAIC(:,37) = piecewise({ALGEBRAIC(:,36)<CONSTANTS(:,54), CONSTANTS(:,54) }, ALGEBRAIC(:,36));
    ALGEBRAIC(:,43) = (ALGEBRAIC(:,35) - ALGEBRAIC(:,37)) - CONSTANTS(:,70);
    ALGEBRAIC(:,44) = piecewise({ALGEBRAIC(:,43)<1.00000e-08, 1.00000e-08 }, ALGEBRAIC(:,43));
    ALGEBRAIC(:,51) = ALGEBRAIC(:,48)+ 2.00000.*(ALGEBRAIC(:,44)+ALGEBRAIC(:,47));
    ALGEBRAIC(:,52) = piecewise({ALGEBRAIC(:,51)>0.600000, 0.600000 }, ALGEBRAIC(:,51));
    ALGEBRAIC(:,53) = ALGEBRAIC(:,51) - 0.600000;
    ALGEBRAIC(:,54) = piecewise({ALGEBRAIC(:,53)<0.00000, 0.00000 }, ALGEBRAIC(:,53));
    ALGEBRAIC(:,55) = ALGEBRAIC(:,52)./( 600.000.*CONSTANTS(:,1))+ALGEBRAIC(:,54)./360.000;
    RATES(:,4) =  ( (ALGEBRAIC(:,47)./ALGEBRAIC(:,55)).*0.000451800 - STATES(:,4)).*1.00000;
   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) = piecewise({CONSTANTS(:,16)>0.00000&CONSTANTS(:,17)<=0.00000, CONSTANTS(:,16) , CONSTANTS(:,17)>0.00000, STATES(:,1) }, CONSTANTS(:,11) - CONSTANTS(:,15));
    ALGEBRAIC(:,2) = STATES(:,3)./CONSTANTS(:,14);
    ALGEBRAIC(:,48) =  power(ALGEBRAIC(:,15), 2.00000).*ALGEBRAIC(:,2).*3.84000;
    ALGEBRAIC(:,50) =  ALGEBRAIC(:,48).*CONSTANTS(:,35);
    ALGEBRAIC(:,35) =  ALGEBRAIC(:,3).*CONSTANTS(:,8).*0.00616190;
    ALGEBRAIC(:,38) = ( ALGEBRAIC(:,35).*CONSTANTS(:,7))./CONSTANTS(:,8);
    ALGEBRAIC(:,27) =  ALGEBRAIC(:,16).*CONSTANTS(:,42) - CONSTANTS(:,43);
    ALGEBRAIC(:,28) = piecewise({ALGEBRAIC(:,27)<1.00000, 1.00000 }, ALGEBRAIC(:,27));
    ALGEBRAIC(:,29) =  ( (ALGEBRAIC(:,14) - 1.20000).*CONSTANTS(:,45)+1.20000).*CONSTANTS(:,46);
    ALGEBRAIC(:,30) = ((ALGEBRAIC(:,16)+CONSTANTS(:,47)) - ALGEBRAIC(:,29)) - ALGEBRAIC(:,28);
    ALGEBRAIC(:,31) =  ALGEBRAIC(:,30).*CONSTANTS(:,48);
    ALGEBRAIC(:,32) = ALGEBRAIC(:,31);
    ALGEBRAIC(:,33) =  (ALGEBRAIC(:,32) - 1.00000).*CONSTANTS(:,50)+1.00000;
    ALGEBRAIC(:,34) = piecewise({ALGEBRAIC(:,33)<0.000100000, 0.000100000 }, ALGEBRAIC(:,33));
    ALGEBRAIC(:,39) =  (ALGEBRAIC(:,34) - 1.00000).*CONSTANTS(:,56);
    ALGEBRAIC(:,40) =  (ALGEBRAIC(:,3) - 1.00000).*CONSTANTS(:,57)+1.00000;
    ALGEBRAIC(:,41) = piecewise({ALGEBRAIC(:,40)<0.100000, 0.100000 }, ALGEBRAIC(:,40));
    ALGEBRAIC(:,42) = ( power(CONSTANTS(:,7)./4.40000, CONSTANTS(:,60)).*CONSTANTS(:,2).*0.0800000.*ALGEBRAIC(:,41))./CONSTANTS(:,72);
    ALGEBRAIC(:,45) = ((ALGEBRAIC(:,38)+ALGEBRAIC(:,42)) - STATES(:,4)) - ALGEBRAIC(:,39);
    ALGEBRAIC(:,47) = piecewise({ALGEBRAIC(:,45)<0.00000, 0.00000 }, ALGEBRAIC(:,45));
    ALGEBRAIC(:,36) =  (( CONSTANTS(:,3).*ALGEBRAIC(:,34).*CONSTANTS(:,51))./CONSTANTS(:,52)).*( (CONSTANTS(:,1) - 1.00000).*CONSTANTS(:,53)+1.00000);
    ALGEBRAIC(:,37) = piecewise({ALGEBRAIC(:,36)<CONSTANTS(:,54), CONSTANTS(:,54) }, ALGEBRAIC(:,36));
    ALGEBRAIC(:,43) = (ALGEBRAIC(:,35) - ALGEBRAIC(:,37)) - CONSTANTS(:,70);
    ALGEBRAIC(:,44) = piecewise({ALGEBRAIC(:,43)<1.00000e-08, 1.00000e-08 }, ALGEBRAIC(:,43));
    ALGEBRAIC(:,51) = ALGEBRAIC(:,48)+ 2.00000.*(ALGEBRAIC(:,44)+ALGEBRAIC(:,47));
    ALGEBRAIC(:,52) = piecewise({ALGEBRAIC(:,51)>0.600000, 0.600000 }, ALGEBRAIC(:,51));
    ALGEBRAIC(:,53) = ALGEBRAIC(:,51) - 0.600000;
    ALGEBRAIC(:,54) = piecewise({ALGEBRAIC(:,53)<0.00000, 0.00000 }, ALGEBRAIC(:,53));
    ALGEBRAIC(:,55) = ALGEBRAIC(:,52)./( 600.000.*CONSTANTS(:,1))+ALGEBRAIC(:,54)./360.000;
    ALGEBRAIC(:,25) =  CONSTANTS(:,35).*ALGEBRAIC(:,14);
    ALGEBRAIC(:,26) =  ALGEBRAIC(:,15).*CONSTANTS(:,35);
    ALGEBRAIC(:,46) =  ALGEBRAIC(:,44).*CONSTANTS(:,35);
    ALGEBRAIC(:,49) =  ALGEBRAIC(:,47).*CONSTANTS(:,35);
    ALGEBRAIC(:,56) =  ALGEBRAIC(:,55).*CONSTANTS(:,35);
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) ~= 22), initialGuess_0 = [1.00051,1.00071,1.00071,1.00071,40,40,40,0.6,42.4737,42.4737,84.8171,1.22057,0.125006,37.8383,1.20569,1.20569,51.842,47.88,6.00368,0.125006,1.00005,1.00051];, 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(:,3) = soln(1);
        ALGEBRAIC(:,4) = soln(2);
        ALGEBRAIC(:,5) = soln(3);
        ALGEBRAIC(:,6) = soln(4);
        ALGEBRAIC(:,7) = soln(5);
        ALGEBRAIC(:,8) = soln(6);
        ALGEBRAIC(:,9) = soln(7);
        ALGEBRAIC(:,10) = soln(8);
        ALGEBRAIC(:,11) = soln(9);
        ALGEBRAIC(:,12) = soln(10);
        ALGEBRAIC(:,13) = soln(11);
        ALGEBRAIC(:,14) = soln(12);
        ALGEBRAIC(:,15) = soln(13);
        ALGEBRAIC(:,16) = soln(14);
        ALGEBRAIC(:,17) = soln(15);
        ALGEBRAIC(:,18) = soln(16);
        ALGEBRAIC(:,19) = soln(17);
        ALGEBRAIC(:,20) = soln(18);
        ALGEBRAIC(:,21) = soln(19);
        ALGEBRAIC(:,22) = soln(20);
        ALGEBRAIC(:,23) = soln(21);
        ALGEBRAIC(:,24) = soln(22);
    else
        SET_ALGEBRAIC(:,3) = logical(1);
        SET_ALGEBRAIC(:,4) = logical(1);
        SET_ALGEBRAIC(:,5) = logical(1);
        SET_ALGEBRAIC(:,6) = logical(1);
        SET_ALGEBRAIC(:,7) = logical(1);
        SET_ALGEBRAIC(:,8) = logical(1);
        SET_ALGEBRAIC(:,9) = logical(1);
        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);
        SET_ALGEBRAIC(:,21) = logical(1);
        SET_ALGEBRAIC(:,22) = logical(1);
        SET_ALGEBRAIC(:,23) = logical(1);
        SET_ALGEBRAIC(:,24) = 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(:,3) = soln(1);
            TEMP_ALGEBRAIC(:,4) = soln(2);
            TEMP_ALGEBRAIC(:,5) = soln(3);
            TEMP_ALGEBRAIC(:,6) = soln(4);
            TEMP_ALGEBRAIC(:,7) = soln(5);
            TEMP_ALGEBRAIC(:,8) = soln(6);
            TEMP_ALGEBRAIC(:,9) = soln(7);
            TEMP_ALGEBRAIC(:,10) = soln(8);
            TEMP_ALGEBRAIC(:,11) = soln(9);
            TEMP_ALGEBRAIC(:,12) = soln(10);
            TEMP_ALGEBRAIC(:,13) = soln(11);
            TEMP_ALGEBRAIC(:,14) = soln(12);
            TEMP_ALGEBRAIC(:,15) = soln(13);
            TEMP_ALGEBRAIC(:,16) = soln(14);
            TEMP_ALGEBRAIC(:,17) = soln(15);
            TEMP_ALGEBRAIC(:,18) = soln(16);
            TEMP_ALGEBRAIC(:,19) = soln(17);
            TEMP_ALGEBRAIC(:,20) = soln(18);
            TEMP_ALGEBRAIC(:,21) = soln(19);
            TEMP_ALGEBRAIC(:,22) = soln(20);
            TEMP_ALGEBRAIC(:,23) = soln(21);
            TEMP_ALGEBRAIC(:,24) = soln(22);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    ALGEBRAIC(:,3) = algebraicCandidate(1);
    ALGEBRAIC(:,4) = algebraicCandidate(2);
    ALGEBRAIC(:,5) = algebraicCandidate(3);
    ALGEBRAIC(:,6) = algebraicCandidate(4);
    ALGEBRAIC(:,7) = algebraicCandidate(5);
    ALGEBRAIC(:,8) = algebraicCandidate(6);
    ALGEBRAIC(:,9) = algebraicCandidate(7);
    ALGEBRAIC(:,10) = algebraicCandidate(8);
    ALGEBRAIC(:,11) = algebraicCandidate(9);
    ALGEBRAIC(:,12) = algebraicCandidate(10);
    ALGEBRAIC(:,13) = algebraicCandidate(11);
    ALGEBRAIC(:,14) = algebraicCandidate(12);
    ALGEBRAIC(:,15) = algebraicCandidate(13);
    ALGEBRAIC(:,16) = algebraicCandidate(14);
    ALGEBRAIC(:,17) = algebraicCandidate(15);
    ALGEBRAIC(:,18) = algebraicCandidate(16);
    ALGEBRAIC(:,19) = algebraicCandidate(17);
    ALGEBRAIC(:,20) = algebraicCandidate(18);
    ALGEBRAIC(:,21) = algebraicCandidate(19);
    ALGEBRAIC(:,22) = algebraicCandidate(20);
    ALGEBRAIC(:,23) = algebraicCandidate(21);
    ALGEBRAIC(:,24) = algebraicCandidate(22);
    resid(1) = ALGEBRAIC(:,6) - ( (ALGEBRAIC(:,3) - 1.00000).*CONSTANTS(:,20)+1.00000);
    resid(2) = ALGEBRAIC(:,5) - piecewise({ALGEBRAIC(:,6)<CONSTANTS(:,21), CONSTANTS(:,21) , ALGEBRAIC(:,6)>CONSTANTS(:,22), CONSTANTS(:,22) }, ALGEBRAIC(:,6));
    resid(3) = ALGEBRAIC(:,4) - (ALGEBRAIC(:,5) - STATES(:,2));
    resid(4) = ALGEBRAIC(:,7) -  CONSTANTS(:,27).*CONSTANTS(:,12).*CONSTANTS(:,64).*ALGEBRAIC(:,4).*CONSTANTS(:,66).*40.0000.*CONSTANTS(:,10);
    resid(5) = ALGEBRAIC(:,9) - ((ALGEBRAIC(:,7) -  CONSTANTS(:,5).*CONSTANTS(:,28))+CONSTANTS(:,28));
    resid(6) = ALGEBRAIC(:,8) - piecewise({ALGEBRAIC(:,9)<CONSTANTS(:,29), CONSTANTS(:,29) }, ALGEBRAIC(:,9));
    resid(7) = ALGEBRAIC(:,10) - ( (ALGEBRAIC(:,4) - 1.00000).*CONSTANTS(:,32)+1.00000);
    resid(8) = ALGEBRAIC(:,12) -  43.3330.*CONSTANTS(:,33).*CONSTANTS(:,68).*ALGEBRAIC(:,10).*CONSTANTS(:,10).*CONSTANTS(:,67);
    resid(9) = ALGEBRAIC(:,11) - piecewise({ALGEBRAIC(:,12)<CONSTANTS(:,34), CONSTANTS(:,34) }, ALGEBRAIC(:,12));
    resid(10) = ALGEBRAIC(:,13) - (ALGEBRAIC(:,8)+ALGEBRAIC(:,11));
    resid(11) = ALGEBRAIC(:,14) - ALGEBRAIC(:,1)./ALGEBRAIC(:,13);
    resid(12) = ALGEBRAIC(:,18) - ( ALGEBRAIC(:,14).*(1.00000 - CONSTANTS(:,9)))./( ALGEBRAIC(:,14).*(1.00000 - CONSTANTS(:,9)) - ALGEBRAIC(:,15));
    resid(13) = ALGEBRAIC(:,17) - piecewise({ALGEBRAIC(:,18)<1.00000, 1.00000 }, ALGEBRAIC(:,18));
    resid(14) = ALGEBRAIC(:,16) - piecewise({CONSTANTS(:,37)>0.00000,  power(ALGEBRAIC(:,17), 1.35000).*CONSTANTS(:,13).*0.980000 }, CONSTANTS(:,13)+4.00000);
    resid(15) = ALGEBRAIC(:,20) -  ALGEBRAIC(:,8).*ALGEBRAIC(:,14);
    resid(16) = ALGEBRAIC(:,19) - (ALGEBRAIC(:,1) - ALGEBRAIC(:,20));
    resid(17) = ALGEBRAIC(:,21) - ((ALGEBRAIC(:,19) - ALGEBRAIC(:,16)) - CONSTANTS(:,38));
    resid(18) = ALGEBRAIC(:,22) -  ALGEBRAIC(:,21).*CONSTANTS(:,39);
    resid(19) = ALGEBRAIC(:,15) - piecewise({ALGEBRAIC(:,22)<CONSTANTS(:,40), CONSTANTS(:,40) }, ALGEBRAIC(:,22));
    resid(20) = ALGEBRAIC(:,23) -  ALGEBRAIC(:,15).*8.00000;
    resid(21) = ALGEBRAIC(:,24) - ( (ALGEBRAIC(:,23) - 1.00000).*CONSTANTS(:,41)+1.00000);
    resid(22) = ALGEBRAIC(:,3) - piecewise({ALGEBRAIC(:,24)<0.00000, 0.00000 }, ALGEBRAIC(:,24));
end

% Compute result of a piecewise function
function x = piecewise(cases, default)
    set = [0];
    for i = 1:2:length(cases)
        if (length(cases{i+1}) == 1)
            x(cases{i} & ~set,:) = cases{i+1};
        else
            x(cases{i} & ~set,:) = cases{i+1}(cases{i} & ~set);
        end
        set = set | cases{i};
        if(set), break, end
    end
    if (length(default) == 1)
        x(~set,:) = default;
    else
        x(~set,:) = default(~set);
    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