function L = Example_38_2b(given)
%Example_38_2b Solve AFCoKaRE Example 38.2 using phase balances

    % Structure array given contains known quantities and constants
    Av = 3./given.Rp*(1-given.eps); % solution, equation (12)
    
    % Gas phase mole balance on A
    % Initial values
    t0 = 0.0;
    z0 = [
        given.CA0*given.VFR
    ];

    % Stopping criterion
    tf = 950.;
    options = odeset('Events',@stop);
    
    % Solve the gas phase mole balance on A
	[t, zz, te, ze, ie] = ode45(@odeqns,[t0, tf],z0,options);
    
    % Extract the final values of the independent and dependent variables
	last_value = length(t);
	t_f = t(last_value); % final value of the independent variable
	z = zz(last_value,:); % final values of the dependent variables
    
    % Warn if the independent variable reached tf above
    if (t_f >= tf)
        display('WARNING: the final value of the independent variable was')
        display('    reached, instead of the final value of the dependent')
        display('    variable. Set tf to a larger value and re-execute');
        display('    the function')
    end % if (t_f >= tf)
	
    %conv_check = (CA0*VFR - z(1))/CA0/VFR
    L = t_f;

    % Function that evaluates the gas phase mole balance ODE
    function dzdt = odeqns(t,z)
        CAs = z(1)/given.VFR;
        % Calculate the flux of A by solving the catalyst phase mole
        % balance on A
        [NA, eta] = Example_38_2_cat_bal(given,CAs);
        % Evaluate the gas phase mole balance on A
        dzdt = [
            pi()*given.D^2/4*Av*NA % solution, equation 11
        ];
    end % of internal function odeqns
    
    % Function that provides the integration stopping criterion
    function [stop_when, isterminal, direction] = stop(t,z)
        isterminal = 1;
        direction = 0;
        
        stop_when = z(1) - (1-given.fA)*given.CA0*given.VFR;
    end % of internal function stop


end % of Example_38_2b
