Wednesday, November 30, 2011

HP 15C Programming Tutorial - Part 13: Indirect Addressing and Subroutines

Indexing and Subroutines

This is a continuation in a two-part series covering indirect addressing. While Part 12 dealt with memory registers, Part 13 will deal with subroutines.

To store a number in the index register, press [STO] [TAN] (I)

To call up subroutine label "I", press [GSB] [TAN] (I)

To branch to label "I", press [GTO] [TAN] (I).

When calling subroutines or branching to labels, only the integer portion of I is considered. For either one of these purposes, 0 ≤ I ≤ 24.

If RI contains... GTO I/GSB I calls label...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 .0
11 .1
12 .2
13 .3
14 .4
15 .5
16 .6
17 .7
18 .8
19 .9
20 A
21 B
22 C
23 D
24 E

We will present two programs that make use of calling subroutines indirectly.

Random Voting Machine

This program uses the calculator's random function to count a specified number of votes between two candidates: Candidate 0 and Candidate 1. In order to get the calculator to round the random number to 0 or 1, we use the setting Fix 0, then execute the RND (round) function.

RND rounds the displayed number, using the display settings for the number of decimal points used.

Labels used: A (main), 0, 1, 2 (loop)

Storage Registers Used:
R2 = total number of votes

Program Listing:

Key Code Key
001 42 21 11 LBL A
002 44 2 STO 2
003 0 0 * Clear R0 and R1
004 44 0 STO 0
005 44 1 STO 1
006 42 7 0 FIX 0 * Fix decimal settings
007 42 21 2 LBL 2
008 42 36 RAN #
009 43 34 RND
010 44 25 STO I * Store result in the
index register
011 32 25 GSB I
012 42 5 2 DSE 2
013 22 2 GTO 2
014 45 0 RCL 0
015 31 R/S
016 45 1 RCL 1
017 43 32 RTN
018 42 21 0 LBL 0
019 1 1
020 44 40 0 STO+ 0
021 43 32 RTN
022 42 21 1 LBL 1
023 1 1
024 44 40 1 STO+ 1
025 43 32 RTN

Instructions:
1. Enter the number of votes to be counted.
2. Press [ f ] [ √ ] (A)
3. The number of votes for Candidate 0 is displayed. Press [R/S] to display the number of votes for Candidate 1.

Example:
A sample vote of 25 votes: 25 [ f ] [ √ ] (A)

You may get 13 votes for Candidate 0 and 12 votes for Candidate 1.

A sample vote of 2,500 votes yields 1,252 votes for Candidate 0 and 1,248 votes for Candidate 1. It took about 17 seconds for the program to run on the HP 15C Limited Edition. It may take considerably longer on the original HP 15C.

Fresnel Integrals

This program calculates the Fresnel Integrals:

Fresnel Sine Integral: S(x) = √(2/π) × ∫((sin u)^2, u, 0, x)

Fresnel Cosine Integral: C(x) = √(2/π) × ∫((cos u)^2, u, 0, x)

This program sets an index. If RI = 0, S(x) is calculated. If RI = 1, C(x) calculated. If RI > 1, the program flashes to communicate an error condition. RI is set before program execution.

Program Listing:

Key Codes Key
001 42 21 11 LBL A
002 45 25 RCL I
003 1 1
004 43 30 8 TEST 8 ( x < y )
005 22 3 GTO 3
007 33 R ↓
008 33 R ↓
009 0 0
010 34 x<>y
011 42 20 2 ∫ 2 * Integer - label 2
012 2 2
013 43 26 π
014 10 ÷
015 11 √
016 20 ×
017 43 32 RTN
018 42 21 2 LBL 2
019 32 25 GSB I
020 43 11 x^2
021 43 32 RTN
022 42 21 0 LBL 0
023 23 SIN
024 43 32 RTN
025 42 21 1 LBL 1
026 24 COS
027 43 32 RTN
028 42 21 3 LBL 3
029 43 4 9 SF 9
030 43 32 RTN

Caution: Because integration is involved, an additional 23 registers is required. The particular program takes up 28 registers (integration, program instructions)

Instructions:
1. To calculate the Fresnel Sine Integral, set register I to 0. To calculate the Fresnel Cosine Integral, set register I to 1.
2. Enter x.
3. Run program A. [ f ] [ √ ] (A).

Examples:

1. S(1.5) [Fresnel Sine Integral]

Key Strokes:
0 [STO] [TAN] ( I )
1.5 [ f ] [ √ ] (A)

Result: S(1.5) ≈ 0.5703

2. C(1.5) [Fresnel Cosine Integral]

Key Strokes:
1 [STO] [TAN] ( I )
1.5 [ f ] [ √ ] (A)

Result: C(1.5) ≈ 0.6266

3. Try to run program A with I = 2.

Key Strokes:
2 [STO] [TAN] ( I )
1.5 [ f ] [ √ ] (A)

The display flashes. Stop the flash by pressing the backspace key or clearing Flag 9.

This concludes Part 13. Until next time, happy programming,

Eddie

This tutorial is property of Edward Shore. © 2011

HP 15C Programming Tutorial - Part 12: Memory and Indirect Registers

The Indirect Register I

Another powerful feature of the HP 15C is indirect addressing. The registers I and (i) can be used not only for loop control (for DSE/ISG) but it can also be used to call different memory registers, call different subroutines, and control flag operations.

We will work with the registers I and (i) in Parts 12 and 13. Part 12 will cover how to use the index registers with data recall and Part 13 will cover how to use the index registers for calling different subroutines.

Index Register and Memory

The HP 15C Manual names the memory register I (RI) the as the Index Register. You can store and recall values in memory register I just like any other memory register. Here is where it gets good: you can use register I to store and recall any other memory register with the integer part of I as the designated register. This is where the variable (i) comes in.

Let I = N where 0 ≤ N ≤ 19* (see Caution below)

STO (i) stores the contents in the display into register N.
RCL (i) recalls the contents of register N.

Caution: The value of N can be anywhere from 0 to 65. The upper limit of N is determined by how the memory registers are partitioned. You can check the number of registers have been partitioned by pressing [ g ] [RCL] (MEM). It is the left-most number. By default the HP 15C partitions 19 as the highest memory register number allowed. For the purposes of this tutorial we will use the default settings.

Example:

Store 2 in RI. Then store π/4 in R2 indirectly (by use of RCL (i)).

Key Strokes:
2 [STO] [TAN] ( I )
[ g ] [EEX] ( π ) 4 [ ÷ ] [STO] [COS] ( (i) )
[backspace] * To clear the display
[RCL] 2 * Display: 0.7854 ≈ π/4

If RI contains... STO (i) and RCL (i) will address
0 R0
1 R1
2 R2
3 R3
4 R4
5 R5
6 R6
7 R7
8 R8
9 R9
10 R.0
11 R.1
12 R.2
13 R.3
14 R.4
15 R.5
16 R.6
17 R.7
18 R.8
19 R.9
20 to 65** R20 to R65**

On final note, storage and recall arithmetic (+, -, ×, ÷) work with RI. Now let's use RI in a couple of programs.

Memory Cycle

In this program, the user cycles through the contents of R0 through R9. One possible solution is to just line up a batch of RCL N, R/S commands. However, with indirect addressing we can considerably shorten the program by use of registers I and (i).

This program is going to use a loop that increases the counter from 0 to 9. The starting value is 0, the ending value is 9. Let I be the counter variable and I = 0.009.

Hint: STO (i) and RCL (i) uses the integer value of I as the index. (any decimal portion of I is ignored)

Labels used: A (main), 0

Program Listing

Key Listing Keys
001 42 21 11 LBL A
002 48 .
003 0 0
004 0 0
005 9 9
006 44 25 STO I * Stores the Index register
007 42 21 0 LBL 0 * Loop starts here
008 45 24 RCL (i) * Indirect recall
009 31 R/S * Stop and display
010 33 R ↓ * Send value to T-register
011 42 6 25 ISG I
012 22 0 GTO 0
013 43 4 9 SF 9 * Flash the display
014 43 32 RTN

