%
%   PIPE_FLOW_1.M   Plots Laminar and Turbulent Velocity Profiles in a Pipe
%
%   This is another example that illustrates function evaluation and plotting in
%   Matlab.  The goal here is simply to evaluate and plot the velocity profiles
%   for laminar and turbulent flow in a circular pipe geometry.  
%
%   For laminar flow, the velocity profile is parabolic, where u(r) is given by
%        u(r) = umax*[1 -(r/R)^2]
%   For turbulent flow the velocity profile is given by
%        u(r) = umax*(1 - r/R)^m   
%   where m = 1/7 for fully developed turbulent flow.
%  
%   In evaluating these functions, we will again illustrate Matlab's vector 
%   processing capabilities and some simple 2-d plotting functions available
%   in Matlab.  It also represents our first use of the fprintf command
%   in Matlab.  This is a relatively straightforward example from the field
%   of Fluid Mechanics.
%
%   File prepared by J. R. White, UMass-Lowell (last update: August 2017)
%

      clear all,  close all,  nfig = 0;
%%
%   define problem parameters 
      Q1 = 64.25;                         % volumetric flow rate (gal/min)
      Q2 = Q1/15850;                      % convert Q to m^3/s
      D = 2*0.0254;                       % pipe diameter (inches converted to m)
      R = D/2;                            % pipe radius (m)
      m = 1/7;                            % exponent in turbulent flow eqns.
%
%   compute some derived quantities
      A = pi*R^2;                         % pipe flow area
      uave = Q2/A;                        % ave velocity
      uLmax = 2*uave;                     % max velocity for laminar flow
      uTmax = (m+1)*(m+2)*uave/2;         % max velocity for turbulent flow
%%
%   evaluate the laminar and turbulent velocity profiles
      Nr = 101;   r = linspace(0,R,Nr);   % discrete r vector
      uL = uLmax*(1 - (r/R).^2);          % laminar velocity profile
      uT = uTmax*(1 - r/R).^m;            % turbulent velocity profile   
%%
%   now plot the results (both profiles on the same plot)
      nfig = nfig+1;   figure(nfig)
      plot(r/R,uL,'r-',r/R,uT,'g--','LineWidth',2), grid
      title('Pipe\_Flow\_1:  Laminar vs. Turbulent Velocity Profiles'); 
      xlabel('Normalized Radial Position - (r/R)'),ylabel('Fluid Velocity (m/s)')
      legend('Laminar Flow','Turbulent Flow')
%
%   finally, let's print out a few summary results
      fprintf(1,'\n   Summary Results for the Pipe Flow Problem \n\n');
      fprintf(1,'  Volumetric flow rate (gal/min):    %10.3f   \n',Q1);
      fprintf(1,'  Volumetric flow rate (m^3/s):      %10.3e   \n',Q2);
      fprintf(1,'  Pipe diameter (inches):            %10.3f   \n',D/0.0254);
      fprintf(1,'  Average velocity (m/s):            %10.3f   \n',uave);
      fprintf(1,'  LAMINAR flow peak velocity (m/s):  %10.3f   \n',uLmax);
      fprintf(1,'  TURBULENT flow peak velocity (m/s):%10.3f   \n',uTmax);
%
%   end of program
