% FLPROPS iterative sample problem.
% By: Tom Ames                   Mark Cutler
%     thomaslames@gmail.com      markjcutler@gmail.com
%  
% The following example is problem 8-37a from "Fundamental Heat and Mass 
% Transfer, 6th Edition."  The problem statement reads as follows:
%  
% Atmospheric air enters a 10-m-long, 150-mm-diameter uninsulated heating
% duct at 60 C and 0.04 kg/s.  The duct surface temperature is
% approxiametly constant at Ts = 15 C.
% 
% a)  What is the outlet air temperature for these conditions?

% Call the function to iterate and send it an intial guess, To = 200
function [answer] = iterative_example()
answer = fsolve(@Prob_8_37,200)
end

% The iterative function
function Tdiff = Prob_8_37(To)

% Define system parameters
D = 0.15;       % (m)
mdot = 0.04;    % (kg/s)
Ti = 60;        % (C)
Ts = 15;        % (C)
Tf = (Ti  + To )/2;     % Film Temperature using guessed To value
x = 10;         % (m)

% Call function flprops for fluid properties at current film temperature
[mu cp k Pr] = flprops('air',Tf+273,'mu','cp','k','Pr');

% Compute Reynolds number and convective coefficient
ReD = 4*mdot/(pi*mu*D);
if Ts>Tf
    n = 0.4;
else
    n = 0.3;
end
NuD = 0.023*ReD^(4/5)*Pr^n;
hbar = k/D*NuD;

% Compute To by minimizing Tdiff
 Tonew = Ts + (Ti - Ts)*exp(-(hbar*pi*x*D)/(mdot*cp));
 Tdiff = To - Tonew
end