Instructions:

1. Run Program A ( [ f ] [ √ ] (A) )
2. The program starts with R0. Press [R/S] to see the contents of R1. This cycles to R9.
3. When R9 is recalled, the stack is reset and the display flashes. Clear Flag 9 or press the backspace button.

Example:

Try this program with the following registers set as:
R0 = 0
R1 = 25
R2 = 15
R3 = 10
R4 = 50
R5 = 5
R6 = 45
R7 = 30
R8 = 40
R9 = 35

Wheel of Numbers

This program will "spin" the wheel using memory registers R0 through R9. Press R/S to "stop" the wheel.

Labels used: B (main), 1

Program Listing

Key Codes Keys
001 42 21 12 LBL B
002 48 .
003 0 0
004 0 0
005 9 9
006 44 25 STO I
007 42 21 1 LBL 1
008 45 24 RCL (i)
009 42 6 25 ISG I
010 22 1 GTO 1
011 1 1 * If I = 10, subtract 1
012 0 0 * and start over.
013 44 30 25 STO- I
014 22 1 GTO 1

This program produces an endless loop, so when you do run it, be sure to press [R/S] or turn the calculator off.

Instructions:
1. Load the wheel values on registers R0 and R9.
2. Run Program B. ( [ f ] [e^x] (B) ) The calculator will display "running".
3. When you feel lucky, press [R/S]. The value of the wheel's space will be displayed.

Example:

Set the wheel with the following values:

R0 = 0
R1 = 25
R2 = 15
R3 = 10
R4 = 50
R5 = 5
R6 = 45
R7 = 30
R8 = 40
R9 = 35

Run Program B a few times. Did you stop on the 50 space? Good luck!

Hint: With modifications, the above program can simulate a very famous wheel on a very famous game show.

That concludes Part 12. As I previously mentioned, we will continue working with indirect registers in Part 13. Signing off,

Eddie

This tutorial is property of Edward Shore. © 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

Monday, November 21, 2011

HP 15C Programming Tutorial - Part 10: Statistics in Programming

Statistics on the HP 15C

The HP 15C performs one and two variable statistics. In statistical calculation, data is entered into Y and X registers. When the key [ ∑+ ] is pressed the following variables are updated:

R2 = n = number of data points
R3 = ∑x = sum of x data
R4 = ∑x^2 = sum of x-squared data
R5 = ∑y = sum of y data
R6 = ∑y^2 = sum of y-squared data
R7 = ∑xy = sum of the products of x-data and y-data

You can recall these values.

Arithmetic Mean: [ g ] [ 0 ]. The arithmetic mean of x-data is displayed. Press [x<>y] to display the arithmetic mean of y-data is displayed.

Standard Deviation: [ g ] [ . ]. The standard deviation of x-data is displayed. Press [x<>y] to display the standard deviation of y-data is displayed.

Clearing the Statistics Registers

[ f ] [GSB] (CLEAR ∑) clears memory registers R2, R3, R4, R5, R6, and R7 to 0.

Linear Regression

The HP 15C includes linear regression functions to fit data to the line:

y = a x + b

where a = slope and b = y-intercept with r = correlation coefficient. If r is near 1 or -1, the data has a good fit to the line. If r is near 0, the data does not have a good fit to a line.

To find a, b, and r:

b: [ f ] [∑+] (L.R.)
a: [ f ] [∑+] (L.R.) [x<>y]
r: a valid number [ f ] [ . ] (y-hat, r) [x<>y]

Entering Data

One Variable Data:
1. Enter x
2. Press [∑+]

Two Variable Data:
1. Enter y
2. Press [ENTER]
3. Enter x
4. Press [∑+]

The key sequence [ g ] [∑+] (∑-) can be used to remove data.

A Example of Linear Regression

Find a linear fit to the data:

x	y
1 3
2 6
4 11
8 15
16 36

Caution: In two variable data, enter the y data first.

Key press:

[ f ] [GSB] (CLEAR ∑)
3 [ENTER] 1 [∑+]
6 [ENTER] 2 [∑+]
11 [ENTER] 4 [∑+]
15 [ENTER] 8 [∑+]
36 [ENTER] 16 [∑+]

First the slope and y-intercept:

[ f ] [∑+] (L.R.) (** 1.0833 is displayed)
[x<>y] (** 2.1156 is displayed)
1 [ f ] [ . ] (y-hat, r) [x<>y] (** 0.9905 is displayed)

Results: The linear fit is approximately is y ≈ 2.1156 + 1.0833x with r ≈ 0.9905 (a very good fit!)

In the next program, we will use the statistical features to fit data to a logarithmic fit.

Logarithmic Regression

This program fits the data to the equation:

y = b + a ln x

Note that this is similar to the linear equation y = b + a x, except that we are working with "ln x" instead of "x".

This program adjusts the x-data to ln(x-data).

Labels used:
Label A: Initialize data; clear registers R2 - R7
Label B: Adjusts the data and enters the data
Label C: Calculates a, b, and r.

Program Listing

Key Codes		Key
001 42 21 22 LBL A * Initialization
002 42 32 CLR ∑
003 43 32 RTN
004 42 21 12 LBL B * Data Entry
005 43 12 LN
006 49 ∑+
007 43 32 RTN
008 42 21 13 LBL C * Calculate b, a, r
009 42 49 L.R.
010 31 R/S * Display b
011 34 x<>y
012 31 R/S * Display a
013 1 1
014 42 48 y-hat, r
015 34 x<>y * Display r
016 43 32 RTN

Example

Data is presented regarding the growth of a human male:

X = Age (in years)	Y = Height (in feet)
1 0.88
2 1.44
3 2.06
4 2.18
5 2.45
10 5.02
15 5.76
20 6.09

Use the program to find a logarithmic fit.

Key Presses:

[ f ] [ √ ] (A)
0.88 [ENTER] 1 [ f ] [e^x] (B)
1.44 [ENTER] 2 [ f ] [e^x] (B)
2.06 [ENTER] 3 [ f ] [e^x] (B)
2.18 [ENTER] 4 [ f ] [e^x] (B)
2.45 [ENTER] 5 [ f ] [e^x] (B)
5.02 [ENTER] 10 [ f ] [e^x] (B)
5.76 [ENTER] 15 [ f ] [e^x] (B)
6.09 [ENTER] 20 [ f ] [e^x] (B)
[ f ] [10^x] (C) (* Display: 0.1363)
[R/S] (* Display: 1.9376)
[R/S] (* Display: 0.9656)

Results:

y ≈ 0.1363 + 1.9376 ln x with r ≈ 0.9656 (very good fit!)

Regressions

The following table shows various fits that can be used using L.R. and y-hat,r functions. Use the necessary adjustments to get the correct data.

For example, for logarithmic fit, take the natural logarithm of x before pressing [∑+]. For exponential fit, take the natural logarithm of y before pressing [∑+]; when pressing [L.R.], take the exponential of both a and b to get the true slope and y-intercept.

Fit		Equation	x	y		a	b
Linear y = ax+b x y a b
Logarithmic y = b + a ln x y a b
Power y = b * x^a ln x ln y a b
Exponential y = b * a^x x ln y e^a e^b
Inverse y = b + a/x 1/x y a b
Logistic y = L/(1+ae^(bx)) x ln(L/y - 1) e^a b
L > 2y_max
Sine y = b + a sin(px) sin(px) y a b
p = (x_max - x_min)/(2π)

Hope you find this table useful.

Until then, happy programming,
Eddie

This tutorial is property of Edward Shore. © 2011

HP 15C Programming Tutorial - Part 9: Derivatives

Numerical Derivatives

In Part 9, we will calculate numerical derivatives of f(x). Computing accurate numerical derivatives can present a challenge. Often, calculation involves a small increment, usually named h. Generally, the smaller h is, the better the calculation. However with certain methods, if h is too small, the final result may be unexpected.

This program uses a five-point formula:

f'(x) ≈ (f(x - 2h) - 8 * f(x - h) + 8 * f(x + h) - f(x + 2h)) / (12h)

