Sunday, December 18, 2011

HP 15C Programming Tutorial - Part 15: Memory Exchange (Area of a Polygon)

Memory Exchange

We will introduce another powerful feature of the HP 15C: memory exchange. This will allow the user to exchange the contents of the X register with a designated memory register.

Key strokes: [ f ] [ 4 ] (x > <)

Example: Store 2 in memory register 5. Call the constant π and exchange it with the contents of memory register 5.

Key strokes:
2 [STO] 5 (Display: 2.0000)
[ g ] [EEX] ( π ) (Display: 3.1416)
[ f ] [ 4 ] (x > <) 5 (Display: 2.0000) - now π in in R5
Check: [RCL] [ 5 ] (Display: 3.1416)

We will use this in a program.

Area of a Polygon with Transverse Coordinates

This program calculates the area of a polygon which vertices are given by (x,y) coordinates by the approximation:

R ≈ 0.5 × abs(∑(y_i * x_i+1 - y_i+1 * x_i, i = 0, n-1))

In geographic terms:


| y > 0, North
|
|
x< 0, |
West |
---------------------------
| x > 0, East
|
|
|
| y < 0, South


Labels Used:
LBL A - Initialize the program and enters the initial coordinates
LBL B - Enters additional coordinates
LBL C - Calculates area

Memory Registers Used:
R0 = Area
R1 = x_i
R2 = y_i
R3 = x_i+1
R4 = y_i+1
R5 = temporary use
R6 = temporary use

Program:

KEY CODES KEYS
001 42 21 11 LBL A
002 0 0
003 44 5 STO 5
004 44 6 STO 6
005 33 R ↓
006 44 1 STO 1
007 34 x<>y
008 44 2 STO 2
009 2 2
010 43 32 RTN
011 42 21 12 LBL B
012 44 3 STO 3
013 34 x<>y
014 44 4 STO 4
015 34 x<>y
016 45 20 2 RCLx 2
017 44 40 6 STO+ 6
018 45 4 RCL 4
019 45 20 1 RCLx 1
020 44 40 5 STO+ 5
021 45 3 RCL 3
022 42 4 1 x<>1 * Exchange X with R1
023 45 4 RCL 4
024 42 4 2 x<>2 * Exchange X with R2
025 43 32 RTN
026 42 21 13 LBL C
027 45 5 RCL 5
028 45 30 6 RCL- 6
029 43 16 ABS
030 2 2
031 10 ÷
032 44 0 STO 0
033 43 32 RTN


Instructions:

1. Enter the initial y coordinate, hit [ENTER], the initial x coordinate, and press [ f ] [ √ ] (A).
2. For the additional points, enter the y coordinate, press [ENTER], enter the x coordinate, and the [ f ] [e^x] (B).
3. To calculate the area, press [ f ] [10^x] (C).

Example:

Find the area of the polygon with the following vertices:
(0, 0)
(4, 4)
(0, 8)
(-2, 8)
(-3, -1)

Key Strokes:

0 [ENTER] 0 [ f ] [ √ ] (A)
4 [ENTER] 4 [ f ] [e^x] (B)
8 [ENTER] 0 [ f ] [e^x] (B)
8 [ENTER] 2 [CHS] [ f ] [e^x] (B)
1 [CHS] [ENTER] 3 [CHS] [ f ] [e^x] (B)
[ f ] [10^x] (C)

Area = 37

I hope you find this program useful and fun. Until next time,

Eddie


This tutorial is property of Edward Shore. © 2011

HP 15C Programming Tutorial - Part 16: Solving Multiple Input Equations (TVM Program)

Solving Multiple Input-Single Output Equations

In Part 16, we will use the HP 15C's solve feature to solve multiple input-single output (MISO) equations.

This tutorial will be using a technique introduced by Karl Schneider. Please click on this link to read about it.

It involves using a program to write the equation f(x1, x2, x3, ...) = 0. The equation begins with the instructions: LBL #, STO(i); where # is the label letter/number of your choice. The next step is to put the variable number in memory register I. Finally, execute the solve function. Let's see this in action.

