%
%   SAND_PITS.M    Obtain various data for a construction company 
%                       using array and matrix operations
%
%   This demo illustrates how to obtain several types of information from
%   data stored within a couple of arrays.  In this case, the data from two
%   tables are entered into Matlab arrays and several manipulations are performed 
%   to obtain the desired information.
%     Table 1:  Composition of sand, fine gravel, and coarse gravel
%                       in three different sand pits
%        C(i,j) = composition of component j in pit i
%
%     Table 2:  Monthly volume of material mined from each pit
%        V(i,k) = cubic yards of material mined from pit i in month k
%
%   Note:  There are three components: 
%           1 -> sand,   2 -> fine gravel,  and   3 -> coarse gravel
%
%   File produced by Prof. J. R. White, UMass-Lowell (last update: September 2017)
%

      clear all, close all, nfig = 0;
%
%   enter data from Tables 1 and 2
      C = [55 30 15; 20 50 30; 25 20 55]/100;     % composition information
      V = [10 12 15 25 28 36 39 42 43 36 26 14;   % volume information
           16 19 23 31 35 45 49 55 55 46 28 18;
            8 10 12 22 27 40 45 52 48 45 25 10]*1000; 
%
%   1. total yearly amount of material from pit number 3
      fprintf(1,'  Total amount of material from pit #3 (cubic yards): %8.0f \n', ...
              sum(V(3,:))); 
%
%   2. max and min amount of fine gravel produced and corresponding months
      A = C(:,2)'*V;           % gives vector by month of total fine gravel
      [Amax,Imax] = max(A);   [Amin,Imin] = min(A);
      months1 = ['Jan'; 'Feb'; 'Mar'; 'Apr'; 'May'; 'Jun';
                 'Jul'; 'Aug'; 'Sep'; 'Oct'; 'Nov'; 'Dec'];
      fprintf(1,'  Max amount of fine gravel produced was %8.0f cubic yards in %s \n', ...
                Amax,months1(Imax,:)); 
      fprintf(1,'  Min amount of fine gravel produced was %8.0f cubic yards in %s \n', ...
                Amin,months1(Imin,:)); 
%
%   3. total amount of sand used during June, July, and August
      fprintf(1,'  Amount of sand used in summer (cubic yards):        %8.0f \n', ...
              sum(C(:,1)'*V(:,6:8))); 
%
%   4. plot of total volume for each pit by month
      nfig = nfig+1;  figure(nfig)
      bar(V'),grid
      title('Sand\_Pits:  Total Volume by Month for Each Pit')
      xlabel('Month of Year'), ylabel('Volume (cubic yards)')
      legend('Pit #1','Pit #2','Pit #3')
%
%   5. plot of total sand, fine gravel, and coarse gravel by month
      nfig = nfig+1;  figure(nfig)
      bar(V'*C),grid
      title('Sand\_Pits:  Total Volume by Month and Sand Type')
      xlabel('Month of Year'), ylabel('Volume (cubic yards)')
      legend('Sand','Fine Gravel','Coarse Gravel')
%
%   6. create pie chart showing the fractional total volume by pit
      totvol = sum(sum(V));
      nfig = nfig+1;  figure(nfig)
      pie(sum(V'),{'Pit #1' 'Pit #2' 'Pit #3'})
      title(['Sand\_Pits:  Fractional Usage of Each Sand Pit  (total ', ...
             num2str(totvol,'%10.2e'),' cubic yards)'])
%
%   7. create pie chart showing the fractional total volume by month
      months2 = {'Jan' 'Feb' 'Mar' 'Apr' 'May' 'June' ...
                 'July' 'Aug' 'Sept' 'Oct' 'Nov' 'Dec'};
      nfig = nfig+1;  figure(nfig)
      pie(sum(V),months2)
      title(['Sand\_Pits:  Fractional Usage by Month  (total ', ...
             num2str(totvol,'%10.2e'),' cubic yards)'])
%
%   end of program       
      