%
%   RECT1D_FIN_1.M   Function Evaluation, Plotting, and Table Formation
%                 Heat Transfer Analysis of a Rectangular Fin Arrangement
%
%   This file does some computational analysis for a rectangular fin with a fixed
%   base or wall temperature and a convective environment at the tip.  The 
%   equations programmed for the temperature profiles assume that the energy flow 
%   in the fin is essentially one-dimensional along the length of the fin.  
%   Several performance parameters are also tabulated for the fins, including
%   the thermal conductivity, the base and tip temperatures, the total heat 
%   transferred from the fin, the fin effectiveness, and the overall fin efficiency.
%
%   The goal of this file is to illustrate several of Matlab's basic capabilities,
%   including function evaluation and plotting and the use of the fprintf command
%   to create a summary table of results in an easy-to-read format.  It also shows
%   how to use a for...end loop to perform a parametric study involving a single 
%   parameter (in this case we look at three different values of the fin's 
%   thermal conductivity).  Also, of course, you should gain a little further 
%   understanding of some simple heat transfer principles (conduction and 
%   convection heat transfer).
%
%   The basic idea for this problem came from the text "Fundamentals of Heat 
%   and Mass Transfer" 5th Ed by Incropera and Dewitt (2002, John Wiley & Sons).
%
%   File prepared by J. R. White, UMass-Lowell  (last update: Sept. 2017)
%

      clear all,  close all,  nfig = 0;
%
%   identify basic problem data
      w = 1;                            % unit width of fin (m)
      thk = 0.01;                       % fin thickness (m)
      L = 0.05;                         % fin length  (m)
      Tb = 200;                         % fin base temperature (C)
      Tinf = 30;                        % environment temperature (C)
      h = 500;                          % heat transfer coeff  (W/m^2-C)
      k = [5 50 200];                   % fin thermal conductivities (W/m-C)
%
%   compute some derived parameters (note the use of 'dot arithmetic' where needed)
      P = 2*w + 2*thk;                  % perimeter
      Ac = w*thk;                       % cross section area (conduction area)
      Af = P*L + Ac;                    % total fin surface area (for convection)
      qmax = h*Af*(Tb - Tinf);          % max feat transfer in fin
      qmin = h*Ac*(Tb - Tinf);          % heat transfer if no fin is present
      Bi = h*thk./k;                    % Biot number
      m = sqrt(h*P./(k*Ac));            % constant in derived equations (see notes)
      M = sqrt(k*Ac*h*P)*(Tb - Tinf);   % constant in derived equations (see notes)
      hmk = h./(m.*k);                  % constant in derived equations (see notes)
      bot = cosh(m*L) + hmk.*sinh(m*L); % constant in derived equations (see notes)
      qf = M.*(sinh(m*L) + hmk.*cosh(m*L))./bot;   % total fin heat transfer
%
%   compute temp profile for different materials (i.e. different thermal conductivities)
      Nk = length(k);                   % number of different materials 
      Nx = 51;  x = linspace(0,L,Nx)';  % independent spatial variable
      T = zeros(Nx,Nk);                 % allocate space for temperature profiles
      lstr = cell(1,Nk);                % initialize space for legend labels
      for n = 1:Nk
        T(:,n) = Tinf + (Tb - Tinf)*(cosh(m(n)*(L-x)) + hmk(n)*sinh(m(n)*(L-x)))/bot(n);
        lstr(n) = {['k = ',num2str(k(n),'%3i'),' W/m-C']};
      end
%
%   plot temp profiles
      nfig = nfig+1;   figure(nfig)
      plot(x*100,T(:,1),'r-.',x*100,T(:,2),'b--',x*100,T(:,3),'g-','LineWidth',2),grid
      title('T(x) Profiles in Fin for Different Values of k ')
      xlabel('Spatial Position (cm)')
      ylabel('Temperature (^oC)')
      legend(lstr)
%
%   write summary table of results (note that this assumes Nk = 3)
      fprintf(1,'\n\n');
      fprintf(1,'           Summary Results from the RECT1D_FIN_1.M Program \n');
      fprintf(1,'\n');
      fprintf(1,'    Case-Independent Parameters      \n');
      fprintf(1,'  Base Temperature (C):               %8.2f    \n',Tb);
      fprintf(1,'  Environment Temperature (C):        %8.2f    \n',Tinf);
      fprintf(1,'  Heat Transfer Coeff (W/m^2-C):      %8.2f    \n',h);
      fprintf(1,'  Fin Length (cm):                    %8.2f    \n',L*100);
      fprintf(1,'  Fin Thickness (cm):                 %8.2f    \n',thk*100);
      fprintf(1,'  Min Heat Transfer possible (W):     %8.2f    \n',qmin);
      fprintf(1,'  Max Heat Transfer possible (W):     %8.2f    \n',qmax);
      fprintf(1,'\n');
      fprintf(1,'    Case-Dependent Parameters          Case #1    Case #2   Case #3 \n');
      fprintf(1,'  Thermal Conductivity (W/m-C):       %8.2f   %8.2f  %8.2f  \n',k);
      fprintf(1,'  Tip Temperature (C):                %8.2f   %8.2f  %8.2f  \n',T(Nx,:));
      fprintf(1,'  Heat Transfer per unit width (W):   %8.2f   %8.2f  %8.2f  \n',qf);
      fprintf(1,'  Fin Effectiveness (dimensionless):  %8.2f   %8.2f  %8.2f  \n',qf/qmin);
      fprintf(1,'  Fin Efficiency (dimensionless):     %8.2f   %8.2f  %8.2f  \n',qf/qmax);
      fprintf(1,'  Biot Number (dimensionless):        %8.2f   %8.2f  %8.2f  \n',Bi);
      fprintf(1,'\n');
      fprintf(1,'    Actual Temperature Profiles, T(x) in C, for the Three Cases \n');
      fprintf(1,'\n');
      fprintf(1,'        Position      Case #1   Case #2   Case #3 \n');
      fprintf(1,'          (cm)          T(x)      T(x)      T(x)  \n');
      for i = 1:2:Nx
        fprintf(1,'         %5.1f      %8.1f  %8.1f  %8.1f  \n',x(i)*100,T(i,:));
      end
%
%  end of problem
