Tuesday, November 8, 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