The error is of the order of h^4.

Source: Burden, Richard L. and J. Douglas Faires. "Numerical Analysis 8th Edition" Thomson Brooks/Cole Belton, CA 2005

Labels Used:
Label B: Main routine
Label 0: The function f(R1). The function starts with R1 loaded on the X register.

Memory Registers Used:
R0 = the numerical derivative
R1 = X
R2 = h

Program Listing

Key Codes		Key
001 42 21 12 LBL B
002 44 2 STO 2 * stores h
003 33 R ↓
004 44 1 STO 1 * stores X
005 2 2 * first term calculation
006 45 20 2 RCL × 2
007 30 -
008 32 0 GSB 0 * f(x - 2h)
009 44 0 STO 0
010 45 1 RCL 1
011 45 30 2 RCL- 2
012 32 0 GSB 0
013 8 8
014 16 CHS
015 20 × * -8 * f(x - h)
016 44 40 0 STO+ 0
017 45 1 RCL 1
018 45 40 2 RCL+ 2
019 32 0 GSB 0
020 8 8
021 20 × * 8 * (f + h)
022 44 40 0 STO+ 0
023 45 1 RCL 1
024 2 2
025 45 20 2 RCL × 2
026 40 +
027 32 0 GSB 0
028 16 CHS * -f(x + h)
029 44 40 0 STO+ 0
030 45 0 RCL 0
031 45 10 2 RCL÷ 2
032 1 1
033 2 2
034 10 ÷
035 44 0 STO 0
036 43 32 RTN

Instructions:
1. Enter the main program (Label B).
2. Enter the function f(R1) (Label 0).
3. Enter X, press [ENTER].
4. Enter h, press [ f ] [e^x] (B).
5. The approximate numerical derivative is displayed.

Caution: Remember to end the function with the RTN command ([ g ] [GSB] (RTN)).

Example 1

Let f(x) = x*e^x

Estimate f'(2) with h = 0.0001

Key Code		Key
001 42 21 0 LBL 0
002 36 ENTER
003 12 e^x
004 20 ×
005 43 32 RTN

To find the derivative:

Press 2 [ENTER] .0001 [ f ] [e^x] (B)

Result: f(2) ≈ 22.1672

Example 2

Let f(x) = -x^2 + 2x + 3

Estimate f'(1.5) with h = 0.0001

We can rewrite f(x) as:

f(x) = -x^2 + 2x + 3

-1 * (x^2 - 2x - 3)

-1 * (x * (x - 2) - 3)

Use the last form as the function.

Key Code		Key
001 42 21 0 LBL 0
002 36 ENTER
003 36 ENTER
004 2 2
005 30 -
006 20 ×
007 3 3
008 30 -
009 16 CHS
010 43 32 RTN

To find the derivative:

1.5 [ENTER] .0001 [ f ] [e^x] (B)

Result: f'(1.5) ≈ -1

Next time, we will look at how we can use statistics in programming. Until next time, happy programming,

Eddie

This tutorial is property of Edward Shore. © 2011

HP 15C Programming Tutorial - Part 8: The Summation Program

Sum

This program computes the sum:

R0 = ∑( X = R1, R2, f(R1))

where:
R0 = the sum
R1 = starting value, which gets updated at each step. R1 is also used as the variable for f(X)
R2 = finishing value

The program uses three labels:

Label A: The main routine. Press [ f ] [ √ ] (A) to run the routine.
Label 1: A subroutine of Label A. This is the loop.
Label 0: Where the function f(X) is stored. Assume that R1 is loaded on the X register when programming the function.

The program presented here will allow the user to enter starting and ending values beyond 999. You can shorten the program by the use of ISG, however, the ending value would be restricted to 999.

Program Listing

Key Code			Keys
001 42 21 11 LBL A
002 0 0
003 44 0 STO 0 * Initializes R0
004 33 R ↓
005 44 2 STO 2 * ending value
006 33 R ↓
007 44 1 STO 1 * starting value
008 42 21 1 LBL 1 * loop starts here
009 45 1 RCL 1
010 32 0 GSB 0
011 44 40 0 STO+ 0 * update total
012 1 1
013 44 40 1 STO+ 1 * update starting value
014 45 2 RCL 2
015 45 1 RCL 1
016 43 10 x≤y * is R1 ≤ R2?
017 22 1 GTO 1
018 45 0 RCL 0
019 43 32 RTN

Instructions:
1. Enter the main program (Labels A and 1).
2. Enter the function f(R1) (Label 0).
3. In run mode, enter the starting value and press [ENTER]
4. Enter the ending value and press [ f ] [ √ ] (A)
5. The sum is calculated and displayed.

Remember: Always finish the function with the RTN command ([ g ] [GSB] (RTN)).

Example 1

Find the sum ∑(X = 1, 50, X). In other words, what is the sum of the integers from 1 to 50?

Program listing for f(R1):

Key Code		Key
001 42 21 0 LBL 0
002 43 32 RTN

Surprised? Remember we already have a copy of R1 in the main program when the instruction GSB 0 is encountered.

1 [ENTER] 50 [ f ] [ √ ] (A)

Result: 1275

Example 2

Find the sum ∑(X = 1, 150, 1/(X^2 + X)).

1/(X^2 + X) can be rewritten as:

(X^2 + X)^-1

((X + 1) X)^-1

We will use the last form for our function.

A Way to Clear Label 0

Clear Label 0 is necessary. In Run mode, press [GTO] [ 0 ]. Then press [ g ] [R/S] (P/R) to enter program mode. Press [SST] until the key codes "43 32" is encountered. Press the backspace button the number of times you pressed [SST]. You should see the key code "42, 21, 0". You are ready to enter the new function.

Program Listing for f(R1):
Key Code		Key
001 42 21 0 LBL 0
002 36 ENTER
003 1 1
004 40 +
005 20 ×
006 15 1/x
007 43 32 RTN

To finish:

1 [ENTER] 150 [ f ] [ √ ] (A)

Result: ≈ 0.9934

That concludes Part 8 of our tutorial. Until next time,

Eddie

This tutorial is property of Edward Shore. © 2011

Wednesday, November 16, 2011

HP 15C Programming Tutorial - Part 7: Solve and Integration

Solving Equations with the HP 15C

The HP 15C has a powerful solve feature that finds the real roots of the equation f(x)=0 given an interval.

Caution: When using the solve feature on an original HP 15C calculator - you will want to allow additional time since the calculator can be slow in the solve process.

Procedure:
1. Program an equation and label it.
2. In Run mode, enter the lower limit of the interval and press [ENTER].
3. Enter the upper limit of the interval, and press [ f ] [ ÷ ] (SOLVE) Label. Label can be 0-9, .0-.9, or A-E.

We will use two examples to illustrate the power of the SOLVE function.

Example 1

Find the root for the equation f(x) = x ln x - 3. We claim that the root is somewhere in the interval [1, 6].

First enter the equation. For this equation, use Label 0. Assume that x is in the display.

Key Codes Key
001 42 21 0 LBL 0
002 36 ENTER * duplicates x
003 43 12 LN * X: ln(x), Y: x
004 20 × * x ln (x)
005 3 3
006 30 - * x ln (x) - 3
007 43 32 RTN

1. Press [ g ] [R/S] (P/R) to go to Run Mode.
2. Press [ 1 ] [ENTER] [ 6 ] [ f ] [ ÷ ] (SOLVE) [ 0 ].

Result: x ≈ 2.8574.

Example 2

Find the two roots to the equation g(x) = x^2 + 4x - 3. Using Horner's Method, we can rewrite the equation as g(x) = (x + 4)x - 3.

We will use Label 1 for g(x).

Key Codes Key
001 42 21 1 LBL 1
002 36 ENTER * duplicates x
003 4 4
004 40 +
005 20 × * (x + 4)x
006 3 3
007 30 - * (x + 4)x - 3
008 43 32 RTN

Let's try to see if we can find a positive root. How about an interval of [0, 3]?

0 [ENTER] 3 [ f ] [ ÷ ] (SOLVE) 1

Result: x ≈ 0.6458.

