The equations from NASA tell us that air temperature depends only on height. The equations, though, are in units of feet and degrees Fahrenheit. We first need to convert that to meters and degrees Kelvin.
Degrees Farenheit and degrees Celsius are related by:
°C = (°F-32)/1.8
Degrees Kelvin and degrees Celsius are related by
°K = °C + 273.15
So
°K = 273.15 + (°F-32)/1.8 = 255.37 + 0.5556°F
Feet and meters are related by:
m = 0.3048 ft or
ft = 3.281 m
Now, there are three equations for temperature, depending on the range of height. The first is:
T°F(h ft) = -205.05 + 0.00164h (h > 82345)
Converting this to Kelvin and meters, we have
T°K(h m) = 255.37 + 0.5556(-205.05 + 0.00164(3.281 h)) [(3.281 h) > 82345]
= 141.44 + 0.00299 h (h > 25098)
The second equation is:
T°F(h ft) = -70 (36152 < h < 82345)
Converting this, we have:
T°K(h m) = 216.5 (11019 < h < 25098)
Finally, the third equation is:
T°F(h ft) = 59 - 0.00356h (h < 36152)
Converting this, we have:
T°K(h m) = 255.27 + 0.5556(59 - 0.00356(3.281h)) (3.281h < 36152)
= 288.05 - 0.00649h (h < 11019)
So, finally, our temperature model is:
We program this twice — once in the original English units, and once in metric units — and compare the two. To help ensure against systematic errors, we will take the English units as primary, and convert metric units into English units as needed. This will not find all systematic errors, such as completely misunderstanding the problem, but will find errors in conversion. We will regard the two temperature functions as equivalent if they agree to within one degree Fahrenheit. Finally, we will graph the temperature function, as well.
We need to write three programs: two temperature functions and one unit test. First, we have the temperature functions.
Program TEMPE (87 bytes):
; Program TEMPE
;
; Computes the temperature in degrees Fahrenheit for a height given in
; feet.
;
; Variables:
; A is height in feet on entry, and temperature in degrees Fahrenheit
; on exit
;
; Symbols:
; >= is greater than or equal to relational
; (-) is unary negation operator
; -> is assignment arrow
If A>=82345 ; See if high region
Then (-)205.05+.00164A->A
; If so, compute temperature
Else If A>=36152 ; Otherwise, see if middle region
Then (-)70->A ; If so, compute temperature
Else 59-.00356A->A ; Otherwise, compute temperature
IfEnd ; [End of test if middle region]
IfEnd ; [End of test if high region]
Program TEMP (92 bytes):
; Program TEMP
;
; Computes the temperature is degrees Kelvin for a height given in
; meters.
;
; Variables:
; A is height in meters on entry, and temperature in degrees Kelvin
; on exit
;
; Symbols:
; <= is less than or equal to relational
; -> is assignment arrow
If A<=11019 ; Test if in low region
Then 288.05-.00649A->A
; If so, compute temperature
Else If A<=25098 ; Otherwise, test if in middle region
Then 216.5->A ; If so, compute temperature
Else 141.44+.00299A->A ; Otherwise, compute temperature
IfEnd ; [End of test if in middle region]
IfEnd ; [End of test if in low region]
Then, we have the the comparison and display program. This program tests each height from 0 to 100,000 feet, in increments of 1,000 feet, and compares the results of these two functions. If they differ by more than one degree Fahrenheit, it stops. It also displays the graph of the temperature function. (The temperature reaches a low of -70 and a high of 59, and the altitude is between 0 and 100,000, so we can just set the view window as the first thing.)
Program UTTEMP (172 bytes):
; Program UTTEMP
;
; Compares the results of TEMPE and TEMP, and also graphs the results
;
; Variables:
; A is height [input to TEMPE, TEMP], result temperature [output from
; TEMPE, TEMP]
; B is height in feet
; C is temperature in F
; D is temperature in K
; E is F equivalent of temperature in K
;
; Symbols:
; (-) is unary negation
; -> is assignment arrow
; * is multiplication
; >= is greater than or equal to relational
; _ is display triangle
; <> is not equal to relational
ViewWindow 0,100000,10000,(-)75,75,25
; Set the view window
For 0->B To 100000 Step 1000
; Loop through altitudes
B->A ; Get the altitude for English units
Prog "TEMPE" ; Get the temperature in F
A->C ; Save the temperature in F
.3048B->A ; Get the altitude in meters
Prog "TEMP" ; Get the temperature in K
A->D ; Save the temperature in K
(D-273.15)*1.8+32->E
; Convert K to F
If Abs (C-E)>=1 ; See if temps differ by more than 1 F
Then "ERROR"_ ; If so, say ERROR and stop
IfEnd ; [End of if temps differ by more than 1F]
Plot B,C ; Plot the altitude, temperature
If B<>0 ; If not the first time,
Then Line ; draw a line
IfEnd ; [End of test if the first time]
Next ; [End of altitude loop]
"DONE" ; Say we are done
Running this program shows no notable difference between the definitive temperature function, in degrees Fahrenheit and feet altitude, and our derived temperature function, in degrees Kelvin and meters altitude. We also get the following plot of temperature as a function of height:
This has three linear regions, as expected. The graph starts out at about 60 and drops to about -70, then stays there for a while, then rises. This seems to be what we would expect from the function, although it does seem somewhat counter-intuitive.
On the whole, it seems as if the temperature function is more or less done.
We need to write three programs: two temperature functions and one unit test. First, we have the temperature functions.
Private Function Temp(Meters As Single) As Single
' FUNCTIONAL DESCRIPTION:
'
' Computes the temperature in degrees Kelvin as a function of
' height in meters.
'
' FORMAL PARAMETERS:
'
' Height.rf.v - The height in meters for which the temperature
' is to be computed.
'
' IMPLICIT INPUTS:
'
' None.
'
' IMPLICIT OUTPUTS:
'
' None.
'
' RETURN VALUE:
'
' The temperature at the specified height in degrees Kelvin.
'
' SIDE EFFECTS:
'
' None.
'
If Meters <= 11019 Then
Temp = 288.05 - 0.00649 * Meters
Else
If Meters <= 25098 Then
Temp = 216.5
Else
Temp = 141.44 + 0.00299 * Meters
End If
End If
End Function
and:
Private Function TempE(Feet As Single) As Single
' FUNCTIONAL DESCRIPTION:
'
' Computes the temperature in degrees Fahrenheit as a function
' of height in feet.
'
' FORMAL PARAMETERS:
'
' Height.rf.v - The height in feet for which the temperature is
' to be computed.
'
' IMPLICIT INPUTS:
'
' None.
'
' IMPLICIT OUTPUTS:
'
' None.
'
' RETURN VALUE:
'
' The temperature at the specified height in degrees Fahrenheit.
'
' SIDE EFFECTS:
'
' None.
'
If Feet > 82345 Then
TempE = -205.05 + 0.00164 * Feet
Else
If Feet > 36152 Then
TempE = -70
Else
TempE = 59 - 0.00356 * Feet
End If
End If
End Function
Then, we have the the comparison and display program. This program tests each height from 0 to 100,000 feet, in increments of 1,000 feet, and compares the results of these two functions. If they differ by more than one degree Fahrenheit, it stops. It also displays the graph of the temperature function using the CalculatorPlot control. (The temperature reaches a low of -70 and a high of 59, and the altitude is between 0 and 100,000, so we can just set the view window as the first thing.)
Private Sub UTTemp()
' FUNCTIONAL DESCRIPTION:
'
' Unit test for the Temp and TempE routines.
'
' For heights from 0 to 100,000 feet, computes the temperature
' at the height using both the Temp and TempE routines, and
' compares the two temperatures to ensure they are within one
' degree Fahrenheit of one another. Also graphs the temperature
' 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:
'
' Displays a message box with the message "Error!" if the two
' temperature computations differ by more than one degree
' Fahrenheit.
'
Dim Feet As Single
Dim Fahrenheit As Single
Dim KelvinAsFahrenheit As Single
plt.ViewWindow 0, 100000, 10000, -75, 75, 25
For Feet = 0 To 100000 Step 1000
Fahrenheit = TempE(Feet)
KelvinAsFahrenheit = (Temp(0.3048 * Feet) - 273.15) * 1.8 + 32
If Abs(Fahrenheit - KelvinAsFahrenheit) > 1 Then
MsgBox "Error!"
Stop
End If
plt.Plot Feet, Fahrenheit
If Feet <> 0 Then
plt.PlotLine
End If
DoEvents
Next Feet
WriteLn "Temperature unit test run."
End Sub
Running this program shows no notable difference between the definitive temperature function, in degrees Fahrenheit and feet altitude, and our derived temperature function, in degrees Kelvin and meters altitude. The plot of temperature resulting from this Visual Basic for Applications program looks remarkably like that from the calculator shown above. The graph has three linear regions, as expected. The graph starts out at about 60 and drops to about -70, then stays there for a while, then rises. This appears correct.
It is time to declare success and move on.
[ 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
Temperature
Answers
Modularization
Data Structures I
Recursion
Program Attributes
Building Blocks II
Algorithm Analysis
Structuring
Data Structures II
Abstract Types
Objects
Problem Analysis
Reference Card
![]()