% Modified version of the AFCoKaRE MATLAB template file SolvNonDif.m used
% in the solution of the first part of Example 29.1 of "A First Course on
% Kinetics and Reaction Engineering."
%
function Example_29_1_a
    % Known quantities and constants
    k0 = 12000.0; % mol/L/min
    E = 6500.0; % cal/mol
    K = 0.21; % mol/L
    VFR = 9.5; % L/min
    CAa = 1.3; % mol/L
    nZa = 0; % mol/min
    Ta = 75 + 273.15; % K
    dH = 4300.0; % cal/mol
    cP = 0.515*1000.0; % cal/L/K
    fA = 0.99;
    % Gas constant
    R = 1.987; % cal/mol/K
    % Calculated constants
    nAa = VFR*CAa;
    nAb = nAa*(1-fA);
    CAb = nAb/VFR;
    
    % Function that evaluates the equations
    function f = evalEqns(z)
        V = z(1);
        nZb = z(2);
        Tb = z(3);
        ra = k0*exp(-E/R/Tb)*CAb/(K + CAb);
        f = [
            nAa - nAb - V*ra
            nZa - nZb + V*ra
            VFR*cP*(Tb - Ta) + V*ra*dH
        ];
    end % of internal function evalEqns
    
    % guesses for the solution
    z_guess = [
        100.
        nAa - nAb
        Ta + 10
    ];

    % 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)
    V = z(1)
end % of Example_29_1_a.m