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 =140;
end
% There are a total of 43 entries in each of the rate and state variable arrays.
% There are a total of 112 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, 1e1];

    % Set numerical accuracy options for ODE solver
    options = odeset('RelTol', 1e-05, 'AbsTol', 1e-16, '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 = {        'q_Ca_i in component environment (fmol)',...
        'q_Ca_SR in component environment (fmol)',...
        'q_IP3 in component environment (fmol)',...
        };
    [~, i_st, i_alg] = find_indices(vlabels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
    if i_st
        plot_selected(i_st,VOI,STATES,'s',LEGEND_STATES,'STATES',ceil(sqrt(length(i_st))))
    end
    if i_alg
        plot_selected(i_alg,VOI,ALGEBRAIC,'s',LEGEND_ALGEBRAIC,'ALGEBRAIC',ceil(sqrt(length(i_alg))))
    end

    % fluxes
    flabs = {'v_R_IPR_main in component IPR (fmol_per_sec)',...
        'v_leak in component Ca_leak (fmol_per_sec)',...
        'v_Ca_i_SERCA in component SERCA (fmol_per_sec)'};
    [~, ~, i_alg] = find_indices(flabs, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
%     plot_selected(i_alg,VOI,ALGEBRAIC,'s',LEGEND_ALGEBRAIC,'EC FLUXES',ceil(sqrt(length(i_alg))))

    if true
        % convert v (fmol/s) to i (uA/uF) 
        F = 96485;
        Cm = 6e-5; % Unit microF %0.001;
        conv = 1e-9*F/Cm;
        figure();
        for j=1:length(flabs)
            subplot(2,2,j)
            plot(VOI, ALGEBRAIC(:,i_alg(j))*conv);
            xlabel(LEGEND_VOI);
            title(flabs(j));
            if verLessThan('matlab','9.5')
                suptitle('AP fluxes uA/uF')    
            else
                sgtitle('AP fluxes uA/uF')    
            end
        end
    end

    if false
        % Ca conservation 
        Ca_labels = {'q_Ca_i in component environment (fmol)',...
            'q_Ca_SR in component environment (fmol)',...
            };
        serca_ca = {};
        [~, i_st, ~] = find_indices(Ca_labels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
        assert(length(Ca_labels)==length(i_st))
        figure,
        plot(VOI, sum(STATES(:,i_st),2));
        title('sum Ca fmol (no gates)')
    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;
    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_leak in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,2) = strpad('kappa_R_IPR_main in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,3) = strpad('kappa_R1_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,4) = strpad('kappa_R2_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,5) = strpad('kappa_R3_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,6) = strpad('kappa_R4_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,7) = strpad('kappa_R5_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,8) = strpad('kappa_R6_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,9) = strpad('kappa_R7_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,10) = strpad('kappa_R8_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,11) = strpad('kappa_R9_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,12) = strpad('kappa_R10_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,13) = strpad('kappa_R11_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,14) = strpad('kappa_R12_a_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,15) = strpad('kappa_R1_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,16) = strpad('kappa_R2_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,17) = strpad('kappa_R3_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,18) = strpad('kappa_R4_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,19) = strpad('kappa_R5_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,20) = strpad('kappa_R6_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,21) = strpad('kappa_R7_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,22) = strpad('kappa_R8_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,23) = strpad('kappa_R9_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,24) = strpad('kappa_R10_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,25) = strpad('kappa_R11_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,26) = strpad('kappa_R12_b_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,27) = strpad('kappa_R1_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,28) = strpad('kappa_R2_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,29) = strpad('kappa_R3_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,30) = strpad('kappa_R4_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,31) = strpad('kappa_R5_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,32) = strpad('kappa_R6_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,33) = strpad('kappa_R7_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,34) = strpad('kappa_R8_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,35) = strpad('kappa_R9_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,36) = strpad('kappa_R10_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,37) = strpad('kappa_R11_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,38) = strpad('kappa_R12_c_IPR in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,39) = strpad('kappa_pCa_R1 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,40) = strpad('kappa_pCa_R2 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,41) = strpad('kappa_SERCA_R1_2 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,42) = strpad('kappa_SERCA_R2_4 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,43) = strpad('kappa_SERCA_R2_2a in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,44) = strpad('kappa_SERCA_R4_5 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,45) = strpad('kappa_SERCA_R5_6 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,46) = strpad('kappa_SERCA_R6_8 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,47) = strpad('kappa_SERCA_R8_9 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,48) = strpad('kappa_SERCA_R9_10 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,49) = strpad('kappa_SERCA_R10_1 in component BG_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,50) = strpad('K_Ca_SR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,51) = strpad('K_Ca_i in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,52) = strpad('K_IP3 in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,53) = strpad('K_Sa_000_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,54) = strpad('K_Sa_001_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,55) = strpad('K_Sa_010_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,56) = strpad('K_Sa_011_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,57) = strpad('K_Sa_100_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,58) = strpad('K_Sa_101_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,59) = strpad('K_Sa_110_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,60) = strpad('K_Sa_111_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,61) = strpad('K_Sb_000_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,62) = strpad('K_Sb_001_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,63) = strpad('K_Sb_010_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,64) = strpad('K_Sb_011_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,65) = strpad('K_Sb_100_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,66) = strpad('K_Sb_101_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,67) = strpad('K_Sb_110_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,68) = strpad('K_Sb_111_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,69) = strpad('K_Sc_000_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,70) = strpad('K_Sc_001_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,71) = strpad('K_Sc_010_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,72) = strpad('K_Sc_011_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,73) = strpad('K_Sc_100_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,74) = strpad('K_Sc_101_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,75) = strpad('K_Sc_110_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,76) = strpad('K_Sc_111_IPR in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,77) = strpad('K_Ca_o in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,78) = strpad('K_pCa in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,79) = strpad('K_pCa_Ca in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,80) = strpad('K_P1_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,81) = strpad('K_P2_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,82) = strpad('K_P2a_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,83) = strpad('K_P4_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,84) = strpad('K_P5_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,85) = strpad('K_P6_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,86) = strpad('K_P8_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,87) = strpad('K_P9_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,88) = strpad('K_P10_SERCA in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,89) = strpad('K_H in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,90) = strpad('K_MgATP in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,91) = strpad('K_MgADP in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,92) = strpad('K_Pi in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,93) = strpad('K_PLBp in component BG_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,94) = strpad('zCa in component BG_parameters (dimensionless)');
    LEGEND_VOI = strpad('time in component environment (second)');
    LEGEND_CONSTANTS(:,95) = strpad('vol_myo in component environment (pL)');
    LEGEND_CONSTANTS(:,96) = strpad('freq in component environment (dimensionless)');
    LEGEND_STATES(:,1) = strpad('q_Ca_i in component environment (fmol)');
    LEGEND_STATES(:,2) = strpad('q_Ca_SR in component environment (fmol)');
    LEGEND_STATES(:,3) = strpad('q_IP3 in component environment (fmol)');
    LEGEND_STATES(:,4) = strpad('q_Ca_o in component environment (fmol)');
    LEGEND_STATES(:,5) = strpad('q_pCa in component environment (fmol)');
    LEGEND_STATES(:,6) = strpad('q_pCa_Ca in component environment (fmol)');
    LEGEND_STATES(:,7) = strpad('q_Sa_000_IPR in component environment (fmol)');
    LEGEND_STATES(:,8) = strpad('q_Sa_001_IPR in component environment (fmol)');
    LEGEND_STATES(:,9) = strpad('q_Sa_010_IPR in component environment (fmol)');
    LEGEND_STATES(:,10) = strpad('q_Sa_011_IPR in component environment (fmol)');
    LEGEND_STATES(:,11) = strpad('q_Sa_100_IPR in component environment (fmol)');
    LEGEND_STATES(:,12) = strpad('q_Sa_101_IPR in component environment (fmol)');
    LEGEND_STATES(:,13) = strpad('q_Sa_110_IPR in component environment (fmol)');
    LEGEND_STATES(:,14) = strpad('q_Sa_111_IPR in component environment (fmol)');
    LEGEND_STATES(:,15) = strpad('q_Sb_000_IPR in component environment (fmol)');
    LEGEND_STATES(:,16) = strpad('q_Sb_001_IPR in component environment (fmol)');
    LEGEND_STATES(:,17) = strpad('q_Sb_010_IPR in component environment (fmol)');
    LEGEND_STATES(:,18) = strpad('q_Sb_011_IPR in component environment (fmol)');
    LEGEND_STATES(:,19) = strpad('q_Sb_100_IPR in component environment (fmol)');
    LEGEND_STATES(:,20) = strpad('q_Sb_101_IPR in component environment (fmol)');
    LEGEND_STATES(:,21) = strpad('q_Sb_110_IPR in component environment (fmol)');
    LEGEND_STATES(:,22) = strpad('q_Sb_111_IPR in component environment (fmol)');
    LEGEND_STATES(:,23) = strpad('q_Sc_000_IPR in component environment (fmol)');
    LEGEND_STATES(:,24) = strpad('q_Sc_001_IPR in component environment (fmol)');
    LEGEND_STATES(:,25) = strpad('q_Sc_010_IPR in component environment (fmol)');
    LEGEND_STATES(:,26) = strpad('q_Sc_011_IPR in component environment (fmol)');
    LEGEND_STATES(:,27) = strpad('q_Sc_100_IPR in component environment (fmol)');
    LEGEND_STATES(:,28) = strpad('q_Sc_101_IPR in component environment (fmol)');
    LEGEND_STATES(:,29) = strpad('q_Sc_110_IPR in component environment (fmol)');
    LEGEND_STATES(:,30) = strpad('q_Sc_111_IPR in component environment (fmol)');
    LEGEND_STATES(:,31) = strpad('q_H in component environment (fmol)');
    LEGEND_STATES(:,32) = strpad('q_Pi in component environment (fmol)');
    LEGEND_STATES(:,33) = strpad('q_MgADP in component environment (fmol)');
    LEGEND_STATES(:,34) = strpad('q_MgATP in component environment (fmol)');
    LEGEND_STATES(:,35) = strpad('q_P1_SERCA in component environment (fmol)');
    LEGEND_STATES(:,36) = strpad('q_P2_SERCA in component environment (fmol)');
    LEGEND_STATES(:,37) = strpad('q_P2a_SERCA in component environment (fmol)');
    LEGEND_STATES(:,38) = strpad('q_P4_SERCA in component environment (fmol)');
    LEGEND_STATES(:,39) = strpad('q_P5_SERCA in component environment (fmol)');
    LEGEND_STATES(:,40) = strpad('q_P6_SERCA in component environment (fmol)');
    LEGEND_STATES(:,41) = strpad('q_P8_SERCA in component environment (fmol)');
    LEGEND_STATES(:,42) = strpad('q_P9_SERCA in component environment (fmol)');
    LEGEND_STATES(:,43) = strpad('q_P10_SERCA in component environment (fmol)');
    LEGEND_CONSTANTS(:,97) = strpad('q_PLBp in component environment (fmol)');
    LEGEND_CONSTANTS(:,98) = strpad('q_PLBp_baseline in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,10) = strpad('v_leak in component Ca_leak (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,39) = strpad('v_R_IPR_main in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,40) = strpad('v_R1_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,41) = strpad('v_R2_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,42) = strpad('v_R3_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,43) = strpad('v_R4_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,44) = strpad('v_R5_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,45) = strpad('v_R6_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,46) = strpad('v_R7_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,47) = strpad('v_R8_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,48) = strpad('v_R9_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,49) = strpad('v_R10_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,50) = strpad('v_R11_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,51) = strpad('v_R12_a_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,52) = strpad('v_R1_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,53) = strpad('v_R2_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,54) = strpad('v_R3_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,55) = strpad('v_R4_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,56) = strpad('v_R5_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,57) = strpad('v_R6_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,58) = strpad('v_R7_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,59) = strpad('v_R8_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,60) = strpad('v_R9_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,61) = strpad('v_R10_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,62) = strpad('v_R11_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,63) = strpad('v_R12_b_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,64) = strpad('v_R1_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,65) = strpad('v_R2_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,66) = strpad('v_R3_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,67) = strpad('v_R4_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,68) = strpad('v_R5_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,69) = strpad('v_R6_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,70) = strpad('v_R7_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,71) = strpad('v_R8_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,72) = strpad('v_R9_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,74) = strpad('v_R10_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,76) = strpad('v_R11_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,78) = strpad('v_R12_c_IPR in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,80) = strpad('v_pCa_R1 in component pCa (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,81) = strpad('v_pCa_R2 in component pCa (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,127) = strpad('v_Ca_i_SERCA in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,133) = strpad('v_Ca_SR_SERCA in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,138) = strpad('v_H_SERCA in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,124) = strpad('v_MgADP_SERCA in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,121) = strpad('v_MgATP_SERCA in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,119) = strpad('v_Pi_SERCA in component SERCA (fmol_per_sec)');
    LEGEND_CONSTANTS(:,99) = strpad('Ca_influx in component environment (fmol_per_sec)');
    LEGEND_CONSTANTS(:,100) = strpad('Ca_influx_IP3 in component environment (per_sec)');
    LEGEND_CONSTANTS(:,101) = strpad('R in component constants (J_per_K_per_mol)');
    LEGEND_CONSTANTS(:,102) = strpad('T in component constants (kelvin)');
    LEGEND_CONSTANTS(:,103) = strpad('F in component constants (C_per_mol)');
    LEGEND_ALGEBRAIC(:,8) = strpad('mu_Ca_SR in component Ca_leak (J_per_mol)');
    LEGEND_ALGEBRAIC(:,9) = strpad('mu_Ca_i in component Ca_leak (J_per_mol)');
    LEGEND_ALGEBRAIC(:,11) = strpad('mu_Ca_i in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,12) = strpad('mu_Ca_SR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,14) = strpad('mu_IP3 in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,15) = strpad('mu_Sa_000_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,16) = strpad('mu_Sa_001_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,17) = strpad('mu_Sa_010_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,18) = strpad('mu_Sa_011_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,19) = strpad('mu_Sa_100_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,20) = strpad('mu_Sa_101_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,21) = strpad('mu_Sa_110_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,22) = strpad('mu_Sa_111_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,23) = strpad('mu_Sb_000_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,24) = strpad('mu_Sb_001_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,25) = strpad('mu_Sb_010_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,26) = strpad('mu_Sb_011_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,27) = strpad('mu_Sb_100_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,28) = strpad('mu_Sb_101_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,29) = strpad('mu_Sb_110_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,30) = strpad('mu_Sb_111_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,31) = strpad('mu_Sc_000_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,32) = strpad('mu_Sc_001_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,33) = strpad('mu_Sc_010_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,34) = strpad('mu_Sc_011_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,35) = strpad('mu_Sc_100_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,36) = strpad('mu_Sc_101_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,37) = strpad('mu_Sc_110_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,38) = strpad('mu_Sc_111_IPR in component IPR (J_per_mol)');
    LEGEND_ALGEBRAIC(:,13) = strpad('v_R_IPR_main_nogate in component IPR (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,73) = strpad('mu_Ca_i in component pCa (J_per_mol)');
    LEGEND_ALGEBRAIC(:,75) = strpad('mu_Ca_o in component pCa (J_per_mol)');
    LEGEND_ALGEBRAIC(:,77) = strpad('mu_pCa in component pCa (J_per_mol)');
    LEGEND_ALGEBRAIC(:,79) = strpad('mu_pCa_Ca in component pCa (J_per_mol)');
    LEGEND_ALGEBRAIC(:,83) = strpad('I_mem_pCa in component pCa (fA)');
    LEGEND_CONSTANTS(:,104) = strpad('n_Ca_i in component SERCA (dimensionless)');
    LEGEND_CONSTANTS(:,105) = strpad('n_Ca_SR in component SERCA (dimensionless)');
    LEGEND_CONSTANTS(:,106) = strpad('n_H in component SERCA (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('q_SERCA_Ca_complexes in component SERCA (fmol)');
    LEGEND_ALGEBRAIC(:,2) = strpad('c_Ca_i in component SERCA (mM)');
    LEGEND_ALGEBRAIC(:,3) = strpad('c_Ca_SR in component SERCA (mM)');
    LEGEND_ALGEBRAIC(:,4) = strpad('c_H in component SERCA (mM)');
    LEGEND_ALGEBRAIC(:,5) = strpad('c_MgADP in component SERCA (mM)');
    LEGEND_ALGEBRAIC(:,6) = strpad('c_MgATP in component SERCA (mM)');
    LEGEND_ALGEBRAIC(:,7) = strpad('c_Pi in component SERCA (mM)');
    LEGEND_CONSTANTS(:,107) = strpad('vol_i in component SERCA (pL)');
    LEGEND_CONSTANTS(:,108) = strpad('vol_sr in component SERCA (pL)');
    LEGEND_CONSTANTS(:,110) = strpad('vol_isr in component SERCA (pL)');
    LEGEND_ALGEBRAIC(:,82) = strpad('mu_Ca_i in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,84) = strpad('mu_Ca_SR in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,85) = strpad('mu_H in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,86) = strpad('mu_MgADP in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,87) = strpad('mu_MgATP in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,89) = strpad('mu_P1 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,122) = strpad('v_P1 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,88) = strpad('mu_Pi in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,90) = strpad('mu_P2 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,129) = strpad('v_P2 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,91) = strpad('mu_P2a in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,126) = strpad('v_P2a in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,92) = strpad('mu_P4 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,131) = strpad('v_P4 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,93) = strpad('mu_P5 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,132) = strpad('v_P5 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,95) = strpad('mu_P6 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,135) = strpad('v_P6 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,97) = strpad('mu_P8 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,137) = strpad('v_P8 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,98) = strpad('mu_P9 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,139) = strpad('v_P9 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,94) = strpad('mu_P10 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,140) = strpad('v_P10 in component SERCA (fmol_per_sec)');
    LEGEND_CONSTANTS(:,109) = strpad('mu_PLBp_baseline in component SERCA (J_per_mol)');
    LEGEND_CONSTANTS(:,111) = strpad('mu_PLBp in component SERCA (J_per_mol)');
    LEGEND_CONSTANTS(:,112) = strpad('mu_PLBp_eff in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,101) = strpad('Af_R1_2 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,102) = strpad('Ar_R1_2 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,118) = strpad('v_SERCA_R1_2 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,103) = strpad('Af_R5_6 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,104) = strpad('Ar_R5_6 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,120) = strpad('v_SERCA_R5_6 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,105) = strpad('Af_R2_2a in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,106) = strpad('Ar_R2_2a in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,123) = strpad('v_SERCA_R2_2a in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,107) = strpad('Af_R2_4 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,108) = strpad('Ar_R2_4 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,125) = strpad('v_SERCA_R2_4 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,109) = strpad('Af_R4_5 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,110) = strpad('Ar_R4_5 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,128) = strpad('v_SERCA_R4_5 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,111) = strpad('Af_R6_8 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,112) = strpad('Ar_R6_8 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,130) = strpad('v_SERCA_R6_8 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,113) = strpad('Af_R8_9 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,114) = strpad('Ar_R8_9 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,134) = strpad('v_SERCA_R8_9 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,115) = strpad('Af_R9_10 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,116) = strpad('Ar_R9_10 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,136) = strpad('v_SERCA_R9_10 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,99) = strpad('Af_R10_1 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,100) = strpad('Ar_R10_1 in component SERCA (J_per_mol)');
    LEGEND_ALGEBRAIC(:,117) = strpad('v_SERCA_R10_1 in component SERCA (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,96) = strpad('v_SERCA_R10_1_noPLBp in component SERCA (fmol_per_sec)');
    LEGEND_RATES(:,1) = strpad('d/dt q_Ca_i in component environment (fmol)');
    LEGEND_RATES(:,4) = strpad('d/dt q_Ca_o in component environment (fmol)');
    LEGEND_RATES(:,5) = strpad('d/dt q_pCa in component environment (fmol)');
    LEGEND_RATES(:,6) = strpad('d/dt q_pCa_Ca in component environment (fmol)');
    LEGEND_RATES(:,2) = strpad('d/dt q_Ca_SR in component environment (fmol)');
    LEGEND_RATES(:,3) = strpad('d/dt q_IP3 in component environment (fmol)');
    LEGEND_RATES(:,31) = strpad('d/dt q_H in component environment (fmol)');
    LEGEND_RATES(:,33) = strpad('d/dt q_MgADP in component environment (fmol)');
    LEGEND_RATES(:,34) = strpad('d/dt q_MgATP in component environment (fmol)');
    LEGEND_RATES(:,32) = strpad('d/dt q_Pi in component environment (fmol)');
    LEGEND_RATES(:,7) = strpad('d/dt q_Sa_000_IPR in component environment (fmol)');
    LEGEND_RATES(:,8) = strpad('d/dt q_Sa_001_IPR in component environment (fmol)');
    LEGEND_RATES(:,9) = strpad('d/dt q_Sa_010_IPR in component environment (fmol)');
    LEGEND_RATES(:,10) = strpad('d/dt q_Sa_011_IPR in component environment (fmol)');
    LEGEND_RATES(:,11) = strpad('d/dt q_Sa_100_IPR in component environment (fmol)');
    LEGEND_RATES(:,12) = strpad('d/dt q_Sa_101_IPR in component environment (fmol)');
    LEGEND_RATES(:,13) = strpad('d/dt q_Sa_110_IPR in component environment (fmol)');
    LEGEND_RATES(:,14) = strpad('d/dt q_Sa_111_IPR in component environment (fmol)');
    LEGEND_RATES(:,15) = strpad('d/dt q_Sb_000_IPR in component environment (fmol)');
    LEGEND_RATES(:,16) = strpad('d/dt q_Sb_001_IPR in component environment (fmol)');
    LEGEND_RATES(:,17) = strpad('d/dt q_Sb_010_IPR in component environment (fmol)');
    LEGEND_RATES(:,18) = strpad('d/dt q_Sb_011_IPR in component environment (fmol)');
    LEGEND_RATES(:,19) = strpad('d/dt q_Sb_100_IPR in component environment (fmol)');
    LEGEND_RATES(:,20) = strpad('d/dt q_Sb_101_IPR in component environment (fmol)');
    LEGEND_RATES(:,21) = strpad('d/dt q_Sb_110_IPR in component environment (fmol)');
    LEGEND_RATES(:,22) = strpad('d/dt q_Sb_111_IPR in component environment (fmol)');
    LEGEND_RATES(:,23) = strpad('d/dt q_Sc_000_IPR in component environment (fmol)');
    LEGEND_RATES(:,24) = strpad('d/dt q_Sc_001_IPR in component environment (fmol)');
    LEGEND_RATES(:,25) = strpad('d/dt q_Sc_010_IPR in component environment (fmol)');
    LEGEND_RATES(:,26) = strpad('d/dt q_Sc_011_IPR in component environment (fmol)');
    LEGEND_RATES(:,27) = strpad('d/dt q_Sc_100_IPR in component environment (fmol)');
    LEGEND_RATES(:,28) = strpad('d/dt q_Sc_101_IPR in component environment (fmol)');
    LEGEND_RATES(:,29) = strpad('d/dt q_Sc_110_IPR in component environment (fmol)');
    LEGEND_RATES(:,30) = strpad('d/dt q_Sc_111_IPR in component environment (fmol)');
    LEGEND_RATES(:,35) = strpad('d/dt q_P1_SERCA in component environment (fmol)');
    LEGEND_RATES(:,36) = strpad('d/dt q_P2_SERCA in component environment (fmol)');
    LEGEND_RATES(:,37) = strpad('d/dt q_P2a_SERCA in component environment (fmol)');
    LEGEND_RATES(:,38) = strpad('d/dt q_P4_SERCA in component environment (fmol)');
    LEGEND_RATES(:,39) = strpad('d/dt q_P5_SERCA in component environment (fmol)');
    LEGEND_RATES(:,43) = strpad('d/dt q_P10_SERCA in component environment (fmol)');
    LEGEND_RATES(:,40) = strpad('d/dt q_P6_SERCA in component environment (fmol)');
    LEGEND_RATES(:,41) = strpad('d/dt q_P8_SERCA in component environment (fmol)');
    LEGEND_RATES(:,42) = strpad('d/dt q_P9_SERCA in component environment (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) = 3.07502e-09;
    CONSTANTS(:,2) = 2.9078e+08;
    CONSTANTS(:,3) = 0.0125066;
    CONSTANTS(:,4) = 16.6342;
    CONSTANTS(:,5) = 0.291916;
    CONSTANTS(:,6) = 1.25066;
    CONSTANTS(:,7) = 5.31398e-05;
    CONSTANTS(:,8) = 0.00973583;
    CONSTANTS(:,9) = 1.18239;
    CONSTANTS(:,10) = 4829.13;
    CONSTANTS(:,11) = 0.0955807;
    CONSTANTS(:,12) = 0.00718894;
    CONSTANTS(:,13) = 2.23094;
    CONSTANTS(:,14) = 152486;
    CONSTANTS(:,15) = 0.0125066;
    CONSTANTS(:,16) = 16.6342;
    CONSTANTS(:,17) = 0.291916;
    CONSTANTS(:,18) = 1.25066;
    CONSTANTS(:,19) = 5.31398e-05;
    CONSTANTS(:,20) = 0.00973583;
    CONSTANTS(:,21) = 1.18239;
    CONSTANTS(:,22) = 4829.13;
    CONSTANTS(:,23) = 0.0955807;
    CONSTANTS(:,24) = 0.00718894;
    CONSTANTS(:,25) = 2.23094;
    CONSTANTS(:,26) = 152486;
    CONSTANTS(:,27) = 0.0125066;
    CONSTANTS(:,28) = 16.6342;
    CONSTANTS(:,29) = 0.291916;
    CONSTANTS(:,30) = 1.25066;
    CONSTANTS(:,31) = 5.31398e-05;
    CONSTANTS(:,32) = 0.00973583;
    CONSTANTS(:,33) = 1.18239;
    CONSTANTS(:,34) = 4829.13;
    CONSTANTS(:,35) = 0.0955807;
    CONSTANTS(:,36) = 0.00718894;
    CONSTANTS(:,37) = 2.23094;
    CONSTANTS(:,38) = 152486;
    CONSTANTS(:,39) = 1654.75;
    CONSTANTS(:,40) = 0.00021208;
    CONSTANTS(:,41) = 2.90729e-06;
    CONSTANTS(:,42) = 0.166593;
    CONSTANTS(:,43) = 40567.4;
    CONSTANTS(:,44) = 0.166593;
    CONSTANTS(:,45) = 0.41262;
    CONSTANTS(:,46) = 3.47304;
    CONSTANTS(:,47) = 12551.8;
    CONSTANTS(:,48) = 12551.8;
    CONSTANTS(:,49) = 6.07543e-05;
    CONSTANTS(:,50) = 391.219;
    CONSTANTS(:,51) = 20.1363;
    CONSTANTS(:,52) = 70087.3;
    CONSTANTS(:,53) = 30.734;
    CONSTANTS(:,54) = 2.31077;
    CONSTANTS(:,55) = 1.31674;
    CONSTANTS(:,56) = 0.0990011;
    CONSTANTS(:,57) = 7233.34;
    CONSTANTS(:,58) = 3948.08;
    CONSTANTS(:,59) = 0.000341014;
    CONSTANTS(:,60) = 169.148;
    CONSTANTS(:,61) = 30.734;
    CONSTANTS(:,62) = 2.31077;
    CONSTANTS(:,63) = 1.31674;
    CONSTANTS(:,64) = 0.0990011;
    CONSTANTS(:,65) = 7233.34;
    CONSTANTS(:,66) = 3948.08;
    CONSTANTS(:,67) = 0.000341014;
    CONSTANTS(:,68) = 169.148;
    CONSTANTS(:,69) = 30.734;
    CONSTANTS(:,70) = 2.31077;
    CONSTANTS(:,71) = 1.31674;
    CONSTANTS(:,72) = 0.0990011;
    CONSTANTS(:,73) = 7233.34;
    CONSTANTS(:,74) = 3948.08;
    CONSTANTS(:,75) = 0.000341014;
    CONSTANTS(:,76) = 169.148;
    CONSTANTS(:,77) = 5.02046e-05;
    CONSTANTS(:,78) = 0.0449472;
    CONSTANTS(:,79) = 0.0116935;
    CONSTANTS(:,80) = 10.9783;
    CONSTANTS(:,81) = 0.170651;
    CONSTANTS(:,82) = 0.157113;
    CONSTANTS(:,83) = 38259;
    CONSTANTS(:,84) = 0.00151486;
    CONSTANTS(:,85) = 9208.24;
    CONSTANTS(:,86) = 0.00677857;
    CONSTANTS(:,87) = 0.50779;
    CONSTANTS(:,88) = 0.0830361;
    CONSTANTS(:,89) = 3108.77;
    CONSTANTS(:,90) = 7.72815;
    CONSTANTS(:,91) = 1.68438e-07;
    CONSTANTS(:,92) = 0.00212752;
    CONSTANTS(:,93) = 78.0122;
    CONSTANTS(:,94) = 2;
    CONSTANTS(:,95) = 34.4;
    CONSTANTS(:,96) = 500;
    STATES(:,1) = 0.007540192;
    STATES(:,2) = 0.763997897;
    STATES(:,3) = 5e-3;
    STATES(:,4) = 9.3276;
    STATES(:,5) = 0.0032;
    STATES(:,6) = 1e-9;
    STATES(:,7) = 5e-6;
    STATES(:,8) = 5e-6;
    STATES(:,9) = 5e-6;
    STATES(:,10) = 5e-6;
    STATES(:,11) = 5e-6;
    STATES(:,12) = 5e-6;
    STATES(:,13) = 5e-6;
    STATES(:,14) = 5e-6;
    STATES(:,15) = 5e-6;
    STATES(:,16) = 5e-6;
    STATES(:,17) = 5e-6;
    STATES(:,18) = 5e-6;
    STATES(:,19) = 5e-6;
    STATES(:,20) = 5e-6;
    STATES(:,21) = 5e-6;
    STATES(:,22) = 5e-6;
    STATES(:,23) = 5e-6;
    STATES(:,24) = 5e-6;
    STATES(:,25) = 5e-6;
    STATES(:,26) = 5e-6;
    STATES(:,27) = 5e-6;
    STATES(:,28) = 5e-6;
    STATES(:,29) = 5e-6;
    STATES(:,30) = 5e-6;
    STATES(:,31) = 0.004028;
    STATES(:,32) = 570;
    STATES(:,33) = 1.3794;
    STATES(:,34) = 3.8;
    STATES(:,35) = 2.54733e-08;
    STATES(:,36) = 2.54733e-08;
    STATES(:,37) = 2.54733e-08;
    STATES(:,38) = 2.54733e-08;
    STATES(:,39) = 2.54733e-08;
    STATES(:,40) = 2.54733e-08;
    STATES(:,41) = 2.54733e-08;
    STATES(:,42) = 2.54733e-08;
    STATES(:,43) = 2.54733e-08;
    CONSTANTS(:,97) = 2.54733e-08;
    CONSTANTS(:,98) = 2.54733e-08;
    CONSTANTS(:,99) = 1e-5;
    CONSTANTS(:,100) = 1e-5;
    CONSTANTS(:,101) = 8.31;
    CONSTANTS(:,102) = 310;
    CONSTANTS(:,103) = 96485;
    CONSTANTS(:,104) = 2;
    CONSTANTS(:,105) = 2;
    CONSTANTS(:,106) = 2;
    CONSTANTS(:,107) = 34.0;
    CONSTANTS(:,108) =  CONSTANTS(:,107).*0.0350000;
    CONSTANTS(:,109) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,93).*CONSTANTS(:,97));
    CONSTANTS(:,110) = CONSTANTS(:,107)+CONSTANTS(:,108);
    CONSTANTS(:,111) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,93).*CONSTANTS(:,98));
    CONSTANTS(:,112) = CONSTANTS(:,109) - CONSTANTS(:,111);
    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(:,11) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,51).*STATES(:,1));
    ALGEBRAIC(:,15) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,53).*STATES(:,7));
    ALGEBRAIC(:,16) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,54).*STATES(:,8));
    ALGEBRAIC(:,40) =  CONSTANTS(:,3).*(exp((ALGEBRAIC(:,15)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,16)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,18) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,56).*STATES(:,10));
    ALGEBRAIC(:,41) =  CONSTANTS(:,4).*(exp((ALGEBRAIC(:,16)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,18)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,14) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,52).*STATES(:,3));
    ALGEBRAIC(:,20) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,58).*STATES(:,12));
    ALGEBRAIC(:,48) =  CONSTANTS(:,11).*(exp((ALGEBRAIC(:,16)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,20)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,8) = (ALGEBRAIC(:,40) - ALGEBRAIC(:,41)) - ALGEBRAIC(:,48);
    ALGEBRAIC(:,19) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,57).*STATES(:,11));
    ALGEBRAIC(:,44) =  CONSTANTS(:,7).*(exp((ALGEBRAIC(:,19)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,20)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,22) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,60).*STATES(:,14));
    ALGEBRAIC(:,45) =  CONSTANTS(:,8).*(exp((ALGEBRAIC(:,20)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,22)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,12) = (ALGEBRAIC(:,44) - ALGEBRAIC(:,45))+ALGEBRAIC(:,48);
    ALGEBRAIC(:,17) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,55).*STATES(:,9));
    ALGEBRAIC(:,43) =  CONSTANTS(:,6).*(exp(ALGEBRAIC(:,17)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,15)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,49) =  CONSTANTS(:,12).*(exp(ALGEBRAIC(:,19)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,15)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,7) =  - ALGEBRAIC(:,40)+ALGEBRAIC(:,43)+ALGEBRAIC(:,49);
    ALGEBRAIC(:,21) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,59).*STATES(:,13));
    ALGEBRAIC(:,47) =  CONSTANTS(:,10).*(exp(ALGEBRAIC(:,21)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,19)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,11) = ( - ALGEBRAIC(:,44)+ALGEBRAIC(:,47)) - ALGEBRAIC(:,49);
    ALGEBRAIC(:,42) =  CONSTANTS(:,5).*(exp(ALGEBRAIC(:,18)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,17)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,50) =  CONSTANTS(:,13).*(exp((ALGEBRAIC(:,18)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,22)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,10) = (ALGEBRAIC(:,41) - ALGEBRAIC(:,42)) - ALGEBRAIC(:,50);
    ALGEBRAIC(:,46) =  CONSTANTS(:,9).*(exp(ALGEBRAIC(:,22)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,21)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,14) = (ALGEBRAIC(:,45) - ALGEBRAIC(:,46))+ALGEBRAIC(:,50);
    ALGEBRAIC(:,51) =  CONSTANTS(:,14).*(exp(ALGEBRAIC(:,21)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,17)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,9) = (ALGEBRAIC(:,42) - ALGEBRAIC(:,43))+ALGEBRAIC(:,51);
    RATES(:,13) = (ALGEBRAIC(:,46) - ALGEBRAIC(:,47)) - ALGEBRAIC(:,51);
    ALGEBRAIC(:,23) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,61).*STATES(:,15));
    ALGEBRAIC(:,24) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,62).*STATES(:,16));
    ALGEBRAIC(:,52) =  CONSTANTS(:,15).*(exp((ALGEBRAIC(:,23)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,24)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,26) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,64).*STATES(:,18));
    ALGEBRAIC(:,53) =  CONSTANTS(:,16).*(exp((ALGEBRAIC(:,24)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,26)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,28) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,66).*STATES(:,20));
    ALGEBRAIC(:,60) =  CONSTANTS(:,23).*(exp((ALGEBRAIC(:,24)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,28)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,16) = (ALGEBRAIC(:,52) - ALGEBRAIC(:,53)) - ALGEBRAIC(:,60);
    ALGEBRAIC(:,27) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,65).*STATES(:,19));
    ALGEBRAIC(:,56) =  CONSTANTS(:,19).*(exp((ALGEBRAIC(:,27)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,28)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,30) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,68).*STATES(:,22));
    ALGEBRAIC(:,57) =  CONSTANTS(:,20).*(exp((ALGEBRAIC(:,28)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,30)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,20) = (ALGEBRAIC(:,56) - ALGEBRAIC(:,57))+ALGEBRAIC(:,60);
    ALGEBRAIC(:,25) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,63).*STATES(:,17));
    ALGEBRAIC(:,55) =  CONSTANTS(:,18).*(exp(ALGEBRAIC(:,25)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,23)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,61) =  CONSTANTS(:,24).*(exp(ALGEBRAIC(:,27)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,23)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,15) =  - ALGEBRAIC(:,52)+ALGEBRAIC(:,55)+ALGEBRAIC(:,61);
    ALGEBRAIC(:,29) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,67).*STATES(:,21));
    ALGEBRAIC(:,59) =  CONSTANTS(:,22).*(exp(ALGEBRAIC(:,29)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,27)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,19) = ( - ALGEBRAIC(:,56)+ALGEBRAIC(:,59)) - ALGEBRAIC(:,61);
    ALGEBRAIC(:,54) =  CONSTANTS(:,17).*(exp(ALGEBRAIC(:,26)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,25)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,62) =  CONSTANTS(:,25).*(exp((ALGEBRAIC(:,26)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,30)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,18) = (ALGEBRAIC(:,53) - ALGEBRAIC(:,54)) - ALGEBRAIC(:,62);
    ALGEBRAIC(:,58) =  CONSTANTS(:,21).*(exp(ALGEBRAIC(:,30)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,29)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,22) = (ALGEBRAIC(:,57) - ALGEBRAIC(:,58))+ALGEBRAIC(:,62);
    ALGEBRAIC(:,63) =  CONSTANTS(:,26).*(exp(ALGEBRAIC(:,29)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,25)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,17) = (ALGEBRAIC(:,54) - ALGEBRAIC(:,55))+ALGEBRAIC(:,63);
    RATES(:,21) = (ALGEBRAIC(:,58) - ALGEBRAIC(:,59)) - ALGEBRAIC(:,63);
    ALGEBRAIC(:,31) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,69).*STATES(:,23));
    ALGEBRAIC(:,32) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,70).*STATES(:,24));
    ALGEBRAIC(:,64) =  CONSTANTS(:,27).*(exp((ALGEBRAIC(:,31)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,32)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,34) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,72).*STATES(:,26));
    ALGEBRAIC(:,65) =  CONSTANTS(:,28).*(exp((ALGEBRAIC(:,32)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,34)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,36) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,74).*STATES(:,28));
    ALGEBRAIC(:,72) =  CONSTANTS(:,35).*(exp((ALGEBRAIC(:,32)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,36)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,24) = (ALGEBRAIC(:,64) - ALGEBRAIC(:,65)) - ALGEBRAIC(:,72);
    ALGEBRAIC(:,35) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,73).*STATES(:,27));
    ALGEBRAIC(:,68) =  CONSTANTS(:,31).*(exp((ALGEBRAIC(:,35)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,36)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,38) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,76).*STATES(:,30));
    ALGEBRAIC(:,69) =  CONSTANTS(:,32).*(exp((ALGEBRAIC(:,36)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,38)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,28) = (ALGEBRAIC(:,68) - ALGEBRAIC(:,69))+ALGEBRAIC(:,72);
    ALGEBRAIC(:,33) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,71).*STATES(:,25));
    ALGEBRAIC(:,67) =  CONSTANTS(:,30).*(exp(ALGEBRAIC(:,33)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,31)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,74) =  CONSTANTS(:,36).*(exp(ALGEBRAIC(:,35)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,31)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,23) =  - ALGEBRAIC(:,64)+ALGEBRAIC(:,67)+ALGEBRAIC(:,74);
    ALGEBRAIC(:,37) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,75).*STATES(:,29));
    ALGEBRAIC(:,71) =  CONSTANTS(:,34).*(exp(ALGEBRAIC(:,37)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,35)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,27) = ( - ALGEBRAIC(:,68)+ALGEBRAIC(:,71)) - ALGEBRAIC(:,74);
    ALGEBRAIC(:,66) =  CONSTANTS(:,29).*(exp(ALGEBRAIC(:,34)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,33)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,76) =  CONSTANTS(:,37).*(exp((ALGEBRAIC(:,34)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,38)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,26) = (ALGEBRAIC(:,65) - ALGEBRAIC(:,66)) - ALGEBRAIC(:,76);
    ALGEBRAIC(:,70) =  CONSTANTS(:,33).*(exp(ALGEBRAIC(:,38)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,37)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,30) = (ALGEBRAIC(:,69) - ALGEBRAIC(:,70))+ALGEBRAIC(:,76);
    ALGEBRAIC(:,78) =  CONSTANTS(:,38).*(exp(ALGEBRAIC(:,37)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,33)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,3) = (((((((((( - ALGEBRAIC(:,48)+ALGEBRAIC(:,49)) - ALGEBRAIC(:,50))+ALGEBRAIC(:,51)) - ALGEBRAIC(:,60))+ALGEBRAIC(:,61)) - ALGEBRAIC(:,62))+ALGEBRAIC(:,63)) - ALGEBRAIC(:,72))+ALGEBRAIC(:,74)) - ALGEBRAIC(:,76))+ALGEBRAIC(:,78);
    RATES(:,25) = (ALGEBRAIC(:,66) - ALGEBRAIC(:,67))+ALGEBRAIC(:,78);
    RATES(:,29) = (ALGEBRAIC(:,70) - ALGEBRAIC(:,71)) - ALGEBRAIC(:,78);
    ALGEBRAIC(:,75) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,77).*STATES(:,4));
    ALGEBRAIC(:,77) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,78).*STATES(:,5));
    ALGEBRAIC(:,79) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,79).*STATES(:,6));
    ALGEBRAIC(:,81) =  CONSTANTS(:,40).*(exp(ALGEBRAIC(:,79)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,75)+ALGEBRAIC(:,77))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,4) = ALGEBRAIC(:,81);
    ALGEBRAIC(:,73) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,51).*STATES(:,1));
    ALGEBRAIC(:,80) =  CONSTANTS(:,39).*(exp((ALGEBRAIC(:,73)+ALGEBRAIC(:,77))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,79)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    RATES(:,5) =  - ALGEBRAIC(:,80)+ALGEBRAIC(:,81);
    RATES(:,6) = ALGEBRAIC(:,80) - ALGEBRAIC(:,81);
    ALGEBRAIC(:,94) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,88).*STATES(:,43));
    ALGEBRAIC(:,99) = ALGEBRAIC(:,94)+CONSTANTS(:,112);
    ALGEBRAIC(:,89) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,80).*STATES(:,35));
    ALGEBRAIC(:,88) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,92).*STATES(:,32));
    ALGEBRAIC(:,100) = ALGEBRAIC(:,89)+ALGEBRAIC(:,88)+CONSTANTS(:,112);
    ALGEBRAIC(:,117) =  CONSTANTS(:,49).*(exp(ALGEBRAIC(:,99)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,100)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,119) = ALGEBRAIC(:,117);
    RATES(:,32) = ALGEBRAIC(:,119);
    ALGEBRAIC(:,87) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,90).*STATES(:,34));
    ALGEBRAIC(:,101) = ALGEBRAIC(:,89)+ALGEBRAIC(:,87)+CONSTANTS(:,112);
    ALGEBRAIC(:,90) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,81).*STATES(:,36));
    ALGEBRAIC(:,102) = ALGEBRAIC(:,90)+CONSTANTS(:,112);
    ALGEBRAIC(:,118) =  CONSTANTS(:,41).*(exp(ALGEBRAIC(:,101)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,102)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,121) =  - ALGEBRAIC(:,118);
    RATES(:,34) = ALGEBRAIC(:,121);
    ALGEBRAIC(:,122) = ALGEBRAIC(:,117) - ALGEBRAIC(:,118);
    RATES(:,35) = ALGEBRAIC(:,122);
    ALGEBRAIC(:,93) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,84).*STATES(:,39));
    ALGEBRAIC(:,103) = ALGEBRAIC(:,93)+CONSTANTS(:,112);
    ALGEBRAIC(:,86) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,91).*STATES(:,33));
    ALGEBRAIC(:,95) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,85).*STATES(:,40));
    ALGEBRAIC(:,104) = ALGEBRAIC(:,86)+ALGEBRAIC(:,95)+CONSTANTS(:,112);
    ALGEBRAIC(:,120) =  CONSTANTS(:,45).*(exp(ALGEBRAIC(:,103)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,104)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,124) = ALGEBRAIC(:,120);
    RATES(:,33) = ALGEBRAIC(:,124);
    ALGEBRAIC(:,85) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,89).*STATES(:,31));
    ALGEBRAIC(:,105) = ALGEBRAIC(:,90)+ALGEBRAIC(:,85)+CONSTANTS(:,112);
    ALGEBRAIC(:,91) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,82).*STATES(:,37));
    ALGEBRAIC(:,106) = ALGEBRAIC(:,91)+CONSTANTS(:,112);
    ALGEBRAIC(:,123) =  CONSTANTS(:,43).*(exp(ALGEBRAIC(:,105)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,106)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,126) = ALGEBRAIC(:,123);
    RATES(:,37) = ALGEBRAIC(:,126);
    ALGEBRAIC(:,8) =  ( CONSTANTS(:,101).*CONSTANTS(:,102)).*log( CONSTANTS(:,50).*STATES(:,2));
    ALGEBRAIC(:,9) =  ( CONSTANTS(:,101).*CONSTANTS(:,102)).*log( CONSTANTS(:,51).*STATES(:,1));
    ALGEBRAIC(:,10) =  CONSTANTS(:,1).*(exp(ALGEBRAIC(:,8)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,9)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,12) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,50).*STATES(:,2));
    ALGEBRAIC(:,39) =  CONSTANTS(:,2).*(exp((ALGEBRAIC(:,12)+ALGEBRAIC(:,21)+ALGEBRAIC(:,29)+ALGEBRAIC(:,37))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,11)+ALGEBRAIC(:,21)+ALGEBRAIC(:,29)+ALGEBRAIC(:,37))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,82) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,51).*STATES(:,1));
    ALGEBRAIC(:,107) = ALGEBRAIC(:,90)+ CONSTANTS(:,104).*ALGEBRAIC(:,82)+CONSTANTS(:,112);
    ALGEBRAIC(:,92) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,83).*STATES(:,38));
    ALGEBRAIC(:,108) = ALGEBRAIC(:,92)+CONSTANTS(:,112);
    ALGEBRAIC(:,125) =  CONSTANTS(:,42).*(exp(ALGEBRAIC(:,107)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,108)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,127) =   - CONSTANTS(:,104).*ALGEBRAIC(:,125);
    RATES(:,1) = ((((((((((((((((((((ALGEBRAIC(:,10)+ALGEBRAIC(:,127)) - ALGEBRAIC(:,80))+ALGEBRAIC(:,39)+ STATES(:,3).*CONSTANTS(:,100)) - ALGEBRAIC(:,40)) - ALGEBRAIC(:,41))+ALGEBRAIC(:,42)+ALGEBRAIC(:,43)) - ALGEBRAIC(:,44)) - ALGEBRAIC(:,45))+ALGEBRAIC(:,46)+ALGEBRAIC(:,47)) - ALGEBRAIC(:,52)) - ALGEBRAIC(:,53))+ALGEBRAIC(:,54)+ALGEBRAIC(:,55)) - ALGEBRAIC(:,56)) - ALGEBRAIC(:,57))+ALGEBRAIC(:,58)+ALGEBRAIC(:,59)) - ALGEBRAIC(:,64)) - ALGEBRAIC(:,65))+ALGEBRAIC(:,66)+ALGEBRAIC(:,67)) - ALGEBRAIC(:,68)) - ALGEBRAIC(:,69))+ALGEBRAIC(:,70)+ALGEBRAIC(:,71);
    ALGEBRAIC(:,129) = ALGEBRAIC(:,118) - ALGEBRAIC(:,125);
    RATES(:,36) = ALGEBRAIC(:,129);
    ALGEBRAIC(:,109) = ALGEBRAIC(:,92)+CONSTANTS(:,112);
    ALGEBRAIC(:,110) = ALGEBRAIC(:,93)+ CONSTANTS(:,106).*ALGEBRAIC(:,85)+CONSTANTS(:,112);
    ALGEBRAIC(:,128) =  CONSTANTS(:,44).*(exp(ALGEBRAIC(:,109)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,110)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,131) = ALGEBRAIC(:,125) - ALGEBRAIC(:,128);
    RATES(:,38) = ALGEBRAIC(:,131);
    ALGEBRAIC(:,132) = ALGEBRAIC(:,128) - ALGEBRAIC(:,120);
    RATES(:,39) = ALGEBRAIC(:,132);
    ALGEBRAIC(:,111) = ALGEBRAIC(:,95)+CONSTANTS(:,112);
    ALGEBRAIC(:,84) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,50).*STATES(:,2));
    ALGEBRAIC(:,97) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,86).*STATES(:,41));
    ALGEBRAIC(:,112) = ALGEBRAIC(:,97)+ CONSTANTS(:,105).*ALGEBRAIC(:,84)+CONSTANTS(:,112);
    ALGEBRAIC(:,130) =  CONSTANTS(:,46).*(exp(ALGEBRAIC(:,111)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,112)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,133) =  CONSTANTS(:,105).*ALGEBRAIC(:,130);
    RATES(:,2) = ( - ALGEBRAIC(:,10) - ALGEBRAIC(:,39))+ALGEBRAIC(:,133);
    ALGEBRAIC(:,135) = ALGEBRAIC(:,120) - ALGEBRAIC(:,130);
    RATES(:,40) = ALGEBRAIC(:,135);
    ALGEBRAIC(:,113) = ALGEBRAIC(:,97)+ CONSTANTS(:,106).*ALGEBRAIC(:,85)+CONSTANTS(:,112);
    ALGEBRAIC(:,98) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,87).*STATES(:,42));
    ALGEBRAIC(:,114) = ALGEBRAIC(:,98)+CONSTANTS(:,112);
    ALGEBRAIC(:,134) =  CONSTANTS(:,47).*(exp(ALGEBRAIC(:,113)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,114)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,137) = ALGEBRAIC(:,130) - ALGEBRAIC(:,134);
    RATES(:,41) = ALGEBRAIC(:,137);
    ALGEBRAIC(:,115) = ALGEBRAIC(:,98)+CONSTANTS(:,112);
    ALGEBRAIC(:,116) = ALGEBRAIC(:,85)+ALGEBRAIC(:,94)+CONSTANTS(:,112);
    ALGEBRAIC(:,136) =  CONSTANTS(:,48).*(exp(ALGEBRAIC(:,115)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,116)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,138) = (( CONSTANTS(:,106).*ALGEBRAIC(:,128) - ALGEBRAIC(:,123)) -  CONSTANTS(:,106).*ALGEBRAIC(:,134))+ALGEBRAIC(:,136);
    RATES(:,31) = ALGEBRAIC(:,138);
    ALGEBRAIC(:,140) = ALGEBRAIC(:,136) - ALGEBRAIC(:,117);
    RATES(:,43) = ALGEBRAIC(:,140);
    ALGEBRAIC(:,139) = ALGEBRAIC(:,134) - ALGEBRAIC(:,136);
    RATES(:,42) = ALGEBRAIC(:,139);
   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(:,11) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,51).*STATES(:,1));
    ALGEBRAIC(:,15) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,53).*STATES(:,7));
    ALGEBRAIC(:,16) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,54).*STATES(:,8));
    ALGEBRAIC(:,40) =  CONSTANTS(:,3).*(exp((ALGEBRAIC(:,15)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,16)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,18) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,56).*STATES(:,10));
    ALGEBRAIC(:,41) =  CONSTANTS(:,4).*(exp((ALGEBRAIC(:,16)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,18)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,14) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,52).*STATES(:,3));
    ALGEBRAIC(:,20) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,58).*STATES(:,12));
    ALGEBRAIC(:,48) =  CONSTANTS(:,11).*(exp((ALGEBRAIC(:,16)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,20)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,19) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,57).*STATES(:,11));
    ALGEBRAIC(:,44) =  CONSTANTS(:,7).*(exp((ALGEBRAIC(:,19)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,20)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,22) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,60).*STATES(:,14));
    ALGEBRAIC(:,45) =  CONSTANTS(:,8).*(exp((ALGEBRAIC(:,20)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,22)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,17) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,55).*STATES(:,9));
    ALGEBRAIC(:,43) =  CONSTANTS(:,6).*(exp(ALGEBRAIC(:,17)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,15)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,49) =  CONSTANTS(:,12).*(exp(ALGEBRAIC(:,19)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,15)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,21) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,59).*STATES(:,13));
    ALGEBRAIC(:,47) =  CONSTANTS(:,10).*(exp(ALGEBRAIC(:,21)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,19)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,42) =  CONSTANTS(:,5).*(exp(ALGEBRAIC(:,18)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,17)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,50) =  CONSTANTS(:,13).*(exp((ALGEBRAIC(:,18)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,22)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,46) =  CONSTANTS(:,9).*(exp(ALGEBRAIC(:,22)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,21)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,51) =  CONSTANTS(:,14).*(exp(ALGEBRAIC(:,21)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,17)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,23) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,61).*STATES(:,15));
    ALGEBRAIC(:,24) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,62).*STATES(:,16));
    ALGEBRAIC(:,52) =  CONSTANTS(:,15).*(exp((ALGEBRAIC(:,23)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,24)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,26) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,64).*STATES(:,18));
    ALGEBRAIC(:,53) =  CONSTANTS(:,16).*(exp((ALGEBRAIC(:,24)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,26)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,28) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,66).*STATES(:,20));
    ALGEBRAIC(:,60) =  CONSTANTS(:,23).*(exp((ALGEBRAIC(:,24)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,28)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,27) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,65).*STATES(:,19));
    ALGEBRAIC(:,56) =  CONSTANTS(:,19).*(exp((ALGEBRAIC(:,27)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,28)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,30) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,68).*STATES(:,22));
    ALGEBRAIC(:,57) =  CONSTANTS(:,20).*(exp((ALGEBRAIC(:,28)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,30)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,25) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,63).*STATES(:,17));
    ALGEBRAIC(:,55) =  CONSTANTS(:,18).*(exp(ALGEBRAIC(:,25)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,23)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,61) =  CONSTANTS(:,24).*(exp(ALGEBRAIC(:,27)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,23)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,29) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,67).*STATES(:,21));
    ALGEBRAIC(:,59) =  CONSTANTS(:,22).*(exp(ALGEBRAIC(:,29)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,27)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,54) =  CONSTANTS(:,17).*(exp(ALGEBRAIC(:,26)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,25)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,62) =  CONSTANTS(:,25).*(exp((ALGEBRAIC(:,26)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,30)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,58) =  CONSTANTS(:,21).*(exp(ALGEBRAIC(:,30)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,29)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,63) =  CONSTANTS(:,26).*(exp(ALGEBRAIC(:,29)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,25)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,31) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,69).*STATES(:,23));
    ALGEBRAIC(:,32) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,70).*STATES(:,24));
    ALGEBRAIC(:,64) =  CONSTANTS(:,27).*(exp((ALGEBRAIC(:,31)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,32)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,34) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,72).*STATES(:,26));
    ALGEBRAIC(:,65) =  CONSTANTS(:,28).*(exp((ALGEBRAIC(:,32)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,34)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,36) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,74).*STATES(:,28));
    ALGEBRAIC(:,72) =  CONSTANTS(:,35).*(exp((ALGEBRAIC(:,32)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,36)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,35) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,73).*STATES(:,27));
    ALGEBRAIC(:,68) =  CONSTANTS(:,31).*(exp((ALGEBRAIC(:,35)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,36)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,38) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,76).*STATES(:,30));
    ALGEBRAIC(:,69) =  CONSTANTS(:,32).*(exp((ALGEBRAIC(:,36)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,38)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,33) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,71).*STATES(:,25));
    ALGEBRAIC(:,67) =  CONSTANTS(:,30).*(exp(ALGEBRAIC(:,33)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,31)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,74) =  CONSTANTS(:,36).*(exp(ALGEBRAIC(:,35)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,31)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,37) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,75).*STATES(:,29));
    ALGEBRAIC(:,71) =  CONSTANTS(:,34).*(exp(ALGEBRAIC(:,37)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,35)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,66) =  CONSTANTS(:,29).*(exp(ALGEBRAIC(:,34)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,33)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,76) =  CONSTANTS(:,37).*(exp((ALGEBRAIC(:,34)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,38)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,70) =  CONSTANTS(:,33).*(exp(ALGEBRAIC(:,38)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,37)+ALGEBRAIC(:,11))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,78) =  CONSTANTS(:,38).*(exp(ALGEBRAIC(:,37)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,33)+ALGEBRAIC(:,14))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,75) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,77).*STATES(:,4));
    ALGEBRAIC(:,77) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,78).*STATES(:,5));
    ALGEBRAIC(:,79) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,79).*STATES(:,6));
    ALGEBRAIC(:,81) =  CONSTANTS(:,40).*(exp(ALGEBRAIC(:,79)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,75)+ALGEBRAIC(:,77))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,73) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,51).*STATES(:,1));
    ALGEBRAIC(:,80) =  CONSTANTS(:,39).*(exp((ALGEBRAIC(:,73)+ALGEBRAIC(:,77))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,79)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,94) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,88).*STATES(:,43));
    ALGEBRAIC(:,99) = ALGEBRAIC(:,94)+CONSTANTS(:,112);
    ALGEBRAIC(:,89) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,80).*STATES(:,35));
    ALGEBRAIC(:,88) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,92).*STATES(:,32));
    ALGEBRAIC(:,100) = ALGEBRAIC(:,89)+ALGEBRAIC(:,88)+CONSTANTS(:,112);
    ALGEBRAIC(:,117) =  CONSTANTS(:,49).*(exp(ALGEBRAIC(:,99)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,100)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,119) = ALGEBRAIC(:,117);
    ALGEBRAIC(:,87) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,90).*STATES(:,34));
    ALGEBRAIC(:,101) = ALGEBRAIC(:,89)+ALGEBRAIC(:,87)+CONSTANTS(:,112);
    ALGEBRAIC(:,90) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,81).*STATES(:,36));
    ALGEBRAIC(:,102) = ALGEBRAIC(:,90)+CONSTANTS(:,112);
    ALGEBRAIC(:,118) =  CONSTANTS(:,41).*(exp(ALGEBRAIC(:,101)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,102)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,121) =  - ALGEBRAIC(:,118);
    ALGEBRAIC(:,122) = ALGEBRAIC(:,117) - ALGEBRAIC(:,118);
    ALGEBRAIC(:,93) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,84).*STATES(:,39));
    ALGEBRAIC(:,103) = ALGEBRAIC(:,93)+CONSTANTS(:,112);
    ALGEBRAIC(:,86) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,91).*STATES(:,33));
    ALGEBRAIC(:,95) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,85).*STATES(:,40));
    ALGEBRAIC(:,104) = ALGEBRAIC(:,86)+ALGEBRAIC(:,95)+CONSTANTS(:,112);
    ALGEBRAIC(:,120) =  CONSTANTS(:,45).*(exp(ALGEBRAIC(:,103)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,104)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,124) = ALGEBRAIC(:,120);
    ALGEBRAIC(:,85) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,89).*STATES(:,31));
    ALGEBRAIC(:,105) = ALGEBRAIC(:,90)+ALGEBRAIC(:,85)+CONSTANTS(:,112);
    ALGEBRAIC(:,91) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,82).*STATES(:,37));
    ALGEBRAIC(:,106) = ALGEBRAIC(:,91)+CONSTANTS(:,112);
    ALGEBRAIC(:,123) =  CONSTANTS(:,43).*(exp(ALGEBRAIC(:,105)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,106)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,126) = ALGEBRAIC(:,123);
    ALGEBRAIC(:,8) =  ( CONSTANTS(:,101).*CONSTANTS(:,102)).*log( CONSTANTS(:,50).*STATES(:,2));
    ALGEBRAIC(:,9) =  ( CONSTANTS(:,101).*CONSTANTS(:,102)).*log( CONSTANTS(:,51).*STATES(:,1));
    ALGEBRAIC(:,10) =  CONSTANTS(:,1).*(exp(ALGEBRAIC(:,8)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,9)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,12) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,50).*STATES(:,2));
    ALGEBRAIC(:,39) =  CONSTANTS(:,2).*(exp((ALGEBRAIC(:,12)+ALGEBRAIC(:,21)+ALGEBRAIC(:,29)+ALGEBRAIC(:,37))./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,11)+ALGEBRAIC(:,21)+ALGEBRAIC(:,29)+ALGEBRAIC(:,37))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,82) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,51).*STATES(:,1));
    ALGEBRAIC(:,107) = ALGEBRAIC(:,90)+ CONSTANTS(:,104).*ALGEBRAIC(:,82)+CONSTANTS(:,112);
    ALGEBRAIC(:,92) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,83).*STATES(:,38));
    ALGEBRAIC(:,108) = ALGEBRAIC(:,92)+CONSTANTS(:,112);
    ALGEBRAIC(:,125) =  CONSTANTS(:,42).*(exp(ALGEBRAIC(:,107)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,108)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,127) =   - CONSTANTS(:,104).*ALGEBRAIC(:,125);
    ALGEBRAIC(:,129) = ALGEBRAIC(:,118) - ALGEBRAIC(:,125);
    ALGEBRAIC(:,109) = ALGEBRAIC(:,92)+CONSTANTS(:,112);
    ALGEBRAIC(:,110) = ALGEBRAIC(:,93)+ CONSTANTS(:,106).*ALGEBRAIC(:,85)+CONSTANTS(:,112);
    ALGEBRAIC(:,128) =  CONSTANTS(:,44).*(exp(ALGEBRAIC(:,109)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,110)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,131) = ALGEBRAIC(:,125) - ALGEBRAIC(:,128);
    ALGEBRAIC(:,132) = ALGEBRAIC(:,128) - ALGEBRAIC(:,120);
    ALGEBRAIC(:,111) = ALGEBRAIC(:,95)+CONSTANTS(:,112);
    ALGEBRAIC(:,84) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,50).*STATES(:,2));
    ALGEBRAIC(:,97) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,86).*STATES(:,41));
    ALGEBRAIC(:,112) = ALGEBRAIC(:,97)+ CONSTANTS(:,105).*ALGEBRAIC(:,84)+CONSTANTS(:,112);
    ALGEBRAIC(:,130) =  CONSTANTS(:,46).*(exp(ALGEBRAIC(:,111)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,112)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,133) =  CONSTANTS(:,105).*ALGEBRAIC(:,130);
    ALGEBRAIC(:,135) = ALGEBRAIC(:,120) - ALGEBRAIC(:,130);
    ALGEBRAIC(:,113) = ALGEBRAIC(:,97)+ CONSTANTS(:,106).*ALGEBRAIC(:,85)+CONSTANTS(:,112);
    ALGEBRAIC(:,98) =  CONSTANTS(:,101).*CONSTANTS(:,102).*log( CONSTANTS(:,87).*STATES(:,42));
    ALGEBRAIC(:,114) = ALGEBRAIC(:,98)+CONSTANTS(:,112);
    ALGEBRAIC(:,134) =  CONSTANTS(:,47).*(exp(ALGEBRAIC(:,113)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,114)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,137) = ALGEBRAIC(:,130) - ALGEBRAIC(:,134);
    ALGEBRAIC(:,115) = ALGEBRAIC(:,98)+CONSTANTS(:,112);
    ALGEBRAIC(:,116) = ALGEBRAIC(:,85)+ALGEBRAIC(:,94)+CONSTANTS(:,112);
    ALGEBRAIC(:,136) =  CONSTANTS(:,48).*(exp(ALGEBRAIC(:,115)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,116)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,138) = (( CONSTANTS(:,106).*ALGEBRAIC(:,128) - ALGEBRAIC(:,123)) -  CONSTANTS(:,106).*ALGEBRAIC(:,134))+ALGEBRAIC(:,136);
    ALGEBRAIC(:,140) = ALGEBRAIC(:,136) - ALGEBRAIC(:,117);
    ALGEBRAIC(:,139) = ALGEBRAIC(:,134) - ALGEBRAIC(:,136);
    ALGEBRAIC(:,1) =  CONSTANTS(:,104).*STATES(:,38)+ CONSTANTS(:,104).*STATES(:,39)+ CONSTANTS(:,105).*STATES(:,40);
    ALGEBRAIC(:,2) = STATES(:,1)./CONSTANTS(:,107);
    ALGEBRAIC(:,3) = STATES(:,2)./CONSTANTS(:,108);
    ALGEBRAIC(:,4) = STATES(:,31)./CONSTANTS(:,110);
    ALGEBRAIC(:,5) = STATES(:,33)./CONSTANTS(:,107);
    ALGEBRAIC(:,6) = STATES(:,34)./CONSTANTS(:,107);
    ALGEBRAIC(:,7) = STATES(:,32)./CONSTANTS(:,107);
    ALGEBRAIC(:,13) =  CONSTANTS(:,2).*(exp(ALGEBRAIC(:,12)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp(ALGEBRAIC(:,11)./( CONSTANTS(:,101).*CONSTANTS(:,102))));
    ALGEBRAIC(:,83) =   - CONSTANTS(:,94).*CONSTANTS(:,103).*ALGEBRAIC(:,81);
    ALGEBRAIC(:,96) =  CONSTANTS(:,49).*(exp(ALGEBRAIC(:,94)./( CONSTANTS(:,101).*CONSTANTS(:,102))) - exp((ALGEBRAIC(:,89)+ALGEBRAIC(:,88))./( CONSTANTS(:,101).*CONSTANTS(:,102))));
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