Volume of a Cylinder

In this first program will turn the volume of a cylinder into a MISO solver. The equation is:

V = π r^2 h

The first step is to make one side of the equation zero.

0 = π r^2 h - V

We'll have to designate memory registers for each of the variables. Let's use:

R1 = r
R2 = h
R3 = V

The equation can be seen as:

0 = π R1^2 R2 - R3

Next enter the program:


Key Codes Keys
001 42 21 11 LBL A
002 44 25 STO(i)
003 43 26 π
004 45 1 RCL 1
005 43 11 x^2
006 20 ×
007 45 20 2 RCLx 2
008 45 3 RCL 3
009 30 -
010 43 32 RTN


Now we can use the program as MISO solver.

Example 1: Given V = 1,000 and h = 10. Find r.

The variable r corresponds to R1. This means we store 1 in memory register I. Give an initial interval (0, 1000).

Key Strokes:
1000 [STO] [ 3 ]
10 [STO] [ 2 ]
1 [STO] [TAN] ( I )
0 [ENTER] 1000 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)

Answer: r ≈ 5.6419

Example 2: Given V = 2,498.65 and r = 39.43. Find h. Use an initial interval (0, 1000). We are looking for h, so store 2 in memory register I.

Key Strokes:
2498.65 [STO] [ 3 ]
39.43 [STO] [ 1 ]
2 [STO] [TAN] ( I )
0 [ENTER] 1000 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)

Answer: h ≈ 0.5116

Time Value of Money

This program uses the solver capabilities to solve common financial programs.

The equation:

0 = PV + PMT × ((1 - (1 + I)^-N) / I) + FV × (1 + I)^-N

We will designate the following registers for the variables:
R1 = N (number of periods)
R2 = I (periodic interest rate, entered as a decimal)
R3 = PV (present value)
R4 = PMT (payment)
R5 = FV (future value)

End-of-period payments is assumed.

We can rewrite the equation as this:

0 = R3 + R4 × ((1 - (1 + R2)^-R1) / R2) + R5 × (1 + R2)^-R1

Program:


Key Codes Keys
001 42 21 11 LBL A
002 44 24 STO(i)
003 1 1
004 45 40 2 RCL+2
005 45 1 RCL 1
006 16 CHS
007 14 y^x
008 16 CHS
009 1 1
010 40 +
011 45 10 2 RCL÷ 2
012 45 20 4 RCLx 4
013 1 1
014 45 40 2 RCL+ 2
015 45 1 RCL 1
016 16 CHS
017 14 y^x
018 45 20 5 RCLx 5
019 40 +
020 45 40 3 RCL+ 3
021 43 32 RTN


Example 1: Given N = 360, I = 5%/12, PV = 200,000, and FV = 0, find PMT. PMT refers to memory register 4. (Store 4 in RI). Give an initial interval of (0, 1000).

Key Strokes:
360 [STO] [ 1 ]
.05 [ENTER] 12 [ ÷ ] [STO] [ 2 ]
200000 [STO] [ 3 ]
0 [STO] [ 5 ]
4 [STO] [TAN] ( I )
0 [ENTER] 1000 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)

Answer: PMT = -1073.6432 ($1,073.64)

Example 2: Given N = 48, PV = 25,000, PMT = -600, and FV = 0, solve for I. The variable I refers to memory register 2. (Store 2 in RI). Give an initial interval of (0.1, 1).

Caution: When solving for interest, do not use 0 as an endpoint of an interval, for this will cause an "Error 0" condition.

Key Strokes:
48 [STO] [ 1 ]
25000 [STO] [ 3 ]
600 [CHS] [STO] [ 4 ]
0 [STO] [ 5 ]
2 [STO] [TAN] ( I )
0.1 [ENTER] 1 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)
(to find the annual rate) 12 [ × ]

Answer: I = .0059 (Periodic Rate 0.59%), .0712 (Annual Rate 7.12%)

