function Pex1()
%---------------------------------------------------------------
%       USAGE: Pex1()
%
%    MODIFIED: Dr. Wayne Brown           DATE:  29 Jan 2005
%
% DESCRIPTION: Given a aircraft's speed and altitude, calculate the
%              distance a gravity bomb travels after its release, along with
%              the time it takes to reach the ground. (Wind and friction
%              are ignored.) 
%
%      INPUTS: None
%
%     OUTPUTS: None
%
%  REFERENCES: None
%---------------------------------------------------------------

%---------------------------------------------------------------
% Physics and conversion constants

G                 = 32.1725;          % gravity acceleration (ft/sec^2)
MPH_to_ft_per_sec = 5280.0 / 3600.0;  % Miles/hr converted to ft/sec

% Limits on the calculations accuracy

Minimum_valid_velocity                     =     0;
Maximum_velocity_for_accurate_calculations =   300;
Minimum_valid_altitude                     =    50;
Maximum_valid_altitude                     = 10000;
Minimum_safe_bombing_altitude              =   200;
Maximum_accurate_bombing_altitude          =  1500;

%---------------------------------------------------------------
% Opening description of the program

clc();
fprintf('This program calculates the distance (in feet) and the time\n');
fprintf('(in seconds) that a gravity bomb travels after being dropped\n');
fprintf('from an airplane at a specified speed (in MPH) and altitude\n');
fprintf('(in feet) from ground level.\n\n');

%---------------------------------------------------------------
% Get user input and validate the input values

Velocity_MPH = input('Enter the speed of the plane (miles per hour) : ');
while Velocity_MPH <= Minimum_valid_velocity
  fprintf('ERROR: %g is less than %g\n', Velocity_MPH, Minimum_valid_velocity);
  Velocity_MPH = input('      Re-enter the speed of the plane (miles per hour) : ');
end

Altitude_ft = input('Enter the height of the plane above ground level (feet) : ');
while Altitude_ft < Minimum_valid_altitude || Altitude_ft > Maximum_valid_altitude
  fprintf('ERROR: %g is not in the valid range of %1.0f to %1.0f\n', ...
           Altitude_ft, Minimum_valid_altitude, Maximum_valid_altitude);
  Altitude_ft = input('      Re-enter the height of the plane above ground level (feet) : ');
end

%---------------------------------------------------------------
% Print appropriate warnings concerning the calculated values

fprintf('\n');
if Velocity_MPH > Maximum_velocity_for_accurate_calculations
  fprintf('WARNING: At this velocity wind resistance is not negligible.\n');
  fprintf('         The predicted impact distance many be high.\n');
else
  fprintf('The velocity is in acceptable range.\n');
end

if Altitude_ft < Minimum_safe_bombing_altitude
  fprintf('WARNING: This is a very low altitude for dropping bombs.\n');
elseif Altitude_ft >= Maximum_accurate_bombing_altitude
  fprintf('WARNING: At this altitude wind resistance is not negligible.\n');
  fprintf('         The predicated impact distance may be inaccurate.\n');
else
  fprintf('The altitude is in acceptable range.\n');
end

%---------------------------------------------------------------
% Calculations

Time_seconds = sqrt(Altitude_ft / (0.5*G));  % Using distance = (1/2)*g*t^2
Distance_ft  = (Velocity_MPH * MPH_to_ft_per_sec) * Time_seconds;

%---------------------------------------------------------------
% Output results

fprintf('\n');
fprintf('GIVEN: Aircraft speed      : %6.0f   miles per hour\n', Velocity_MPH);
fprintf('       Aircraft altitude   : %6.0f   feet\n', Altitude_ft);
fprintf('BOMB:  Horizontal distance : %6.0f   feet.\n', Distance_ft);
fprintf('       Time to impact      : %8.1f seconds.\n', Time_seconds);

