% Modified version of the MATLAB template file SolvNonDif.m used in the
% solution of Example 23.1 of "A First Course on Kinetics and Reaction
% Engineering."
%
function Example_23_1_ss
    % Known quantities and constants (in consistent units)
    VFR = 59; % cm3/min
    CS0 = 0.04; % g/cm3
    CX0 = 8e-5; % g/cm3
    mS0 = VFR*CS0; % g/min
    mX0 = VFR*CX0; % g/min
    V = 4430; % cm3
    
    % Function that evaluates the equations
	function f = evalEqns(z)
        % mapping of unknowns into vector z
        mS = z(1);
        mX = z(2);
        % calculate variables quantities
        CS = mS/VFR;
        CX = mX/VFR;
        r2 = 0.014*CS*CX/(0.001 + CS);
        % evaluate the functions
        f = [
            mS0 - mS - 2.2*V*r2
            mX0 - mX + V*r2
        ];
    end % of internal function evalEqns
    
    % guesses for the solution
    z_guess = [
        1.35
        0.4
    ];

    % Solve the set of algebraic equations and display the results
    z = fsolve(@evalEqns, z_guess);
    display('The solver found the following values for the unknowns:');
    z
    display('The corresponding values of the functions being solved are as follows:');
    f = evalEqns(z)
  
    % Report the results
    mS_ss = z(1)
    mX_ss = z(2)
    	
end % of Example_23_1_ss.m