Now that we have functions for the air pressure and the air temperature as a function of height, we can compute the air density as a function of height. We return to the ideal gas law:

PV = nRT

Density is mass per unit volume, so recasting this, we have:

n/V = P/RT

This will give us moles per volume. Recall from the speed of sound section that air has an approximate effective molecular weight of 29.1 g/mole, so the air density is

ρ = (0.0291 kg mole^{-1})P/(8.314 kg m^{2}
mole^{-1} °K^{-1} sec^{-2})T

= 0.003500 P/T °K sec^{2} m^{-2}

If the pressure is measured in Newtons per square meter (kg
m^{-1} sec^{-2}), and temperature is in
degrees Kelvin (°K), the density will be in kilograms
per cubic meter.

This can be programmed very straightforwardly.

We can also quickly write a unit test for this routine.
We have nothing to compare the density with, however: all we can do
is plot it.
In keeping with our philosophy of using English units for our
graphs, however, we do note that one kilogram per cubic meter is
about 0.001941 slugs per cubic foot.
(Yes, a “slug” is the English unit of mass.
It is the mass that will be accelerated by one foot/second^{2}
by a force of one pound.
It weighs 32.174 pounds when subjected to one gravity.)

Program DENSH (57 bytes):

; Program DENSH ; ; Compute the air density in kilograms per cubic meter at a specific ; height in meters ; ; Variables: ; A is height [on entry], pressure [on exit from PRESSH], temperature ; [on exit from TEMP], density [on exit from this routine] ; B is destroyed [PRESSH], pressure ; C is saved height ; ; Symbols: ; -> is assignment arrow ; / is division operator A->C ; Save height in C Prog "PRESSH" ; Get pressure in Newtons/square meter A->B ; Save pressure in B C->A ; Get height in A Prog "TEMP" ; Get temperature at height .0035B/A->A ; Compute density

Program UTDENSH (119 bytes):

; Program UTDENSH ; ; Call DENSH for heights from 0 to 100,000 feet and display the ; logarithm of the result. ; ; Variables: ; A is height [to DENSH], density [from DENSH] ; B-C are destroyed by DENSH ; D is height ; ; Symbols: ; -> is assignment arrow ; EE is EXP key ; (-) is unary negation operator ; <> is not equal to relational ViewWindow 0,100000,10000,(-)5,(-)2,1 ; Set the view window For 0->D To 100000 Step 1000 ; Loop through the altitudes .3048D->A ; Convert height to meters Prog "DENSH" ; Get density in kilograms/cubic meter 1.941EE(-)3A->A ; Convert to slugs per cubic foot Plot D,log A ; Plot the point If D<>0 ; If this is not the first point Then Line ; connect last point and this one IfEnd ; [End of test if this is the first point] Next ; [End of loop through altitudes] "DONE" ; Notify of completion

Out of this, we get the following graph of the logarithm of air density (in slugs per cubic foot) as a function of height (in feet):

The graph’s Y values range from -5 to -2, and so the calculator excludes the X axis, and just gives the tick marks instead.

This does not tell us much. However, the NASA page defining the properties of air mentions that at sea level on a standard day, the air density is 0.00237 slugs per cubic foot. Our routine computes 0.00239 slugs per cubic foot for an altitude of 0, which at least gives us some hope that we got it right.

We can code the air density function and unit test in Visual Basic for Applications as follows.

Private Function Density(ByVal Meters As Single) As Single ' FUNCTIONAL DESCRIPTION: ' ' Computes the density of air in kilograms per cubic meter as a ' function of height in meters. ' ' FORMAL PARAMETERS: ' ' Height.rf.v - The height in meters for which the air density ' is to be computed. ' ' IMPLICIT INPUTS: ' ' None. ' ' IMPLICIT OUTPUTS: ' ' None. ' ' RETURN VALUE: ' ' The air density in kilograms per cubic meter at the specified ' height. ' ' SIDE EFFECTS: ' ' None. ' Dim sngPressure As Single Dim sngTemp As Single sngPressure = Pressure(Meters) sngTemp = Temp(Meters) Density = 0.0035 * sngPressure / sngTemp End Function

Private Sub UTDensity() ' FUNCTIONAL DESCRIPTION: ' ' Unit test for the air density routine. ' ' Graphs the common logarithm of air density as a function of ' height. ' ' FORMAL PARAMETERS: ' ' None. ' ' IMPLICIT INPUTS: ' ' None. ' ' IMPLICIT OUTPUTS: ' ' plt - The CalculatorGraph control. ' The text of the current document. ' ' RETURN VALUE: ' ' None. ' ' SIDE EFFECTS: ' ' None. ' Dim sngFeet As Single plt.ViewWindow 0, 100000, 10000, -5, -2, 1 For sngFeet = 0 To 100000 Step 1000 plt.Plot sngFeet, Log10(0.001941 * Density(0.3048 * sngFeet)) If sngFeet <> 0 Then plt.PlotLine End If DoEvents Next sngFeet WriteLn "Density unit test run." End Sub

Running the unit test gives us the same results as the calculator.

[ Previous page | Top of page | Next page ]

Copyright © 2002 Brian Hetrick

Page last updated 10 February 2002.

Tutorial

Building Blocks I

Control Flow II

Basic I/O

Algorithms

A First Program

Examples

Pumpkins

Density

Answers

Modularization

Data Structures I

Recursion

Program Attributes

Building Blocks II

Algorithm Analysis

Structuring

Data Structures II

Abstract Types

Objects

Problem Analysis

Reference Card