Are there any roots bigger than 0.6458? Try an interval of [3, 100].

3 [ENTER] 100 [ f ] [ ÷ ] (SOLVE) 1

Result: x ≈ 0.6458 (no different)

How about any roots below 0? Try an interval [-100, 0].

100 [CHS] [ENTER] 0 [ f ] [ ÷ ] (SOLVE) 1

Result: x ≈ -4.6458

So our two roots are x ≈ {-4.6458, 0.6458}.

Integral

The HP 15C also has a built in integral function ( ∫ ). This is useful for finding definite integrals for functions, even when the function is not defined at the end points.

Caution: Just like the Solve function - allow extra time with the Integral function when using the original HP 15C calculators. In addition, using the Integral function requires 23 memory registers to operate. It may be a good idea to clear the program memory prior to entering functions for integration.

Procedure:
1. Enter a function in a program. The program must have a label.
2. Enter the lower limit of integration and press [ENTER].
3. Enter the upper limit of integration and press [ f ] [ × ] ( ∫ ).

We will use two examples to illustrate the Integration feature.

Example 1

Calculate ∫ ( e ^ (-x^2), x, 0, 2). Use Label 2.

Key Code Keys
001 42 21 2 LBL 2
002 43 11 x^2
003 16 CHS
004 12 e^x * e^(-x^2)
005 43 32 RTN

Press [ g ] [R/S] (P/R) to switch to Run Mode. Then press [ 0 ] [ENTER] [ 2 ] [ f ] [ × ] ( ∫ ) [ 2 ].

Result: Integral ≈ 0.8821

Example 2

Calculate ∫ ( |sin x|, x, 0, 2π). Use Label 3.

Key Code Keys
001 42 21 3 LBL 3
003 23 SIN
004 43 16 ABS * |sin x|
005 43 32 RTN

In Run mode:

0 [ENTER] [ g ] [EEX] ( π ) 2 [ × ] [ f ] [ × ] ( ∫ ) 3

Result: Integral = 4

This concludes Part 7 of the tutorial. In the next part we will use the knowledge of programming functions and equations to calculate derivatives and sums.

This tutorial is property of Edward Shore. © 2011

HP 15C Programming Tutorial - Part 6: Loops and Counters

Loops

The HP 15C offers two programming instructions for loop control:

1. ISG - Increment and Skip if Greater than
2. DSE - Decrement and Skip if Less than or Equal to

Both instructions use a counter variable for loop control. Let's take a look at ISG and DSE
one at a time.

ISG

ISG uses a counter variable and performs a test:

If the current value is greater than the final value, then the next instruction is skipped.

Loops are created with both the use of ISG and GTO.

The counter variable for ISG is in the form of SSSSS.EEEII where:

SSSSS is the starting value. When ISG is encountered, the starting value is increased by II.

EEE is the ending value. The ending value can be anywhere from 000 to 999. Leading zeros MUST be included. For example, if you want the ending value to be 25, EEE must be 025.

II is the increment value. The increment can be anywhere from 00 to 99, and like the ending value, leading zeros MUST be included.

Hint: If an increment value is not included, the HP 15C assumes an increment value of 1. For example, a counter value of 1.015 increments the counter variable from 1 to 15 in steps of 1.

The counter variable is stored before-hand in a memory register. You can either do this in the program or you can do this outside the program.

Key stroke sequence:
[ f ] [ 6 ] (ISG) register number

Let's illustrate ISG in a program.

The Future Value of Monies Deposited Today

A family buys a \$1,000 bond that pays back 5% annual interest. The bond matures (terminates) in five years. What is the value of the bond as each year progresses?

The value of the bond can be found by the formula:

FV = PV ( 1 + I/100 ) ^ N where

PV (Present Value) = Value of the bond today
I (Periodic Interest Rate) = Interest Rate
N (Number of Periods) = Number of Years
FV (Future Value) = Value of the bond N periods from now

Store the following values in these memory registers:
R0 = PV = 1000
R1 = 1 + I/100 = 1.05
R2 = Counter Variable = 1.005

1.005 means, start at 1 and end when 5 is reached. Since the increment is not specified, the HP 15C assumes the increment is 1.

Program Listing:

Key Code Keys
001 42 21 11 LBL A
002 42 7 2 FIX 2 * - Display Setting
003 45 1 RCL 1
004 45 2 RCL 2 * - Counter Variable
005 43 44 INT * - integer of R2
006 14 y^x
007 45 20 0 RCL × 0
008 31 R/S
009 42 6 2 ISG 2 * - increase int(2) by 1
010 22 11 GTO A * - if int(R2) ≤ end value
011 43 32 RTN * - skip if int(R2) > end value

Running the program ([ f ] [ √ ] (A)) and pressing [R/S] four times yields:

Bond Value:
After 1 year, \$1,050.00
After 2 years, \$1,102.50
After 3 years, \$1,157.63
After 4 years, \$1,215.51
After 5 years, \$1,276.28

The family should expected to be paid \$1,276.28 at the maturity of the bond.

DSE

DSE uses a counter variable and performs a test:

If the current value is less than or equal to the final value, then the next instruction is skipped.

Loops are created with both the use of DSE and GTO.

The counter variable for DSE is in the form of SSSSS.EEEII where:

SSSSS is the starting value. When DSE is encountered, the starting value is decreased by II.

EEE is the ending value. The ending value can be anywhere from 000 to 999. Leading zeros MUST be included. For example, if you want the ending value to be 25, EEE must be 025.

II is the decrement value. The decrement can be anywhere from 00 to 99, and like the ending value, leading zeros MUST be included.

Hint: If an decrement value is not included, the HP 15C assumes an decrement value of 1. For example, a counter value of 15.001 decrements the counter variable from 15 to 2 in steps of 1. Remember DSE tests whether the counter value is less than or equal to the final value.

The counter variable is stored before-hand in a memory register. You can either do this in the program or you can do this outside the program.

Key stroke sequence:
[ f ] [ 5 ] (DSE) register number

Let's illustrate DSE in a program.

Countdown

Make a program that has the calculator count down from n to 1.

Let R0 be the counter. We want to the loop to stop at 1, so set the final value to be 0.
We will use Label B for this program.

Program Listing:

Key Codes Key
001 42 21 12 LBL B
002 42 7 0 FIX 0 * - set decimal setting
003 43 44 INT
004 44 0 STO 0
005 42 21 0 LBL 0 * - loop starts here
006 45 0 RCL 0
007 31 R/S
008 42 5 0 DSE 0
009 22 0 GTO 0
010 43 32 RTN

Instructions:

1. Enter the number on the display.
2. Press [ f ] [e^x] (B).
3. Press [R/S] until the display is counted to 1.

Next time we will learn how to solve equations and integrate functions. Happy programming,

Eddie

This tutorial is property of Edward Shore. © 2011

HP 15C Programming Tutorial - Part 5: Subroutines

Subroutines

A subroutine is a set a of repeated instructions that is called within a main program. The subroutine is keyed in only once which saves memory. There is a return command at the end of each subroutine which allows the calculator to return to the point where the subroutine is called.

For example, let's program the path as someone enters a house after getting home from work. Typically, the person has to open doors many times. Let this be the subroutine. A program diagram would look like this:

Person Enters House

Subroutine: Open Door

Enter Living Room

Go to the Bedroom

Subroutine: Open Door

Enter Bedroom

Go to Closet

Subroutine: Open Door

Put down briefcase, purse, and/or backpack

Relax - END

Subroutine: Open Door

Is the door locked?
Yes: Use appropriate key to unlock it
No: Go onto the next step

Use hand to turn the knob

Push the door

Walk in to the next room

RETURN

Subroutines on the HP 15C

In Programming Mode, press the [GSB] key (3rd row, 2nd key) followed by the label (0-9, .0-.9, A-E). According to the HP 15C manual, GSB stands for go to subroutine.

Subroutines can call other subroutines, subroutines of subroutines are called nested subroutines. On the HP 15C, subroutines can be nested seven levels deep.