Example 3: Given N = 10, I = .03, PV = -1000, PMT = -250, solve for FV. This time we are solving for variable # 5 (FV). Use an initial interval (1000, 5000).

Key Strokes:
10 [STO] [ 1 ]
.03 [STO] [ 2 ]
1000 [CHS] [STO] [ 3 ]
250 [CHS] [STO] [ 4 ]
5 [STO] [TAN] ( I )
1000 [ENTER] 5000 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)

Answer: FV = 4209.8862 ($4,209.89)

Thank you for joining me for another session.

Source: Schneider, Karl. "SOLVE and INTEG on HP's RPN-based models" HP Articles. Museum of HP Calculators. October 2, 2005. http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/articles.cgi?read=556

Thank you Karl!!!




This tutorial is property of Edward Shore. © 2011

Sunday, December 11, 2011

HP 15C Programming Tutorial: Part 14 - Complex Numbers

Complex Numbers and the HP 15C

The HP 15C can operate with complex numbers. To use complex numbers, set flag 8 by pressing [ g ] [ 4 ] (SF). You will know that flag 8 is set when the display has a "C" indicator.

A complex number (a + bi) consists of two parts: its real part (a) and its imaginary part (b). Each part is dealt with separately. When the calculator is in complex mode, each of the four stack registers, X, Y, Z, and T, has a real part and a separate part for the imaginary part. The display only shows the real part of the X register - this is important to know.

The stack:


--------------------------------------------
real part of T | imaginary part of T
---------------------------------------------
real part of Z | imaginary part of Z
--------------------------------------------
real part of Y | imaginary part of Y
--------------------------------------------
real part of X | imaginary part of X
--------------------------------------------


Complex Number Basics

Complex Number Entry

Two ways to enter complex numbers, one use the I register, the other uses the Re<>Im function.

Way 1:

real part [ENTER] imaginary part [ f ] [TAN] (I)

Effects on the stack:

-------------------------------------------
T: contents previously held in Y
-------------------------------------------
Z: contents previously held in Y
-------------------------------------------
Y: contents previously held in X
-------------------------------------------
X: a + bi
------------------------------------------


The contents previously held in stack registers Z and T are lost.

If you want the stack registers Y, Z, and T to stay intact, use Way 2.

Way 2:

imaginary part [ f ] [ - ] (Re<>Im) real part

Effects on the stack:

------------------------------------------
T: contents previously held in T
------------------------------------------
Z: contents previously held in Z
------------------------------------------
Y: contents previously held in Y
------------------------------------------
X: a + bi
------------------------------------------


Entering Pure Imaginary Numbers

You can easily enter imaginary numbers by using the following key stroke sequence:

imaginary part [ f ] [ - ] (Re<>Im)

Viewing the Imaginary Part of a Complex Number

To view the imaginary part of a complex number without affecting anything, press [ f ] and hold [TAN] (I) to view the imaginary part. Letting go of the [TAN] key returns the display after about a half of second.

Hint: Keep in mind that [ f ] [ - ] (Re<>Im) switches the real and imaginary parts of the X register.

Conjugate of a Complex Number

The conjugate of a complex number a + bi is a - bi. Use this key sequence:

[ f ] [ - ] (Re<>Im) [CHS] [ f ] [ - ] (Re<>Im)

Negation of a Complex Number

Since we can only work with one part of a complex number at a time, simply pressing [CHS] is not enough. To negate a complex number, use the key sequence:

1 [CHS] [ × ]

Memory Registers and Complex Numbers

Each memory register can have only either the real part or the imaginary part of a complex number. Two memory registers have to be used to store a complete complex number. Furthermore, during programming, you will have construct the complex number from the two parts.

In the program presented in Part 14, five complex numbers will be stored in memory. We will need ten registers to store the real and imaginary parts for each complex number.

Trigonometry and Complex Numbers


