function plot_air_temp
%
% Plot the temperature of the Earth's atmosphere (in degrees F) as a function
%   of altitude (in feet), from sea level to a great height.
% 
% Orient the plot so that altitude runs up the y-axis, which makes 
%   sense for this particular sort of graph.
%
%
% MWR 4/23/2003

  % limits of altitude (m)
  start_altitude = 0;
  end_altitude = 50000;

  % Walk through the range of altitude, calculating the density at
  %   each point.  Fill two arrays with data so we can 
  %   later make a nice graph.
  delta_altitude = 100;
  num_pieces = (end_altitude - start_altitude)/delta_altitude;
  for (i = 1 : num_pieces)
    altitude = start_altitude + i*delta_altitude;
    temp = air_temperature(altitude);

    y(i) = altitude;
    x(i) = temp;
  end

  
  % now create the graph
  clf;
  plot(x, y, 'b+');
  xlabel('Temperature of air (degrees F)');
  ylabel('Height above sea level (m)');


% end of function "plot_air_density"


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function temp = air_temperature(altitude)
%
% Calculate the temperature of the Earth's atmosphere (in degrees F)
%   as a function of height (in meters).  Based on infomation from NASA's
%   Glenn Research Center:
%
%       http://www.grc.nasa.gov/WWW/K-12/airplane/atmos.html
%
%
% Arguments:      altitude    (input)     height above sea level (meters)
%
%                 temp        (output)    temperature of air (degrees F)
%
% MWR 4/23/2003

  if (altitude < 0)
    error('air_temperature: altitude cannot be negative!');
  end

  % convert altitude from meters to feet, since our model uses feet
  %   as the basic unit
  feet = altitude/0.3048;

  % for the troposphere ...
  if (feet < 36152)
    temp = 59 - 0.00356*feet;

  % for the lower stratosphere
  elseif (feet < 82345)
    temp = -70;
  
  % for the upper stratosphere
  else 
    temp = -205.05 + 0.00164*feet;
  end

% end of function "air_temperature"
