Previous page Next page Navigation bar

Calculator Programming Tutorial

Programming Building Blocks I

Examples

Ballistic Pumpkins

Air Pressure
Program Analysis

The equations from NASA tell us air pressure as a function of height and temperature. We already know temperature as a function of height, and so can substitute. However, the pressure equations from NASA give air pressure in pounds per square foot.

Well, we know a pound is 4.448 newtons, where each newton is 1 kg m sec-2. And we know a foot is 0.3048 meters, so a square foot is .09291 square meters. So a pound per square foot is 4.448/0.09291 = 47.88 kg m-1 sec-2.

The first air pressure region is h < 36152 ft = 11019 m. The equation, and its conversion into metric units, is:

Air pressure equation 1, click for description

Apparently the additive constant is supposed to go away when we convert to absolute temperature, but it does not, quite.

The second air pressure region is 36152 ft < h < 82345 ft, or 11019 m < h < 25098 m. The equation, and its conversion into metric units, is:

p(h) = exp (11.76 - 0.0001575h)

At the surface, or height 0, this yields a pressure of 128027 Newtons per square meter, or 18.6 PSI. This is a little high — we seem to remember “air pressure” is about 16 pounds per square inch — but is certainly acceptable, particularly since the surface is very much outside the range of validity of this approximation.

The third and final air pressure region is 82345 ft < h, or 25098m < h. In this region, the equation, and its conversion into metric units, is:

Equation for air pressure, click for explanation

Now, these are easy enough to program directly, so we do so.

Just as we did with the temperature model, we will also program this in English units, then compare the two implementations.

Finally, our unit test will now, as is familiar, run the two routines in parallel, convert from the metric units back to English units, compare the two, and graph. However, since air pressure goes from 15 PSI to 0.1 PSI exponentially, it will actually graph the common logarithm of the air pressure, rather than the air pressure itself.

Calculator Code

The calculator code for the two pressure routines follows.

Program PRESSH (132 bytes):

; Program PRESSH
;
; Find the pressure (in Newtons per square meter) as a function of the
; altitude (in meters)
;
; Variables:
; A is the altitude [entry, input to TEMP], temperature [output from
;   TEMP], pressure [output from this routine]
; B is the altitude
;
; Symbols:
; -> is assignment arrow
; <= is less than or equal to relational
; EE is EXP key
; (-) is unary negation
; ^ is exponentation operation
; e^x is e to the x power key
 
A->B               ; Save altitude to B
Prog "TEMP"        ; Get temperature
If B<=11019        ; Check if altitude region 1
Then 1.19752EE(-)8(A+.01667)^5.256->A
                   ;   If so, compute pressure
Else If B<=25098   ;   If not, check if altitude region 2
Then e^x(11.76-1.575EE(-)4B)->A
                   ;     If so, compute pressure
Else 9.903EE29A^(-)11.388->A
                   ;     If not, compute pressure
IfEnd              ;   [End of test if altitude region 2]
IfEnd              ; [End of test if altitude region 1]

Program PRESSE (151 bytes):

; Program PRESSE
;
; Find the pressure (in pounds per square foot) as a function of the
; height (in feet)
;
; Variables:
; A is the altitude [entry, input to TEMP], temperature [output from
;   TEMP], pressure [output from this routine]
; B is the altitude
;
; Symbols:
; -> is assignment arrow
; <= is less than or equal to relational
; (-) is unary negation
; ^ is exponentation operation
; e^x is e to the x power key
 
A->B               ; Save altitude to B
Prog "TEMPE"       ; Get temperature
If B>=82345        ; Check if altitude region 3
Then 51.97((A+459.7)/389.98)^(-)11.388->A
                   ;   If so, compute pressure
Else If B>=36152   ;   If not, check if altitude region 2
Then 473.1e^x(1.73-.000048B)->A
                   ;     If so, compute pressure
Else 2116((A+459.7)/518.6)^5.256->A
                   ;     If not, compute pressure
IfEnd              ;   [End of check if altitude region 2]
IfEnd              ; [End of check if altitude region 1]

The calculator code for the unit test follows.

Program UTPRESSH (169 bytes):

; UTPRESSH
;
; Unit test driver for PRESSH routine.  Compare PRESSH against PRESSE
; and graph results.
;
; Variables:
; A is input to and output from PRESSE, PRESSH
; B is destroyed by PRESSE, PRESSH
; C is altitude in feet
; D is PRESSE pressure in lbs/sq in
; E is PRESSH pressure in lbs/sq in
;
; Symbols:
; (-) is unary negation
; -> is assignment arrow
; / is division operator
; EE is EXP key
; >= is greater than or equal to relational
; _ is display triangle
; <> is not equal to relational
 
ViewWindow 0,100000,10000,(-)1,1.2,1
                   ; Set the view window
For 0->C To 100000 Step 1000
                   ; Loop through altitudes
C->A               ;   Set argument to PRESSE
Prog "PRESSE"      ;   Get pressure in lbs/sq ft
A/144->D           ;   Save pressure in lbs/sq in to D
.3048C->A          ;   Get argument to PRESSH
Prog "PRESSH"      ;   Get pressure in N/sq m
1.45EE(-)4A->E     ;   Save pressure in lbs/sq in to E
If Abs (D-E)>=.01D ;   Check for a discrepancy of 1% or more
Then "ERROR"_      ;     If there is one, signal an error
IfEnd              ;   [End of discrepancy check]
Plot C,log D       ;   Plot the point
If C<>0            ;   If this is not the first point
Then Line          ;     draw a line from the previous point
IfEnd              ;   [End of test if first point]
Next               ; [End of loop through altitudes]
"DONE"             ; Report completion