Part 5 contains two programs to illustrate the use of subroutines. In the second program presented, we will make use of a subroutine help to program the Quadratic Formula.

Using a Subroutine

Let f(x) = cos^-1 (sin 2x/π) * sin^-1 (cos (2x/π)^2) * (2x/π)^3 ; where x is in radians. Calculate f(1), f(2), and f(3).

Note that the expression 2x/π repeats. Let's use this as a subroutine.

In this example, we will label the main program C and use Label 2 as the subroutine.

Program Listing:

Key Code Key
001 42 21 13 LBL C
003 44 0 STO 0
004 32 2 GSB 2
005 23 SIN
006 43 24 COS^-1
007 45 0 RCL 0
008 32 2 GSB 2
009 43 11 x^2
010 24 COS
011 43 23 SIN^-1
012 20 ×
013 45 0 RCL 0
014 32 2 GSB 2
015 3 3
016 14 y^x
017 20 ×
018 45 0 RCL 0
019 32 2 GSB 2
020 10 ÷
021 43 32 RTN

022 42 21 2 LBL 2 *subroutine starts
023 2 2
024 20 ×
025 43 26 π
026 10 ÷
027 43 32 RTN

Instructions

Enter x. Press [ f ] [10^x] (C).

Examples

f(1) ≈ 0.4413
f(2) ≈ -0.0243
f(3) ≈ -1.3169

This program find the roots the quadratic equation:

ax^2 + bx + c = 0

Let's assign memory registers R1, R2, and R3 as follows:

R1 = a
R2 = b
R3 = c

Values for R1, R2, and R3 are stored before program execution.

The other memory registers used in this program are R0, R4, and R5, where:

R0 = discriminant = b^2 - 4ac

If R0 < 0, then
R4 = the real part of the complex roots x ± yi
R5 = the imaginary part of the complex roots x ± yi

If R0 ≥ 0 then
R4 = one of the real roots
R5 = one of the real roots

Program Instructions

Key Code Key
001 42 21 11 LBL A
002 45 2 RCL 2
003 43 11 x^2
004 4 4
005 45 20 1 RCL × 1
006 45 20 3 RCL × 3
007 30 -
008 44 0 STO 0
009 31 R/S
010 43 30 2 x<0? * TEST 2
011 22 0 GTO 0 * R0 < 0 - complex roots
012 4 4 * real roots
013 10 ÷
014 45 1 RCL 1
015 43 11 x^2
016 10 ÷
017 11 √
018 32 1 GSB 1
019 34 x<>y
020 40 +
021 44 4 STO 4
022 31 R/S
023 43 36 LSTx
024 2 2
025 20 ×
026 30 -
027 44 5 STO 5
028 43 32 RTN

029 42 21 0 LBL 0 * complex roots
030 32 1 GSB 1
031 44 4 STO 4
032 31 R/S
033 45 0 RCL 0
034 43 16 ABS
035 11 √
036 2 2
037 10 ÷
038 45 10 1 RCL÷ 1
039 44 5 STO 5
040 43 32 RTN

041 42 21 1 LBL 1 * subroutine starts here
042 45 2 RCL 2
043 16 CHS
044 2 2
045 10 ÷
046 45 10 1 RCL÷ 1
047 43 32 RTN

ax^2 + bx + c = 0

Instructions:
1. Store a in R0, b in R1, and c in R2.
2. Press [ f ] [ √ ] (A).
3. The discriminant is displayed (R0).
4. Press [R/S] displays R4.
5. Press [R/S] displays R5.

If R0 ≥ 0, R4 and R5 are the real roots of the quadratic equation.

If R0<0, R4 is the real part of the complex root, and R5 is ±imaginary part of the complex root.

Examples:

1. x^2 + 4x + 6
R1 = 1, R2 = 4, R3 = 6
Result: R0 = -8, R4 = -2, R5 ≈ 1.4142
The roots are -2 ± √2 i

2. x^2 - 5x + 3
R1 = 1, R2 = -5, R3 = 3
Result: R0 = 13, R5 ≈ 0.6972, R4 ≈ 4.3028
The roots are approximately 0.6972 and 4.3028.

I hope found Part 5 helpful, and I'll see you for Part 6! Eddie

This tutorial is property of Edward Shore. © 2011

Tuesday, November 8, 2011

HP 15C Programming Tutorial - Part 4: Tests

Part 4 of the HP 15C tutorial is going to cover tests and branching. Mastering this is vital to good keystroke programming.

The GTO key

The GTO (Go To) key transfers program control to any label. The label can be located either earlier or later in the set of programming instructions.

