Wednesday, November 30, 2011

HP 15C Programming Tutorial - Part 11: Flags

Flags

A cool feature of HP programmable calculator is the use of flags. The HP 15C is no exception. Flags are "on-off" switches: the calculator does one calculation while the flag is switched on and another while the flag is switched off.

The HP 15C has 10 flags, labeled 0-9. The first eight flags, 0-7, are user flags. User flags have no predefined meeting. You define the user flag's settings in the program.

Flags 8 and 9 are system flags - they affect the operation of the calculator.

Flag 8:

Set: Complex Mode is on

Clear: Complex Mode is off

Flag 9:
Set: The display is flashing. Flag 9 is automatically set when an overflow is encountered. An overflow is encountered if an immediate calculation either exceeds 9.999999999 × 10^99 or falls below -9.999999999 × 10^99. When this flag is set, further operation cannot continue until: (1) the flag is cleared, (2) the calculator is turned off, or (3) the backspace button is pressed. Sometimes, you can intentionally set Flag 9 as way of communicating to the user.

Clear: The display is not flashing.

Flag Operations

The HP 15C has three flag operations:

SF N: This turns flag N on. Key sequence: [ g ] [ 4 ] (SF) N.

CF N: This turns flag N off. Key sequence: [ g ] [ 5 ] (CF) N.

F? N: This the Flag Set? test. If flag N is on, the next instruction is executed. Otherwise, the next instruction is skipped. Key sequence: [ g ] [ 6 ] (F?) N.

Part 11 will feature two programs involving flags.

Ideal Gas Law

This program has the user solve for either volume or pressure. Temperature can be entered in either Kelvins or Degrees Celsius.

The Ideal Gas Law:

P V = n R T

where:

P = pressure (in kPa - kiloPascals)
V = volume (in L - liters)
n = moles of the gas
R = the Ideal Gas Constant = 8.314 J • K^-1 • mol^-1
T = temperature (in K - Kelvins)

If the temperature is entered in Degrees Celsius then use the conversion:
T K = T ºC + 273.15

We will set up the following registers and flags as:

R1 = P
R2 = V
R3 = n
R4 = T
R5 = 8.314

Flag 0:
Set: Solve for Volume (R2)
Clear: Solve for Pressure (R1)

Flag 4:
Set: Enter temperature in Degrees Celsius (ºC)
Clear: Enter temperature in Kelvins (K)

Labels Used: D (Main), 8, 9

Program Listing:

Key Codes			Key
001 42 21 14 LBL D * Main Program
002 45 3 RCL 3
003 45 20 5 RCLx 5
004 45 4 RCL 4
005 43 6 4 F? 4 * Is Flag 4 Set?
006 32 9 GSB 9 * Convert ºC to K
007 20 ×
008 43 6 0 F? 0 * Is Flag 0 Set?
009 22 8 GTO 8
010 45 10 1 RCL÷ 1 * Solve for Pressure
011 44 2 STO 2
012 43 32 RTN
013 42 21 8 LBL 8 * Solve for Volume
014 45 10 2 RCL÷ 2
015 44 1 STO 1
016 43 32 RTN
017 42 21 9 LBL 9 * Convert subroutine
018 2 2
019 7 7
020 3 3
021 48 .
022 1 1
023 5 5
024 40 +
025 43 32 RTN


Instructions:

To Solve for Volume:
1. Store pressure in R1, moles of gas in R3, temperature in R4, and 8.314 in R5.
2. Clear Flag 0. ( [ g ] [ 5 ] (CF) [ 0 ] )
3. If the temperature is in Kelvins, clear Flag 4. If the temperature is in Degrees Celsius, set Flag 4.
4. Run Program D. ( [ f ] [y^x] (D) )

Example:
Find volume with the following data: P = 200 kPa, n = 0.5, and T = 200 K.

Key Strokes:
100 [STO] 1
.5 [STO] 3
200 [STO] 4
8.314 [STO] 5
[ g ] 5 (CF) 0 * Solve for Volume
[ g ] 5 (CF) 4 * Temperature is in Kelvins
[ f ] [y^x] (D)

Result: V ≈ 8.314 L

To Solve for Pressure:
1. Store volume in R2, moles of gas in R3, temperature in R4, and 8.314 in R5.
2. Set Flag 0. ( [ g ] [ 4 ] (SF) [ 0 ] )
3. If the temperature is in Kelvins, clear Flag 4. If the temperature is in Degrees Celsius, set Flag 4.
4. Run Program D. ( [ f ] [y^x] (D) )