According to the HP 15C manual, the calculator treats the number to be in Radians regardless of the angle setting when the trigonometric functions (SIN, COS, TAN, SIN^-1, COS^-1, TAN^-1) are executed.

The exception is the polar and rectangular conversions. The angle mode is taken into account. Only the X stack register is affected.

Square Roots of Negative Numbers

You will need to be in complex number mode to calculate square roots of negative numbers - otherwise "Error 0" occurs.

Conditional Tests with Complex Numbers

Let X = a + bi and Y = c + di.

There are only four conditional tests available with complex numbers:
[ g ] [ × ] (x = 0): Does a + bi = 0 + 0i?
[ g ] [ - ] (TEST) 0 (x ≠ 0): Does a + bi ≠ 0 + 0i?
[ g ] [ - ] (TEST) 5 (x = y): Does a + bi = c + di
[ g ] [ - ] (TEST) 6 (x ≠ y): Does a + bi ≠ c + di?

Leaving Complex Mode

Clear flag 8.

The Quadratic Equation with Complex Coefficients

The following equation solves the quadratic equation:

A × Z^2 + B × Z + C = 0

where A, B, C, and Z are complex numbers.

The roots are:

Z = ( -B ± √(B^2 - 4 A C)) / (2 A)

Memory Registers used:

R0 = real part of A
R1 = imaginary part of A
R2 = real part of B
R3 = imaginary part of B
R4 = real part of C
R5 = imaginary part of C
R6 = real part of Root 1
R7 = imaginary part of Root 1
R8 = real part of Root 2
R9 = imaginary part of Root 2

So:
A = R0 + R1 i
B = R2 + R3 i
C = R4 + R5 i
Root 1 = R6 + R7 i
Root 2 = R8 + R9 i

Labels used: A (Main), 0 and 1 (Subroutines)

Program Listing:


Key Codes Keys
001 42 21 11 LBL A
002 43 4 8 SF 8 * Set complex mode
003 32 0 GSB 0
004 32 1 GSB 1
005 44 6 STO 6
006 31 R/S
007 42 30 Re<>Im
008 44 7 STO 7
009 31 R/S
010 32 0 GSB 0
011 1 1
012 16 CHS
013 20 ×
014 32 1 GSB 1
015 44 8 STO 8
016 31 R/S
017 42 30 Re<>Im
018 44 9 STO 9
019 31 R/S
020 42 30 Re<>Im
021 43 32 RTN
022 42 21 0 LBL 0 * Discriminant Subroutine
023 45 0 RCL 0
024 45 1 RCL 1
025 42 25 I * [ f ] [TAN] (I)
026 45 4 RCL 4
027 45 5 RCL 5
028 42 25 I
029 20 ×
030 4 4
031 16 CHS
032 20 ×
033 45 2 RCL 2
034 45 3 RCL 3
035 42 25 I
036 43 11 x^2
037 40 +
038 11 √
039 43 32 RTN
040 42 21 1 LBL 1 * B^2/2A Subroutine
041 45 2 RCL 2
042 16 CHS
043 45 3 RCL 3
044 16 CHS
045 42 25 I
046 40 +
047 2 2
048 10 ÷
049 45 0 RCL 0
050 45 1 RCL 1
051 42 25 I
052 10 ÷
053 43 32 RTN


Instructions:

1. Store the real and imaginary parts of A, B, and C. See the above for the appropriate registers.
2. Press [ f ] [ √ ] (A)
3. The real part of Root 1 is displayed. Press [R/S].
4. The imaginary part of Root 1 is displayed. Press [R/S].
5. The real part of Root 2 is displayed. Press [R/S].
6. The imaginary part of Root 2 is displayed.

Example 1:

A = 2 + 3i
B = -3 - 4i
C = 2i

Registers:

R0 = 2
R1 = 3
R2 = -3
R3 = -4
R4 = 0
R5 = 2

Root 1 ≈ 0.2578 + 0.3769i
Root 2 ≈ 1.1268 - 0.4538i

Example 2:

A = -4 + 5i
B = 3
C = 2√2 - 3i