The following diagram is example of how GTO works. The instruction in line 001 says "GTO A". Program execution jumps to where "LBL A" starts, which is in line 005.
001		GTO A	("Look for Label A")
002
003
004
005		LBL A	("Here is Label A - continue from here)

Comparison Tests Against Zero

The HP 15C has six comparison tests that compare the contents in Register X (the display) against 0. If the condition is true, the next command is executed. If it is false, the next command is skipped.

In practice, most comparison tests are followed by one or two GTO statements. One label is for instructions when the condition is true, and the other label is for instructions when the condition is false.

The following diagram is an example of how comparison tests work. Let's say the conditional test x=0 is the instruction on Line 001. If x=0, Line 002 is executed. If not, Line 002 is skipped and Line 003 is the next instruction executed.

001		x = 0		(Comparison test)
002		GTO A		(If x = 0, go to label A)
.
.
.
011		LBL A		(continue here from Line 002)
.
.
.

The six comparison tests against zero are:

Test		Key Stroke Sequence
x = 0		[ g ] [ × ] ( x = 0 )
x ≠ 0		[ g ] [ - ] (TEST) [ 0 ]
x > 0		[ g ] [ - ] (TEST) [ 1 ]
x < 0		[ g ] [ - ] (TEST) [ 2 ]
x ≥ 0		[ g ] [ - ] (TEST) [ 3 ]
x ≤ 0		[ g ] [ - ] (TEST) [ 4 ]

Comparison Tests Involving X and Y

The HP 15C has six comparison tests involving the contents of registers X and Y, which work in the same way as the comparison tests against zero. If the condition is true, the next command is executed, if not the next command is skipped.

Here is an example diagram:
001		x ≤ y		(Comparison test)
002		GTO A		(If X ≤ Y, go to label A)
.
.
.
011		LBL A		(continue here from Line 002)
.
.
.

The six comparison tests between X and Y are:

Test		Key Stroke Sequence
x = y		[ g ] [ - ] (TEST) [ 5 ]
x ≠ y		[ g ] [ - ] (TEST) [ 6 ]
x > y		[ g ] [ - ] (TEST) [ 7 ]
x < y		[ g ] [ - ] (TEST) [ 8 ]
x ≥ y		[ g ] [ - ] (TEST) [ 9 ]
x ≤ y		[ g ] [ ÷ ] (x ≤ y)

Hint: Only two conditional tests are listed on the keyboard, x = 0 and x ≤ y. For the others, you will need to press [ g ] [ - ] (TEST) [ # ]. A reference of all codes and tests available are located on the back of the calculator.

Let's put some of these conditional tests to work in next two programs.

The Sign Function

The sign function (sometimes called signum) is defined as follows:

{-1 if x < 0
sign(x)= {0 if x = 0
{1 if x > 0

Note: It is assumed that you are familiar with switching the calculator between Run and Program modes. Furthermore, I assume that you are familiar clearing the program memory. Please review Part 2 if you are not familiar with these procedures.

The Sign Function Program

The following program can be shown in the following diagram:

↓
Is X = 0?
Yes:  Display 0 and stop.
No:  Go on to the next step.
↓
Is X < 0?
Yes:  Go to Label 0.
No:  Display 1 and stop, because X > 0.
↓
LABEL 0:
Display -1 and stop.
↓
END

We will use label C in this program. The display is on the left column, the required calculator keystrokes are on the right.

Program Listing: The Sign Function

Display				Keystrokes
001 -	42	21	13	[ f ] [SST] (LBL) [10^x] (C)
002 -		43	20	[ g ] [ × ] ( x = 0 )
003 -		43	32	[ g ] [GSB] (RTN)
004 - 	43	30	2	[ g ] [ - ] (TEST) [ 2 ] ( x < 0 )
005 - 		22	0	[GTO] [ 0 ]
006 -			1	[ 1 ]
007 - 		43	32	[ g ] [GSB] (RTN)
008 -	42	21	0	[ f ] [SST] (LBL) [ 0 ]
009 -			1	[ 1 ]
010 -			16	[CHS]
011 -		43	32	[ g ] [GSB] (RTN)

Notes:
* Line 002 - first test, X = 0.
* Line 004 - second test X < 0.
* Lines 006 and 007, instructions when X > 0
* Lines 008 through 011, instructions when X < 0

Examples:
1. sign(-52):

-52 [ f ] [10^x] (C)

Result: -1

2. sign(0):

0 [ f ] [10^x] (C)

Result: 0

3. sign(36):

36 [ f ] [10^x] (C)

Result: 1
The Game of Bust

This program is the game of Bust. The player builds a total from values chosen randomly from 1 to 10. The goal is to get the total close to 21 without going over.

This program uses three labels: A, B, and 0.
* Label A: Initializes the program
* Label B: Draw a number and add it to the total.
* Label 0: Instructions when the player "busts" (goes over 21).

Program Diagram:
Label A:  Initialization
Store 0 in R0 (memory register 0)
↓
Label B:
Put 1 on the X stack
↓
Obtain a random number
↓
Multiply the random number by 10
↓
Take the integer part of the result and add it to 1
↓
Stop and display the number
↓
Add the number to the total
↓
Is the total > 21?
Yes: Go to Label 0
No: Display the totalHuman Decision:  Draw again?
Execute Label B.

Label 0:  (Total > 21)
Display and flash the total.

END

Program Listing: The Game of Bust

Display				Keystrokes
001 -	42	21	11	[ f ] [SST] (LBL) [ √ ] (A)
002 -			0	[ 0 ]
003 -		44	0	[STO] [ 0 ]
004 -		43	32	[ g ] [GSB] (RTN)
005 -	42	21	12	[ f ] [SST] (LBL) [e^x] (B)
006 -			1	[ 1 ]
007 -			36	[ENTER]
008 -			1	[ 1 ]
009 -			0	[ 0 ]
010 -		42	36	[ f ] [ENTER] (RAN#)
011 -			20	[ × ]
012 -		43	44	[ g ] [STO] (INT)
013 -			40	[ + ]
014 - 			31	[R/S]
015 -	44	40	0	[STO] [ + ] [ 0 ]
016 -		45	0	[RCL] [ 0 ]
017 - 			2	[ 2 ]
018 -			1	[ 1 ]
019 -	43	30	8	[ g ] [ - ] (TEST) [ 8 ] (x020 -		22	0	[GTO] [ 0 ]
021 -			34	[x<>y]
022-			43	32	[ g ] [GSB] (RTN)
023 -	42	21	0	[ f ] [SST] (LBL) [ 0 ]
024 -			34	[x<>y]
025 -	43	4	9	[ g ] [ 4 ] (SF) [ 9 ]
026 -		43	32	[ g ] [GSB] (RTN)

Instructions:
1. Press [ f ] [ √ ] (A) to start a new game.
2. Press [ f ] [e^x] (B) to draw numbers. The random number is displayed.
3. Press [R/S] to display the total.
4. If the total is over 21, you have busted, and the display flashes. Press [ g ] [ 5 ] (CF) [ 9 ], [ ← ], or [ON] to stop the flashing.
5. If the total is not over 21, decide whether to stop. If you want to draw again, repeat instructions 2 and 3. ([ f ] [e^x] (B))

A example game:
Paul, Julie, and Chris are playing the game of Bust.

Paul
[ f ] [ √ ] (A)
[ f ] [e^x] (B); (Display: 5); [R/S]; (Total: 5)
[ f ] [e^x] (B); (Display: 1); [R/S]; (Total: 6)
[ f ] [e^x] (B); (Display: 5); [R/S]; (Total: 11)
[ f ] [e^x] (B); (Display: 1); [R/S]; (Total: 12)
[ f ] [e^x] (B); (Display: 9); [R/S]; (Total: 20, decides to stop)

Julie:
[ f ] [ √ ] (A)
[ f ] [e^x] (B); (Display: 6); [R/S]; (Total: 6)
[ f ] [e^x] (B); (Display: 4); [R/S]; (Total: 10)
[ f ] [e^x] (B); (Display: 10); [R/S]; (Total: 20, decides to stop)

Chris:
[ f ] [ √ ] (A)
[ f ] [e^x] (B); (Display: 6); [R/S]; (Total: 6)
[ f ] [e^x] (B); (Display: 5); [R/S]; (Total: 11)
[ f ] [e^x] (B); (Display: 3); [R/S]; (Total: 14)
[ f ] [e^x] (B); (Display: 8); [R/S]; (Total: 22 - flashing - busted - [ ← ])

Playoff:
Paul: [ f ] [ √ ] (A) [ f ] [e^x] (B) - Display: 3
Julie: [ f ] [ √ ] (A) [ f ] [e^x] (B) - Display: 8 - (Julie wins the playoff)
Next time, we will tackle the Quadratic Formula. Until then,

Eddie

This tutorial is property of Edward Shore. Mass reproduction and distribution requires express permission. © 2011

HP 15C Programming Tutorial - Part 3: Registers

Register Arithmetic

In Part 3, we will work with register arithmetic. Register arithmetic comes in two types: storage and recall. Register arithmetic is a very powerful tool in keystroke programming, and helps speed up execution of HP 15C programs.

Register arithmetic is available for the following functions: addition ( + ), subtraction ( - ), multiplication ( × ), and division ( ÷ ).

Storage Arithmetic

Storage arithmetic performs the operation on the contents of the memory register AND stores the result in that memory register.

Where the # in R# is the register number and X represents the contents of the X register on the stack:

[STO] [ + ] (0-9, .0-.9, (i)) executes R# = R# + X
[STO] [ - ] (0-9, .0-.9, (i)) executes R# = R# - X
[STO] [ × ] (0-9, .0-.9, (i)) executes R# = R# × X
[STO] [ ÷ ] (0-9, .0-.9, (i)) executes R# = R# ÷ X

Please note the order of the operands.

Recall Arithmetic

Recall arithmetic calls up the contents of a memory register and applies the operation to that and the contents of the X register. The T, Y, and Z registers on the stack are not affected.

Where the # in R# is the register number and X represents the contents of the X register on the stack:

[RCL] [ + ] (0-9, .0-.9, (i)) executes X + R#
[RCL] [ - ] (0-9, .0-.9, (i)) executes X - R#
[RCL] [ × ] (0-9, .0-.9, (i)) executes X × R#
[RCL] [ ÷ ] (0-9, .0-.9, (i)) executes X ÷ R#

We will first present an example on how register arithmetic works and then create a program using register arithmetic.
Bank Account

Your bank account has a balance of \$1,000.00. The following transactions happened during one weekend:

* You purchased groceries for \$76.11.
* You went to your favorite electronics store an purchased a MP3 player. After sales tax, the total came to \$217.45.
* You tutored a friend who paid you \$60.00.

What is the balance? And if on Monday you had to write a \$250.00 check for the credit card, would be the balance be?

We'll use R0 (memory register 0) for the bank account balance.

Key Strokes:

In Run Mode:
1000 [STO] [ 0 ] - * Stores 1000 in R0
76.11 [STO] [ - ] [ 0 ] - * Subtracts 76.11 from R0, Display: 76.11
217.45 [STO] [ - ] [ 0 ] - * Subtracts 217.45 from R0, Display: 217.45
60 [STO] [ + ] [ 0 ] - * Adds 60 to R0, Display: 60
[RCL] [ 0 ] - * Recalls the contents of R0: 766.44

The bank balance is \$766.44.

250 [CHS] [RCL] [ + ] [ 0 ] - * -250 + R0; Display: 516.44

After the credit card check, the balance would be \$516.44.

Caution: The order of the arguments is important in register arithmetic.

Storage arithmetic: R# = R# [+, -, ×, ÷] X
Recall arithmetic: X [+, -, ×, ÷] R#
Program: Polynomial Evaluation

This program evaluates the polynomial f(x) = .57x^3 + .66x^2 - .34x + 1. Find f(-.01), f(0), and f(.01).

Horner's Method

In keystroke programming, a popular way to program polynomials is the use of Horner's Method.

To prepare for this method, first fill the stack with the contents of x. This is easily accomplished by pressing [ENTER] three times. Then it is a matter of alternating operations: ×, +, ×, +, ... etc, until the constant is added.

This method not only works for the HP 15C but also most RPN calculators (including HP 12C, HP 35S, HP 17BII+ (in RPN mode), HP 30b (in RPN mode), and apps GO-25 SciRPN and Active RPN (when Replicate t-register is turned on)).

Horner's Method for a quadratic polynomial:

a * x^2 + b * x + c = (a * x + b) * x + c

Horner's Method for a cubic polynomial:

a * x^3 + b * x^2 + c * x + d = ((a * x + b) * x + c) * x + d

Horner's Method for a quartic polynomial:

a*x^4 + b*x^3 + c*x^2 + d*x + e = (((a*x + b)*x + c)*x + d)*x + e

Polynomials of higher powers can be worked out in a similar method.

Often, to save space the program calls that we store constants to memory registers prior to running the program. Pre-loading memory has two advantages: (1) program space is saved and (2) any memory register can be easily changed in what-if calculations. The cost is that for each memory register used, 7 bytes of programming steps may not be available.

I recommend of using the low-number registers whenever possible. Keep in mind that certain registers are used in certain types of calculations (statistics and matrices).

To restate the problem:

Evaluate the polynomial f(x) = .57x^3 + .66x^2 - .34x + 1. Find f(-.01), f(0), and f(.01).

Let R0 = .57, R1 = .66, R2 = -.34, and R3 = 1. [R0*x^3 + R1*x^2 + R2*x + R3]

Let's label this program B. If necessary, clear the program memory before you begin.

Keying in the Program

In Run Mode:
[ . ] [ 5 ] [ 7 ] [STO] [ 0 ]
[ . ] [ 6 ] [ 6 ] [STO] [ 1 ]
[ . ] [ 3 ] [ 4 ] [CHS] [STO] [ 2 ]
[ 1 ] [STO] [ 3 ] - * Storage registers are used
[f] [R ↓ ] (CL PRGM)
[g] [R/S] (P/R)

In Program Mode:
(Clear the program memory if needed - [f] [R ↓ ] (CL PRGM))
[f] [SST] (LBL) [e^x] (B) - * Label B
[ENTER]
[ENTER]
[ENTER]
[RCL] [ × ] [ 0 ] - * recall arithmetic
[RCL] [ 1 ]
[ + ]
[ × ]
[RCL] [ 2 ]
[ + ]
[ × ]
[RCL] [ 3 ]
[ + ]
[g] [GSB] (RTN)
[g] [R/S] (P/R)

Program Listing
000-					Keys
001-	42	21	12		[ f ] [SST] (LBL) [e^x] (B)
002-			36		[ENTER]
003-			36		[ENTER]
004-			36		[ENTER]
005-	45	20	0		[RCL] [ × ] [ 0 ]
006-		45	1		[RCL] [ 1 ]
007-			40		[ + ]
008-			20		[ × ]
009-		45	2		[RCL] [ 2 ]
010-			40		[ + ]
011-			20		[ × ]
012-		45	3		[RCL] [ 3 ]
013-			40		[ + ]
014-		43	32		[ g ] [GSB] (RTN)

How to Use
In Run Mode:
1. Enter x
2. Press either [ f ] [e^x] (B) or [GSB] [e^x] (B)

Examples:
1. f(-0.01)

.01 [CHS] [ f ] [e^x] (B)

Result: (≈) 1.0035

2. f(0)

0 [ f ] [e^x] (B)

Result: 1.0000

3. f(0.01)

.01 [ f ] [e^x] (B)

Result: (≈) 0.9967

This concludes Part 3. Next time in Part 4, we will work with comparison tests.

Happy programming,

Eddie

This tutorial is property of Edward Shore. Mass reproduction and distribution requires express permission from the author. © 2011

HP 15C Programming Tutorial - Part 2: Basic Programming

Basic Programming

In Part 2, we will type our first two programs using the HP 15C calculator. Keystroke programming is fairly simple - all we are doing is recording a set of keystrokes to be used at a later time.

The HP15C allows for 25 "named" programs: A, B, C, D, E, the digits 0 through 9, and the decimal point/number names ".0" through ".9". Programs do not necessarily have to be named.

How to Get to Program Mode

The key sequence [g] [R/S] (P/R) switches the calculator between Run mode and Program mode.

Run mode is where programs are executed. In addition, the regular calculator operations take place in this mode.

Program mode is where programs are edited. When the 15C is in Program mode, an PRGM indicator is shown on the bottom right hand of the screen.

The Display in Program Mode

The display of the 15C in Program Mode looks something like this:

NNN - AA, BB, CC

where:
* NNN indicates the program steps.
* AA, BB, and CC are the key codes.

A key code is a two digit code consisting of a row number and a column number. Rows are labeled 1 through 4 from the top. Columns are labeled 1 through 9, then 0 (the rightmost column) from the left.

Examples:
[ √ ] has the key code 11.
[CHS] has the key code 16.
[SIN] has the key code 23.
[ × ] has the key code 20.
[R ↓ ] has the key code 33.
[ - ] has the key code 30.
[STO] has the key code 44.

Notable exceptions:
* The number keys [ 0 ] through [ 9 ] has their own digit key codes. The key code for the key [ 0 ] is 0, [ 1 ] is 1, [ 2 ] is 2, etc.
* The key code for the [ENTER] key is always 36.

[R/S] Key

The [R/S] key is the run/stop key, which stops program execution. Use this to display immediate or final results. The key also serves a prompt for additional input.

Clearing Programs

The key sequence [f] [R ↓ ] (CL PRGM) serves two different functions:

* In Run mode, it resets the program counter to step 000.

* In Program mode, it completely clears program memory.

Scrolling and Editing Program Contents

While in Program Mode:

[SST] views the next step in the program.

[g] [SST] (BST) view the previous step in the program.

[← ] deletes the program step in display.

Entering a new instruction in the middle of a program inserts the instruction at that step and pushes all proceeding instructions "below" it.

The First Program

Three students drove to a friend's house for a party. Each of the students live 30.5 miles from the party. Each of the students arrive at the party in 30 minutes, 40 minutes, and 50 minutes, respectfully. Calculate how fast each of the students were going.

From basic physics, we know that:

Distance = Speed × Time, or

Speed = Distance ÷ Time

In our case, we know that Distance is 30.5 miles so:

Speed = 30.5 ÷ Time

Keying in the program

In Run Mode:
[f] [R ↓ ] (CLEAR PRGM)
* Set the counter to 000
[g] [R/S] (P/R)
* Switch to Program mode

In Program Mode:
[f] [R ↓ ] (CLEAR PRGM) -- * Clears program mode
[ 3 ]
[ 0 ]
[ . ]
[ 5 ]
[x<>y] -- * Switches X and Y registers to set proper order for division
[ ÷ ]
[g] [GSB] (RTN) -- * End program
[g] [R/S] (P/R)
* Switch to Run mode

In Run Mode:
[f] [R ↓ ] (CLEAR PRGM)
* Set the counter to 000

Key Stroke Instructions
000-
001-			3		[ 3 ]
002-			0		[ 0 ]
003-			48		[ . ]
004-			5		[ 5 ]
005-			34		[x<>y]
006-			10		[ ÷ ]
007-		43	32		[ g ] [GSB] (RTN)

How to Use
In Run mode, enter the time and press [R/S].

So:
0.5 [R/S] yields 61.0000,
40 [ENTER] 60 [ ÷ ] [R/S] yields 45.75000, and
50 [ENTER] 60 [ ÷ ] [R/S] yields 36.60000.

The student who arrived in 30 minutes (1/2 an hour) drove 61 mph (miles per hour), the student who arrived in 40 minutes drove 45.75 mph, and the student who arrived in 50 minutes drove 36.6 mph.

Volume of a Cylinder

In second program of Part 2, we will learn how to label (or name) a program.

The [f] [SST] (LBL) key sequence followed by name (A-E, 0-9, .0-.9) gives the proceeding set of instructions a name, and is almost always ended with a RTN (return) command.

In Run mode, programs labeled A-E can be used by pressing [f] by the appropriate letter. All programs that are labeled can be ran by using the [GSB] (Gosub) key.

This program will calculate the volume of a cylinder given height and radius. The formula for the volume is:

V = π × h × r^2

The stack will be set up with h in the Y register and r in the X register.

The operations needed to calculate the volume are:
1. Square r
2. Multiply r^2 and h
3. Call up π
4. Multiply π and r^2 × h

Keying in the Program

In Run Mode:
[f] [R ↓ ] (CL PRGM)
[g] [R/S] (P/R)

In Program Mode:
[f] [SST] (LBL) [ √ ] (A) - * Label A
[g] [ √ ] (x^2)
[ × ]
[g] [EEX] ( π )
[ × ]
[g] [GSB] (RTN)
[g] [R/S] (P/R)

Key Stroke Instructions
000-
001-	42	21	11		[f] [SST] (LBL) [ √ ] (A)
002-		43	11		[g] [ √ ] (x^2)
003-			20		[ × ]
004-		43	26		[g] [EEX] ( π )
005-			20		[ × ]
006-		43	32		[ g ] [GSB] (RTN)

How to Use
1. Enter the height
2. Press [ENTER]
4. Press [ f ] [ √ ] (A) or [GSB] [ √ ] (A)

Examples:
1. Find the volume of a cylinder with a height of 1.5 in and a radius of 1.2 in.

1.5 [ENTER] 1.2 [ f ] [ √ ] (A)

Result: (≈) 6.7858 in^2

2. Find the volume of a cylinder with a height of 2.08 cm and radius of 0.95 cm. Remember order is important!

2.08 [ENTER] 0.95 [ f ] [ √ ] (A)

Result: (≈) 5.8974 cm^2

Tip: If you accidentally enter radius first then height, just press the swap key, [x<>y] prior to pressing [ f ] [ √ ] (A).

That is all for Part 2. Next time, we will work storage and recall arithmetic, a very powerful feature of the HP 15C.

Happy programming,

Eddie

This tutorial is property of Edward Shore. Mass reproduction and distribution requires express permission of the author. © 2011

HP 15C Programming Tutorial - Part 1: The Stack

Welcome to Programming with the 15C

This tutorial series covers keystroke programming with the Hewlett Packard 15C calculator. This is a powerful scientific calculator that is RPN (Reverse Polish Notation) operated. Features include complex numbers, statistics, matrices, integration, and the ability to solve equations.

Calculator.org gives a short introduction to RPN here.

I also give a short RPN tutorial on my blog, which can be found here.

The HP 15C was originally released in 1982 and proved to be one of the most popular calculators. In fact, it was so popular that demand for the HP 15C lasted well after Hewlett Packard ended its original production run in 1989. In September 2011, Hewlett Packard released a HP 15C Limited Edition calculator, that works just like the original, except it is much faster. We can only hope that Hewlett Packard considers making the HP 15C a permanent production once again.

The calculator has has amount 67 storage registers. Each storage register can contain a number in memory, or 7 bytes of programming.

At default, the HP 15C allocates 19 storage registers as numeric storage, and 46 storage registers available for either programming or numeric storage. 2 storage registers, R0 and RI, will always be used for numeric storage. Our tutorial will use this default setting.

The first lesson will briefly cover the basics of the stack - which a working knowledge is essential to successfully programming the HP 15C.
The Stack

The HP 15C calculator contains four stacks: T, Z, Y, and X. The X stack is what you see in the display. We will go over how the stack is affected by the common operations of the calculator.

T
-------
Z
-------
Y
-------
X

[ENTER]

The [ENTER] key does many functions. First, it terminates entry of a number and prepares the calculator for another. Second, the number that was just entered not only stays in the X register, but is also copied to the Y register. The next number you enter replaces the contents of the X register. The contents of register T are lost.

T:  contents of Z are moved here
-------
Z:  contents of Y are moved here
-------
Y:  the number just entered is copied here
-------
X:  ready for the next number

One-Number Functions

One-number functions, f(X), operate on the contents of the X register and returns the result on the X register. One-number functions include √, x^2, LN, e^x, LOG, 10^x, 1/x, CHS, SIN, SIN^-1, COS, COS^-1, TAN, TAN^-1, HYP (trig), HYP^-1 (trig), ABS, FRAC, INT, x!, → H (hours), → HMS (hours, minutes, seconds), → RAD, and → DEG.

T:  unaffected
-------
Z:  unaffected
-------
Y:  unaffected
-------
X:  f(X)

Two-Number Functions

Two-number functions, g(X,Y), operate on the contents of the X and Y registers and returns the result on the X register. The contents of Z register are moved to the Y register, and the contents of the T register are copied to the Z register.

Two-number functions include +, - (returns Y - X), × , ÷ (returns Y ÷ X), y^x, % (returns Y * X/100), Px,y (permutation), Cx,y (combination), and ∆% (percent change).

T:  unaffected
-------
Z: contents of T are copied here
-------
Y: contents of Z are moved here
-------
X: the result of g(X,Y)

π and Last x

Obviously, the key sequence [g] [EEX] ( π ) enters π (pi) on the X stack.

The key sequence [g] [ENTER] (LST x) recalls the value that was in the display prior to the execution of a numeric operation. LST x is good for correct errors and faster calculations should a number need to be repeated again.

Both π and LST x push the stack up. The contents of register T are lost.

T: contents of Z are moved here
-------
Z: contents of Y are moved here
-------
Y: contents of X are moved here
-------
X:  π or Last X

Polar and Rectangular Conversion

[f] [ 1 ] (→ R ) is the Polar to Rectangular conversion function.

T:  unaffected
-------
Z:  unaffected
-------
Y:  y is converted to θ
-------
X:  x is converted to r

[g] [ 1 ] (→ P ) is the Rectangular to Polar conversion function.

T:  unaffected
-------
Z:  unaffected
-------
Y:  θ is converted to y
-------
X:  r is converted to x

The [x<>y] Key - Swap

The [x<>y] switches the contents both registers X and Y. This key is useful in ordering values in the correct order and viewing two-result operations (such as the rectangular and polar conversions).

T:  unaffected
-------
Z:  unaffected
-------
Y:  contents of X are moved here
-------
X:  contents of Y are moved here

The Roll Keys

[R ↓ ] is the roll down key. Pressing this "rolls" the contents of the stack down one register. Pressing [R ↓ ] four times allows you to view the entire stack.

T:  contents of X are moved here
-------
Z:  contents of T are moved here
-------
Y:  contents of Z are moved here
-------
X:  contents of Y are moved here

The key sequence [g] [R ↓ ] (R ↑ ) is the roll up key sequence, which "rolls" the contents of the stack up one register. Executing this key sequence four times allows you to view the entire stack.

T:  contents of Z are moved here
-------
Z:  contents of Y are moved here
-------
Y:  contents of X are moved here
-------
X:  contents of T are moved here

Practice Tip: When designing a program, sometimes I find it helpful to draw the stack in a horizontal matter (X | Y | Z | T) to see how each program step affects the stack.

Well, that is it for Part 1. Next time, we actually start programming. I hope this helps you understand the basics of the stack. Until next time,

Eddie

This tutorial is the property of Edward Shore - mass duplication and reproduction requires express permission of the author. © 2011

HP Prime: Renaming Headers in Statistic Editors

HP Prime:  Renaming Headers in Statistic Editors I recently received an email asking for help of how to rename columns of the Statistics ...