%
%   PLOT_DEMO3.M   Evaluate and plot multiple curves on a single axis 
%                     (Demo for Differential Equations Homework)
%
%   The solution to one of the problems in HW#5 gives the volume of alcohol in a two 
%   tank system versus time.  As part of HW#5 you are asked to plot the resultant 
%   time dependent functions for three different flow rates.  As an example of how 
%   to do this in Matlab, consider the following quantities that are simple functions 
%   of two variables, t and q:
%      V1(t) = V1o*exp(-3*q*t/VT)               =>  alcohol volume for tank 1
%      V2(t) = (0.5*q*V1o/VT)*t*exp(-3*q*t/VT)  =>  alcohol volume for tank 2
%   where  VT is total tank volume
%          V1o is the initial alcohol volume of tank 1
%          q is the flow rate (constant throughtout system)
%
%   Let's plot the alcohol volumes versus time for THREE different flow rates.
%  
%   NOTE THAT THESE EXPRESSIONS ARE SIMILAR TO THE RESULTS FOR YOUR PROBLEM, BUT THEY 
%   ARE NOT THE EXACT RESULTS.  ONCE YOU DEVELOP THE CORRECT EQUATIONS, YOU CAN 
%   EASILY MODIFY THIS ROUTINE TO GENERATE THE DESIRED PLOTS FOR YOUR CASE.  ALSO,
%   YOU SHOULD TRY TO REALLY UNDERSTAND THE LOGIC OF THE PROGRAM...
%
%   File prepared by J. R. White, UMass-Lowell (Jan. 2005)
%

%
%   getting started
      clear all;   close all;   nfig = 0;
%
%   define some parameters
      VT = 200;                  % total tank volume (VT1 = VT2) (gal)
      V1o = 150;                 % initial alcohol volume in tank 1
      q = [10 30 50];            % THREE different flow rates (gal/min)
      t = linspace(0,30,101)';   % time vector for plotting solutions
%
%   now evaluate the alcohol volumes versus time
      Nq = length(q);     Nt = length(t);    % number of flow rates and time points
      V1 = zeros(Nt,Nq);  V2 = zeros(Nt,Nq); % initialize 2-D arrays to store volumes
      for i = 1:Nq     % start loop over number of flow rates
        V1(:,i) = V1o*exp(-3*q(i)*t/VT);    
        V2(:,i) = (0.5*q(i)*V1o/VT)*t.*exp(-3*q(i)*t/VT);
      end              % end loop over number of flow rates
%
%   plot family of curves for V1 & V2 for THREE different flow rates
      nfig = nfig+1;   figure(nfig)
      plot(t,V1(:,1),'r-',t,V1(:,2),'g--',t,V1(:,3),'b-.','LineWidth',2), grid
      title('Plot\_Demo3:  Alcohol volume for Tank 1 vs time for various flow rates')
      xlabel('Time (min)'),ylabel('Alcohol Volume (gal)')
      legend(['q = ',num2str(q(1)),' gal/min'], ...
             ['q = ',num2str(q(2)),' gal/min'], ...
             ['q = ',num2str(q(3)),' gal/min']);
%
      nfig = nfig+1;   figure(nfig)
      plot(t,V2(:,1),'r-',t,V2(:,2),'g--',t,V2(:,3),'b-.','LineWidth',2), grid
      title('Plot\_Demo3:  Alcohol volume for Tank 2 vs time for various flow rates')
      xlabel('Time (min)'),ylabel('Alcohol Volume (gal)')
      legend(['q = ',num2str(q(1)),' gal/min'], ...
             ['q = ',num2str(q(2)),' gal/min'], ...
             ['q = ',num2str(q(3)),' gal/min']);
%
%   end of program