When we run this program, it finds no discrepancy of 1% or more, and also produces the following graph of the logarithm of air pressure as a function of height:

Screen shot of calculator

Note how the pressure starts at about 15 pounds per square inch and falls to about 0.1 pound per square inch, essentially logarithmically.

Visual Basic for Applications Code

The Visual Basic for Applications code for the two pressure routines follows.

Private Function Pressure(ByVal Meters As Single) As Single
 
'   FUNCTIONAL DESCRIPTION:
'
'       Computes the air pressure in Newtons per  square  meter  as  a
'       function of altitude in meters.
'
'   FORMAL PARAMETERS:
'
'       Height.rf.v - The height in meters for which the air  pressure
'           is to be found.
'
'   IMPLICIT INPUTS:
'
'       None.
'
'   IMPLICIT OUTPUTS:
'
'       None.
'
'   RETURN VALUE:
'
'       The air pressure in Newtons per square meter at the  specified
'       altitude.
'
'   SIDE EFFECTS:
'
'       None.
'
 
    Dim sngTemp As Single
 
    sngTemp = Temp(Meters)
    If Meters <= 11019 Then
        Pressure = 0.0000000119752 * (sngTemp + 0.01667) ^ 5.256
    Else
        If Meters <= 25098 Then
            Pressure = Exp(11.76 - 0.0001575 * Meters)
        Else
            Pressure = 9.903E+29 * sngTemp ^ -11.388
        End If
    End If
 
End Function
Private Function PressureE(ByVal Feet As Single) As Single
 
'   FUNCTIONAL DESCRIPTION:
'
'       Computes the air pressure in  pounds  per  square  foot  as  a
'       function of altitude in feet.
'
'   FORMAL PARAMETERS:
'
'       Height.rf.v - The height in feet for which the air presure  is
'           to be found.
'
'   IMPLICIT INPUTS:
'
'       None.
'
'   IMPLICIT OUTPUTS:
'
'       None.
'
'   RETURN VALUE:
'
'       The air pressure in pounds per square foot  at  the  specified
'       altitude.
'
'   SIDE EFFECTS:
'
'       None.
'
 
    Dim sngTemp As Single
 
    sngTemp = TempE(Feet)
    If Feet >= 82345 Then
        PressureE = 51.97 * ((sngTemp + 459.7) / 389.98) ^ -11.388
    Else
        If Feet >= 36152 Then
            PressureE = 473.1 * Exp(1.73 - 0.000048 * Feet)
        Else
            PressureE = 2116 * ((sngTemp + 459.7) / 518.6) ^ 5.256
        End If
    End If
 
End Function

Visual Basic for Applications has no built-in common logarithm function. However, it is easy enough to change the base of a logarithm: one simply divided by the logarithm of the new base. Using this, we see that:

log10 x = (log x)/(log 10)

We need this one-line subprogram for our unit test, which will graph the common logarithm of the pressure.

Private Function Log10(ByVal X As Single) As Single
 
'   FUNCTIONAL DESCRIPTION:
'
'       Computes the common logarithm of a number.
'
'   FORMAL PARAMETERS:
'
'       Number.rf.v - The number for which the common logarithm is  to
'           be found.
'
'   IMPLICIT INPUTS:
'
'       None.
'
'   IMPLICIT OUTPUTS:
'
'       None.
'
'   RETURN VALUE:
'
'       The common logarithm of the number.
'
'   SIDE EFFECTS:
'
'       None.
'
 
    Log10 = Log(X) / Log(10!)
 
End Function
Private Sub UTPressure()
 
'   FUNCTIONAL DESCRIPTION:
'
'       Unit test for the air pressure routine.
'
'       Graphs the common logarithm of air pressure 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 Feet As Single
    Dim PSI As Single
    Dim NewtonsPerMSqAsPSI As Single
 
    plt.ViewWindow 0, 100000, 10000, -1, 1.2, 1
    For Feet = 0 To 100000 Step 1000
        PSI = PressureE(Feet) / 144!
        NewtonsPerMSqAsPSI = 0.000145 * Pressure(0.3048 * Feet)
        If Abs(PSI - NewtonsPerMSqAsPSI) >= 0.01 * PSI Then
            MsgBox "Error!"
            Stop
        End If
        plt.Plot Feet, Log10(PSI)
        If Feet <> 0 Then
            plt.PlotLine
        End If
        DoEvents
    Next Feet
    WriteLn "Pressure unit test run."
 
End Sub

Running this unit test in Visual Basic for Applications gives us the same results as did the calculator.

[ Previous page | Top of page | Next page ]

Previous page Top of page Next page Navigation bar

Copyright © 2002 Brian Hetrick
Page last updated 10 February 2002.

Brian’s Casio Calculator Corner

Home

Programs

Tutorial

Preface

Introduction

Fundamentals

Building Blocks I

Introduction

Comments

Data Types

Numbers

Variables

Expressions

Control Flow I

Control Flow II

Subprograms

Basic I/O

Algorithms

A First Program

Examples

Introduction

Logical Ops

Bitwise Ops

Pumpkins

Problem

Temperature

Sound

Pressure

Density

Drag

Simulation

Exercises

Answers

Modularization

Data Structures I

Recursion

Program Attributes

Building Blocks II

Algorithm Analysis

Structuring

Data Structures II

Abstract Types

Objects

Problem Analysis

Reference Card

References

Puzzles

Site Information

Your Privacy

Site Map

E-mail

Site Technical Data