Registers:
R0 = -4
R1 = 5
R2 = 3
R3 = 0
R4 = 2√2 (2 [ENTER] [ √ ] [ × ])
R5 = -3

Root 1 ≈ -0.6500 + 0.1165i
Root 2 ≈ 0.9427+ 0.2493i

That is all for the tutorial. Until next time, happy programming! Eddie



This tutorial is property of Edward Shore. © 2011

The 12 Days of Christmas

Holiday Cheer!

So it is that time of the year: most of us are in a rush to purchase Christmas presents for our loved ones, perhaps taking advantage of the vast of amount of discounts stores are offering to entice us to buy. This is also the time where Christmas Carols are sung, even played 24/7 on some radio stations. Where I live (Azusa, CA which is about 25 miles east of Los Angeles, or Dodgertown for the baseball fans), I know of two stations that are all-Holiday music all the time until Christmas.

Chances are that you have heard about the "12 Days of Christmas" sung by about every recording artist that's ever lived (at least in America). The song was originally published around 1780 and came to the United States about 1910. The song tells about a very generous person give his/her true love presents for each day of Christmas. The days of Christmas starts on December 25 (or 26 for some cultures), and the giving lasts for 12 days.

How Many Presents?

How may presents did the generous lover give his/her true love? It depends on how the gifts are counted.

Take the second day for example:

On the second day of Christmas, my true love gave to me...
Two turtle doves and
A partridge in a pear tree.


Taking a literal count, we can say the true love received a total of three gifts: the doves AND another partridge in a pear tree.

However, some view each day as a summary of all the gifts after the new set of gifts were received. Hence, the true love received only the two doves because the true love was already given the partridge in a pear tree.

Some even count the partridge as one gift and the pear tree as another. For simplicity, I'll count the partridge in a pear tree as one gift.

The Math

To calculate the number of gifts given during the 12 Days of Christmas, it is only a matter of addition.

However, we can use the shortcut if we are adding consecutive numbers from 1 to N:

S = ∑(K = 1, N, K) = ((N + 1) × N) ÷ 2

How do we get this?

Let S represent the sum:

S = 1 + 2 + 3 + ... + (n-2) + (n-1) + n

Add S to both sides:

2 × S = 1 + 2 + 3 + ... + (n-2) + (n-1) + n + 1 + 2 + 3 + .... + (n-2) + (n-1) + n

Note that addition is communicative. That is for any two numbers a and b, a + b = b + a. This will allow us to rearrange the terms on the right side of the equation like so:

2 × S = 1 + n + 2 + (n-1) + 3 + (n-2) + .... + (n-2) + 3 + (n-1) + 2 + n + 1

Add each pair of terms on the right side. Note that 2 + (n-1) = n+1, 3 + (n-2) = n+1, and so on.

2 × S = (n+1) + (n+1) + (n+1) + .... + (n+1) + (n+1) + (n+1)

Observe that there are n terms of (n+1) on the right side.

2 × S = n × (n+1)

Divide both sides by 2 and we get:

S = (n × (n + 1)) / 2

We'll use this formula to help us count the number of presents.

Summary Count

If each day the lucky recipient merely recaps what he/she has received to that day, then the number of gifts the person gets is 78.

S = (12 × (12 + 1)) / 2 = (12 × 13) / 2 = 78 which consists of:


One partridge in a pear tree, (received on the first day)
Two turtle doves, (received on the second day)
Three French hens, (etc...)
Four calling birds,
Five gold rings,
Six geese-a-lying,
Seven Swans-a-swimming,
Eight Maids-a-milking,
Nine Ladies dancing,
Ten Lords-a-leaping,
Eleven Pipers pipping, and
Twelve Drummers drumming


Note there is no mention to what exactly the maids were milking but I digress.

Literal Count

