tic;    
[VOI, STATES, ALGEBRAIC, CONSTANTS] = mainFunction();
toc;

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 =92;
end
% There are a total of 39 entries in each of the rate and state variable arrays.
% There are a total of 71 entries in the constant variable array.
%

function [VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel()
    [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends();
    % 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, 1];

    % Set numerical accuracy options for ODE solver
    options = odeset('RelTol', 1e-05, 'AbsTol', 1e-35, 'MaxStep', 1e-5);

    % 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);
    vlabels = {'pH_i in component environment (dimensionless)',...
        'pH_o in component environment (dimensionless)',...
        'q_Cl_i in component environment (fmol)',...
        'q_Na_i in component environment (fmol)',...
        'q_HCO3_i in component environment (fmol)',...
        'q_HB1 in component environment (fmol)'        };
    [~, i_st, i_alg] = find_indices(vlabels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
    if i_st
        vol_myo = 38; % pL
        plot_selected(i_st,VOI,STATES/vol_myo,'s',LEGEND_STATES,'concentrations mM',ceil(sqrt(length(i_st))))
    end
    plot_selected(i_alg,VOI,ALGEBRAIC,'s',LEGEND_ALGEBRAIC,'ALGEBRAIC',ceil(sqrt(length(i_alg))))

    % fluxes
    vlabels = {
		'v_R12_AE in component AE (fmol_per_sec)',...
		'v_R45_AE in component AE (fmol_per_sec)',...
		'v_R12_CHE in component CHE (fmol_per_sec)',...
		'v_R45_CHE in component CHE (fmol_per_sec)',...
		'v_Re_CO2hyd in component CO2_hyd (fmol_per_sec)',...
		'v_R12_NBC in component NBC (fmol_per_sec)',...
		'v_R45_NBC in component NBC (fmol_per_sec)',...
		'v_R12_NHE in component NHE (fmol_per_sec)',...
		'v_R45_NHE in component NHE (fmol_per_sec)',...
		'v_Re_B1 in component pH_B1 (fmol_per_sec)',...
		'v_Re_B2 in component pH_B2 (fmol_per_sec)'	};
    
	[~, ~, i_alg] = find_indices(vlabels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
    if true
        plot_selected(i_alg,VOI,ALGEBRAIC,'s',LEGEND_ALGEBRAIC,'FLUXES',ceil(sqrt(length(i_alg))))
    end
    
    % check conservation of species
    if true
        Hlabels = {'q_HCO3_i in component environment (fmol)',...
            'q_HCO3_o in component environment (fmol)',...
            'q_H_i in component environment (fmol)',...
            'q_H_o in component environment (fmol)',...
            'q_HB1 in component environment (fmol)',...
            'q_HB2 in component environment (fmol)',...
            'q_S3_NBC in component NBC (fmol)',...
            'q_S4_NBC in component NBC (fmol)',...
            'q_S1_AE in component AE (fmol)',...
            'q_S6_AE in component AE (fmol)',...
            'q_S1_NHE in component NHE (fmol)',...
            'q_S6_NHE in component NHE (fmol)'
        };
%         Hlabels = {'q_H_i in component environment (fmol)',...
%             'q_H_o in component environment (fmol)',...
%             'q_HB1 in component environment (fmol)',...
%             'q_HB2 in component environment (fmol)',...
%             'q_S1_NHE in component NHE (fmol)',...
%             'q_S6_NHE in component NHE (fmol)'
%         };

        [~, i_hst, ] = find_indices(Hlabels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
        sumH = zeros(length(VOI),1);
        for i = 1:length(i_hst)
            sumH = sumH + STATES(:,i_hst(i));
        end
        istart = 1;
%         istart = find(VOI > 0.0015, 1);
        figure,
        subplot(2,2,1)
        plot(VOI(istart:end),sumH(istart:end));
        title('sumH');

        % Na
        Nalabels = {'q_Na_i in component environment (fmol)',...
            'q_Na_o in component environment (fmol)',...
            'q_S2_NBC in component NBC (fmol)',...
            'q_S3_NBC in component NBC (fmol)',...
            'q_S4_NBC in component NBC (fmol)',...
            'q_S5_NBC in component NBC (fmol)',...
            'q_S3_NHE in component NHE (fmol)',...
            'q_S4_NHE in component NHE (fmol)'
        };
        [~, i_hst, ] = find_indices(Nalabels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
        sumH = zeros(length(VOI),1);
        for i = 1:length(i_hst)
            sumH = sumH + STATES(:,i_hst(i));
        end
        istart = 1;
%         istart = find(VOI > 0.0015, 1);
        subplot(2,2,2)
        plot(VOI(istart:end),sumH(istart:end));
        title('sumNa');
    
        % Cl
        Cllabels = {'q_Cl_i in component environment (fmol)',...
            'q_Cl_o in component environment (fmol)',...
            'q_S3_AE in component AE (fmol)',...
            'q_S4_AE in component AE (fmol)',...
            'q_S3_CHE in component CHE (fmol)',...
            'q_S4_CHE in component CHE (fmol)'
        };
        [~, i_hst, ] = find_indices(Cllabels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
        sumH = zeros(length(VOI),1);
        for i = 1:length(i_hst)
            sumH = sumH + STATES(:,i_hst(i));
        end
        istart = 1;
%         istart = find(VOI > 0.0015, 1);
        subplot(2,2,3)
        plot(VOI(istart:end),sumH(istart:end));
        title('sumCl');

    end
end            


function [i_con, i_st, i_alg] = find_indices(labels, LEGEND_CONSTANTS, LEGEND_STATES, LEGEND_ALGEBRAIC)
% return the indices for the selected labels
    all_legends = [LEGEND_CONSTANTS; LEGEND_STATES; LEGEND_ALGEBRAIC];
    
    i_con = [];
    for i = 1:length(labels)
        i_con = [i_con; find(strcmp(labels{i},LEGEND_CONSTANTS))];
    end
    i_st = [];
    for i = 1:length(labels)
        i_st = [i_st; find(strcmp(labels{i},LEGEND_STATES))];
    end
    i_alg = [];
    for i = 1:length(labels)
        i_alg = [i_alg; find(strcmp(labels{i},LEGEND_ALGEBRAIC))];
    end
    
    if length(i_con) + length(i_st) + length(i_alg) < length(labels)
        error('missing index');
    end
end


function [] = plot_selected(ids,x,y,legend_x,legend_y,titlestr,ns)
    istart = 1;
%     istart = find(x > 0.0015, 1);
    figure();
%     plot stimuli
    for i = 1:length(ids)
        subplot(ns,ns,i)
        plotting_x = x(istart:end);
        plotting_y = y(istart:end,ids(i));
        if isempty(plotting_y) % probably a constant scalar
            plotting_y = repmat(y(ids(i)),2);
            plotting_x = [x(istart), x(end)];
        end
        plot(plotting_x, plotting_y);
        xlabel('time (s)');
        str = split(legend_y(ids(i),:), ' ');
        str = legend_y(ids(i),:);
        l = legend(str);
        set(l,'interpreter','none');
    end
    if verLessThan('matlab','9.5')
        suptitle(titlestr)    
    else
        sgtitle(titlestr)    
    end
end

function [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends()
    LEGEND_STATES = ''; LEGEND_ALGEBRAIC = ''; LEGEND_VOI = ''; LEGEND_CONSTANTS = '';
    LEGEND_CONSTANTS(:,1) = strpad('kappa_R61_AE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,2) = strpad('kappa_R12_AE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,3) = strpad('kappa_R23_AE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,4) = strpad('kappa_R34_AE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,5) = strpad('kappa_R45_AE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,6) = strpad('kappa_R56_AE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,7) = strpad('kappa_R61_CHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,8) = strpad('kappa_R12_CHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,9) = strpad('kappa_R23_CHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,10) = strpad('kappa_R34_CHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,11) = strpad('kappa_R45_CHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,12) = strpad('kappa_R56_CHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,13) = strpad('kappa_Re_CO2hyd in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,14) = strpad('kappa_R61_NBC in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,15) = strpad('kappa_R12_NBC in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,16) = strpad('kappa_R23_NBC in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,17) = strpad('kappa_R34_NBC in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,18) = strpad('kappa_R45_NBC in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,19) = strpad('kappa_R56_NBC in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,20) = strpad('kappa_R61_NHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,21) = strpad('kappa_R12_NHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,22) = strpad('kappa_R23_NHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,23) = strpad('kappa_R34_NHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,24) = strpad('kappa_R45_NHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,25) = strpad('kappa_R56_NHE in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,26) = strpad('kappa_Re_B1 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,27) = strpad('kappa_Re_B2 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,28) = strpad('K_HCO3_i in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,29) = strpad('K_HCO3_o in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,30) = strpad('K_Cl_i in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,31) = strpad('K_Cl_o in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,32) = strpad('K_S1_AE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,33) = strpad('K_S2_AE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,34) = strpad('K_S3_AE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,35) = strpad('K_S4_AE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,36) = strpad('K_S5_AE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,37) = strpad('K_S6_AE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,38) = strpad('K_OH_i in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,39) = strpad('K_OH_o in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,40) = strpad('K_S1_CHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,41) = strpad('K_S2_CHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,42) = strpad('K_S3_CHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,43) = strpad('K_S4_CHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,44) = strpad('K_S5_CHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,45) = strpad('K_S6_CHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,46) = strpad('K_CO2_i in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,47) = strpad('K_H_i in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,48) = strpad('K_Na_i in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,49) = strpad('K_Na_o in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,50) = strpad('K_S1_NBC in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,51) = strpad('K_S2_NBC in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,52) = strpad('K_S3_NBC in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,53) = strpad('K_S4_NBC in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,54) = strpad('K_S5_NBC in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,55) = strpad('K_S6_NBC in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,56) = strpad('K_H_o in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,57) = strpad('K_S1_NHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,58) = strpad('K_S2_NHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,59) = strpad('K_S3_NHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,60) = strpad('K_S4_NHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,61) = strpad('K_S5_NHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,62) = strpad('K_S6_NHE in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,63) = strpad('K_B1 in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,64) = strpad('K_HB1 in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,65) = strpad('K_B2 in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,66) = strpad('K_HB2 in component BG_parameters (per_fmol)');
    LEGEND_VOI = strpad('time in component environment (second)');
    LEGEND_STATES(:,1) = strpad('q_HCO3_i in component environment (fmol)');
    LEGEND_STATES(:,2) = strpad('q_HCO3_o in component environment (fmol)');
    LEGEND_STATES(:,3) = strpad('q_Cl_i in component environment (fmol)');
    LEGEND_STATES(:,4) = strpad('q_Cl_o in component environment (fmol)');
    LEGEND_STATES(:,5) = strpad('q_OH_i in component environment (fmol)');
    LEGEND_STATES(:,6) = strpad('q_OH_o in component environment (fmol)');
    LEGEND_STATES(:,7) = strpad('q_Na_i in component environment (fmol)');
    LEGEND_STATES(:,8) = strpad('q_Na_o in component environment (fmol)');
    LEGEND_STATES(:,9) = strpad('q_H_i in component environment (fmol)');
    LEGEND_STATES(:,10) = strpad('q_H_o in component environment (fmol)');
    LEGEND_STATES(:,11) = strpad('q_B1 in component environment (fmol)');
    LEGEND_STATES(:,12) = strpad('q_HB1 in component environment (fmol)');
    LEGEND_STATES(:,13) = strpad('q_B2 in component environment (fmol)');
    LEGEND_STATES(:,14) = strpad('q_HB2 in component environment (fmol)');
    LEGEND_STATES(:,15) = strpad('q_CO2_i in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,27) = strpad('v_R61_AE in component AE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,28) = strpad('v_R12_AE in component AE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,29) = strpad('v_R23_AE in component AE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,30) = strpad('v_R34_AE in component AE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,31) = strpad('v_R45_AE in component AE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,33) = strpad('v_R56_AE in component AE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,52) = strpad('v_R61_CHE in component CHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,54) = strpad('v_R12_CHE in component CHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,56) = strpad('v_R23_CHE in component CHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,58) = strpad('v_R34_CHE in component CHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,60) = strpad('v_R45_CHE in component CHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,62) = strpad('v_R56_CHE in component CHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,41) = strpad('v_Re_CO2hyd in component CO2_hyd (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,63) = strpad('v_R61_NBC in component NBC (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,64) = strpad('v_R12_NBC in component NBC (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,65) = strpad('v_R23_NBC in component NBC (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,66) = strpad('v_R34_NBC in component NBC (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,67) = strpad('v_R45_NBC in component NBC (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,68) = strpad('v_R56_NBC in component NBC (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,79) = strpad('v_R61_NHE in component NHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,80) = strpad('v_R12_NHE in component NHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,81) = strpad('v_R23_NHE in component NHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,82) = strpad('v_R34_NHE in component NHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,83) = strpad('v_R45_NHE in component NHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,84) = strpad('v_R56_NHE in component NHE (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,88) = strpad('v_Re_B1 in component pH_B1 (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,92) = strpad('v_Re_B2 in component pH_B2 (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,16) = strpad('pH_i in component environment (dimensionless)');
    LEGEND_ALGEBRAIC(:,17) = strpad('pH_o in component environment (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('cH_i in component environment (mM)');
    LEGEND_ALGEBRAIC(:,2) = strpad('cH_o in component environment (mM)');
    LEGEND_CONSTANTS(:,67) = strpad('w_i in component environment (pL)');
    LEGEND_CONSTANTS(:,68) = strpad('w_o in component environment (pL)');
    LEGEND_CONSTANTS(:,69) = strpad('R in component constants (J_per_K_per_mol)');
    LEGEND_CONSTANTS(:,70) = strpad('T in component constants (kelvin)');
    LEGEND_CONSTANTS(:,71) = strpad('F in component constants (C_per_mol)');
    LEGEND_STATES(:,16) = strpad('q_S1_AE in component AE (fmol)');
    LEGEND_STATES(:,17) = strpad('q_S2_AE in component AE (fmol)');
    LEGEND_STATES(:,18) = strpad('q_S3_AE in component AE (fmol)');
    LEGEND_STATES(:,19) = strpad('q_S4_AE in component AE (fmol)');
    LEGEND_STATES(:,20) = strpad('q_S5_AE in component AE (fmol)');
    LEGEND_STATES(:,21) = strpad('q_S6_AE in component AE (fmol)');
    LEGEND_ALGEBRAIC(:,3) = strpad('mu_HCO3_i in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,18) = strpad('mu_HCO3_o in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,19) = strpad('mu_Cl_i in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,20) = strpad('mu_Cl_o in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,21) = strpad('mu_S1_AE in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,22) = strpad('mu_S2_AE in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,23) = strpad('mu_S3_AE in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,24) = strpad('mu_S4_AE in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,25) = strpad('mu_S5_AE in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,26) = strpad('mu_S6_AE in component AE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,4) = strpad('B_tot in component AE (fmol)');
    LEGEND_ALGEBRAIC(:,5) = strpad('S_tot in component AE (fmol)');
    LEGEND_ALGEBRAIC(:,6) = strpad('S1S6_tot in component AE (fmol)');
    LEGEND_ALGEBRAIC(:,7) = strpad('Cl_tot in component AE (fmol)');
    LEGEND_STATES(:,22) = strpad('q_S1_CHE in component CHE (fmol)');
    LEGEND_STATES(:,23) = strpad('q_S2_CHE in component CHE (fmol)');
    LEGEND_STATES(:,24) = strpad('q_S3_CHE in component CHE (fmol)');
    LEGEND_STATES(:,25) = strpad('q_S4_CHE in component CHE (fmol)');
    LEGEND_STATES(:,26) = strpad('q_S5_CHE in component CHE (fmol)');
    LEGEND_STATES(:,27) = strpad('q_S6_CHE in component CHE (fmol)');
    LEGEND_ALGEBRAIC(:,32) = strpad('mu_OH_i in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,34) = strpad('mu_OH_o in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,36) = strpad('mu_Cl_i in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,38) = strpad('mu_Cl_o in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,40) = strpad('mu_S1_CHE in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,42) = strpad('mu_S2_CHE in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,44) = strpad('mu_S3_CHE in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,46) = strpad('mu_S4_CHE in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,48) = strpad('mu_S5_CHE in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,50) = strpad('mu_S6_CHE in component CHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,35) = strpad('mu_CO2_i in component CO2_hyd (J_per_mol)');
    LEGEND_ALGEBRAIC(:,37) = strpad('mu_HCO3_i in component CO2_hyd (J_per_mol)');
    LEGEND_ALGEBRAIC(:,39) = strpad('mu_H_i in component CO2_hyd (J_per_mol)');
    LEGEND_STATES(:,28) = strpad('q_S1_NBC in component NBC (fmol)');
    LEGEND_STATES(:,29) = strpad('q_S2_NBC in component NBC (fmol)');
    LEGEND_STATES(:,30) = strpad('q_S3_NBC in component NBC (fmol)');
    LEGEND_STATES(:,31) = strpad('q_S4_NBC in component NBC (fmol)');
    LEGEND_STATES(:,32) = strpad('q_S5_NBC in component NBC (fmol)');
    LEGEND_STATES(:,33) = strpad('q_S6_NBC in component NBC (fmol)');
    LEGEND_ALGEBRAIC(:,43) = strpad('mu_Na_i in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,45) = strpad('mu_Na_o in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,47) = strpad('mu_HCO3_i in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,49) = strpad('mu_HCO3_o in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,51) = strpad('mu_S1_NBC in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,53) = strpad('mu_S2_NBC in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,55) = strpad('mu_S3_NBC in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,57) = strpad('mu_S4_NBC in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,59) = strpad('mu_S5_NBC in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,61) = strpad('mu_S6_NBC in component NBC (J_per_mol)');
    LEGEND_ALGEBRAIC(:,8) = strpad('B_tot in component NBC (fmol)');
    LEGEND_ALGEBRAIC(:,9) = strpad('S_tot in component NBC (fmol)');
    LEGEND_ALGEBRAIC(:,10) = strpad('Na_tot in component NBC (fmol)');
    LEGEND_STATES(:,34) = strpad('q_S1_NHE in component NHE (fmol)');
    LEGEND_STATES(:,35) = strpad('q_S2_NHE in component NHE (fmol)');
    LEGEND_STATES(:,36) = strpad('q_S3_NHE in component NHE (fmol)');
    LEGEND_STATES(:,37) = strpad('q_S4_NHE in component NHE (fmol)');
    LEGEND_STATES(:,38) = strpad('q_S5_NHE in component NHE (fmol)');
    LEGEND_STATES(:,39) = strpad('q_S6_NHE in component NHE (fmol)');
    LEGEND_ALGEBRAIC(:,69) = strpad('mu_H_i in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,70) = strpad('mu_H_o in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,71) = strpad('mu_Na_i in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,72) = strpad('mu_Na_o in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,73) = strpad('mu_S1_NHE in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,74) = strpad('mu_S2_NHE in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,75) = strpad('mu_S3_NHE in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,76) = strpad('mu_S4_NHE in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,77) = strpad('mu_S5_NHE in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,78) = strpad('mu_S6_NHE in component NHE (J_per_mol)');
    LEGEND_ALGEBRAIC(:,11) = strpad('H_tot in component NHE (fmol)');
    LEGEND_ALGEBRAIC(:,12) = strpad('H_only in component NHE (fmol)');
    LEGEND_ALGEBRAIC(:,13) = strpad('Na_tot in component NHE (fmol)');
    LEGEND_ALGEBRAIC(:,14) = strpad('S_tot in component NHE (fmol)');
    LEGEND_ALGEBRAIC(:,15) = strpad('S1S6_tot in component NHE (fmol)');
    LEGEND_ALGEBRAIC(:,85) = strpad('mu_H_i in component pH_B1 (J_per_mol)');
    LEGEND_ALGEBRAIC(:,86) = strpad('mu_B1 in component pH_B1 (J_per_mol)');
    LEGEND_ALGEBRAIC(:,87) = strpad('mu_HB1 in component pH_B1 (J_per_mol)');
    LEGEND_ALGEBRAIC(:,89) = strpad('mu_H_i in component pH_B2 (J_per_mol)');
    LEGEND_ALGEBRAIC(:,90) = strpad('mu_B2 in component pH_B2 (J_per_mol)');
    LEGEND_ALGEBRAIC(:,91) = strpad('mu_HB2 in component pH_B2 (J_per_mol)');
    LEGEND_RATES(:,1) = strpad('d/dt q_HCO3_i in component environment (fmol)');
    LEGEND_RATES(:,2) = strpad('d/dt q_HCO3_o in component environment (fmol)');
    LEGEND_RATES(:,3) = strpad('d/dt q_Cl_i in component environment (fmol)');
    LEGEND_RATES(:,4) = strpad('d/dt q_Cl_o in component environment (fmol)');
    LEGEND_RATES(:,7) = strpad('d/dt q_Na_i in component environment (fmol)');
    LEGEND_RATES(:,8) = strpad('d/dt q_Na_o in component environment (fmol)');
    LEGEND_RATES(:,5) = strpad('d/dt q_OH_i in component environment (fmol)');
    LEGEND_RATES(:,6) = strpad('d/dt q_OH_o in component environment (fmol)');
    LEGEND_RATES(:,9) = strpad('d/dt q_H_i in component environment (fmol)');
    LEGEND_RATES(:,10) = strpad('d/dt q_H_o in component environment (fmol)');
    LEGEND_RATES(:,11) = strpad('d/dt q_B1 in component environment (fmol)');
    LEGEND_RATES(:,12) = strpad('d/dt q_HB1 in component environment (fmol)');
    LEGEND_RATES(:,13) = strpad('d/dt q_B2 in component environment (fmol)');
    LEGEND_RATES(:,14) = strpad('d/dt q_HB2 in component environment (fmol)');
    LEGEND_RATES(:,15) = strpad('d/dt q_CO2_i in component environment (fmol)');
    LEGEND_RATES(:,16) = strpad('d/dt q_S1_AE in component AE (fmol)');
    LEGEND_RATES(:,17) = strpad('d/dt q_S2_AE in component AE (fmol)');
    LEGEND_RATES(:,18) = strpad('d/dt q_S3_AE in component AE (fmol)');
    LEGEND_RATES(:,19) = strpad('d/dt q_S4_AE in component AE (fmol)');
    LEGEND_RATES(:,20) = strpad('d/dt q_S5_AE in component AE (fmol)');
    LEGEND_RATES(:,21) = strpad('d/dt q_S6_AE in component AE (fmol)');
    LEGEND_RATES(:,22) = strpad('d/dt q_S1_CHE in component CHE (fmol)');
    LEGEND_RATES(:,23) = strpad('d/dt q_S2_CHE in component CHE (fmol)');
    LEGEND_RATES(:,24) = strpad('d/dt q_S3_CHE in component CHE (fmol)');
    LEGEND_RATES(:,25) = strpad('d/dt q_S4_CHE in component CHE (fmol)');
    LEGEND_RATES(:,26) = strpad('d/dt q_S5_CHE in component CHE (fmol)');
    LEGEND_RATES(:,27) = strpad('d/dt q_S6_CHE in component CHE (fmol)');
    LEGEND_RATES(:,28) = strpad('d/dt q_S1_NBC in component NBC (fmol)');
    LEGEND_RATES(:,29) = strpad('d/dt q_S2_NBC in component NBC (fmol)');
    LEGEND_RATES(:,30) = strpad('d/dt q_S3_NBC in component NBC (fmol)');
    LEGEND_RATES(:,31) = strpad('d/dt q_S4_NBC in component NBC (fmol)');
    LEGEND_RATES(:,32) = strpad('d/dt q_S5_NBC in component NBC (fmol)');
    LEGEND_RATES(:,33) = strpad('d/dt q_S6_NBC in component NBC (fmol)');
    LEGEND_RATES(:,34) = strpad('d/dt q_S1_NHE in component NHE (fmol)');
    LEGEND_RATES(:,35) = strpad('d/dt q_S2_NHE in component NHE (fmol)');
    LEGEND_RATES(:,36) = strpad('d/dt q_S3_NHE in component NHE (fmol)');
    LEGEND_RATES(:,37) = strpad('d/dt q_S4_NHE in component NHE (fmol)');
    LEGEND_RATES(:,38) = strpad('d/dt q_S5_NHE in component NHE (fmol)');
    LEGEND_RATES(:,39) = strpad('d/dt q_S6_NHE in component NHE (fmol)');
    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) = 0.24087;
    CONSTANTS(:,2) = 770508;
    CONSTANTS(:,3) = 192939;
    CONSTANTS(:,4) = 0.00694111;
    CONSTANTS(:,5) = 189724;
    CONSTANTS(:,6) = 759563;
    CONSTANTS(:,7) = 7.95543;
    CONSTANTS(:,8) = 0.0283611;
    CONSTANTS(:,9) = 64516;
    CONSTANTS(:,10) = 0.000244086;
    CONSTANTS(:,11) = 3.75516e+06;
    CONSTANTS(:,12) = 1.65076e+06;
    CONSTANTS(:,13) = 0.00018267;
    CONSTANTS(:,14) = 0.758017;
    CONSTANTS(:,15) = 167885;
    CONSTANTS(:,16) = 257175;
    CONSTANTS(:,17) = 273.529;
    CONSTANTS(:,18) = 0.00450451;
    CONSTANTS(:,19) = 43.4747;
    CONSTANTS(:,20) = 2076.03;
    CONSTANTS(:,21) = 104.001;
    CONSTANTS(:,22) = 5982.98;
    CONSTANTS(:,23) = 0.000130522;
    CONSTANTS(:,24) = 1630.31;
    CONSTANTS(:,25) = 28.278;
    CONSTANTS(:,26) = 974.566;
    CONSTANTS(:,27) = 0;
    CONSTANTS(:,28) = 0.0766951;
    CONSTANTS(:,29) = 3.80421e-05;
    CONSTANTS(:,30) = 0.30705;
    CONSTANTS(:,31) = 0.000151922;
    CONSTANTS(:,32) = 1440.61;
    CONSTANTS(:,33) = 9682.05;
    CONSTANTS(:,34) = 51000.5;
    CONSTANTS(:,35) = 51864.9;
    CONSTANTS(:,36) = 0.664318;
    CONSTANTS(:,37) = 1461.37;
    CONSTANTS(:,38) = 0.698479;
    CONSTANTS(:,39) = 345.593;
    CONSTANTS(:,40) = 31.4251;
    CONSTANTS(:,41) = 28954.8;
    CONSTANTS(:,42) = 279001;
    CONSTANTS(:,43) = 4793.4;
    CONSTANTS(:,44) = 0.0335637;
    CONSTANTS(:,45) = 0.539255;
    CONSTANTS(:,46) = 1.01691e+08;
    CONSTANTS(:,47) = 39.0188;
    CONSTANTS(:,48) = 0.67679;
    CONSTANTS(:,49) = 0.000228816;
    CONSTANTS(:,50) = 73.877;
    CONSTANTS(:,51) = 290.079;
    CONSTANTS(:,52) = 0.00031185;
    CONSTANTS(:,53) = 17804.4;
    CONSTANTS(:,54) = 1.12019e+06;
    CONSTANTS(:,55) = 13.1527;
    CONSTANTS(:,56) = 0.0131633;
    CONSTANTS(:,57) = 6.13294e-05;
    CONSTANTS(:,58) = 8022.54;
    CONSTANTS(:,59) = 217.335;
    CONSTANTS(:,60) = 797.586;
    CONSTANTS(:,61) = 1.35735;
    CONSTANTS(:,62) = 0.000225558;
    CONSTANTS(:,63) = 39.3849;
    CONSTANTS(:,64) = 0.0370592;
    CONSTANTS(:,65) = 1e-6;
    CONSTANTS(:,66) = 1e-6;
    STATES(:,1) = 509.2;
    STATES(:,2) = 122.2952;
    STATES(:,3) = 824.6;
    STATES(:,4) = 725.48;
    STATES(:,5) = 5.36764E-06;
    STATES(:,6) = 1.30166E-06;
    STATES(:,7) = 481.46;
    STATES(:,8) = 725.48;
    STATES(:,9) = 2.69019E-06;
    STATES(:,10) = 2.06299E-07;
    STATES(:,11) = 0.05*3199.6; % pH_B1 (fmol)
    STATES(:,12) = 1e-12;
    STATES(:,13) = 1117.2;
    STATES(:,14) = 1e-12;
    STATES(:,15) = 46.968;
    CONSTANTS(:,67) = 38;
    CONSTANTS(:,68) = 5.182;
    CONSTANTS(:,69) = 8.31;
    CONSTANTS(:,70) = 310;
    CONSTANTS(:,71) = 96485;
    STATES(:,16) = 1.666e-7;
    STATES(:,17) = 1.666e-7;
    STATES(:,18) = 1.666e-7;
    STATES(:,19) = 1.666e-7;
    STATES(:,20) = 1.666e-7;
    STATES(:,21) = 1.666e-7;
    STATES(:,22) = 1.666e-7;
    STATES(:,23) = 1.666e-7;
    STATES(:,24) = 1.666e-7;
    STATES(:,25) = 1.666e-7;
    STATES(:,26) = 1.666e-7;
    STATES(:,27) = 1.666e-7;
    STATES(:,28) = 1.666e-7;
    STATES(:,29) = 1.666e-7;
    STATES(:,30) = 1.666e-7;
    STATES(:,31) = 1.666e-7;
    STATES(:,32) = 1.666e-7;
    STATES(:,33) = 1.666e-7;
    STATES(:,34) = 1.666e-7;
    STATES(:,35) = 1.666e-7;
    STATES(:,36) = 1.666e-7;
    STATES(:,37) = 1.666e-7;
    STATES(:,38) = 1.666e-7;
    STATES(:,39) = 1.666e-7;
    if (isempty(STATES)), warning('Initial values for states not set');, end
end

function [RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS)
	disp(VOI)
    global algebraicVariableCount;
    statesSize = size(STATES);
    statesColumnCount = statesSize(2);
    if ( statesColumnCount == 1)
        STATES = STATES';
        ALGEBRAIC = zeros(1, algebraicVariableCount);
        utilOnes = 1;
    else
        statesRowCount = statesSize(1);
        ALGEBRAIC = zeros(statesRowCount, algebraicVariableCount);
        RATES = zeros(statesRowCount, statesColumnCount);
        utilOnes = ones(statesRowCount, 1);
    end
    ALGEBRAIC(:,21) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,32).*STATES(:,16));
    ALGEBRAIC(:,26) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,37).*STATES(:,21));
    ALGEBRAIC(:,27) =  CONSTANTS(:,1).*(exp(ALGEBRAIC(:,26)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,21)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,18) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,29).*STATES(:,2));
    ALGEBRAIC(:,22) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,33).*STATES(:,17));
    ALGEBRAIC(:,28) =  CONSTANTS(:,2).*(exp(ALGEBRAIC(:,21)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,22)+ALGEBRAIC(:,18))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,16) = ALGEBRAIC(:,27) - ALGEBRAIC(:,28);
    ALGEBRAIC(:,20) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,31).*STATES(:,4));
    ALGEBRAIC(:,23) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,34).*STATES(:,18));
    ALGEBRAIC(:,29) =  CONSTANTS(:,3).*(exp((ALGEBRAIC(:,22)+ALGEBRAIC(:,20))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,23)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,17) = ALGEBRAIC(:,28) - ALGEBRAIC(:,29);
    ALGEBRAIC(:,24) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,35).*STATES(:,19));
    ALGEBRAIC(:,30) =  CONSTANTS(:,4).*(exp(ALGEBRAIC(:,23)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,24)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,18) = ALGEBRAIC(:,29) - ALGEBRAIC(:,30);
    ALGEBRAIC(:,19) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,30).*STATES(:,3));
    ALGEBRAIC(:,25) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,36).*STATES(:,20));
    ALGEBRAIC(:,31) =  CONSTANTS(:,5).*(exp(ALGEBRAIC(:,24)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,25)+ALGEBRAIC(:,19))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,19) = ALGEBRAIC(:,30) - ALGEBRAIC(:,31);
    ALGEBRAIC(:,3) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,28).*STATES(:,1));
    ALGEBRAIC(:,33) =  CONSTANTS(:,6).*(exp((ALGEBRAIC(:,25)+ALGEBRAIC(:,3))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,26)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,20) = ALGEBRAIC(:,31) - ALGEBRAIC(:,33);
    RATES(:,21) = ALGEBRAIC(:,33) - ALGEBRAIC(:,27);
    ALGEBRAIC(:,35) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,46).*STATES(:,15));
    ALGEBRAIC(:,37) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,28).*STATES(:,1));
    ALGEBRAIC(:,39) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,47).*STATES(:,9));
    ALGEBRAIC(:,41) =  CONSTANTS(:,13).*(exp(ALGEBRAIC(:,35)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,37)+ALGEBRAIC(:,39))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,15) =  - ALGEBRAIC(:,41);
    ALGEBRAIC(:,34) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,39).*STATES(:,6));
    ALGEBRAIC(:,40) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,40).*STATES(:,22));
    ALGEBRAIC(:,42) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,41).*STATES(:,23));
    ALGEBRAIC(:,54) =  CONSTANTS(:,8).*(exp(ALGEBRAIC(:,40)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,42)+ALGEBRAIC(:,34))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,6) = ALGEBRAIC(:,54);
    ALGEBRAIC(:,50) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,45).*STATES(:,27));
    ALGEBRAIC(:,52) =  CONSTANTS(:,7).*(exp(ALGEBRAIC(:,50)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,40)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,22) = ALGEBRAIC(:,52) - ALGEBRAIC(:,54);
    ALGEBRAIC(:,38) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,31).*STATES(:,4));
    ALGEBRAIC(:,44) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,42).*STATES(:,24));
    ALGEBRAIC(:,56) =  CONSTANTS(:,9).*(exp((ALGEBRAIC(:,42)+ALGEBRAIC(:,38))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,44)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,4) =  - ALGEBRAIC(:,29) - ALGEBRAIC(:,56);
    RATES(:,23) = ALGEBRAIC(:,54) - ALGEBRAIC(:,56);
    ALGEBRAIC(:,46) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,43).*STATES(:,25));
    ALGEBRAIC(:,58) =  CONSTANTS(:,10).*(exp(ALGEBRAIC(:,44)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,46)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,24) = ALGEBRAIC(:,56) - ALGEBRAIC(:,58);
    ALGEBRAIC(:,36) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,30).*STATES(:,3));
    ALGEBRAIC(:,48) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,44).*STATES(:,26));
    ALGEBRAIC(:,60) =  CONSTANTS(:,11).*(exp(ALGEBRAIC(:,46)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,48)+ALGEBRAIC(:,36))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,3) = ALGEBRAIC(:,31)+ALGEBRAIC(:,60);
    RATES(:,25) = ALGEBRAIC(:,58) - ALGEBRAIC(:,60);
    ALGEBRAIC(:,32) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,38).*STATES(:,5));
    ALGEBRAIC(:,62) =  CONSTANTS(:,12).*(exp((ALGEBRAIC(:,48)+ALGEBRAIC(:,32))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,50)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,5) =  - ALGEBRAIC(:,62);
    RATES(:,26) = ALGEBRAIC(:,60) - ALGEBRAIC(:,62);
    RATES(:,27) = ALGEBRAIC(:,62) - ALGEBRAIC(:,52);
    ALGEBRAIC(:,51) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,50).*STATES(:,28));
    ALGEBRAIC(:,61) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,55).*STATES(:,33));
    ALGEBRAIC(:,63) =  CONSTANTS(:,14).*(exp(ALGEBRAIC(:,61)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,51)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,45) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,49).*STATES(:,8));
    ALGEBRAIC(:,53) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,51).*STATES(:,29));
    ALGEBRAIC(:,64) =  CONSTANTS(:,15).*(exp((ALGEBRAIC(:,45)+ALGEBRAIC(:,51))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,53)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,28) = ALGEBRAIC(:,63) - ALGEBRAIC(:,64);
    ALGEBRAIC(:,49) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,29).*STATES(:,2));
    ALGEBRAIC(:,55) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,52).*STATES(:,30));
    ALGEBRAIC(:,65) =  CONSTANTS(:,16).*(exp((ALGEBRAIC(:,49)+ALGEBRAIC(:,53))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,55)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,2) = ALGEBRAIC(:,28) - ALGEBRAIC(:,65);
    RATES(:,29) = ALGEBRAIC(:,64) - ALGEBRAIC(:,65);
    ALGEBRAIC(:,57) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,53).*STATES(:,31));
    ALGEBRAIC(:,66) =  CONSTANTS(:,17).*(exp(ALGEBRAIC(:,55)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,57)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,30) = ALGEBRAIC(:,65) - ALGEBRAIC(:,66);
    ALGEBRAIC(:,47) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,28).*STATES(:,1));
    ALGEBRAIC(:,59) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,54).*STATES(:,32));
    ALGEBRAIC(:,67) =  CONSTANTS(:,18).*(exp(ALGEBRAIC(:,57)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,59)+ALGEBRAIC(:,47))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,1) =  - ALGEBRAIC(:,33)+ALGEBRAIC(:,67)+ALGEBRAIC(:,41);
    RATES(:,31) = ALGEBRAIC(:,66) - ALGEBRAIC(:,67);
    ALGEBRAIC(:,43) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,48).*STATES(:,7));
    ALGEBRAIC(:,68) =  CONSTANTS(:,19).*(exp(ALGEBRAIC(:,59)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,61)+ALGEBRAIC(:,43))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,32) = ALGEBRAIC(:,67) - ALGEBRAIC(:,68);
    RATES(:,33) = ALGEBRAIC(:,68) - ALGEBRAIC(:,63);
    ALGEBRAIC(:,70) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,56).*STATES(:,10));
    ALGEBRAIC(:,73) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,57).*STATES(:,34));
    ALGEBRAIC(:,74) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,58).*STATES(:,35));
    ALGEBRAIC(:,80) =  CONSTANTS(:,21).*(exp(ALGEBRAIC(:,73)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,74)+ALGEBRAIC(:,70))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,10) = ALGEBRAIC(:,80);
    ALGEBRAIC(:,78) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,62).*STATES(:,39));
    ALGEBRAIC(:,79) =  CONSTANTS(:,20).*(exp(ALGEBRAIC(:,78)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,73)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,34) = ALGEBRAIC(:,79) - ALGEBRAIC(:,80);
    ALGEBRAIC(:,72) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,49).*STATES(:,8));
    ALGEBRAIC(:,75) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,59).*STATES(:,36));
    ALGEBRAIC(:,81) =  CONSTANTS(:,22).*(exp((ALGEBRAIC(:,74)+ALGEBRAIC(:,72))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,75)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,8) =  - ALGEBRAIC(:,64) - ALGEBRAIC(:,81);
    RATES(:,35) = ALGEBRAIC(:,80) - ALGEBRAIC(:,81);
    ALGEBRAIC(:,76) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,60).*STATES(:,37));
    ALGEBRAIC(:,82) =  CONSTANTS(:,23).*(exp(ALGEBRAIC(:,75)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,76)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,36) = ALGEBRAIC(:,81) - ALGEBRAIC(:,82);
    ALGEBRAIC(:,71) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,48).*STATES(:,7));
    ALGEBRAIC(:,77) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,61).*STATES(:,38));
    ALGEBRAIC(:,83) =  CONSTANTS(:,24).*(exp(ALGEBRAIC(:,76)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,77)+ALGEBRAIC(:,71))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,7) = ALGEBRAIC(:,68)+ALGEBRAIC(:,83);
    RATES(:,37) = ALGEBRAIC(:,82) - ALGEBRAIC(:,83);
    ALGEBRAIC(:,69) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,47).*STATES(:,9));
    ALGEBRAIC(:,84) =  CONSTANTS(:,25).*(exp((ALGEBRAIC(:,77)+ALGEBRAIC(:,69))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,78)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,38) = ALGEBRAIC(:,83) - ALGEBRAIC(:,84);
    RATES(:,39) = ALGEBRAIC(:,84) - ALGEBRAIC(:,79);
    ALGEBRAIC(:,85) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,47).*STATES(:,9));
    ALGEBRAIC(:,86) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,63).*STATES(:,11));
    ALGEBRAIC(:,87) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,64).*STATES(:,12));
    ALGEBRAIC(:,88) =  CONSTANTS(:,26).*(exp((ALGEBRAIC(:,85)+ALGEBRAIC(:,86))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,87)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,11) =  - ALGEBRAIC(:,88);
    RATES(:,12) = ALGEBRAIC(:,88);
    ALGEBRAIC(:,89) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,47).*STATES(:,9));
    ALGEBRAIC(:,90) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,65).*STATES(:,13));
    ALGEBRAIC(:,91) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,66).*STATES(:,14));
    ALGEBRAIC(:,92) =  CONSTANTS(:,27).*(exp((ALGEBRAIC(:,89)+ALGEBRAIC(:,90))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,91)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    RATES(:,9) = (( - ALGEBRAIC(:,84) - ALGEBRAIC(:,88)) - ALGEBRAIC(:,92))+ALGEBRAIC(:,41);
    RATES(:,13) =  - ALGEBRAIC(:,92);
    RATES(:,14) = ALGEBRAIC(:,92);
   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(:,21) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,32).*STATES(:,16));
    ALGEBRAIC(:,26) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,37).*STATES(:,21));
    ALGEBRAIC(:,27) =  CONSTANTS(:,1).*(exp(ALGEBRAIC(:,26)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,21)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,18) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,29).*STATES(:,2));
    ALGEBRAIC(:,22) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,33).*STATES(:,17));
    ALGEBRAIC(:,28) =  CONSTANTS(:,2).*(exp(ALGEBRAIC(:,21)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,22)+ALGEBRAIC(:,18))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,20) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,31).*STATES(:,4));
    ALGEBRAIC(:,23) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,34).*STATES(:,18));
    ALGEBRAIC(:,29) =  CONSTANTS(:,3).*(exp((ALGEBRAIC(:,22)+ALGEBRAIC(:,20))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,23)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,24) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,35).*STATES(:,19));
    ALGEBRAIC(:,30) =  CONSTANTS(:,4).*(exp(ALGEBRAIC(:,23)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,24)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,19) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,30).*STATES(:,3));
    ALGEBRAIC(:,25) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,36).*STATES(:,20));
    ALGEBRAIC(:,31) =  CONSTANTS(:,5).*(exp(ALGEBRAIC(:,24)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,25)+ALGEBRAIC(:,19))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,3) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,28).*STATES(:,1));
    ALGEBRAIC(:,33) =  CONSTANTS(:,6).*(exp((ALGEBRAIC(:,25)+ALGEBRAIC(:,3))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,26)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,35) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,46).*STATES(:,15));
    ALGEBRAIC(:,37) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,28).*STATES(:,1));
    ALGEBRAIC(:,39) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,47).*STATES(:,9));
    ALGEBRAIC(:,41) =  CONSTANTS(:,13).*(exp(ALGEBRAIC(:,35)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,37)+ALGEBRAIC(:,39))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,34) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,39).*STATES(:,6));
    ALGEBRAIC(:,40) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,40).*STATES(:,22));
    ALGEBRAIC(:,42) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,41).*STATES(:,23));
    ALGEBRAIC(:,54) =  CONSTANTS(:,8).*(exp(ALGEBRAIC(:,40)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,42)+ALGEBRAIC(:,34))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,50) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,45).*STATES(:,27));
    ALGEBRAIC(:,52) =  CONSTANTS(:,7).*(exp(ALGEBRAIC(:,50)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,40)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,38) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,31).*STATES(:,4));
    ALGEBRAIC(:,44) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,42).*STATES(:,24));
    ALGEBRAIC(:,56) =  CONSTANTS(:,9).*(exp((ALGEBRAIC(:,42)+ALGEBRAIC(:,38))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,44)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,46) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,43).*STATES(:,25));
    ALGEBRAIC(:,58) =  CONSTANTS(:,10).*(exp(ALGEBRAIC(:,44)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,46)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,36) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,30).*STATES(:,3));
    ALGEBRAIC(:,48) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,44).*STATES(:,26));
    ALGEBRAIC(:,60) =  CONSTANTS(:,11).*(exp(ALGEBRAIC(:,46)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,48)+ALGEBRAIC(:,36))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,32) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,38).*STATES(:,5));
    ALGEBRAIC(:,62) =  CONSTANTS(:,12).*(exp((ALGEBRAIC(:,48)+ALGEBRAIC(:,32))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,50)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,51) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,50).*STATES(:,28));
    ALGEBRAIC(:,61) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,55).*STATES(:,33));
    ALGEBRAIC(:,63) =  CONSTANTS(:,14).*(exp(ALGEBRAIC(:,61)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,51)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,45) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,49).*STATES(:,8));
    ALGEBRAIC(:,53) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,51).*STATES(:,29));
    ALGEBRAIC(:,64) =  CONSTANTS(:,15).*(exp((ALGEBRAIC(:,45)+ALGEBRAIC(:,51))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,53)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,49) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,29).*STATES(:,2));
    ALGEBRAIC(:,55) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,52).*STATES(:,30));
    ALGEBRAIC(:,65) =  CONSTANTS(:,16).*(exp((ALGEBRAIC(:,49)+ALGEBRAIC(:,53))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,55)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,57) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,53).*STATES(:,31));
    ALGEBRAIC(:,66) =  CONSTANTS(:,17).*(exp(ALGEBRAIC(:,55)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,57)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,47) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,28).*STATES(:,1));
    ALGEBRAIC(:,59) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,54).*STATES(:,32));
    ALGEBRAIC(:,67) =  CONSTANTS(:,18).*(exp(ALGEBRAIC(:,57)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,59)+ALGEBRAIC(:,47))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,43) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,48).*STATES(:,7));
    ALGEBRAIC(:,68) =  CONSTANTS(:,19).*(exp(ALGEBRAIC(:,59)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,61)+ALGEBRAIC(:,43))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,70) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,56).*STATES(:,10));
    ALGEBRAIC(:,73) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,57).*STATES(:,34));
    ALGEBRAIC(:,74) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,58).*STATES(:,35));
    ALGEBRAIC(:,80) =  CONSTANTS(:,21).*(exp(ALGEBRAIC(:,73)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,74)+ALGEBRAIC(:,70))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,78) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,62).*STATES(:,39));
    ALGEBRAIC(:,79) =  CONSTANTS(:,20).*(exp(ALGEBRAIC(:,78)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,73)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,72) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,49).*STATES(:,8));
    ALGEBRAIC(:,75) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,59).*STATES(:,36));
    ALGEBRAIC(:,81) =  CONSTANTS(:,22).*(exp((ALGEBRAIC(:,74)+ALGEBRAIC(:,72))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,75)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,76) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,60).*STATES(:,37));
    ALGEBRAIC(:,82) =  CONSTANTS(:,23).*(exp(ALGEBRAIC(:,75)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,76)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,71) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,48).*STATES(:,7));
    ALGEBRAIC(:,77) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,61).*STATES(:,38));
    ALGEBRAIC(:,83) =  CONSTANTS(:,24).*(exp(ALGEBRAIC(:,76)./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp((ALGEBRAIC(:,77)+ALGEBRAIC(:,71))./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,69) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,47).*STATES(:,9));
    ALGEBRAIC(:,84) =  CONSTANTS(:,25).*(exp((ALGEBRAIC(:,77)+ALGEBRAIC(:,69))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,78)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,85) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,47).*STATES(:,9));
    ALGEBRAIC(:,86) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,63).*STATES(:,11));
    ALGEBRAIC(:,87) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,64).*STATES(:,12));
    ALGEBRAIC(:,88) =  CONSTANTS(:,26).*(exp((ALGEBRAIC(:,85)+ALGEBRAIC(:,86))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,87)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,89) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,47).*STATES(:,9));
    ALGEBRAIC(:,90) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,65).*STATES(:,13));
    ALGEBRAIC(:,91) =  CONSTANTS(:,69).*CONSTANTS(:,70).*log( CONSTANTS(:,66).*STATES(:,14));
    ALGEBRAIC(:,92) =  CONSTANTS(:,27).*(exp((ALGEBRAIC(:,89)+ALGEBRAIC(:,90))./( CONSTANTS(:,69).*CONSTANTS(:,70))) - exp(ALGEBRAIC(:,91)./( CONSTANTS(:,69).*CONSTANTS(:,70))));
    ALGEBRAIC(:,1) = STATES(:,9)./CONSTANTS(:,67);
    ALGEBRAIC(:,2) = STATES(:,10)./CONSTANTS(:,68);
    ALGEBRAIC(:,4) = STATES(:,1)+STATES(:,2)+STATES(:,16)+STATES(:,21);
    ALGEBRAIC(:,5) = STATES(:,16)+STATES(:,17)+STATES(:,18)+STATES(:,19)+STATES(:,20)+STATES(:,21);
    ALGEBRAIC(:,6) = STATES(:,16)+STATES(:,21);
    ALGEBRAIC(:,7) = STATES(:,3)+STATES(:,4)+STATES(:,18)+STATES(:,19);
    ALGEBRAIC(:,8) = STATES(:,1)+STATES(:,2)+STATES(:,30)+STATES(:,31);
    ALGEBRAIC(:,9) = STATES(:,28)+STATES(:,29)+STATES(:,30)+STATES(:,31)+STATES(:,32)+STATES(:,33);
    ALGEBRAIC(:,10) = STATES(:,7)+STATES(:,8)+STATES(:,33)+STATES(:,28);
    ALGEBRAIC(:,11) = STATES(:,9)+STATES(:,10)+STATES(:,34)+STATES(:,39);
    ALGEBRAIC(:,12) = STATES(:,9)+STATES(:,10);
    ALGEBRAIC(:,13) = STATES(:,7)+STATES(:,8)+STATES(:,36)+STATES(:,37);
    ALGEBRAIC(:,14) = STATES(:,34)+STATES(:,35)+STATES(:,36)+STATES(:,37)+STATES(:,38)+STATES(:,39);
    ALGEBRAIC(:,15) = STATES(:,34)+STATES(:,39);
    ALGEBRAIC(:,16) =  - arbitrary_log(ALGEBRAIC(:,1), 10);
    ALGEBRAIC(:,17) =  - arbitrary_log(ALGEBRAIC(:,2), 10);
end

% Compute a logarithm to any base" +
function x = arbitrary_log(a, base)
    x = log(a) ./ log(base);
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

