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 =53;
end
% There are a total of 7 entries in each of the rate and state variable arrays.
% There are a total of 114 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 (day)');
    LEGEND_STATES(:,1) = strpad('OBp in component OBp (pM)');
    LEGEND_CONSTANTS(:,1) = strpad('OBp_t0 in component OBp (pM)');
    LEGEND_ALGEBRAIC(:,50) = strpad('OBp_in in component OBp (flux)');
    LEGEND_ALGEBRAIC(:,10) = strpad('OBp_out in component OBp (flux)');
    LEGEND_CONSTANTS(:,2) = strpad('OBu_t0 in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,78) = strpad('DifferOBpNormal in component OBp (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,3) = strpad('D_OBp_t0 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,4) = strpad('TGFbNormal in component OBp (pM)');
    LEGEND_CONSTANTS(:,5) = strpad('KD_TGF_beta_repress in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,82) = strpad('ProlifOBpNormal in component OBp (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('frac_prolifOBp_vs_differOBu in component OBp (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('OBpNormal in component OBp (pM)');
    LEGEND_CONSTANTS(:,8) = strpad('OBp_sat in component OBp (pM)');
    LEGEND_CONSTANTS(:,87) = strpad('DifferOBu in component OBp (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,2) = strpad('ProlifOBp in component OBp (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,9) = strpad('D_OBu_t0 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('pd_OBp_t0 in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,83) = strpad('Pi_TGFbeta_OBu_act_t0 in component TGF_beta (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('Pi_TGFbeta_OBp_rep in component TGF_beta (dimensionless)');
    LEGEND_ALGEBRAIC(:,47) = strpad('Pi_WNT in component Wnt (dimensionless)');
    LEGEND_CONSTANTS(:,113) = strpad('Pi_WNT_0 in component Wnt (dimensionless)');
    LEGEND_STATES(:,2) = strpad('OBa in component OBa (pM)');
    LEGEND_ALGEBRAIC(:,11) = strpad('OBa_in in component OBa (flux)');
    LEGEND_ALGEBRAIC(:,3) = strpad('OBa_out in component OBa (flux)');
    LEGEND_CONSTANTS(:,11) = strpad('pd_OBa_t0 in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,12) = strpad('D_OBa_t0 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('A_OBa_t0 in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,3) = strpad('OCp in component OCp (pM)');
    LEGEND_ALGEBRAIC(:,53) = strpad('OCp_in in component OCp (flux)');
    LEGEND_ALGEBRAIC(:,48) = strpad('OCp_out in component OCp (flux)');
    LEGEND_CONSTANTS(:,14) = strpad('OCu_t0 in component model_parameters (pM)');
    LEGEND_ALGEBRAIC(:,45) = strpad('Pi_RANKL_act_OCp in component RANK_RANKL_OPG (dimensionless)');
    LEGEND_ALGEBRAIC(:,51) = strpad('Pi_RANKL_act_OCu in component RANK_RANKL_OPG (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('D_OCu_t0 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('pd_OCp_t0 in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,17) = strpad('D_OCp_t0 in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,4) = strpad('OCa in component OCa (pM)');
    LEGEND_ALGEBRAIC(:,52) = strpad('OCa_in in component OCa (flux)');
    LEGEND_ALGEBRAIC(:,8) = strpad('OCa_out in component OCa (flux)');
    LEGEND_CONSTANTS(:,18) = strpad('pd_OCa_t0 in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,19) = strpad('A_OCa_t0 in component model_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,6) = strpad('Pi_TGFbeta_OCa_act in component TGF_beta (dimensionless)');
    LEGEND_STATES(:,5) = strpad('fbm in component fbm (dimensionless)');
    LEGEND_CONSTANTS(:,20) = strpad('K_form in component model_parameters (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,21) = strpad('K_res in component model_parameters (second_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,1) = strpad('dfbmdt in component fbm (first_order_rate_constant)');
    LEGEND_STATES(:,6) = strpad('OCY in component OCY (pM)');
    LEGEND_ALGEBRAIC(:,4) = strpad('OCY_act in component OCY (pM)');
    LEGEND_CONSTANTS(:,80) = strpad('OCY_act_0 in component OCY (pM)');
    LEGEND_CONSTANTS(:,22) = strpad('fact_0 in component model_parameters (pM)');
    LEGEND_STATES(:,7) = strpad('vm in component vm (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('XKAPPA in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,24) = strpad('vmmax in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,81) = strpad('PTH_tot in component PTH (pM)');
    LEGEND_CONSTANTS(:,84) = strpad('Pi_PTH_act in component PTH (dimensionless)');
    LEGEND_CONSTANTS(:,85) = strpad('Pi_PTH_rep in component PTH (dimensionless)');
    LEGEND_CONSTANTS(:,25) = strpad('Beta_PTH in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,26) = strpad('P_PTH_d in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,27) = strpad('Deg_PTH in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,28) = strpad('KD_PTH_act in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,29) = strpad('KD_PTH_rep in component model_parameters (pM)');
    LEGEND_ALGEBRAIC(:,37) = strpad('RANK in component RANK_RANKL_OPG (pM)');
    LEGEND_ALGEBRAIC(:,38) = strpad('RANKL in component RANK_RANKL_OPG (pM)');
    LEGEND_ALGEBRAIC(:,39) = strpad('OPG in component RANK_RANKL_OPG (pM)');
    LEGEND_ALGEBRAIC(:,40) = strpad('RANKL_tot in component RANK_RANKL_OPG (pM)');
    LEGEND_CONSTANTS(:,86) = strpad('RANKL_max in component RANK_RANKL_OPG (pM)');
    LEGEND_ALGEBRAIC(:,41) = strpad('P_RANKL1 in component RANK_RANKL_OPG (flux)');
    LEGEND_ALGEBRAIC(:,42) = strpad('P_RANKL2 in component RANK_RANKL_OPG (flux)');
    LEGEND_ALGEBRAIC(:,43) = strpad('P_RANKL in component RANK_RANKL_OPG (flux)');
    LEGEND_CONSTANTS(:,30) = strpad('P_RANKL_d in component model_parameters (flux)');
    LEGEND_ALGEBRAIC(:,35) = strpad('Pi_NO_PTH_act_rep in component NO_PTH (dimensionless)');
    LEGEND_CONSTANTS(:,31) = strpad('N_RANK_OCp in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,32) = strpad('N_RANKL_OBp_max in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,33) = strpad('K_RANK_RANKL in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,34) = strpad('Beta_OPG in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,35) = strpad('OPG_max in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,36) = strpad('Deg_OPG in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,37) = strpad('Deg_OPG_RANKL in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,38) = strpad('K_OPG_RANKL in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,39) = strpad('Beta_RANKL_OCY in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,40) = strpad('Beta_RANKL_OBp in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,41) = strpad('Deg_RANKL in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,42) = strpad('Deg_RANK_RANKL in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,43) = strpad('KD_RANKL_act_OCp in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,44) = strpad('KD_RANKL_act_OCu in component model_parameters (pM)');
    LEGEND_ALGEBRAIC(:,5) = strpad('TGF_beta in component TGF_beta (pM)');
    LEGEND_CONSTANTS(:,79) = strpad('TGF_beta_t0 in component TGF_beta (pM)');
    LEGEND_CONSTANTS(:,45) = strpad('OCa_t0 in component TGF_beta (pM)');
    LEGEND_CONSTANTS(:,46) = strpad('Alpha in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,47) = strpad('KD_TGF_beta_activate in component model_parameters (pM)');
    LEGEND_ALGEBRAIC(:,7) = strpad('Pi_TGFbeta_OBu_act in component TGF_beta (dimensionless)');
    LEGEND_ALGEBRAIC(:,46) = strpad('Scl in component Scl (pM)');
    LEGEND_CONSTANTS(:,112) = strpad('Scl_0 in component Scl (pM)');
    LEGEND_ALGEBRAIC(:,49) = strpad('P_Scl_b in component Scl (flux)');
    LEGEND_CONSTANTS(:,114) = strpad('P_Scl_b_0 in component Scl (flux)');
    LEGEND_ALGEBRAIC(:,34) = strpad('A in component Scl (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,109) = strpad('A_0 in component Scl (second_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,36) = strpad('B in component Scl (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,110) = strpad('B_0 in component Scl (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,44) = strpad('C in component Scl (flux)');
    LEGEND_CONSTANTS(:,111) = strpad('C_0 in component Scl (flux)');
    LEGEND_ALGEBRAIC(:,32) = strpad('OCYprod in component Scl (flux)');
    LEGEND_CONSTANTS(:,108) = strpad('OCYprod_0 in component Scl (flux)');
    LEGEND_CONSTANTS(:,90) = strpad('Wnt in component Wnt (pM)');
    LEGEND_CONSTANTS(:,48) = strpad('Wnt_0 in component model_parameters (pM)');
    LEGEND_ALGEBRAIC(:,30) = strpad('Pi_eps_rep in component SED (dimensionless)');
    LEGEND_CONSTANTS(:,107) = strpad('Pi_eps_rep_stst in component SED (dimensionless)');
    LEGEND_CONSTANTS(:,88) = strpad('Beta_Scl in component Scl (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,49) = strpad('Beta_Scl_0 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,50) = strpad('KD_SclLRP5 in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,51) = strpad('Deg_Scl in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,52) = strpad('Scl_max in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,53) = strpad('KD_WntLRP5 in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,54) = strpad('Deg_SclLRP5 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,55) = strpad('LRP5perCell in component model_parameters (dimensionless)');
    LEGEND_ALGEBRAIC(:,12) = strpad('LRP5tot in component Scl (pM)');
    LEGEND_CONSTANTS(:,89) = strpad('LRP5tot_0 in component Scl (pM)');
    LEGEND_CONSTANTS(:,56) = strpad('P_Scl_d in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,91) = strpad('PTH_tot in component NO_PTH (pM)');
    LEGEND_ALGEBRAIC(:,31) = strpad('NO_tot in component NO_PTH (pM)');
    LEGEND_CONSTANTS(:,104) = strpad('NO_eq in component NO_PTH (pM)');
    LEGEND_CONSTANTS(:,92) = strpad('Beta_NO in component NO_PTH (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,57) = strpad('Beta_NO_0 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,58) = strpad('P_NO_d in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,59) = strpad('Deg_NO in component model_parameters (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,29) = strpad('Pi_eps_act in component SED (dimensionless)');
    LEGEND_CONSTANTS(:,103) = strpad('Pi_eps_act_stst in component SED (dimensionless)');
    LEGEND_CONSTANTS(:,60) = strpad('NO_max in component model_parameters (pM)');
    LEGEND_CONSTANTS(:,105) = strpad('KD_rep_NO in component NO_PTH (pM)');
    LEGEND_CONSTANTS(:,61) = strpad('aa in component NO_PTH (dimensionless)');
    LEGEND_CONSTANTS(:,93) = strpad('Pi_PTH_act in component NO_PTH (dimensionless)');
    LEGEND_ALGEBRAIC(:,33) = strpad('Pi_NO_rep in component NO_PTH (dimensionless)');
    LEGEND_CONSTANTS(:,62) = strpad('lambda_s in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,63) = strpad('lambda_c in component model_parameters (dimensionless)');
    LEGEND_ALGEBRAIC(:,27) = strpad('SED in component SED (MPa)');
    LEGEND_ALGEBRAIC(:,28) = strpad('SED_bm in component SED (MPa)');
    LEGEND_CONSTANTS(:,64) = strpad('sig_macro_t0 in component model_parameters (MPa)');
    LEGEND_CONSTANTS(:,65) = strpad('de_sig_macro in component model_parameters (MPa)');
    LEGEND_CONSTANTS(:,99) = strpad('sig_macro in component SED (MPa)');
    LEGEND_CONSTANTS(:,94) = strpad('sig_1 in component SED (MPa)');
    LEGEND_CONSTANTS(:,95) = strpad('sig_2 in component SED (MPa)');
    LEGEND_CONSTANTS(:,100) = strpad('sig_3 in component SED (MPa)');
    LEGEND_CONSTANTS(:,96) = strpad('sig_4 in component SED (MPa)');
    LEGEND_CONSTANTS(:,97) = strpad('sig_5 in component SED (MPa)');
    LEGEND_CONSTANTS(:,98) = strpad('sig_6 in component SED (MPa)');
    LEGEND_ALGEBRAIC(:,21) = strpad('eps_1 in component SED (dimensionless)');
    LEGEND_ALGEBRAIC(:,22) = strpad('eps_2 in component SED (dimensionless)');
    LEGEND_ALGEBRAIC(:,23) = strpad('eps_3 in component SED (dimensionless)');
    LEGEND_ALGEBRAIC(:,24) = strpad('eps_4 in component SED (dimensionless)');
    LEGEND_ALGEBRAIC(:,25) = strpad('eps_5 in component SED (dimensionless)');
    LEGEND_ALGEBRAIC(:,26) = strpad('eps_6 in component SED (dimensionless)');
    LEGEND_ALGEBRAIC(:,13) = strpad('dens_tis in component SED (g_per_cm3)');
    LEGEND_CONSTANTS(:,66) = strpad('vo in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,67) = strpad('rho_m in component model_parameters (g_per_cm3)');
    LEGEND_CONSTANTS(:,68) = strpad('rho_o in component model_parameters (g_per_cm3)');
    LEGEND_ALGEBRAIC(:,14) = strpad('fvas in component SED (dimensionless)');
    LEGEND_ALGEBRAIC(:,15) = strpad('dens in component SED (g_per_cm3)');
    LEGEND_ALGEBRAIC(:,16) = strpad('dens_1 in component SED (dimensionless)');
    LEGEND_ALGEBRAIC(:,17) = strpad('E_mod in component SED (MPa)');
    LEGEND_CONSTANTS(:,69) = strpad('nu in component model_parameters (dimensionless)');
    LEGEND_ALGEBRAIC(:,18) = strpad('S_11 in component SED (per_MPa)');
    LEGEND_ALGEBRAIC(:,19) = strpad('S_12 in component SED (per_MPa)');
    LEGEND_ALGEBRAIC(:,20) = strpad('S_44 in component SED (per_MPa)');
    LEGEND_CONSTANTS(:,70) = strpad('omega in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,101) = strpad('valeq in component SED (dimensionless)');
    LEGEND_CONSTANTS(:,71) = strpad('taueq in component model_parameters (MPa)');
    LEGEND_CONSTANTS(:,72) = strpad('alphaAct in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,73) = strpad('alphaRep in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,74) = strpad('gammaAct in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,75) = strpad('gammaRep in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,76) = strpad('rhoAct in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,77) = strpad('rhoRep in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,102) = strpad('deltaAct in component SED (MPa)');
    LEGEND_CONSTANTS(:,106) = strpad('deltaRep in component SED (MPa)');
    LEGEND_RATES(:,1) = strpad('d/dt OBp in component OBp (pM)');
    LEGEND_RATES(:,2) = strpad('d/dt OBa in component OBa (pM)');
    LEGEND_RATES(:,3) = strpad('d/dt OCp in component OCp (pM)');
    LEGEND_RATES(:,4) = strpad('d/dt OCa in component OCa (pM)');
    LEGEND_RATES(:,5) = strpad('d/dt fbm in component fbm (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt OCY in component OCY (pM)');
    LEGEND_RATES(:,7) = strpad('d/dt vm in component vm (dimensionless)');
    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) = 1.1631869976e-03;
    CONSTANTS(:,1) = 1.1631869976e-03;
    CONSTANTS(:,2) = 0.01;
    CONSTANTS(:,3) = 0.185;
    CONSTANTS(:,4) = 0.0001;
    CONSTANTS(:,5) = 0.000175426051821094;
    CONSTANTS(:,6) = 0.5;
    CONSTANTS(:,7) = 0.001;
    CONSTANTS(:,8) = 0.005;
    CONSTANTS(:,9) = 0.166;
    CONSTANTS(:,10) = 0;
    STATES(:,2) = 9.1880026722e-04;
    CONSTANTS(:,11) = 0;
    CONSTANTS(:,12) = 0.0212;
    CONSTANTS(:,13) = 0.1908;
    STATES(:,3) = 1.3556879825e-03;
    CONSTANTS(:,14) = 0.01;
    CONSTANTS(:,15) = 0.0219;
    CONSTANTS(:,16) = 0;
    CONSTANTS(:,17) = 0.01958;
    STATES(:,4) = 1.8376005344e-05;
    CONSTANTS(:,18) = 0;
    CONSTANTS(:,19) = 10;
    STATES(:,5) = 15;
    CONSTANTS(:,20) = 50;
    CONSTANTS(:,21) = 2500;
    STATES(:,6) = 1.2734398187e-02;
    CONSTANTS(:,22) = 0.000414138;
    STATES(:,7) = 3.5893417161e-01;
    CONSTANTS(:,23) = 0.007;
    CONSTANTS(:,24) = 0.516;
    CONSTANTS(:,25) = 250;
    CONSTANTS(:,26) = 0;
    CONSTANTS(:,27) = 86;
    CONSTANTS(:,28) = 0.65;
    CONSTANTS(:,29) = 0.222581427709954;
    CONSTANTS(:,30) = 0;
    CONSTANTS(:,31) = 4160;
    CONSTANTS(:,32) = 2703476;
    CONSTANTS(:,33) = 10;
    CONSTANTS(:,34) = 162490033.783568;
    CONSTANTS(:,35) = 131.428571428571;
    CONSTANTS(:,36) = 532608.695652174;
    CONSTANTS(:,37) = 10.132471014805;
    CONSTANTS(:,38) = 0.0151142857142857;
    CONSTANTS(:,39) = 5660;
    CONSTANTS(:,40) = 23600;
    CONSTANTS(:,41) = 10.132471014805;
    CONSTANTS(:,42) = 10.132471014805;
    CONSTANTS(:,43) = 3.34;
    CONSTANTS(:,44) = 16.7;
    CONSTANTS(:,45) = 0.0001;
    CONSTANTS(:,46) = 1;
    CONSTANTS(:,47) = 0.000563278809675429;
    CONSTANTS(:,48) = 170;
    CONSTANTS(:,49) = 24000;
    CONSTANTS(:,50) = 10;
    CONSTANTS(:,51) = 1;
    CONSTANTS(:,52) = 70;
    CONSTANTS(:,53) = 1000;
    CONSTANTS(:,54) = 50;
    CONSTANTS(:,55) = 5;
    CONSTANTS(:,56) = 0;
    CONSTANTS(:,57) = 3440;
    CONSTANTS(:,58) = 0;
    CONSTANTS(:,59) = 0.0021;
    CONSTANTS(:,60) = 200000000;
    CONSTANTS(:,61) = 2;
    CONSTANTS(:,62) = 0.450454;
    CONSTANTS(:,63) = 0.900909;
    CONSTANTS(:,64) = -0.1457;
    CONSTANTS(:,65) = 0;
    CONSTANTS(:,66) = 0.428571428571428;
    CONSTANTS(:,67) = 3.2;
    CONSTANTS(:,68) = 1.41;
    CONSTANTS(:,69) = 0.3;
    CONSTANTS(:,70) = 0.95;
    CONSTANTS(:,71) = 0.006652;
    CONSTANTS(:,72) = 1;
    CONSTANTS(:,73) = 1;
    CONSTANTS(:,74) = 7;
    CONSTANTS(:,75) = 8.01559;
    CONSTANTS(:,76) = 0;
    CONSTANTS(:,77) = 0;
    CONSTANTS(:,78) = ( CONSTANTS(:,3).*1.00000)./(1.00000+CONSTANTS(:,4)./CONSTANTS(:,5));
    CONSTANTS(:,79) =  CONSTANTS(:,46).*CONSTANTS(:,45);
    CONSTANTS(:,80) =  CONSTANTS(:,22).*20.0000;
    CONSTANTS(:,81) = (CONSTANTS(:,25)+CONSTANTS(:,26))./CONSTANTS(:,27);
    CONSTANTS(:,82) = ( CONSTANTS(:,6).*CONSTANTS(:,78))./(1.00000 - CONSTANTS(:,7)./CONSTANTS(:,8));
    CONSTANTS(:,83) = CONSTANTS(:,79)./(CONSTANTS(:,47)+CONSTANTS(:,79));
    CONSTANTS(:,84) = CONSTANTS(:,81)./(CONSTANTS(:,81)+CONSTANTS(:,28));
    CONSTANTS(:,85) = 1.00000./(1.00000+CONSTANTS(:,81)./CONSTANTS(:,29));
    CONSTANTS(:,86) =  CONSTANTS(:,32).*CONSTANTS(:,1);
    CONSTANTS(:,87) =  (1.00000 - CONSTANTS(:,6)).*CONSTANTS(:,9).*CONSTANTS(:,83);
    CONSTANTS(:,88) = CONSTANTS(:,49);
    CONSTANTS(:,89) =  CONSTANTS(:,55).*CONSTANTS(:,1);
    CONSTANTS(:,90) = CONSTANTS(:,48);
    CONSTANTS(:,91) = (CONSTANTS(:,25)+CONSTANTS(:,26))./CONSTANTS(:,27);
    CONSTANTS(:,92) = CONSTANTS(:,57);
    CONSTANTS(:,93) = CONSTANTS(:,91)./(CONSTANTS(:,91)+CONSTANTS(:,28));
    CONSTANTS(:,94) = 0.00000;
    CONSTANTS(:,95) = 0.00000;
    CONSTANTS(:,96) = 0.00000;
    CONSTANTS(:,97) = 0.00000;
    CONSTANTS(:,98) = 0.00000;
    CONSTANTS(:,99) = CONSTANTS(:,64)+CONSTANTS(:,65);
    CONSTANTS(:,100) = CONSTANTS(:,99);
    CONSTANTS(:,101) = CONSTANTS(:,70);
    CONSTANTS(:,102) =  CONSTANTS(:,71).*power((CONSTANTS(:,72) - CONSTANTS(:,101))./(CONSTANTS(:,101) - CONSTANTS(:,76)), 1.00000./CONSTANTS(:,74));
    CONSTANTS(:,103) = CONSTANTS(:,76)+( (CONSTANTS(:,72) - CONSTANTS(:,76)).*power(CONSTANTS(:,71), CONSTANTS(:,74)))./(power(CONSTANTS(:,102), CONSTANTS(:,74))+power(CONSTANTS(:,71), CONSTANTS(:,74)));
    CONSTANTS(:,104) = ( CONSTANTS(:,57).*CONSTANTS(:,103).*CONSTANTS(:,80))./(CONSTANTS(:,59)+( CONSTANTS(:,57).*CONSTANTS(:,103).*CONSTANTS(:,80))./CONSTANTS(:,60));
    CONSTANTS(:,105) = CONSTANTS(:,104)./CONSTANTS(:,61);
    CONSTANTS(:,106) =  CONSTANTS(:,71).*power((CONSTANTS(:,101) - CONSTANTS(:,77))./(CONSTANTS(:,73) - CONSTANTS(:,101)), 1.00000./CONSTANTS(:,75));
    CONSTANTS(:,107) = CONSTANTS(:,73) - ( (CONSTANTS(:,73) - CONSTANTS(:,77)).*power(CONSTANTS(:,71), CONSTANTS(:,75)))./(power(CONSTANTS(:,106), CONSTANTS(:,75))+power(CONSTANTS(:,71), CONSTANTS(:,75)));
    CONSTANTS(:,108) =  CONSTANTS(:,49).*CONSTANTS(:,80).*CONSTANTS(:,107);
    CONSTANTS(:,109) =  (1.00000./CONSTANTS(:,50)).*(CONSTANTS(:,51)+CONSTANTS(:,108)./CONSTANTS(:,52));
    CONSTANTS(:,110) = ( CONSTANTS(:,109).*CONSTANTS(:,50).*(1.00000+CONSTANTS(:,48)./CONSTANTS(:,53))+( CONSTANTS(:,54).*CONSTANTS(:,89))./CONSTANTS(:,50)) - (CONSTANTS(:,56)+CONSTANTS(:,108))./CONSTANTS(:,50);
    CONSTANTS(:,111) =   - (CONSTANTS(:,56)+CONSTANTS(:,108)).*(1.00000+CONSTANTS(:,48)./CONSTANTS(:,53));
    CONSTANTS(:,112) = ( - CONSTANTS(:,110)+power((power(CONSTANTS(:,110), 2.00000) -  4.00000.*CONSTANTS(:,109).*CONSTANTS(:,111)), 1.0 ./ 2))./( 2.00000.*CONSTANTS(:,109));
    CONSTANTS(:,113) = CONSTANTS(:,48)./( CONSTANTS(:,53).*(1.00000+CONSTANTS(:,48)./CONSTANTS(:,53)+CONSTANTS(:,112)./CONSTANTS(:,50)));
    CONSTANTS(:,114) =  CONSTANTS(:,108).*(1.00000 - CONSTANTS(:,112)./CONSTANTS(:,52));
    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(:,7) =  CONSTANTS(:,23).*(CONSTANTS(:,24) - STATES(:,7)) - ( STATES(:,7).*CONSTANTS(:,20).*STATES(:,2))./STATES(:,5);
    ALGEBRAIC(:,1) =  CONSTANTS(:,20).*STATES(:,2) -  CONSTANTS(:,21).*STATES(:,4);
    RATES(:,5) = ALGEBRAIC(:,1);
    RATES(:,6) =  CONSTANTS(:,22).*ALGEBRAIC(:,1);
    ALGEBRAIC(:,5) =  CONSTANTS(:,46).*STATES(:,4);
    ALGEBRAIC(:,9) = 1.00000./(1.00000+ALGEBRAIC(:,5)./CONSTANTS(:,5));
    ALGEBRAIC(:,10) =  CONSTANTS(:,3).*ALGEBRAIC(:,9).*STATES(:,1);
    ALGEBRAIC(:,11) = ALGEBRAIC(:,10)+CONSTANTS(:,11);
    ALGEBRAIC(:,3) =  (CONSTANTS(:,12)+CONSTANTS(:,13)).*STATES(:,2);
    RATES(:,2) = ALGEBRAIC(:,11) - ALGEBRAIC(:,3);
    ALGEBRAIC(:,2) =  CONSTANTS(:,82).*(1.00000 - STATES(:,1)./CONSTANTS(:,8));
    ALGEBRAIC(:,4) =  CONSTANTS(:,22).*STATES(:,5);
    ALGEBRAIC(:,13) = 1.00000+ (CONSTANTS(:,67) - 1.00000).*STATES(:,7)+ (CONSTANTS(:,68) - 1.00000).*CONSTANTS(:,66);
    ALGEBRAIC(:,15) = ( ALGEBRAIC(:,13).*STATES(:,5))./100.000;
    ALGEBRAIC(:,16) = ALGEBRAIC(:,15)./1.00000;
    ALGEBRAIC(:,17) = piecewise({ALGEBRAIC(:,16)<1.20000,  2014.00.*power(ALGEBRAIC(:,16), 2.50000) },  1763.00.*power(ALGEBRAIC(:,16), 3.20000));
    ALGEBRAIC(:,18) = 1.00000./ALGEBRAIC(:,17);
    ALGEBRAIC(:,19) =  - CONSTANTS(:,69)./ALGEBRAIC(:,17);
    ALGEBRAIC(:,21) =  ALGEBRAIC(:,18).*CONSTANTS(:,94)+ ALGEBRAIC(:,19).*CONSTANTS(:,95)+ ALGEBRAIC(:,19).*CONSTANTS(:,100);
    ALGEBRAIC(:,22) =  ALGEBRAIC(:,19).*CONSTANTS(:,94)+ ALGEBRAIC(:,18).*CONSTANTS(:,95)+ ALGEBRAIC(:,19).*CONSTANTS(:,100);
    ALGEBRAIC(:,23) =  ALGEBRAIC(:,19).*CONSTANTS(:,94)+ ALGEBRAIC(:,19).*CONSTANTS(:,95)+ ALGEBRAIC(:,18).*CONSTANTS(:,100);
    ALGEBRAIC(:,20) = (2.00000+ 2.00000.*CONSTANTS(:,69))./ALGEBRAIC(:,17);
    ALGEBRAIC(:,24) =  ALGEBRAIC(:,20).*CONSTANTS(:,96);
    ALGEBRAIC(:,25) =  ALGEBRAIC(:,20).*CONSTANTS(:,97);
    ALGEBRAIC(:,26) =  ALGEBRAIC(:,20).*CONSTANTS(:,98);
    ALGEBRAIC(:,27) =  0.500000.*( CONSTANTS(:,94).*ALGEBRAIC(:,21)+ CONSTANTS(:,95).*ALGEBRAIC(:,22)+ CONSTANTS(:,100).*ALGEBRAIC(:,23)+ CONSTANTS(:,96).*ALGEBRAIC(:,24)+ CONSTANTS(:,97).*ALGEBRAIC(:,25)+ CONSTANTS(:,98).*ALGEBRAIC(:,26));
    ALGEBRAIC(:,14) = 1.00000 - STATES(:,5)./100.000;
    ALGEBRAIC(:,28) = ALGEBRAIC(:,27)./power(1.00000 - ALGEBRAIC(:,14), 2.00000);
    ALGEBRAIC(:,30) = CONSTANTS(:,73) - ( (CONSTANTS(:,73) - CONSTANTS(:,77)).*power(ALGEBRAIC(:,28), CONSTANTS(:,75)))./(power(CONSTANTS(:,106), CONSTANTS(:,75))+power(ALGEBRAIC(:,28), CONSTANTS(:,75)));
    ALGEBRAIC(:,32) =  CONSTANTS(:,88).*ALGEBRAIC(:,4).*ALGEBRAIC(:,30);
    ALGEBRAIC(:,34) =  (1.00000./CONSTANTS(:,50)).*(CONSTANTS(:,51)+ALGEBRAIC(:,32)./CONSTANTS(:,52));
    ALGEBRAIC(:,12) =  CONSTANTS(:,55).*STATES(:,1);
    ALGEBRAIC(:,36) = ( ALGEBRAIC(:,34).*CONSTANTS(:,50).*(1.00000+CONSTANTS(:,90)./CONSTANTS(:,53))+( CONSTANTS(:,54).*ALGEBRAIC(:,12))./CONSTANTS(:,50)) - (CONSTANTS(:,56)+ALGEBRAIC(:,32))./CONSTANTS(:,50);
    ALGEBRAIC(:,44) =   - (CONSTANTS(:,56)+ALGEBRAIC(:,32)).*(1.00000+CONSTANTS(:,90)./CONSTANTS(:,53));
    ALGEBRAIC(:,46) = ( - ALGEBRAIC(:,36)+power((power(ALGEBRAIC(:,36), 2.00000) -  4.00000.*ALGEBRAIC(:,34).*ALGEBRAIC(:,44)), 1.0 ./ 2))./( 2.00000.*ALGEBRAIC(:,34));
    ALGEBRAIC(:,47) = CONSTANTS(:,90)./( CONSTANTS(:,53).*(1.00000+CONSTANTS(:,90)./CONSTANTS(:,53)+ALGEBRAIC(:,46)./CONSTANTS(:,50)));
    ALGEBRAIC(:,50) =  CONSTANTS(:,87).*CONSTANTS(:,2)+( ALGEBRAIC(:,2).*STATES(:,1).*ALGEBRAIC(:,47))./CONSTANTS(:,113)+CONSTANTS(:,10);
    RATES(:,1) = ALGEBRAIC(:,50) - ALGEBRAIC(:,10);
    ALGEBRAIC(:,29) = CONSTANTS(:,76)+( (CONSTANTS(:,72) - CONSTANTS(:,76)).*power(ALGEBRAIC(:,28), CONSTANTS(:,74)))./(power(CONSTANTS(:,102), CONSTANTS(:,74))+power(ALGEBRAIC(:,28), CONSTANTS(:,74)));
    ALGEBRAIC(:,31) = ( CONSTANTS(:,92).*ALGEBRAIC(:,29).*ALGEBRAIC(:,4)+CONSTANTS(:,58))./(CONSTANTS(:,59)+( CONSTANTS(:,92).*ALGEBRAIC(:,29).*ALGEBRAIC(:,4))./CONSTANTS(:,60));
    ALGEBRAIC(:,33) = 1.00000./(1.00000+ALGEBRAIC(:,31)./CONSTANTS(:,105));
    ALGEBRAIC(:,35) =  CONSTANTS(:,62).*(CONSTANTS(:,93)+ALGEBRAIC(:,33))+ CONSTANTS(:,63).*CONSTANTS(:,93).*ALGEBRAIC(:,33);
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
    ALGEBRAIC(:,45) = ALGEBRAIC(:,38)./(CONSTANTS(:,43)+ALGEBRAIC(:,38));
    ALGEBRAIC(:,48) =  CONSTANTS(:,17).*ALGEBRAIC(:,45).*STATES(:,3);
    ALGEBRAIC(:,52) = ALGEBRAIC(:,48)+CONSTANTS(:,18);
    ALGEBRAIC(:,6) = ALGEBRAIC(:,5)./(CONSTANTS(:,47)+ALGEBRAIC(:,5));
    ALGEBRAIC(:,8) =  CONSTANTS(:,19).*ALGEBRAIC(:,6).*STATES(:,4);
    RATES(:,4) = ALGEBRAIC(:,52) - ALGEBRAIC(:,8);
    ALGEBRAIC(:,51) = ALGEBRAIC(:,38)./(CONSTANTS(:,44)+ALGEBRAIC(:,38));
    ALGEBRAIC(:,53) =  CONSTANTS(:,15).*CONSTANTS(:,14).*ALGEBRAIC(:,51)+CONSTANTS(:,16);
    RATES(:,3) = ALGEBRAIC(:,53) - ALGEBRAIC(:,48);
   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(:,20).*STATES(:,2) -  CONSTANTS(:,21).*STATES(:,4);
    ALGEBRAIC(:,5) =  CONSTANTS(:,46).*STATES(:,4);
    ALGEBRAIC(:,9) = 1.00000./(1.00000+ALGEBRAIC(:,5)./CONSTANTS(:,5));
    ALGEBRAIC(:,10) =  CONSTANTS(:,3).*ALGEBRAIC(:,9).*STATES(:,1);
    ALGEBRAIC(:,11) = ALGEBRAIC(:,10)+CONSTANTS(:,11);
    ALGEBRAIC(:,3) =  (CONSTANTS(:,12)+CONSTANTS(:,13)).*STATES(:,2);
    ALGEBRAIC(:,2) =  CONSTANTS(:,82).*(1.00000 - STATES(:,1)./CONSTANTS(:,8));
    ALGEBRAIC(:,4) =  CONSTANTS(:,22).*STATES(:,5);
    ALGEBRAIC(:,13) = 1.00000+ (CONSTANTS(:,67) - 1.00000).*STATES(:,7)+ (CONSTANTS(:,68) - 1.00000).*CONSTANTS(:,66);
    ALGEBRAIC(:,15) = ( ALGEBRAIC(:,13).*STATES(:,5))./100.000;
    ALGEBRAIC(:,16) = ALGEBRAIC(:,15)./1.00000;
    ALGEBRAIC(:,17) = piecewise({ALGEBRAIC(:,16)<1.20000,  2014.00.*power(ALGEBRAIC(:,16), 2.50000) },  1763.00.*power(ALGEBRAIC(:,16), 3.20000));
    ALGEBRAIC(:,18) = 1.00000./ALGEBRAIC(:,17);
    ALGEBRAIC(:,19) =  - CONSTANTS(:,69)./ALGEBRAIC(:,17);
    ALGEBRAIC(:,21) =  ALGEBRAIC(:,18).*CONSTANTS(:,94)+ ALGEBRAIC(:,19).*CONSTANTS(:,95)+ ALGEBRAIC(:,19).*CONSTANTS(:,100);
    ALGEBRAIC(:,22) =  ALGEBRAIC(:,19).*CONSTANTS(:,94)+ ALGEBRAIC(:,18).*CONSTANTS(:,95)+ ALGEBRAIC(:,19).*CONSTANTS(:,100);
    ALGEBRAIC(:,23) =  ALGEBRAIC(:,19).*CONSTANTS(:,94)+ ALGEBRAIC(:,19).*CONSTANTS(:,95)+ ALGEBRAIC(:,18).*CONSTANTS(:,100);
    ALGEBRAIC(:,20) = (2.00000+ 2.00000.*CONSTANTS(:,69))./ALGEBRAIC(:,17);
    ALGEBRAIC(:,24) =  ALGEBRAIC(:,20).*CONSTANTS(:,96);
    ALGEBRAIC(:,25) =  ALGEBRAIC(:,20).*CONSTANTS(:,97);
    ALGEBRAIC(:,26) =  ALGEBRAIC(:,20).*CONSTANTS(:,98);
    ALGEBRAIC(:,27) =  0.500000.*( CONSTANTS(:,94).*ALGEBRAIC(:,21)+ CONSTANTS(:,95).*ALGEBRAIC(:,22)+ CONSTANTS(:,100).*ALGEBRAIC(:,23)+ CONSTANTS(:,96).*ALGEBRAIC(:,24)+ CONSTANTS(:,97).*ALGEBRAIC(:,25)+ CONSTANTS(:,98).*ALGEBRAIC(:,26));
    ALGEBRAIC(:,14) = 1.00000 - STATES(:,5)./100.000;
    ALGEBRAIC(:,28) = ALGEBRAIC(:,27)./power(1.00000 - ALGEBRAIC(:,14), 2.00000);
    ALGEBRAIC(:,30) = CONSTANTS(:,73) - ( (CONSTANTS(:,73) - CONSTANTS(:,77)).*power(ALGEBRAIC(:,28), CONSTANTS(:,75)))./(power(CONSTANTS(:,106), CONSTANTS(:,75))+power(ALGEBRAIC(:,28), CONSTANTS(:,75)));
    ALGEBRAIC(:,32) =  CONSTANTS(:,88).*ALGEBRAIC(:,4).*ALGEBRAIC(:,30);
    ALGEBRAIC(:,34) =  (1.00000./CONSTANTS(:,50)).*(CONSTANTS(:,51)+ALGEBRAIC(:,32)./CONSTANTS(:,52));
    ALGEBRAIC(:,12) =  CONSTANTS(:,55).*STATES(:,1);
    ALGEBRAIC(:,36) = ( ALGEBRAIC(:,34).*CONSTANTS(:,50).*(1.00000+CONSTANTS(:,90)./CONSTANTS(:,53))+( CONSTANTS(:,54).*ALGEBRAIC(:,12))./CONSTANTS(:,50)) - (CONSTANTS(:,56)+ALGEBRAIC(:,32))./CONSTANTS(:,50);
    ALGEBRAIC(:,44) =   - (CONSTANTS(:,56)+ALGEBRAIC(:,32)).*(1.00000+CONSTANTS(:,90)./CONSTANTS(:,53));
    ALGEBRAIC(:,46) = ( - ALGEBRAIC(:,36)+power((power(ALGEBRAIC(:,36), 2.00000) -  4.00000.*ALGEBRAIC(:,34).*ALGEBRAIC(:,44)), 1.0 ./ 2))./( 2.00000.*ALGEBRAIC(:,34));
    ALGEBRAIC(:,47) = CONSTANTS(:,90)./( CONSTANTS(:,53).*(1.00000+CONSTANTS(:,90)./CONSTANTS(:,53)+ALGEBRAIC(:,46)./CONSTANTS(:,50)));
    ALGEBRAIC(:,50) =  CONSTANTS(:,87).*CONSTANTS(:,2)+( ALGEBRAIC(:,2).*STATES(:,1).*ALGEBRAIC(:,47))./CONSTANTS(:,113)+CONSTANTS(:,10);
    ALGEBRAIC(:,29) = CONSTANTS(:,76)+( (CONSTANTS(:,72) - CONSTANTS(:,76)).*power(ALGEBRAIC(:,28), CONSTANTS(:,74)))./(power(CONSTANTS(:,102), CONSTANTS(:,74))+power(ALGEBRAIC(:,28), CONSTANTS(:,74)));
    ALGEBRAIC(:,31) = ( CONSTANTS(:,92).*ALGEBRAIC(:,29).*ALGEBRAIC(:,4)+CONSTANTS(:,58))./(CONSTANTS(:,59)+( CONSTANTS(:,92).*ALGEBRAIC(:,29).*ALGEBRAIC(:,4))./CONSTANTS(:,60));
    ALGEBRAIC(:,33) = 1.00000./(1.00000+ALGEBRAIC(:,31)./CONSTANTS(:,105));
    ALGEBRAIC(:,35) =  CONSTANTS(:,62).*(CONSTANTS(:,93)+ALGEBRAIC(:,33))+ CONSTANTS(:,63).*CONSTANTS(:,93).*ALGEBRAIC(:,33);
    ALGEBRAIC(:,45) = ALGEBRAIC(:,38)./(CONSTANTS(:,43)+ALGEBRAIC(:,38));
    ALGEBRAIC(:,48) =  CONSTANTS(:,17).*ALGEBRAIC(:,45).*STATES(:,3);
    ALGEBRAIC(:,52) = ALGEBRAIC(:,48)+CONSTANTS(:,18);
    ALGEBRAIC(:,6) = ALGEBRAIC(:,5)./(CONSTANTS(:,47)+ALGEBRAIC(:,5));
    ALGEBRAIC(:,8) =  CONSTANTS(:,19).*ALGEBRAIC(:,6).*STATES(:,4);
    ALGEBRAIC(:,51) = ALGEBRAIC(:,38)./(CONSTANTS(:,44)+ALGEBRAIC(:,38));
    ALGEBRAIC(:,53) =  CONSTANTS(:,15).*CONSTANTS(:,14).*ALGEBRAIC(:,51)+CONSTANTS(:,16);
    ALGEBRAIC(:,7) = ALGEBRAIC(:,5)./(CONSTANTS(:,47)+ALGEBRAIC(:,5));
    ALGEBRAIC(:,49) =  ALGEBRAIC(:,32).*(1.00000 - ALGEBRAIC(:,46)./CONSTANTS(:,52));
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) ~= 7), initialGuess_0 = [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(:,37) = soln(1);
        ALGEBRAIC(:,38) = soln(2);
        ALGEBRAIC(:,39) = soln(3);
        ALGEBRAIC(:,40) = soln(4);
        ALGEBRAIC(:,41) = soln(5);
        ALGEBRAIC(:,42) = soln(6);
        ALGEBRAIC(:,43) = soln(7);
    else
        SET_ALGEBRAIC(:,37) = logical(1);
        SET_ALGEBRAIC(:,38) = logical(1);
        SET_ALGEBRAIC(:,39) = logical(1);
        SET_ALGEBRAIC(:,40) = logical(1);
        SET_ALGEBRAIC(:,41) = logical(1);
        SET_ALGEBRAIC(:,42) = logical(1);
        SET_ALGEBRAIC(:,43) = 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(:,37) = soln(1);
            TEMP_ALGEBRAIC(:,38) = soln(2);
            TEMP_ALGEBRAIC(:,39) = soln(3);
            TEMP_ALGEBRAIC(:,40) = soln(4);
            TEMP_ALGEBRAIC(:,41) = soln(5);
            TEMP_ALGEBRAIC(:,42) = soln(6);
            TEMP_ALGEBRAIC(:,43) = soln(7);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    ALGEBRAIC(:,37) = algebraicCandidate(1);
    ALGEBRAIC(:,38) = algebraicCandidate(2);
    ALGEBRAIC(:,39) = algebraicCandidate(3);
    ALGEBRAIC(:,40) = algebraicCandidate(4);
    ALGEBRAIC(:,41) = algebraicCandidate(5);
    ALGEBRAIC(:,42) = algebraicCandidate(6);
    ALGEBRAIC(:,43) = algebraicCandidate(7);
    resid(1) = ALGEBRAIC(:,37) - ( CONSTANTS(:,31).*STATES(:,3))./(1.00000+ (1.00000./CONSTANTS(:,33)).*ALGEBRAIC(:,38));
    resid(2) = ALGEBRAIC(:,39) - ( CONSTANTS(:,34).*CONSTANTS(:,85).*STATES(:,2))./(( CONSTANTS(:,34).*CONSTANTS(:,85).*STATES(:,2))./CONSTANTS(:,35)+CONSTANTS(:,36)+ (CONSTANTS(:,37)./CONSTANTS(:,38)).*ALGEBRAIC(:,38));
    resid(3) = ALGEBRAIC(:,40) -  ALGEBRAIC(:,38).*(1.00000+ALGEBRAIC(:,37)./CONSTANTS(:,33)+ALGEBRAIC(:,39)./CONSTANTS(:,38));
    resid(4) = ALGEBRAIC(:,41) -  CONSTANTS(:,39).*(1.00000 - ALGEBRAIC(:,40)./CONSTANTS(:,86)).*ALGEBRAIC(:,4);
    resid(5) = ALGEBRAIC(:,42) -  CONSTANTS(:,40).*ALGEBRAIC(:,35).*(1.00000 - ALGEBRAIC(:,40)./CONSTANTS(:,86)).*STATES(:,1);
    resid(6) = ALGEBRAIC(:,43) - (ALGEBRAIC(:,41)+ALGEBRAIC(:,42));
    resid(7) = ALGEBRAIC(:,38) - (ALGEBRAIC(:,43)+CONSTANTS(:,30))./(CONSTANTS(:,41)+ (CONSTANTS(:,42)./CONSTANTS(:,33)).*ALGEBRAIC(:,37)+ (CONSTANTS(:,37)./CONSTANTS(:,38)).*ALGEBRAIC(:,39));
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