Example:
Find pressure with the following data; V = 100 L, n = .25 mol, T = 35ºC.

Key Strokes:
100 [STO] 2
.25 [STO] 3
8.314 [STO] 5
35 [STO] 4
[ g ] 4 (SF) 0 * Solve for Pressure
[ g ] 4 (SF) 4 * Temperature is in Degrees Celsius
[ f ] [y^x] (D)

Result: P ≈ 6.4049 kPa

Extended Statistics Program

Remember the statistics program we did a few parts back (logarithmic regression)? We are going to expand on the program. This statistics program offers four regression models:

Linear (LIN): y = a + b x

Logarithmic (LOG): y = b + a ln x

Power (PWR): y = b × x^a

Exponential (EXP): y = b × e^(ax)

We can use the following transformations to allow us to use the linear regression functions of the HP 15C:


Model x y a b Flags
LIN x y a b
LOG ln x y a b Flag 1 is Set
PWR ln x ln y a e^b Flags 1 and 2 Set
EXP x ln y a e^b Flag 2 is Set

Where:
x = independent variable
y = dependent variable
a = slope
b = intercept


Labels Used:
Label A: Initialization
Label B: Enter Data
Label C: Analysis (b, a, r)

Caution: With this program, a new set of data must be entered for each calculation.

Program Listing:

Key Codes Key
001 42 21 11 LBL A * Initialization
002 42 32 CLR ∑
003 43 32 RTN
004 42 21 12 LBL B * Begin entry routine
005 43 6 1 F? 1 * Is Flag 1 Set?
006 43 12 LN * If yes, ln(x)
007 34 x<>y
008 43 6 2 F? 2 * Is Flag 2 Set?
009 43 12 LN * If yes, ln(y)
010 34 x<>y
011 49 ∑+ * Enters Data
012 43 32 RTN
013 42 21 13 LBL C * Analysis
014 42 49 L.R.
015 43 6 2 F? 2 * Is Flag 2 Set?
016 12 e^x
017 31 R/S * Display b
018 34 x<>y
019 31 R/S * Display a
020 1 1
021 42 48 y-hat, r
022 34 x<>y
023 43 32 RTN * Display r (end program)


Instructions:

1. Run Program A. ( [ f ] [ √ ] (A) )
2. Set and/or clear flags 1 and 2 to select the regression model.
3. Enter y data point, press [ENTER].
4. Enter x data point, press [ f ] [e^x] (B).
5. Repeat steps 2 and 3 as necessary.
6. Run Program C. ( [ f ] [10^x] (C) ).

Regression Models:
Linear: Clear Flag 1, Clear Flag 2
Logarithmic: Set Flag 1, Clear Flag 2
Power: Set Flag 1, Set Flag 2
Exponential: Clear Flag 1, Set flag 2

Example:

Fit the following data to the four regressions: linear, logarithmic, exponential, and power.
X		Y
40.5 104.5
38.6 102
37.9 100
36.2 97.5
35.1 95.5
34.6 94


Source: HP 33S Manual

A run through for Linear Regression (key strokes are similar for the others, just set and/or clear flags where necessary):
[ f ] [√ ] (A)
104.5 [ENTER] 40.5 [ f ] [e^x] (B)
102 [ENTER] 38.6 [ f ] [e^x] (B)
100 [ENTER] 37.9 [ f ] [e^x] (B)
97.5 [ENTER] 36.2 [ f ] [e^x] (B)
95.5 [ENTER] 35.1 [ f ] [e^x] (B)
94 [ENTER] 34.6 [ f ] [e^x] (B)
[ f ] [10^x] (C)

Results: r ≈ 0.9955, a ≈ 1.7601, b ≈ 33.5271

Here are results:
VAR	LIN		LOG		EXP		PWR
b 33.5271 65.8446 0.0177 0.6640
a 1.7601 -139.0088 51.1312 8.9730
r 0.9955 0.9965 0.9945 0.9959


This concludes Part 11. Next time we will work with indirect addressing.

Until then,

Eddie


This tutorial is property of Edward Shore. © 2011

No comments:

Post a Comment

HP Prime: Area by Quadratic Splines

HP Prime:  Area by Quadratic Splines Introduction The program QUADSUM calculates the area under the curve described by the set o...