A popular view to find the number of gifts is to make a literal count. That means each time the singer person sings about what he/she got from his/her true love actually was received. From this perspective, after the 12 Days of Christmas were over, the person actually got a total of 12 partridges in pear trees, along with 22 turtle doves, and so on. Hopefully the person has a big yard to fit all the pear trees, the turtle doves, a lake for all the geese, and many rooms to house the maids, ladies, lords, pipers, and drummers.

Let's calculate the total shall we?
Let S = number of gifts received that day and T = cumulative total of all the gifts received

First Day:
A Partridge in a Pear Tree

This is easy:
S = 1
T = 1

Second Day:
Two Turtle doves and
A Partridge in a pear tree

S = 1 + 2 = (2 × 3) / 2 = 3
T = 3 + 1 = 4 (1 from Day 1 and 3 from Day 2)

Third Day:
Three French hens,
Two Turtle doves, and
A Partridge in a pear tree

S = 1 + 2 + 3 = (3 × 4) / 2 = 6
T = 4 + 6 = 10 (6 from Day 3 and 4 from the past two days)

Fourth Day:
Four calling birds,
Three French hens,
Two Turtle doves, and
A Partridge in a pear tree

S = 1 + 2 + 3 + 4 = (4 × 5) / 2 = 10
T = 10 + 10 = 20

Fifth Day:
Five golden rings,
Four calling birds,
Three French hens,
Two Turtle doves, and
A Partridge in a pear tree

S = 1 + 2 + 3 + 4 + 5 = (5 × 6) / 2 = 15
T = 20 + 15 = 35

Sixth Day:
Six geese-a-laying,
Five golden rings,
Four calling birds,
Three French hens,
Two Turtle doves, and
A Partridge in a pear tree

S = 1 + 2 + 3 + 4 + 5 + 6 = (6 × 7) / 2 = 21
T = 35 + 21 = 56

If the true love stopped here, the person has already accumulated a nice haul of 56 presents! But we know the true love kept going, so...

Seventh Day:
Seven Swans-a-swimming and everything else from the first six days

S = 1 + 2 + 3 + 4 + 5 + 6 + 7 = (7 × 8)/2 = 28
T = 56 + 28 = 84

Eighth Day:
Eight Maids-a-milking and everything else from the first seven days

S = 1 + ... + 8 = (8 × 9)/2 = 36
T = 84 + 36 = 120

This has got to cost the true love a fortune!

Ninth Day:
Nine Ladies dancing and everything else from the first eight days

S = 1 + ... + 9 = (9 × 10)/2 = 45
T = 120 + 45 = 165

Tenth Day:
Ten Lords-a-leaping and everything else from the first nine days

S = 1 + ... + 10 = (10 × 11)/2 = 55
T = 165 + 55 = 220

Eleventh Day:
Eleven Pipers pipping and everything else from the first ten days

S = 1 + ... + 11 = (11 × 12)/2 = 66
T = 220 + 66 = 286

Twelfth Day:
Twelve Drummers drumming and everything else from the first eleven days

S = 1 + ... + 12 = (12 × 13)/2 = 78
T = 286 + 78 = 364

The grand total of all the gifts were 364. I can only imagine that the last few days busy the person and the true love.





Here is a link that has the lyrics of "The Twelve Days of Christmas".

Twelve Days of Christmas

Happy Holidays everyone! Merry Christmas! May this month (and all months thereafter) find you peace, love, warmth, and gratitude!

Eddie

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:
R0 = number of votes Candidate 0 receives
R1 = number of votes Candidate 1 receives
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
006 43 8 RAD
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

Indirect Addressing - Memory Registers


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.

Additional statistical functions are available:

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
002 43 8 RAD * use Radians mode
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
002 43 8 RAD
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

Quadratic Formula

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)
003		instruction	(skip to here if x ≠ 0)
.
.
.
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)
003		instruction	(skip to here if X > Y)
.
.
.
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:

Start with X
	↓
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 ] (x<y)
020 -		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).

Here we will introduce two concepts: Horner's Method and Pre-loading Memory.

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.

Pre-loading Memory

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]
3. Enter the radius
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