%
%   SLANTED_GATE_1.M       Comprehensive Application
%               Analysis of a Slanted Gate on a Water Reservoir
%
%   This file does some computational analysis for the Slanted Gate Problem
%   discussed in the notes.  It illustrates several of Matlab's capabilities
%   including the use of anonymous functions, function subprograms, the fzero and 
%   fminbnd optimization routines, and finding roots of polynomials with the 
%   roots command.  It also uses several simple plotting features including the 
%   hold on/off command, subplots, and the text command to place text at a specific
%   location on the plot.  Finally, it also illustrates proper file documentation 
%   using internal comments and the fprintf command and it provides a number of 
%   fairly complicated arithmetic expressions where "dot" arithmetic is needed.  
%   Overall, this program should give a good view of some of the analysis 
%   capabilities that are easily implemented in Matlab.  A good understanding of 
%   this example will go a long way in helping you apply Matlab in a variety of 
%   different situations. 
%
%   The basic idea for this problem was obtained from Example 11.1.2 on
%   pages 481-485 in "An Engineer's Guide to Matlab" by Magrab, et. al. 
%   (2000, Prentice Hall).
%
%   File prepared by J. R. White, UMass-Lowell  (last update:  Nov. 2017)
%

      clear all,  close all,  nfig = 0;
%
%   identify basic problem data
      a = 5;                         % initial width of reservoir (m)
      B = 10;                        % depth of reservoir into page (m)
      L = 10;                        % vertical length of gate (m)
      Vw = a*L*B;                    % initial water volume
      W = 100;                       % weight of gate (kN)
      spwt = 9.81;                   % specific weight of water (kN/m^3)
%
%   Part I:  Determine total volume vs gate angle.  Also find max volume 
%            and associated angle.
%
%   graphical analysis
      VofTh = @(th) -Vw*cos(th).*(1 + L*sin(th)/(2*a));  % anonymous function
      th = linspace(0,pi/2,181);  
      Vol = -VofTh(th);
      nfig = nfig+1;   figure(nfig)
      plot(th*180/pi,Vol/Vw,'r-','LineWidth',2),grid
      title('Slanted\_Gate\_1:  Reservoir Volume vs Gate Angle')
      xlabel('Gate Angle (degrees)'),ylabel('Relative Volume (V/V_w)')
%
%   formal optimization routine for finding Vmax
      [thmax,Vmax] = fminbnd(VofTh,0,pi/2);  
      Vmax = -Vmax/Vw;   thmax = thmax*180/pi;
      fprintf(1,'\n       Summary Results from the Slanted_Gate_1.m Program \n\n');
      fprintf(1,'  Max possible value of V/Vw is %6.3f \n',Vmax);
      fprintf(1,'  Max V/Vw occurs at a gate angle of %6.3f degrees \n',thmax);
      r = axis;   r(4) = 1.5;   axis(r);
      hold on
      plot(thmax,Vmax,'gs','LineWidth',2)
      text(20,Vmax+.13,['V_{max}/V_{w} = ',num2str(Vmax,'%5.2f')])
      text(20,Vmax+.06,['    at \theta = ',num2str(thmax,'%5.1f'),'\circ'])
%
%   Part II:  Find the limiting angle so the water does not spill (put on above plot)
%
      fth = @(th) Vw*cos(th)*(1 + L*sin(th)/(2*a)) - Vw;
      [thlim_r] = fzero(fth,[pi/6 pi/2]);
      Vlim = -VofTh(thlim_r)/Vw;  thlim_d = thlim_r*180/pi;   
      fprintf(1,'\n');
      fprintf(1,'  Limiting angle for water not to spill is %6.3f degrees\n',thlim_d);
      fprintf(1,'  The value of V/Vw at this point is %6.3f \n',Vlim);
      plot(thlim_d,Vlim,'gs','LineWidth',2)
      text(thlim_d+1,Vlim+.05,['\theta_{lim} = ',num2str(thlim_d,'%5.2f'),'\circ'])
      hold off
%
%   Part III:  Find water height vs gate angle for no spillage (via roots command)
%
      Nth = 100;  th = linspace(0,thlim_r,Nth);  h = zeros(1,Nth);
      for i = 1:Nth
        p = [B*tan(th(i))/2 a*B -Vw];  rr = roots(p);  h(i) = max(rr);
      end
      nfig = nfig+1;   figure(nfig)
      plot(th*180/pi,h/L,'r-','LineWidth',2),grid
      title('Slanted\_Gate\_1:  Relative Water Height vs Gate Angle')
      xlabel('Gate Angle (degrees)'),ylabel('Relative Water Height (h/L)')
%
%   Part IV:  Determine resultant force, FR, vs gate angle and location of 
%            the center of pressure relative to the hinge (for fixed V = Vw).
%
      FR = spwt*B*h.*h./(2*cos(th));  ycp = h./(3*cos(th));
      nfig = nfig+1;   figure(nfig)
      subplot(2,1,1),plot(th*180/pi,FR,'r-','LineWidth',2),grid
      title('Slanted\_Gate\_1:  Force due to Water vs Gate Angle')
      ylabel('Resultant Force (kN)')
      subplot(2,1,2),plot(th*180/pi,ycp/L,'r-','LineWidth',2),grid
      title('Slanted\_Gate\_1:  Center of Pressure vs Gate Angle')
      xlabel('Gate Angle (degrees)'),ylabel('Relative CP Location (y_{cp}/L)')
%
%   Part V:  Determine rod force, Frod, vs gate angle and find an optimum 
%               angle which minimizes the rod force.
%
%   Note:  Since I plan to use fminbnd to find the formal minimum of Frod vs theta,
%          let's put all the needed computations in a function file.  This is a
%          little redundant since most of the information is already available.
%          However, since efficiency is not the real goal here, let's just focus
%          on the current analysis (and forget that we already computed most of the 
%          needed parameters).  Here we will simply implement eqn. (6) from the notes
%          -- and any intermediate results that may be needed...
%
      Nth = 100;  th = linspace(0,thlim_r,Nth);
      Frod = slanted_gate_1a(th,a,B,L,Vw,W,spwt);
      nfig = nfig+1;   figure(nfig)
      plot(th*180/pi,Frod,'r-','LineWidth',2),grid
      title('Slanted\_Gate\_1:  Rod Force vs Gate Angle')
      xlabel('Gate Angle (degrees)'),ylabel('Rod Force (kN)')
      hold on
%
      [thmin,Frodmin] = fminbnd(@slanted_gate_1a,0,thlim_r,[],a,B,L,Vw,W,spwt);  
      fprintf(1,'\n');
      fprintf(1,'  Minimum value of Frod is %6.3f kN \n',Frodmin);
      fprintf(1,'  Minimum Frod occurs at a gate angle of %6.3f degrees \n',thmin*180/pi);
      plot(thmin*180/pi,Frodmin,'gs','LineWidth',2)
      text(29,Frodmin-75,['Frod_{min} = ',num2str(Frodmin,'%5.1f'),' kN'])
      text(29,Frodmin-150,['      at \theta = ',num2str(thmin*180/pi,'%5.1f'),'\circ'])
%
%  end of problem
