%
%   PLOT_DEMO2.M   Demo of how to plot simple functions in Matlab
%                  (Demo for Differential Equations Homework)
%
%   The solution to Prob. 28 pg. 18 in the 2nd Ed. of Edwards & Penny (Prentice 
%   Hall, 2000) gives:
%      v(t) = vo + a*t             % velocity of the car after braking
%      x(t) = vo*t + (1/2)*a*t^2   % distance travelled from braking point
%
%   where a = constant acceleration (a negative value for deceleration that is  
%             assumed valid for all operating speeds)
%                                                
%   However, we are interested in the time it takes to stop and the braking distance
%   needed as a function of the initial speed of the car.  For this situation,
%   vf = v(tf) = 0, and the stopping time, tf, and stopping distance, xf, can be
%   written as
%      tf = -vo/a     and      xf = -vo^2/2a
%
%   Let's plot these quantities versus initial speed, vo.
%
%   File prepared by J. R. White, UMass-Lowell (Jan. 2005)
%

%
%   getting started
      clear all;   close all;   nfig = 0;
%
%   define some initial parameters
      a = -8.333e+4;  % const deceleration (km/hr^2) (determined from problem info)
      cf1 = 1000;     % conversion factor (m per km)
      cf2 = 3600;     % conversion factor (sec per hr)
%
%   define range of reasonable speeds
      vo = linspace(0,150,21);     % speed in km/hr
%
%   convert units to m and sec
      aa = a*cf1/(cf2^2);    % deceleration constant in m/s^2
      vvo = vo*cf1/cf2;      % initial speed in m/s
%
%   now evaluate desired quantities 
      tf = -vvo/aa;   xf = -vvo.^2/(2*aa);
%
%   plot desired quantities
      nfig = nfig+1;   figure(nfig)
      subplot(2,1,1),plot(vo,tf,'r','LineWidth',2), grid
      title('Plot\_Demo2:  Stopping time and distance versus initial speed')
      ylabel('Stopping Time (sec)')
%
      subplot(2,1,2),plot(vo,xf,'g','LineWidth',2), grid
      ylabel('Stopping Distance (m)'),xlabel('Initial Speed (km/hr)')
%
%   end of program
