% Modified version of the MATLAB template file SolvNonDif.m used to solve
% Example 1 of Supplemental Unit S2 of "A First Course on Kinetics and
% Reaction Engineering."
%
function z = S2_Example_1
    % Known quantities and constants (in units of mol and min)
    A = 0.2083;
    B = 5.472;
    C = 0.4167;
    
    % Define the internal functions that will be needed
    
    % Function that evaluates the equations
	function f = evalEqns(z)
        term1 = z(1)^1.5*z(2)^0.5;
		f = [
			A - B*term1 - z(1);
            C - B*term1 - z(2);
            B*term1 - z(3);
            B*term1 - z(4);
		];
    end % of internal function evalEqns
    
    % All the necessary internal functions are now defined

    % guesses for the solution
    z_guess = [
        1
        1
        1
        1
    ];

    % Solve the set of algebraic equations
    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)
    
    % compute the requested ratio
    ratio = z(1)/z(2)
	
end % of S7_Example_1.m