Tuesday, October 18, 2011

RPL Programming Tutorial - Part 14 - HP 49g+/50g: Additional Examples

What else can we do?

Plenty. Part 14 will have three programs that illustrate some of what we have learned so far. One point that I really want to stress: with the HP 49g+ and 50g containing over 2,300 commands; so we have not even scratched the surface!

If you have not already, I recommend that you download the HP 50g Advanced User's Reference Manual from Hewlett Packard's website.

HP 48gII/49g+/50g Advanced User's Manual

This manual contains the syntax for ALL of the commands for the 50g and 49g+. The first section also has a section on programming with additional programming examples. It is a really good reference book; I have a copy on my iPad that I can access through iBooks for handy reference.

Today's tutorial will feature three programs:

1. PDM: Test to see if a matrix (with real eigenvalues) is a positive definite matrix.
2. NEWCD: Determines a point in alternate coordinate system with the origin translated (new center) and rotated (in a counterclockwise fashion)
3. SCTR: Scatterplot of data.

The program PDM

A matrix M is said to be a positive definite matrix if M is symmetric and for all non-zero column vectors (z):

z^T M z > 0

Where z^T is a transpose of the vector z.

A simple test is that if:

1. M is symmetric, that is M^T = M, and,
2. The eigenvalues of M are positive,

then M is a positive definite matrix.

Caution: This program gives the error message "> Error: Bad Argument Type" if M has complex valued eigenvalues. Do not conclude anything from the use of PDM if this is encountered.

One of the safest ways to clear errors is to press [ON].


Commands Used:

EGVL: Eigenvalues of a matrix
TRAN: Transpose of a matrix
SIZE: Size of a matrix, vector, or list
∑LIST: Sum of all the elements in a list
==: The comparison does x = y?

[RS] [ + ] (<< >>) [RS] 0 (→) [ALPHA] [F1] (A)
* Store the matrix in local variable A
[RS] [ + ] (<< >>) [LS] [EVAL] (PRG) [F3] (BRCH) [RS] [F1] (IF)
* IF-THEN-ELSE-END structure
[ALPHA] [F1] (A)
[LS] 5 (MATRICES) [NXT] [F1] (EIGEN) [F3] (EGVL)

* EGVL (Eigenvalue command)
[LS] 6 (CONVERT) [F5] (MATRX) [F1] (AXL)
* AXL (List ←→ Vector Command)
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
[LS] [EVAL] (PRG) [F6] (LIST) [F1] (ELEM) [F5] (SIZE)

* SIZE command
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP)
0 [RS] [1/X] ( > )
[LS] [SYMB] (MTH) [F3] (LIST) [F2] (∑LIST)

* ∑LIST command
[LS] [EVAL] (PRG) [F4] (TEST) [F1] ( == )
[ALPHA] [F1] (A)
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
[L5] 5 (MATRICES) [F2] (OPER) [NXT] [NXT] [F5] (TRAN)

* TRAN - transpose command
[LS] [EVAL] (PRG) [F4] (TEST) [NXT] [F1] (AND) 1 [LS] [NXT] (PREV) [F1] ( == )
* Finish the condition
[ ↓ ] 1 [ ↓ ] 0 [ENTER]
* Finish the program
[ ' ] [ALPHA] [ALPHA] [SYMB] (P) [F4] (D) [HIST] (M) [ENTER] [STO>]

The completed program:

<< → A
<< IF A EGVL AXL DUP SIZE SWAP 0 > ∑LIST == A DUP TRAN == AND 1 ==
THEN 1
ELSE 0
END >> >>


Instructions:

1. Enter the matrix
2. Run PDM

The result is either:

0, the matrix is not positive definite
1, the matrix is positive definite

Examples:

1. The matrix

[[2, 1]
[1, 3]]

returns 1 and is a positive definite matrix.

2. The matrix

[[4, 2, 6]
[3, 0, 7]
[-2, -1, -3]]

has complex eigenvalues and error condition (comparing complex numbers to real numbers) occurs. No conclusion.

3. The matrix

[[2, 1, 0]
[0, 3, 0]
[1, 0, 4]]

returns 0 and is not a positive definite matrix.

Source Used: Richard L. Burden and J. Douglas Faires "Numerical Analysis" 8 ed. Thomson Brooks/Cole 2005

The program NEWCD

This program returns a coordinate in a translated and rotated coordinated system.

Commands Used:

NEG: Negate - used when the user presses [+/-] in a program when the sign is not attached to a number.
R→C: Makes a complex number or ordered pair from two real numbers (Level 2, Level 1)

[RS] [ + ] ( << >> ) [RS] 0 (→)
[ALPHA] [ALPHA] [big X] [ ' ] (O) [NXT] (L) [F4] (D)
[SPC] [1/X] (Y) [ ' ] (O) [NXT] (L) [F4] (D)
[SPC] [big X] [F3] (C) [COS] (T) [ √ ] (R)
[SPC] [1/X] (Y) [F3] (C) [COS] (T) [ √ ] (R)
[SPC] [RS] [COS] (Θ) [ALPHA]

* The theta character (Θ), [RS] [COS] - the character is not printed on the keyboard
[RS] [ + ] ( << >> )
[ALPHA] [ALPHA] [big X] [ ' ] (O) [NXT] (L) [F4] (D)
[SPC] [1/X] (Y) [ ' ] (O) [NXT] (L) [F4] (D) [ALPHA]
[LS] [EVAL] (PRG) [F5] (TYPE) [NXT] [F2] (R→C)

* First of 3 R→C conversions
[ALPHA] [ALPHA] [big X] [F3] (C) [COS] (T) [ √ ] (R)
[SPC] [1/X] (Y) [F3] (C) [COS] (T) [ √ ] (R) [ALPHA]
[F2] (R→C) [ - ]
[ALPHA] [RS] [COS] (Θ)
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP) [COS] [RS] [ENTER] (→NUM)
[F2] (SWAP) [+/-] [SIN] [RS] [ENTER] (→NUM)
[LS] [EVAL] (PRG) [F5] (TYPE) [NXT] [F2] (R→C) [ x ] [ENTER]

* Finish the program
[ ' ] [ALPHA] [ALPHA] [EVAL] (N) [F5] (E) [+/-] (W) [F3] (C) [F4] (D)

The completed program:

<< → XOLD YOLD XCTR YCTR Θ
<< XOLD YOLD R→C XCTR YCTR R→C - Θ DUP COS →NUM SWAP NEG SIN →NUM R→C * >> >>


Instructions:

1. Set up the stack like this:

Level 5: original x coordinate (XOLD)
Level 4: original y coordinate (YOLD)
Level 3: new center - x coordinate (XCTR)
Level 2: new center - y coordinate (YCTR)
Level 1: angle of rotation, counterclockwise (Θ)

2. Run NEWCD

Examples:

All the examples are in degrees mode.

1. Original coordinates: (1,1)
New center: (3,3)
Angle: 45º

Stack:

Level 5: 1
Level 4: 1
Level 3: 3
Level 2: 3
Level 1: 45

Result: (-2.82842712475, 0)

2. Original coordinates: (2, 0)
New center: (0, 0)
Angle: 90º
(pure rotation)

Stack:

Level 5: 2
Level 4: 0
Level 3: 0
Level 2: 0
Level 1: 90

Result: (0, -2)

3. Original coordinates: (4, 2)
New center: (-1, 5)
Angle: 0º
(pure translation)

Stack:

Level 5: 4
Level 4: 2
Level 3: -1
Level 2: 5
Level 1: 0

Result: (5, -3)

The program SCTR

This program takes data from two lists, with a list of x-coordinates listed on Level 2 and a list of corresponding y-coordinates listed on Level 1.

[RS] [ + ] ( << >> )
[RS] 0 (→) [big X] [SPC] [ALPHA] [1/X] (Y)
[RS] [ + ] ( << >> )
1 [SPC] [big X]
[LS] [EVAL] (PRG) [F5] (LIST) [F1] (ELEM) [F5] (SIZE)
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F4] (FOR)

* Inserts the FOR-NEXT structure
[ALPHA] [STO] (K) [SPC] [big X] [ALPHA] [STO] (K)
[LS] [EVAL] (PRG) [F6] (LIST) [F1] (ELEM) [F1] (GET)
[ALPHA] [1/X] (Y) [SPC] [ALPHA] [STO] (K) [F1] (GET)
[LS] [EVAL] (PRG) [F5] (TYPE) [NXT] [F2] (R→C)
[LS] [EVAL] (PRG) [NXT] [F2] (PICT) [NXT] [F1] (PIXON) [ ↓ ]

* PIXON turns a pixel on the picture screen
[RS] [SYMB] (CAT) [ALPHA] [F4] (D) find DRAX [F6] (OK)
[RS] [SYMB] (CAT) [ALPHA] [NXT] (L) find LABEL [F6] (OK)

* LABEL labels the axes
[RS] [SYMB] (CAT) [ALPHA] [SYMB] (P) find PICTURE [ENTER]
* Finish the program

[ ' ] [ALPHA] [ALPHA] [SIN] (S) [F3] (C) [COS] (T) [ √ ] (R) [ENTER] [STO>]

The completed program:

<< → X Y
<< 1 X SIZE
FOR K X K GET Y K GET R→C PIXON
NEXT DRAX LABEL PICTURE >> >>


Instructions:

1. Enter a list of x-coordinates.
2. Enter a list of corresponding y-coordinates.
3. Run SCTR. Adjust the plot screen if necessary primary to running SCTR.

Example:

Here is a plot of the following coordinates:

(-4, 2)
(-3, 3)
(-2, 4)
(-1, 1)
(0, 0)
(1, 1)
(2, 4)
(3, 3)
(4, 2)

Stack:

Level 2: {-4, -3, -2, -1, 0, 1, 2, 3, 4}
Level 1: {2, 3, 4, 1, 0 ,1 4, 3, 2}

That concludes Part 14. I hope you are having as much fun learning about RPL program as I am typing the tutorials.


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

RPL Programming Tutorial - Part 13 - HP 49g+/50g: CST Menu and Quadratic Fit

Custom Menus

This may be Part 13 of our RPL tutorial, but it is not going to be an unlucky section (knock on wood). Today, we will create a custom menu and use it for programming.

The calculator contains a custom menu, which can contain as many commands as you want, and whatever commands and programs you want. The custom menu can be changed at any time to fit your needs.

The contents of the menu is a list of contents stored in the variable CST. The custom menu can be accessed by the key sequence [LS] [MODE] (CUSTOM). Best of all, the custom menu can be accessed during program.

To store the custom menu:

1. Have the menu ready at Level 1 on the stack.
2. Press [ ' ] [LS] [EVAL] (PRG) [NXT] [F4] (MODES) [F5] (MENU) [F2] (CST).
3. Press [STO>].

In the program QREG (Quadratic Regression), we will develop a custom menu to assist us.

The QREG takes a two-column matrix, with the first column acting as the x-variable (dependent) and the second column acting as the y-variable (independent) and attempting to find a quadratic fit (see the equation below) of the data by the least-squares method.

y = a + b x + c x^2

The coefficients are found by solving the system of normal equations:

n * a + ∑x * b + ∑x^2 * c = ∑y
∑x * a + ∑x^2 *b + ∑x^3 * c = ∑xy
∑x^2 * a + ∑x^3 * b + ∑x^4 * c = ∑x^2y

Please be aware that QREG is a large program; please take your time with entry. I recommend that you set up the suggested custom menu first - it will save a lot of keystrokes during the entry of the main program.

Source Used: Murray R. Spiegel, Ph. D., John Liu, Ph. D., and Seymour Lipschutz, Ph. D. "Schaum's Outlines: Mathematical Handbook of Formulas and Tables" 2 ed. McGraw-Hill, 1999

The Program QREG

We are going to do things in a little different order in Part 13, starting with the finished program and then let's deal with the keystrokes needed.

<< ->COL DROP SWAP DUP AXL SQ AXL
OVER AXL 3 ^ AXL
3 PICK AXL 4 ^ AXL
5 ROLL
DUP AXL 6 PICK AXL * AXL
2 PICK AXL 6 PICK AXL * AXL
7 COL-> STO∑ CLEAR
N∑
TOT 1 GET
TOT 2 GET
TOT 5 GET
4 ->LIST AXL
TOT 1 GET
TOT 2 GET
TOT 3 GET
TOT 6 GET
4 ->LIST AXL
TOT 2 GET
TOT 3 GET
TOT 4 GET
TOT 7 GET
4 ->LIST AXL
3 ROW-> RREF 4 COL- >>


Note that the commands AXL, STO∑, TOT, PICK, GET, and &#8594LIST appear frequently. Let's make a custom menu with these six commands before we start programming QREG.

Setting Up the Custom Menu

[LS] [ + ] ( { } )
* Start the list
[LS] 6 [F5] (MATRX) [F1] (AXL)
* The first command AXL
[RS] [SYMB] (CAT) [ALPHA] [SIN] (S) find STO∑ [F6] (OK)
* The second command STO∑
[RS] [SYMB] (CAT) [ALPHA] [COS] (T) find TOT [F6] (OK)
* The third command TOT
[LS] [EVAL] (PRG) [F1] (STACK) [NXT] [F3] (PICK)
* The fourth command PICK
[LS] [EVAL] (PRG) [F6] (LIST) [F1] (ELEM) [F1] (GET)
* The fifth command GET
[NXT] [F6] (LIST) [F4] (->LIST)
* The sixth command &#8594LIST, going from the current menu
[LS] [EVAL] (PRG) [NXT] [F4] (MODES) [F5] (MENU) [ ' ] [F2] (CST) [ENTER] [STO>]
* Stores the custom menu.

The commands in the custom menu:

AXL: a toggle. AXL changes a list to a vector. AXL changes a vector to a list.
STO∑: stores a matrix into the variable ∑DAT. ∑DAT is used for statistical analysis.
TOT: Totals each column of ∑DAT.
PICK: Picks a certain level from the stack. Syntax: n PICK
GET: Gets or retrieves an element of a list, vector, or matrix. Syntax: list n GET
->LIST: Takes n levels from the stack and creates a list. Syntax: n &#8594LIST

The custom menu will set up with the soft keys:

[F1] (AXL)
[F2] (STO∑)
[F3] (TOT)
[F4] (PICK)
[F5] (GET)
[F6] (->LIST)

Other Commands Used

->COL: Matrix to Columns command. Extracts a column from a matrix.
COL->: Columns to Matrix command. Transfers a given set of column vectors to a matrix.
ROW->: Rows to Matrix command. Creates a matrix from a given set of row vectors.
RREF: Calculates the row-reduced-echelon form of a matrix.


Setting up ∑DAT

In order to get ∑x, ∑x^2, ∑x^3, ∑x^4, ∑y, ∑xy, and ∑x^2y, the statistics matrix ∑DAT is set up as:

Column 1: x-data
Column 2: x-data^2
Column 3: x-data^3
Column 4: x-data^4
Column 5: y-data
Column 6: x-data * y-data
Column 7: x-data^2 * y-data

TOT will be used to extract the sums of the columns.

Keystrokes - Main Program (QREG)

This assumes that you have the custom menu set up as discussed above.


[RS] [ + ] (<< >>)
[LS] 5 (MATRICES) [F1] (CREAT) [F1] (COL) [F1] (->COL)
[LS] [EVAL] (PRG) [F1] (STACK) [F3] (DROP) [F2] (SWAP) [F1] (DUP)
[LS] [MODE] (CUSTOM) [F1] (AXL) [LS] [ √ ] (x^2) [F1] (AXL)

* First of many instances the custom menu is used - building the x-data^2 column
[LS] [EVAL] (PRG) [F1] (STACK) [F4] (OVER)
[LS] [MODE] (CUSTOM) [F1] (AXL) 3 [y^x] [F1] (AXL)

* Building the x-data^3 column
3 [F4] (PICK) [F1] (AXL) 4 [y^x] [F1] (AXL)
* Building the x-data^4 column
5 [LS] [EVAL] (PRG) [F1] (STACK) [NXT] [F1] (ROLL)
[LS] [NXT] (PREV)

* PREV goes to the previous page of a menu
[F1] (DUP)
[LS] [MODE] (CUSTOM) [F1] (AXL) 6 [F4] (PICK) [F1] (AXL) [ x ] [F1] (AXL)

* Building the x-data * y-data and x-data^2 * y-data columns 

2 [LS] [MODE] (CUSTOM) [F4] (PICK)
[F1] (AXL) 6 [F4] (PICK) [F1] (AXL) [ x ] [F1] (AXL)
* Thanks to Alan Jones for pointing out to me that I was missing this point in the instruction. (added 12/30/2015)

7 [LS] 5 (MATRICES) [F1] (CREAT) [F1] (COL) [F2] (COL&#8594)
* Build the matrix from the 7 column vectors
[LS] [MODE] (CUSTOM) [F2] (STO∑) [RS] [backspace] (CLEAR)
* Stores the matrix to ∑DAT and clears the stack - the Custom menu (should be) is active
[RS] [SYMB] (CAT) [ALPHA] [EVAL] (N) find N∑ [F6] (OK)
[F3] (TOT) 1 [F5] (GET)
[F3] (TOT) 2 [F5] (GET)
[F3] (TOT) 5 [F5] (GET)
4 [F6] (->LIST) [F1] (AXL)

* First row of the normal equations is formed. The next two equations are formed in the similar way.
[F3] (TOT) 1 [F5] (GET)
[F3] (TOT) 2 [F5] (GET)
[F3] (TOT) 3 [F5] (GET)
[F3] (TOT) 6 [F5] (GET)
4 [F6] (->LIST) [F1] (AXL)

* Second normal equation
[F3] (TOT) 2 [F5] (GET)
[F3] (TOT) 3 [F5] (GET)
[F3] (TOT) 4 [F5] (GET)
[F3] (TOT) 7 [F5] (GET)
4 [F6] (->LIST) [F1] (AXL)

* The third and final normal equation
3 [LS] 5 (MATRICES) [F1] (CREAT) [F2] (ROW) [F2] (ROW->)
* Form the matrix from the three row vectors
[LS] 5 (MATRICES) [F5] (LIN S) [F4] (RREF)
* Row-reduced echelon form
4 [LS] 5 (MATRICES) [F1] (CREAT) [F1] (COL) [F4] (COL-) [ENTER]
* Extract the column [a, b, c] and terminates program entry.
[ ' ] [ALPHA] [ALPHA] [y^x] (Q) [ √ ] (R) [F5] (E) [APPS] (G) [ENTER] [STO>]

Instructions:

1. Enter a two column matrix. One way to enter matrices is to use the calculator's matrix writer by pressing the sequence [LS] [ ' ] (MTRW). To enter the matrix, press [ENTER] after entering the last element. To cancel matrix entry, press [ON].
2. Run QREG.

Results:

Level 2: Matrix
Level 1: Coefficients Vector

Coefficients vector [ a b c ] represents:

y = a + b * x + c * x^2

If the matrix is an identity matrix of size 3 ([[1,0,0][0,1,0][0,0,1]]), then a definite solution has been found.
Example:

Fit a quadratic curve to this data:

(1, 1)
(2, 3.6)
(3, 8.9)
(4, 17)
(5, 26.5)

So the matrix that is needed is:

[[1 1]
[2 3.6]
[3 8.9]
[4 17]
[5 26.5]]

To four decimal places QREG returns:

Level 2: [[1 0 0][0 1 0][0 0 1]]
Level 1: [.3800 -.6743 1.1857]

That means the quadratic curve that best fits the data is:

y ≈ 0.38 - 0.6743x + 1.1857x^2

Have fun fitting other sets of data to quadratic equations. Next time I will present additional programming examples. Keep on programming, Eddie!

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

RPL Programming Tutorial - Part 12 - HP 49g+/50g: Subroutines and the START Loop

Subroutines

Welcome to Part 12 of the RPL Tutorial. In Part 12, we will cover subroutines. A subroutine is a named routine defined in the program, which is saved for later use. the subroutine is called whenever the need arises.

One way to approach subroutines is to type and store the subroutines at the beginning at the program. Unless the user desires to store the subroutine permanently, the subroutine must be purged at the end of the program. Here is one template for a program using a subroutine:

<< << subroutine >> 'SUB1' STO
main program
'SUB1' PURGE >>


Using two or more subroutines:

<< < subroutine1 >> 'SUB1' STO
<< subroutine2 >> 'SUB2' STO
...
main program
...
{'SUB1' 'SUB2' ... } PURGE >>


START-NEXT Structure

The START-NEXT structure is like the FOR-NEXT structure, except that the START-NEXT structure does not use a counter. A structure looks like this:

<< start stop START commands NEXT >>

Keystroke sequence: [LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F3] (START)

The program presented in Part 12 will illustrate using a subroutine and a START-NEXT structure.

Building a list of random numbers

In this program, we are going to pick d integers from low (l) to high (h) - allowing the choices to repeat.

The program uses the subroutine named SUB1:

<< H L - RAND x IP L + >>

SUB1 will be called up in the START-NEXT loop.

The other variables used in the program are:

L = low number,
H = high number, and
D = # of draws

To build the list, the stack is cleared, allowing the program to fill it with draws. Once the draws are complete, DEPTH →LIST will be used to create the list.

The Program SAMP

[RS] [ + ] ( << >> )
[RS] [ + ] ( << >> )
[ALPHA] [MODE] (H) [SPC] [ALPHA] [NXT] (L) [ - ]
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND) [ x ]
[LS] [SYMB] (MTH) [F5] (REAL) [NXT] [F5] (IP) [ALPHA] [NXT] (L) [ + ] [ ↓ ]
[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA] [ → ]
[STO>]

* Enters are stores the subroutine SUB1
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [NXT] (L) [ ' ] (O) [+/-] (W) [RS] 3 (?)
* [ALPHA] [RS] 3 sequence gives the question mark character, the ? is not marked on the keyboard
[ALPHA] [ → ] [LS] [EVAL] (PRG) [NXT] [F5] (IN) [NXT] [F1] (PROMP) [ ' ] [ALPHA] [NXT] (L) [ → ] [STO>]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [MODE] (H) [TOOL] (I) [APPS] (G) [MODE] (H) [RS] 3 (?) [ALPHA] [ → ] [F1] (PROMP) [ ' ] [ALPHA] [MODE] (H) [ → ] [STO>]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [LS] 3 (#) [SPC] [F4] (D) [ √ ] (R) [F1] (A) [+/-] (W) [SIN] (S) [RS] 3 (?) [ALPHA] [ → ] [F1] (PROMP) [ ' ] [ALPHA] [F4] (D) [ → ] [STO>] [RS] [backspace] (CLEAR)

* Enters the prompts for H, L, and D.
1 [SPC] [ALPHA] [F4] (D)
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F3] (START)

* Inserts the START-NEXT structure
[ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA]
* Calls the subroutine SUB1
[ ↓ ]
[LS] [EVAL] (PRG) [F1] (STACK) [NXT] [F6] (DEPTH)
[LS] [EVAL] (PRG) [F6] (LIST) [F4] ( →LIST )

* Turns the stack into a list with DEPTH →LIST
[LS] [ + ] ( { } )
[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA] [ → ]
[SPC] [ ' ] [ALPHA] [MODE] (H) [ → ]
[SPC] [ ' ] [ALPHA] [NXT] (L) [ → ]
[SPC] [ ' ] [ALPHA] [F4] (D) [ → ] [ → ] [ → ]
[LS] [EVAL] (PRG) [F2] (MEM) [F1] (PURGE) [ENTER]

* Clean up and entry termination

[ ' ] [ALPHA] [ALPHA] [SIN] (S) [F1] (A) [HIST] (M) [SYMB] (P) [ENTER] [STO>]

The complete program:

<<
<< H L - RAND x IP L + >> 'SUB1' STO
"LOW?" PROMPT 'L' STO
"HIGH?" PROMPT 'H' STO
"# DRAWS?" PROMPT 'D' STO
CLEAR
1 D START SUB1 NEXT
DEPTH →LIST { SUB1 H L D } PURGE >>


Instructions:

1. Run SAMP
2. Enter the low number at the "LOW?" prompt, press [LS] [ON] (CONT)
3. Enter the high number at the "HIGH?" prompt, press [LS] [ON] (CONT)
4. Enter the number of draws at the "# OF DRAWS?" prompt, press [LS] [ON] (CONT)
5. Obtain a list. Use [HIST] [F2] (VIEW) to see the list.

Example:

1. Draw 6 numbers from 100 to 999. A result may be:

{361, 779, 156, 295, 349, 382}

2. Draw 12 2 digit numbers (10 to 99). A result may be:

{52, 63, 83, 76, 52, 54, 71, 47, 70, 16, 23, 41}

This concludes Part 12 of our tutorial. Up next we will make a custom menu and use it to help make programming easier. As always, thank you visiting - Eddie


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

RPL Programming Tutorial - Part 11 - HP 49g+/50g: The CASE Structure and Labeling Results

In this case, do this; in that case, do that...

Part 11 of our tutorial will cover the CASE structure, which is really the IF-THEN-ELSE-END structure repeated more than one time.

The CASE structure looks like this:

CASE test_1 THEN commands if test_1 is true END
test_2 THEN commands if test_2 is true END
test_3 THEN commands if test_3 is true END
...
commands if none of the tests are true (default, optional) END


The default commands are optional. If there is no default commands, the structure ends without any action. Note that CASE is only needed once, and there must always be a final END to this structure. CASE is found in the PRG-BRCH menu.

Key [LS] [F2] (CASE) to get:

CASE
THEN
END
END

Use this key sequence as a starter.

Key [RS] [F2] (CASE) to get:

THEN
END

Use this key sequence to add additional cases. Remember only one CASE is needed per structure.

Labeling Results

Sometimes we want to label results. One way to label results is the relatively easy command →TAG. This command can be accessed by the keystroke sequence:

[LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)

→TAG takes the result from Level 2 and a string (your label) from Level 1. An example would be:

2: 3.14159265359
1: "PI"

Executing →TAG returns:

1: PI:3.14159265359

→TAG automatically adds a colon ( : ) to the label. A tagged object (number) can be used in any calculation like normal. However once a calculation is executed, the tag is lost.

What's my tax?

To illustrate the CASE structure, let's analyze and calculate how much income tax liability taxpayers have in a given state. The state takes the taxable income of a taxpayer and uses this graduated schedule to determine payment:

$0 - $14,999: 5% of taxable income
$15,000 - $99,999: 7% of taxable income exceeding $14,999 plus $1,050
$100,000 and above: 9% of taxable income exceeding $99,999 plus $7,000

Here the tax preparer wants to know how much is the tax liability. In addition, the preparer wants to label both the income and tax liability.

The CASE structure is set up like this:

Case 1: If income < 15,000, then calculate income * 5%
Case 2: If 15,000 ≤ income < 100,000, then calculate income above 14,999 * 7% + 1050, We'll break up the condition for Case 2 this way: 15,000 ≤ income AND income < 100,000.
Otherwise: Calculate income above 99,999 * 9% + 7000

The Program INTAX

[RS] [ + ] (<< >>)
[LS] [SYMB] (MTH) [F5] (REAL) 0 [NXT] [NXT] [F1] (RND)

* Rounds the entry to the nearest integer (number 0 RND)
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F2] (CASE)
* The primary CASE-THEN-END-END structure
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
15000 [RS] [big X] ( < )

* First condition: income < 15,000
[ ↓ ] [F1] (DUP) .05 [ x ]
* First calculation
[ ↓ ]
[F1] (DUP) [F1] (DUP) 100000 [RS] [big X] ( < ) [F2] (SWAP) 15000 [LS] [1/X] ( ≥ )
[LS] [EVAL] (PRG) [F4] (TEST) [NXT] [F1] (AND)
[LS] [EVAL] (PRG) [F3] (BRCH) [RS] [F2] (CASE)

* Second condition: 15,000 ≤ income < 100,000 - connect two required conditions with AND
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP) 14999 [ - ] .07 [ x ] 1050 [ + ]
* Second calculation
[F1] (DUP) 99999 [ - ] .09 [ x ] 7000 [ + ]
* Default calculation
[ ↓ ]
* Exits the CASE structure
0 [LS] [SYMB] (MTH) [F5] (REAL) [NXT] [NXT] [F1] (RND)
* Rounds the final result to the nearest integer
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [COS] (T) [F1] (A) [big X] [ALPHA] [ → ]
[LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)

* Apply the tag on the income tax.
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP)
[RS] [ x ] [ALPHA] [ALPHA] [TOOL] (I) [EVAL] (N) [F3] (C) [ ' ] (O) [HIST] (M) [F5] (E) [ALPHA] [ → ] [LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)

* Apply the tag on the income.
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP) [ENTER]
* Finish the program

[ ' ] [ALPHA] [ALPHA] [TOOL] (I) [EVAL] (N) [COS] (T) [F1] (A) [big X] [ENTER] [STO>]

The completed program:

<< 0 RND
CASE DUP 15000 < THEN DUP .05 * END
DUP DUP 100000 < SWAP 15000 ≥ AND THEN
DUP 14999 - .07 * 1050 + END
DUP 99999 - .09 * 7000 + END
0 RND "TAX" →TAG SWAP "INCOME" →TAG SWAP >>


Instructions:

1. Enter the taxable income.
2. Run INTAX

Examples:

1. Income: $13,000; Tax: $650
2. Income: $15,000; Tax: $1,050
3. Income: $50,000; Tax: $3,500
4. Income: $100,000; Tax: $7,000
5. Income: $150,000; Tax: $11,500

This wraps it up for Part 11 - on to Part 12 where we work with a subroutine. Until next time, Eddie.

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

RPL Programming Tutorial - Part 10 - HP 49g+/50g: The DO Loop

Do It Until It's Done!

Welcome to the third week of my RPL tutorials for the HP 49g+ and 50g Hewlett Packard graphing calculators. We will start this batch of tutorials with Part 10: the Do Loop.

The DO-UNTIL-END loop structure works like the WHILE-REPEAT-END loop structure except instead of repeating a bunch of designated commands while a certain condition is met, this time we are repeating a bunch of designated commands until a condition is met.

A DO-UNTIL-END structure looks like this:

DO commands
UNTIL this condition is met
END


Key sequence from the PRG-BRCH menu:

[LS] [F5] (DO)

Let's illustrate a use of a DO-UNTIL-END structure with a simple program: have the calculator generate a bunch of random numbers until the sum of 1 is reached.

It's a new week - so here is a reminder of how I label shift keys:

[RS] is the right shift key, 2nd key up from the ON button on the left side of the keyboard. [RS] is red on the 49g+ and orange on the 50g.

[LS] is the left shift key, 3rd key up from the ON button on the left side of the keyboard. [LS] is green on the 49g+ and white on the 50g.

[ALPHA] is the 4th key up from the ON button on the left side of the keyboard. On both the 49g+ and the 50g, the key is yellow. Pressing [ALPHA] twice puts the calculator in ALPHA-LOCK mode.

[big X] is the "X" key on the 4th row from the bottom, 3rd from the left. This is so I distinguish X from the times key ([ x ]).

Finally, the soft keys labeled F1 - F6 are on the top row.

Add Random Numbers until I get at least 1

This program will keep adding random numbers until the sum exceeds one.

The Program RAND1

Comments are italicized beginning with an asterisk. Here we go!

[RS] [ + ]
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND)

* Inserts the first random number. RAND always enters numbers between 0 and 1.
[LS] [EVAL] (PRG) [F3] (BRCH)
[LS] [F5] (DO)

* Inserts the DO-UNTIL-END structure
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND)
[ + ] [ ↓ ]

* Enter the commands that are to be repeated (RAND + )
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
1 [LS] [1/X] ≥

* Enters the condition (sum≥1)
[ENTER]
* Terminates program entry

[ ' ] [ALPHA] [ALPHA] [ √ ] (R) [F1] (A) [EVAL] (N) [F4] (D) [ENTER] [STO>]

The completed program:

<< RAND
DO RAND +
UNTIL DUP 1 ≥
END >>


Instructions:

Just run RAND1.

Examples of some results you might get: 1.46211570675, 1.12775622211, 1.50610262525

That wraps up Part 10 of the RPL Tutorial Series. Next time, we'll look at the CASE structure. See you then!

Eddie

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

Tuesday, October 11, 2011

RPL Programming Tutorial - Part 9 - HP 49g+/50g: Temporary Menus and a Simple Statistics Application

Make Your Own Menus

Today in Part 9, we will create two programs where menus are created. The first one is a simple program involving free fall. The second program is a more complex menu that allows users to easily enter statistics and perform simple analysis.

Creating menus in RPL is not hard - but you really have to keep track of what you are doing because it can be easy to be lost in the symbols that are involved. These two programs may the ones to insert line breaks (done by [RS] [ . ]) to make program entry easier to see.

The menu structure in RPL is:

{ { "title" { << program >> << left shift program >> << right shift program >> } repeat the last nested list as needed }

A simplified structure is:

{ { "title" << program >>} repeat the last nested list as needed }

In Part 9, we will use the simplified structure in both the programs.

Either of the structures are followed by either MENU or TMENU.

MENU: Creates a user-defined menu and also replaces the calculator's custom menu for future use. To access the custom menu, press [LS] [MODE] (CUSTOM).

TMENU: Works the same as MENU except the user menu does not replace the calculator's custom menu for future use.

The Program FALL

This program calculates the length of a free fall of an object dropped straight down in either feet or meters. The gravitational constant is determined by the desired measurement. Air resistance is ignored in this calculation.

Comments are italicized and start with an asterisk. This program assumes the time (in seconds) is on Level 1.

g = 9.80665 m/s^2 = 32.1740468 ft/s^2

d = -1/2 * t^2 * g

[RS] [ + ] (<< >>) [LS] [ + ] ( { } ) [RS] [ . ]**
* The [RS] [ . ] sequence is the carriage return sequence and is not required. I just put the sequence in here to program entry look better.
[LS] [ + ] ( { } ) [RS] [ . ] [RS] [ x ] ( " " ) [ALPHA] [ALPHA] [F6] (F) [COS] (T) [ALPHA] [ &rarr ] [RS] [ + ] (<< >>)
* Creates the first menu key "FT"
[LS] [ √ ] (x^2) 2 [ ÷ ] 32.1740468 [ x ] [RS] [ENTER] ( &rarr NUM)
* The commands associated with "FT"
[ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ]
* Exits the first nested list, you want to be in between the two right list brackets. ( } )
[LS] [ + ] ( { } ) [RS] [ . ]
[RS] [ x ] ( " " ) [ALPHA] [HIST] (M) [ &rarr ]
* Creates the second menu key "M"
[RS] [ + ] (<< >>) [LS] [ √ ] (x^2) 2 [ ÷ ] 9.80665 [ x ] [RS] [ENTER] (&rarr NUM)
* The commands associated with "M"
[ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ]
* Exits the second nested list and the menu structure.
[LS] [EVAL] (PRG) [NXT] [F4] (MODES) [F5] (MENU) [F3] (TMENU)
* Enters the TMENU command - you can use the MENU command (F1) desired.
[ENTER]
* Terminates program entry

[ ' ] [ALPHA] [ALPHA] [F6] (F) [F1] (A) [NXT] (L) [NXT] (L) [ENTER] [STO>]

The completed program:

<< {
{"FT" << SQ 2 / 32.1740468 * &rarr NUM >>}
{"M" << SQ 2 / 9.80665 * &rarr NUM >>} } TMENU >>


Doesn't look that complicated does it?

Instructions:

1. Enter the time in seconds.
2. Run FALL
3. Select either [F1] for distance in feet, or [F2] for distance in meters.

Example:

I am going to drop an egg from a building. The fall lasts 3 seconds. How far did it go?

In Feet:

Run FALL, 3 [F1] (FT)

Result: 144.7832106 feet

In Meters:

Run FALL, 3 [F2] (M)

Result: 44.129925 meters

The Statistics Program

This program will allow the user to easily enter data and calculate the mean, standard deviation, and the sum of the data. The program creates a six key menu where:

* [F1] (CLEAR) clears the statistics data matrix and the stack
* [F2] (∑+) enters the next data point and clears the stack. It also returns the number of data points in the statistics data matrix.
* [F3] (∑-) deletes the latest data point and clears the stack. It also returns the number of data points in the statistics data matrix.
* [F4] (MEAN) calculates the mean of each column of the statistics data matrix.
* [F5] (SDEV) calculates the standard deviation of each column of the statistics data matrix.
* [F6] (SUM) calculates the sum of each column of the statistics data matrix.

The statistics data matrix is the system variable ∑DAT.

The Program STAT1

Note:

The key sequence [RS] [ . ] inserts a carriage return to improve readability of program entry and is not required.

[RS] [ + ] (<< >>) [LS] [ + ] ( { } ) [RS] [ . ]
[LS] [ + ] ( { } ) [RS] [ . ]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [F3] (C) [NXT] (L) [SIN] (S) [COS] (T) [ALPHA] [ &rarr ]
[RS] [ + ] (<< >>) [RS] [SYMB] (CAT) [ALPHA] [F3] (C) find CL∑ [F6] (OK) [RS] [backspace] (CLEAR)
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [F1] (A) [NXT] (L) [NXT] (L) [SPC] [F3] (C) [NXT] (L) [F5] (E) [F1] (A) [ √ ] (R)

* The first menu item "CLST" and its instructions
[ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ]
* To get in between the two right list brackets } }
[LS] [ + ] ( { } ) [RS] [ . ]
[RS] [ x ] ( " " ) [RS] [SIN] (∑) [ &larr ] [ &larr ] [backspace] [ &rarr ] [ALPHA] [ + ]
* To type "∑+ "
[ &rarr ] [ &rarr ] [RS] [ + ] (<< >>) [RS] [SYMB] (CAT) [RS] [SIN] (∑) find ∑+ [F6] (OK)
[RS] [backspace] (CLEAR)
[RS] [SYMB] (CAT) [ALPHA] [EVAL] (N) find N∑ [F6] (OK)

* The second menu item "∑+" and its instructions
[ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ]
* To get in between the two right list brackets } }
[LS] [ + ] ( { } ) [RS] [ . ]
[RS] [ x ] ( " " ) [RS] [SIN] (∑) [ &larr ] [ &larr ] [backspace] [ &rarr ] [ALPHA] [ - ] [ &rarr ] [ &rarr ]
[RS] [ + ] ( << >>) [RS] [SYMB] (CAT) [RS] [SIN] (∑) find ∑- [F6] (OK) [RS] [backspace] (CLEAR)
[RS] [SYMB] (CAT) [ALPHA] [EVAL] (N) find N∑ [F6] (OK)

* The third menu item "∑-" and its instructions
[ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ]
* To get in between the two right list brackets } }
[LS] [ + ] ( { } ) [RS] [ . ]
[RS] [ x ] [ALPHA] [ALPHA] [HIST] (M) [F5] (E) [F1] (A) [EVAL] (N) [ALPHA]
[ &rarr ] [RS] [ + ] (<< >>) [RS] [SYMB] (CAT) [ALPHA] [HIST] (M) find MEAN [F6] (OK)

* The fourth menu item "MEAN" and its instructions
[ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ]
* To get in between the two right list brackets } } - just two menu commands to go!
[LS] [ + ] ( { } ) [RS] [ . ]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [SIN] (S) [F4] (D) [F5] (E) [EEX] (V) [ALPHA]
[ &rarr ] [RS] [ + ] (<< >>) [RS] [SYMB] (CAT) [ALPHA] [SIN] (S) find SDEV [F6] (OK)

* The fifth menu item "SDEV" and its instructions
[ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ]
* To get in the between the two right list brackets } } - last time for this program
[LS] [ + ] ( { } ) [RS] [ . ]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [HIST] (M) [ALPHA]
[ &rarr ] [RS] [ + ] (<< >>) [RS] [SYMB] (CAT) [ALPHA] [COS] (T) find TOT [F6] (OK)

* The sixth (and final) menu item "SUM" and its instructions
[ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ] [ &rarr ]
* To get past the two right list brackets } }
[LS] [EVAL] (PRG) [NXT] [F4] (MODES) [F5] (MENU) [F3] (TMENU) [ENTER]
* Enter the TMENU command and terminate program entry

[ ' ] [ALPHA] [ALPHA] [SIN] (S) [COS] (T) [F1] (A) [COS] (T) 1 [ENTER] [STO>]

The completed program:

<< {
{ "CLST" << CL∑ CLEAR "ALL CLEAR" >> }
{ "∑+" << ∑+ CLEAR N∑ >> }
{ "∑-" << ∑- CLEAR N∑ >> }
{ "MEAN" << MEAN >> }
{ "SDEV" << SDEV >> }
{ "TOT" << TOT >> } } TMENU >>


Instructions:

1. Run STAT1
2. Use the following soft keys for the following functions:

* [F1] (CLEAR) clears the statistics data matrix and the stack
* [F2] (∑+) enters the next data point and clears the stack. It also returns the number of data points in the statistics data matrix. For entering multi-variate data, see the hint below.
* [F3] (∑-) deletes the latest data point and clears the stack. It also returns the number of data points in the statistics data matrix.
* [F4] (MEAN) calculates the mean of each column of the statistics data matrix.
* [F5] (SDEV) calculates the standard deviation of each column of the statistics data matrix.
* [F6] (SUM) calculates the sum of each column of the statistics data matrix.

Hint:

In STAT1, you can enter multivariate data. To do so, first clear the statistics matrix. Then for each point, press [LS] [ x ] ( [ ] ). Separate each point with a comma or space. The press [F2] (∑+). You can operate the other commands (∑-, MEAN, SDEV, SUM) like normal.

Example:

A research company compiled retrieved a list of the last 15 winning amounts from a popular, long-running game show. The company wants to find the average winning amount, it's deviation, and the total amount won.

Data:
$ 35,000
$ 27,000
$ 15,000
$ 16,000
$ 9,000
$ 7,500
$ 34,600
$ 18,800
$ 24,300
$ 40,000
$ 11,500
$ 3,800
$ 17,600
$ 25,000
$ 35,000

Keystrokes:

Remember you can remove the last data point entered by pressing [F3] (∑-).

Run STAT1.
[F1] (CLST) - display says "ALL CLEAR"
35000 [F2] (∑+) - displays says 1
27000 [F2] (∑+) - displays says 2
15000 [F2] (∑+) - displays says 3
16000 [F2] (∑+) - displays says 4
9000 [F2] (∑+) - displays says 5
7500 [F2] (∑+) - displays says 6 (6 data entry points so far)
34600 [F2] (∑+) - displays says 7
18800 [F2] (∑+) - displays says 8
24300 [F2] (∑+) - displays says 9
40000 [F2] (∑+) - displays says 10
11500 [F2] (∑+) - displays says 11
3800 [F2] (∑+) - displays says 12
17600 [F2] (∑+) - displays says 13
25000 [F2] (∑+) - displays says 14
35000 [F2] (∑+) - displays says 15
[F4] (MEAN) - $21,340 is the average amount won.
[F5] (SDEV) - $11,303.7793933 is the standard deviation
[F6] (SUM) - $321,000 is the total winnings

This ends Part 9 of the RPL Programming Tutorial. Until next time, Eddie.


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

RPL Programming Tutorial - Part 8 - HP 49g+/50g: The WHILE Loop

While We Are Here...

Welcome to Part 8 of the RPL Programming Tutorial. Today, we will cover the WHILE-REPEAT-END programming structure. This structure is similar to the WHILE-WEND programming structure in BASIC. In this loop certain commands are repeated while a condition is true.

The WHILE-REPEAT-END structure looks like this:

WHILE this condition is met
REPEAT these instructions
END


The Program DBY2

This program takes the absolute number from the stack. As long as the number is greater than 1, the program keeps dividing the number by 2.

Note the use of DUP in this program. The DUP (Duplicate) command duplicates the contents of Level 1. It is necessary because tests consume the contents of levels 2 and 1.

[RS] [ + ] (<< >>)
[LS] [ ÷ ]
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)

* The DUP command
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F6] (WHILE)
* Enters the WHILE-REPEAT-END structure
1 [LS] [1/X] ≥
* Enters the while condition
[ &darr ] 2 [ ÷ ] [LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
* Enters the loop instructions
[EVAL] [ENTER]
* Simplifies the number and terminates program entry

[ ' ] [ALPHA] [ALPHA] [F4] (D) [F2] (B) [1/X] (Y) 2 [ENTER] [STO>]

The Complete Program:

<< ABS DUP
WHILE 1 ≥
REPEAT 2 / DUP
END EVAL >>


Instructions:

1. Enter a number.
2. Run DBY2.

Examples:

10 returns 5/8
96 returns 3/4
-105 returns 105/128

This concludes Part 8 of our tutorial. Next time, we will create custom menus. It's going to be a lot of fun! Until next time, Eddie.


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

RPL Programming Tutorial - Part 7 - HP 49g+/50g: Solver

How Do You Solve This Thing Again?

Welcome to Part 7 of the RPL Tutorials for the HP 49g+ and 50g calculators. Today, we are going to call the calculator's solver in a program. It is a fairly simple process:

1. The first step is setup the equation. We can set up the equation either as an Algebraic object or an RPN program.
2. We follow the equation by the commands STEQ 30 MENU. STEQ (Store Equation) stores the equation for solving purposes. The commands 30 MENU brings up a soft-key menu solver. The soft-key menu solver is a classic solver that was used in the HP 48S and HP 48G series.

In this program, we will use the classic solver to expand the capability of the UTPN (Upper Tail Probability Normal Distribution) function.

UTPN

The UTPN function returns the upper tail area (p) of the normal distribution given the mean (µ), variance (v), and point on the curve (x).

Mathematically,

p = (√2 π v) ^ -1 * ∫(x, ∞, e^( -(t - µ)^2 / (2v) ), t)

In a standard normal distribution, µ = 0 and v = 1, so the equation simplifies to:

p = (√2 π) ^ -1 * ∫(x, ∞, e^(-t^2/2), t)

where x = (z - µ)/v

A Note Using 30 MENU

The variables using 30 MENU are global. Therefore, it may be a wise idea to purge each of the variables after use. In this tutorial, we will create a second program to clean up the variables - this program is optional.

The Program NSLVR (Normal Distribution Solver)

Comments will be italicized, starting with an asterisk.

Variables used:
M = the mean
V = the variance
X = the point
P = the upper tail area, probability, -1 ≤ p ≤ 1

[RS] [ + ] (<< >>)
[RS] [ + ] (<< >>)

* To use an RPN string in enter an equation, a second set of program brackets is necessary.
[ALPHA] [ALPHA] [HIST] (M) [SPC] [EEX] (V) [SPC] [big X] [SPC] [ALPHA]
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [NXT] [F3] (UTPN)
[ALPHA] [SYMB] (P) [SPC] [ - ]

* Enter the equation UTPN(M,V,X) = P
[ &darr ] [RS] [SYMB] (CAT) [ALPHA] [SIN] (S) find STEQ
30 [LS] [EVAL] (PRG) [NXT] [F4] (MODES) [F5] (MENU) [F1] (MENU)

* Enter STEQ 30 MENU
[ENTER]
* Terminate program entry

[ ' ] [ALPHA] [ALPHA] [EVAL] (N) [SIN] (S) [NXT] (L) [EEX] (V) [ √ ] (R) [ENTER] [STO>]

The completed program:

<<
<< M V X UTPN - >>
STEQ 30 MENU >>


(Optional) The program NSCLR (Normal Distribution Solver Cleaner)

[RS] [ + ] (<< >>)
[LS] [ + ] ( { } )

* Start a list
[ALPHA] [ALPHA] [HIST] (M) [SPC] [EEX] (V) [SPC] [big X] [SPC] [SYMB] (P) [ALPHA] [ &rarr ] [LS] [EVAL] (PRG) [F2] (MEM) [F1] (PURGE)
* Enters the variables used in NSLVR and purges them
[SPC] [RS] [ x ] [ALPHA] [ALPHA] [F4] (D) [ ' ] (O) [EVAL] (N) [F5] (E) [ENTER]
* Inserts a "DONE" message and ends program entry.

[ ' ] [ALPHA] [ALPHA] [EVAL] (N) [SIN] (S) [F3] (C) [NXT] (L) [ √ ] (R) [ENTER] [STO>]

The completed program:

<< { M V X P } PURGE "DONE" >>

Instructions for NSLVR:

To solve for upper tail area (P):
1. Run NSLVR.
2. Enter M, press [F1]
3. Enter V, press [F2]
4. Enter X, press [F3]
5. Press [LS] [F4] (P)

Examples:

1. Find the upper tail area with M = 0, V = 1, and X = 1.

Keystrokes: 0 [F1] (M) 1 [F2] (V) 1 [F3] (X) [LS] [F4] (P)

Result: 0.158655253931

2. Find the area between X = 5 and X = 15, using M = 10 and V = 5.8.

Keystrokes: 10 [F1] (M) 5.8 [F2] (V) 15 [F3] (X) [LS] [F4] (P)
* Area to the right of X = 15 (15 to ∞)
5 [F3] (X) [LS] [F4] (P)
* Area to the right of X = 5 (5 to ∞)
[LS] [ &rarr ] [ - ]
* Subtract the area above X = 5

Result: 0.962118717684

3. Find the lower tail area with M = 0, V = 1, and X = 0.

Keystrokes: 0 [F1] (M) 1 [F2] (V) 0 [F3] (X) [LS] [F4] (P)

Result: 0.5

To solve for the point (X): [Inverse Normal Distribution Function]
1. Run NSLVR.
2. Enter M, press [F1]
3. Enter V, press [F2]
4. Enter P, press [F4]
5. Press [LS] [F3] (X)

Examples:

1. Find X when the lower tail probability is P = 0.95. Use M = 0 and V = 1.

Keystrokes: 0 [F1] (M) 1 [F2] (V) 1 [ENTER] .95 [ - ] [F4] (P) [LS] [F3] (X)

Result: 1.64485362695

Remember: P is the upper tail area.

2. With M = 44.2 and V = 1.7, find the point where the upper tail probability is 55% (P = 0.55).

Keystrokes: 44.2 [F1] (M) 1.7 [F2] (V) .55 [F4] (P) [LS] [F3] (X)

Result: 44.0361576491

To clean up: Run NSCLR

That wraps up Part 7 of our Tutorial. Please join us next time while we explore the WHILE-REPEAT-END structure. So long for now, Eddie.

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




RPL Programming Tutorial - Part 6 - HP 49g+/50g: Algebra & Calculus

Algebra & Calculus

Welcome to Part 6. Today we are going to take a break from programming and learn more about some of the cool stuff we can do with algebraic objects. Part 5 gave an introduction to algebraic objects.

Recall that algebraic objects are functions or expressions that are enclosed in single quotes. Examples include:

'A=B+C'
'√5'
'SIN(X+π/2)'

Expressions can usually be entered in one of three ways:

1. As a string of RPN commands
2. As an algebraic expression entered between single quotes
3. From the Equation Writer ([RS] [ ' ] (EQW))

When programming, generally only the first two methods are available.

Set Up: ** HP 49g+ and 50g **

For Part 6, we will use X as the CAS variable. You can tell if it is by the 'X' indicator at the top of the screen. If it doesn't (i.e. it reads 'T' or 'N', or something else), follow the procedure below.

To set X as the CAS variable:
1. Press [MODE].
2. Press [F3] (CAS).
3. Type [ ' ] [big X] at the Indep Var prompt. Press [F6] (OK) twice.

To clear X:
1. Type [ ' ] [big X] [ENTER].
2. Press [TOOLS] [F5] (PURGE).

Now we're ready to go!

The important EVAL

The [EVAL] key simplifies expressions. Use this key to force the calculator to simplify the expressions (and make them look nice).

Comma

Sometimes commands have more than one argument. If you are entering commands with multiple required arguments, use a comma. The keystroke for a comma is [RS] [SPC] ( , ).

Note: [big X] is located on the 5th rows of keys up from the bottom. I will use this to distinguish the X character from the times key [ x ].

Some Common Algebra CAS Commands

EXPAND: Rewrites algebraic expressions by distributing powers over sums and multiplication over addition and subtraction.

Keystrokes: [SYMB] [F1] (ALG) [F1] (EXPAN)

Example: Expand (x + 2)^5.

By RPN Entry: [big X] [ENTER] 2 [ + ] 5 [y^x] [SYMB] [F1] (ALG) [F1] (EXPAN) [EVAL]

By Algebraic Object: [ ' ] [LS] [ - ] (parenthesis) [big X] [ + ] 2 [ &rarr ] [y^x] 5 [ENTER] [SYMB] [F1] (ALG) [F1] (EXPAN) [EVAL]

Result: x^5 + 10x^4 + 40x^3 + 80x^2 + 80x + 32
(Use [HIST] [F2] (VIEW) or [ &uarr ] [F2] (VIEW) to see the entire result as needed.)

FACTOR: Attempts to factor the expression.

Keystrokes: [SYMB] [F1] (ALG) [F2] (FACTO)

Example: Factor x^2 + 5x + 6.

By RPN Entry: [big X] [ENTER] [ENTER] [LS] [ √ ] (x^2) [LS] [ &rarr ] (SWAP) 5 [ x ] [ + ] 6 [ + ] [EVAL] [SYMB] [F1] (ALG) [F2] (FACTO)

By Algebraic Object: [ ' ] [big X] [y^x] 2 [ + ] 5 [ x ] [big X] [ + ] 6 [ENTER] [SYMB] [F1] (ALG) [F2] (FACTO)

Result: (x + 2)(x + 3)

SUBST: Substitutes an expression for a given variable.

Stack Setup:
Level 2: expression
Level 1: 'variable = value'

Keystrokes: [SYMB] [F1] (ALG) [F5] (SUBST)

Example: Given the function f(x) = x^2 + e^-x, find a numerical approximation of f(1). Remember, you find numeric approximations by the [RS] [ENTER] (&rarr NUM) sequence.

By RPN Entry: [big X] [LS] [ √ ] (x^2) [big X] [+/-] [LS] [y^x] (e^x) [ + ] [big X] [ENTER] 1 [RS] [+/-] (=) [SYMB] [F1] (ALG) [F5] (SUBST) [RS] [ENTER] (&rarr NUM)

By Algebriac Object: [ ' ] [big X] [y^x] 2 [ + ] [LS] [y^x] (e^x) [+/-] [big X] [ENTER]
[ ' ] [big X] [RS] [+/-] (=) 1 [ENTER] [SYMB] [F1] (ALG) [F5] (SUBST) [RS] [ENTER] (&rarr NUM)


Result: 1.36787944117

PROPFRAC: Gives the proper fraction of a rational expression p(x)/q(x). p(x) and q(x) are not necessarily polynomials.

Keystrokes: [SYMB] [F2] (ARITH) [F5] (PROPF)

Example: Find the proper fraction of (x^3 + 2x + 1)/(x - 5).

By RPN Entry: [big X] [ENTER] 3 [y^x] 2 [ENTER] [big X] [ x ] [ + ] 1 [ + ] * (numerator) [big X] [ENTER] 5 [ - ] * (demoniator) [ ÷ ] [SYMB] [F2] (ARITH) [F5] (PROPF)

By Algebriac Object:
Note: We can divide objects (and use other mathematical operators), so let's enter the numerator and denominator separately. Obviously, you can use parenthesis and enter the entire expression as one.
[ ' ] [big X] [y^x] 3 [ + ] 2 [ x ] [big X] [ + ] 1 [ENTER]
[ ' ] [big X] [ - ] 5 [ENTER]
[ ÷ ] [SYMB] [F2] (ARITH) [F5] (PROPF)


Result: x^2 + 5x + 27 + 136/(x-5)

Some Common Calculus CAS Commands

DERVX: First derivative of any expression where X is the independent variable (assumes you have X set up as the Independent Variable)

Keystrokes: [SYMB] [F3] (CALC) [F2] (DERVX)

Example: Find ∂/∂x sin(x^2 + 1)

By RPN: [big X] [LS] [ √ ] (x^2) 1 [ + ] [SIN] [SYMB] [F3] (CALC) [F2] (DERVX) [EVAL]

By Algebraic Object: [ ' ] [SIN] [big X] [y^x] 2 [ + ] 1 [ENTER] [SYMB] [F3] (CALC) [F2] (DERVX) [EVAL]

Result: 2x cos(x^2 + 1)

: First Derivative to any variable.

Stack Setup:
Level 2: Expression
Level 1: 'Variable'

Keystrokes: [RS] [COS] ( ∂ )

As an algebraic object: ' ∂variable(expression) ' Use [EVAL] to evaluate.

Example: Find ∂/∂T cos(T^2 + π/T)

Get the stack to read:
Level 2: 'COS(T^2+π/T)'
Level 1: 'T'

Press [RS] [COS] (∂) [EVAL]

Result: ((π - 2T^3)*sin((π + T^3)/T)/T^2

Alternatively:
Level 1: '∂T(COS(T^2+π/T))'
[RS] [COS] ( ∂ ) [EVAL]

INTVX: Symbolic indefinite integral of any expression where X is the independent variable (assumes you have X set up as the independent variable)

Keystrokes: [SYMB] [F3] (CALC) [F4] (INTVX)

Example: Find ∫ e^(-5x) dx.

By RPN: 5 [+/-] [ENTER] [big X] [ x ] [LS] [y^x] (e^x) [SYMB] [F3] (CALC) [F4] (INTVX)

By Algebraic Object: [ ' ] [LS] [y^x] (e^x) [+/-] 5 [ x ] [big X] [ENTER] [SYMB] [F3] (CALC) [F4] (INTVX)

Result: e^(-5x)/-5

: Integration using lower and upper limits. Use this when you want to execute definite integrals. You can use any variable of integration with ∫.

Stack Setup:
Level 4: lower limit
Level 3: upper limit
Level 2: integrand
Level 1: variable of integration

Keystrokes: [RS] [TAN] ( ∫ )

As an algebraic object: ' ∫(lower,upper,integrand,variable of integration) ' Use [EVAL] to evaluate.

Example: Find the definite integral of x^2 -2 from 0 to 2.

Get the stack to read:
4: 0
3: 2
2: 'X^2 - 2'
1: 'X'

Press [RS] [TAN] ( ∫ )

Result: -4/3

Alternatively:
1: ' ∫(0,2,X^2-2,X) '
[RS] [TAN] ( ∫ )

Sums and Roots

To alternate between Real and Complex Mode

Press and HOLD [LS], then press [TOOL]. A "R=" indicator states that you are operating in the Real mode. A "C=" indicator states that you are operating in the Complex mode.

You can also press [MODE] [F3] (CAS) and check (or uncheck) Complex and press [F6] (OK) twice.

The imaginary character has the sequence [LS] [TOOL] (i). You must put a multiplication sign between the imaginary part and i. Example: 5*i

ZEROS: Finds the roots of an expression in terms of any variable.

Stack Setup:
Level 2: 'expression (don't put an equals sign in it)'
Level 1: 'variable to solve for'

Here we are assuming that the expression equals 0.

Keystrokes: [SYMB] [F5] (SOLVE) [F6] (ZEROS)

Example: Find the roots of x^4 - 1.

Get the stack to read this:
Level 2: 'X^4-1'
Level 1: 'X'
Press [SYMB] [F5] (SOLVE) [F6] (ZEROS)

Result:
{-1, 1} in Real Mode
{i, -1, -i, 1} in Complex Mode

Possible set of keystrokes (by Algebraic Object): [ ' ] [big X] [y^x] 4 [ - ] 1 [ENTER] [ ' ] [big X] [ENTER] [SYMB] [F5] (SOLVE) [F6] (ZEROS)

SOLVEVX: Solves for X in any equation.

Keystrokes: [SYMB] [F5] (SOLVE) [F4] (1st SOLVE)

Example: Find the solution to 2√x + 3 = 5.

Possible set of keystrokes (by Algebraic Object): [ ' ] 2 [ x ] [ √ ] [big X] [ + ] 3 [RS] [+/-] (=) 5 [ENTER] [SYMB] [F5] (SOLVE) [F4] (1st SOLVE)

Result: 1

CAUTION: Not all equations can be solved this way.

SOLVE: Solves an equation for any variable.

Stack setup:
Level 2: equation
Level 1: 'variable'

Keystrokes: [SYMB] [F5] (SOLVE) [F5] (2nd SOLVE)

Try the above example using SOLVE.

: The Summation function. You can calculate finite sums and convergent infinite sums.

Stack Setup:
Level 1: variable (summation index, I recommend X)
Level 2: initial value
Level 3: final value
Level 4: summand, expression

Keystrokes: [RS] [SIN] ( ∑ )

To type infinity: [LS] 0 ( ∞ )

Alternatively: ' ∑(index=initial value,final value,expression) ' [EVAL] (or [RS] [ENTER] for a numerical approximation)

Example: Find the sum of the numbers from 1 to 25.

Get the stack to read this:
Level 1: 'X'
Level 2: 1
Level 3: 25
Level 4: 'X'
Press [RS] [SIN] ( ∑ )

Alternatively:
Level 1: ' ∑(X=1, 25, X)'
Press [RS] [SIN] ( ∑ )

Result: 325

This wraps up Part 6. We get back to programming in Part 7, where we invoke the solver. Thanks for reading, Eddie.


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

RPL Programming Tutorial - Part 5 - HP 49g+/50g: Prompt and Introduction to Algebraic Objects

Excuse me, but I must prompt you for something.

Welcome to the second week of my RPL tutorials, using the Hewlett Packard HP 49g+ and 50g calculators. Today's session will cover the PROMPT command.

The PROMPT command sets the program to ask the user for input. That input can be whatever you practically want: text, numbers, matrices, and equations. The user enters the requested information and presses the key sequence [LS] [ON] (CONT) . The [LS] [ON] sequence tells the calculator that the user is ready to move on.

The PROMPT structure is this:

"String that asks the user for input" PROMPT

Today we will do two programs using PROMPT: a simple program that picks an integer from 1 to N, and a more complex one that takes an equation and graphs it.

In this bag we have lotto balls from 1 to 49, and the first number is...

This program will ask you for a number (N), the number of draws (X), and the calculator will return X integers randomly selected from 1 to N. One caveat in this program is that the numbers selected can repeat.

The Program ONETO

Comments will be italicized, starting with an asterisk. You will not need to pre-load the stack with ONETO - all the inputs will be prompted for.

As a reminder:

[LS] means the left shift key (white on the 50g, green on the 49g+)
[RS] means the right shift key (orange on the 50g, red on the 49g+)
Pressing [ALPHA] twice will put the calculator in ALPHA-LOCK mode. If you want lower case letters, press [LS] then the letter. For greek and other characters, select [RS] then the letter.

Hint:

During program entry, you can press [RS] [ . ] for a carriage return. This can make program entry easier to see and read. Best of all, this does not affect program entry or execution.

Note:

The key [big X] is located on the 5th rows of keys up from the bottom. I will use this to distinguish the X character from the times key [ x ].

Program Input:

[RS] [ + ] ( << >> )
* Start the program entry
[RS] [ x ] ( "" )
[ALPHA] [ALPHA] [MODE] (H) [TOOL] (I) [APPS] (G) [MODE] (H) [SPC] [EVAL] (N) [TAN] (U) [HIST] (M) [F2] (B) [F5] (E) [ √ ] (R) [RS] [+/-] (=) [ALPHA]
[ &rarr ] [LS] [EVAL] (PRG) [NXT] [F5] (IN) [NXT] [F1] (PROMPT)

* Enters the first prompt request, "HIGH NUMBER="
[RS] [ x ] ( "" )
[ALPHA] [ALPHA] [LS] 3 (#) [SPC] [ ' ] (O) [F6] (F) [SPC] [F4] (D) [ √ ] (R) [F1] (A) [+/-] (W) [SIN] (S) [RS] [+/-] (=) [ALPHA]
[ &rarr ] [F1] (PROMPT)

* Enters the second prompt request, "# OF TIMES="
[RS] 0 ( &rarr ) [ALPHA] [EVAL] (N) [SPC] [big X]
* Enters N and X and declares them as local variables
[RS] [ + ] (<< >>) 1 [SPC] [big X]
[F6] (PRG) [F3] (BRCH) [LS] [F4] (FOR)

* Use the current menu to go back to the Program level, enters FOR-NEXT structure
[ALPHA] [STO>] (K) [SPC] [ALPHA] [EVAL] (N)
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND)
[ x ] 1 [ + ] [NXT] [F6] (MTH) [F5] (REAL) [NXT] [IP] (F5)

* Enters the commands for the For loop
[ENTER]
* Terminate program entry

[ ' ] [ALPHA] [ALPHA] [ ' ] (O) [EVAL] (N) [F5] (E) [COS] (T) [ ' ] (O) [ALPHA] [ENTER] [STO>]

The completed program:

<< "HIGH NUMBER=" PROMPT
"# OF DRAWS=" PROMPT
&rarr N X
<< 1 X
FOR K N RAND * 1 + IP
NEXT >> >>


Instructions:

1. Run ONETO
2. The calculator will prompt "HIGH NUMBER=" at the top of the screen. Enter the high number and then press [LS] [ON] (CONT).
3. The calculator will prompt "# OF DRAWS=" at the top of the screen. Enter the number of draws and then press [LS] [ON] (CONT).
4. The calculator fills the stack with randomly selected numbers. Use [HIST] and [ &uarr ] to view the numbers. Press [ON] when done viewing the stack.
5. You may want to clear the stack before continuing.

Example:

A possible set of 12 random integers from 1-12 is: 9, 4, 7, 2, 11, 1, 10, 4, 1, 4, 1, 7.

Graphing an equation from the Stack Program

This program graphs a given an function y(x) using specified window settings. This is just one way of graphing an equation, which is an alternate way to graphing functions without using the graphing set up screens (Y=, WIN, GRAPH, and 2D/3D). The function can be traced and analyzed just like any other functions that are plotted.

The Catalog

You can access all the commands that the 49g+ and 50g has in the catalog. To access the catalog, press [RS] [SYMB] (CAT). If you press [ALPHA] and a letter, you can quickly scroll to that part of the catalog. You may be able to get a few letters if you are fast enough.

The other thing that is nice about the 49g+ and 50g is that the commands can be typed. This may be a helpful and faster alternate than trying to hunt commands in the menu or catalog. However, in this tutorial, the keystrokes I show will make use of the catalog.

Commands that are Used

* XRNG: Sets the left and right boundaries of the plot screen. The left boundary is taken from Level 2; the right boundary is taken from Level 1.
* YRNG: Sets the bottom and top boundaries of the plot screen. The bottom boundary is taken from Level 2; the top boundary is taken from Level 1.
* FUNCTION: Sets the calculator to Function mode.
* STEQ (Store Equation): Stores an equation in the EQ variable. The EQ variable is a system variable which the calculator uses to plot and solve equations with. EQ can have a list of more than one equation or expression.
* INDEP: Sets a variable as independent. Useful when setting up a plot. It makes sense to make the independent variable 'X' or even 'T' or ' θ', but you can make the independent variable anything you want.
* ERASE: Erases the plot screen and gets the calculator ready to draw a fresh, new plot.
* DRAW: Has the calculator draws whatever is stored in EQ.
* DRAX (Draw the Axes): Has the calculator draw the axes.
* PICTURE: Switches the calculator to the plot (picture) environment.

With all these commands to play with, let's program GRPFX.

The Program GRPFX

[RS] [ + ] (<< >>)
[RS] [ x ] ( "" )
[ALPHA] [ALPHA] [1/x] (Y) [LS] [ - ] (parenthesis) [big X] [RS] [+/-] (=)

* Input the string "Y="
[LS] [EVAL] (PRG) [NXT] [F5] (IN) [NXT] [F1] (PROMPT)
* Inserts the Prompt command. Leave this menu open.
[RS] [SYMB] (CAT) [ALPHA] [SIN] (S) scroll until you find STEQ
[F6] (OK)

* Use the catalog to find the STEQ command. You can hold [ &darr ] and [ &uarr ] to quickly scroll the catalog.
[RS] [SYMB] (CAT) [ALPHA] [F6] (F) find FUNCTION [F6] (OK)
* Set the calculator to FUNCTION mode
[ ' ] [ (big) X ] [ &rarr ] [RS] [SYMB] [ALPHA] [TOOL] (I) find INDEP [F6] (OK)
* Set X to be the independent variable.
[RS] [ x ] ( "") [ALPHA] [ALPHA] [big X] [HIST] (M) [TOOL] (I) [EVAL] (N) [RS] [+/-] (=) [F1] (PROMPT)
[RS] [ x ] ( "" ) [ALPHA] [ALPHA] [big X] [HIST] (M) [F1] (A) [big X] [RS] [+/-] (=) [F1] (PROMPT)
[RS] [SYMB] (CAT) [big X] find XRNG [F6] (OK)

* Prompt for the left and right boundaries, then use the XRNG command.
[RS] [ x ] ( "" ) [ALPHA] [ALPHA] [1/X] (Y) [HIST] (M) [TOOL] (I) [EVAL] (N) [RS] [+/-] (=) [F1] (PROMPT)
[RS] [ x ] ( "" ) [ALPHA] [ALPHA] [1/X] (Y) [HIST] (M) [F1] (A) [big X] [RS] [+/-] (=) [F1] (PROMPT)
[RS] [SYMB] (CAT) [ALPHA] [1/X] (Y) find YRNG [F6] (OK)

* Prompt for the bottom and top boundaries, then use the YRNG command.
[RS] [SYMB] (CAT) [ALPHA] [F5] (E) find ERASE [F6] (OK)
[RS] [SYMB] (CAT) [ALPHA] [F4] (D) find DRAX [F6] (OK)
[RS] [SYMB] (CAT) [ALPHA] [F4] (D) find DRAW [F6] (OK)

* Input the ERASE, DRAX, and DRAW commands.
[RS] [SYMB] (CAT) [ALPHA] [SYMB] (P) find PICTURE [F6] (OK) [ENTER]
* Input the PICTURE command and terminate program entry.

The completed program:

<< "Y(X)=" PROMPT
STEQ
FUNCTION
'X' INDEP
"XMIN=" PROMPT "XMAX=" PROMPT XRNG
"YMIN=" PROMPT "YMAX=" PROMPT YRNG
ERASE DRAX DRAW
PICTURE >>



Algebraic Objects

Simply put, algebraic objects are equations or formulas that are enclosed in single quotes. Examples include:

'1'
'X+2/5'
'SIN(π-1/2)'
'e^5.2-SQ(3)'

Algebraic objects gives the user an alternative (and maybe easier) way for the user to enter equations. In Part 6, we will explore several common functions that can be used with algebraic objects.

We can also enter equations using the RPN method. Let's try an example:

To enter 'X^2 + 3X - 1', we can enter this two ways. Assume X does not have anything stored in it. Purge X if need be.

RPN Way: [big X] [ENTER]
* duplicates X.
[LS] [ √ ] (x^2) [LS] [ &rarr ] (SWAP) 3 [ x ] [ + ] 1 [ - ] [EVAL]

Use [EVAL] to simplify the expression.

Algebraic Object: [ ' ] [big X] [y^x] 2 [ + ] 3 [ x ] [big X] [ - ] 1 [ENTER]

Instructions for GRPFX:

1. Run GRPFX. All the input will be asked for you, so you don't need anything in the stack to start with.
2. At the "Y(X)=" prompt, enter a function in X. You can use RPN, an algebraic object, or use the equation writer. Please be aware that the prompt may disappear when you are entering the function, just continue. Press [LS] [ON] (CONT) to continue.
3. At the "XMIN=" prompt, enter the left boundary, then press [LS] [ON] (CONT).
4. At the "XMAX=" prompt, enter the right boundary, then press [LS] [ON] (CONT).
5. At the "YMIN=" prompt, enter the bottom boundary, then press [LS] [ON] (CONT).
6. At the "YMAX=" prompt, enter the top boundary, then press [LS] [ON] (CONT).
7. You will see the function plotted. Use [F3] [F2] to trace the function. [F4] will give you access to function analysis such as ROOT, SLOPE, and AREA. Press [ON] to get back to the home screen.

The following pictures show GRPHX in action while plotting y(x) = x^2 + 3x - 1. Using the window, X = [-10, 10] and Y = [-10, 10]. (I am took pictures at a coffee shop using the iPad)

Hope you enjoyed Part 5. Come back for Part 6 where we explore cool functions with algebraic objects.

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

Thursday, October 6, 2011

RPL Programming Tutorial - Part 4 - HP 49g+/50g: For-Next

What's it all FOR?

OK, corny titles aside, welcome to Part 4 of the RPL Tutorial for the HP 49g+ and 50g calculators. Part 4 will introduce another fundamental structure in mathematical programming: the FOR-NEXT structure.

The FOR-NEXT structure allows the programmer to designate a set of commands to be repeated a set number of times. The structure can use a counter, known as a dummy variable. The counter can also be part of a calculation.

In general, the FOR-NEXT structure looks something like this:

FOR counter-variable = start-number TO end-number
Instructions go here
NEXT (or END)

Duplicating Pascal's Triangle


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1


This is the first six rows of the famous Pascal's Triangle. Pascal's Triangle is used for many purposes. One use of the triangle is to determine the coefficients of the binomial expansion of (x + y)^n, where n is an integer. The top row of Pascal's Triangle is designated as "Row 0".

Any entry in Pascal's Triangle can be found using the Combination function:

COMB(r, n) = r! / (n! * (r - n)!)

Where:
r = the row number
n = the entry number

This program will return all of the entries of a given row.

The Program PASCAL

Comments will be italicized, starting with an asterisk. This program starts with the desired row number R on Level 1 of the Stack.

[RS] [ + ] (<< >>)
[RS] [ 0 ] (&rarr) [ALPHA] [ √ ] (R)

* Store the desired row number in local variable R
[RS] [ + ] (<< >>)
[LS] [ + ] ( { } )

* Puts an empty list on the stack, all the entries will go in this list
[ &rarr ] [SPC]
0 [SPC] [ALPHA] [ √ ] (R)

* Start-number = 0, End-number = R
[LS] [EVAL] (PRG)
[F3] (BRCH)
[LS] [F4] (FOR)

* Inserts the FOR-NEXT structure
[ALPHA] [STO] (K)
* Let K be the counter-variable (or dummy variable)
[SPC] [ALPHA] [ √ ] (R) [SPC] [ALPHA] [STO] (K)
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F1] (COMB)

* Calculates COMB(R, K)
[ + ]
* Adds result to the list
[ENTER]
* Terminates program entry

[ ' ] [ALPHA] [ALPHA] [SYMB] (P) [F1] (A) [SIN] (S) [F3] (C) [F1] (A) [NXT] (L) [ENTER] [STO>]

The completed program:
<< &rarr R << { } 0 R FOR K R K COMB + NEXT >> >>

Instructions:
1. Enter the desired row number on Level 1 of the Stack.
2. Run PASCAL
3. If the list exceeds the width of the screen, you can press either [HIST] [F2] (VIEW) or [ &uarr ] [F2] (VIEW) to get the list in View Mode. Use the arrow keys ( [ &rarr ] and [ &larr ] ) to scroll the list's contents.

Test:
3rd Row: {1 3 3 1}

6th Row: {1 6 15 20 15 6 1}

12th Row: {1 12 66 220 495 792 924 792 495 220 66 12 1}

This is just one example of using the FOR-NEXT structure. In Part 5, coming soon, we get the calculator to ask for a number. See you then!

Eddie


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

RPL Programming Tutorial - Part 3 - HP 49g+/50g: If-Then-Else

If Only...

Welcome to Part 3 of the RPL Programming Tutorial with the HP 49g+ and HP 50g calculator. To recap, Part 1 discussed the basics of RPL programming and Part 2 introduced the concept of local variables.

In Part 3 we will dive into the IF-THEN and IF-THEN-ELSE structures. These structures test data against a condition, and then instructs the machine to execute designated program code based on the results.

In general, an IF-THEN-ELSE-END structure looks like this:

IF condition listed here (i.e. x = 5, y > 10, z ≤ 0, a ≠ b, etc.)
THEN commands to be done if the condition is true
ELSE commands to be done if the condition is false
END

How to find the IF menu

You can input the IF, THEN, ELSE, and END program commands separately. However, the HP 50g (and 49g+) gives the user another choice. With a keystroke sequence, you can enter the entire sequence as a template.

To insert the templates:

Start by pressing [LS] [EVAL] (PRG) [F3] (BRCH) .

To insert an IF-THEN-END structure, press [LS] [F1] (IF).

To insert an IF-THEN-ELSE-END structure, press [RS] [F1] (IF).

The next two programs will show an example of each of the two structures.

The Online Shipping Deal

A famous online store is running a promotion: Order at least $100.00 from our store, and we'll pay the $5.95 shipping fee! This program calculates the total amount of the order, with shipping if any. Assume the store charges 7.5% sales tax on all orders including shipping.

Notes:
1. The local variable A will be used to designate the amount of goods purchased.
2. Since A + y% = A + x * y /100 = A * (1 + y /100), we will multiply the amount plus shipping by 1.075. (Let y = 7.5%)
3. The $5.95 shipping charge applies only when the order amount is less than $100.00. Set up the test this way: If A < 100, add 5.95 to A, otherwise do nothing. Because we are only executing further instructions only when the condition is true, no ELSE command is necessary.

The Program ONSALE

Comments will be italicized, starting with an asterisk. This program assumes that the purchase amount is on Level 1 of the stack.

[RS] [ + ] (<< >>)
* Start of the program
[RS] [ 0 ] (&rarr)
[ALPHA] [F1] (A)

* Assign the amount to the local variable A
[RS] [ + ] (<< >>)
* Start the main program
[ALPHA] [F1] (A)
* Call A to the stack
[LS] [EVAL] (PRG)
[F3] (BRCH)
[LS] [F1] (IF)

* Insert the IF-THEN-END structure
[ALPHA] [F1] (A)
[SPC] 100 [RS] [ X ] (<)

* Insert the test condition A < 100
[ &darr ] (down arrow)
5.95 [ + ]

* Add shipping charge if A < 100
[ &darr ] 1.075 [ x ] [RS] [ENTER] (&rarr NUM) [ENTER]
* Finish the program

[ ' ] [ALPHA] [ALPHA] [ ' ] (O) [EVAL] (N) [SIN] (S) [F1] (A) [NXT] (L) [F5] (E) [ENTER] [STO>]

The completed program ONSALE:
<< &rarr A << A IF A 100 < THEN 5.95 + END 1.075 * &rarr NUM >> >>

Instructions:
1. Enter the amount on Level 1 of the stack.
2. Run ONSALE.

Test Data:

Input = 50, Result = 60.14625 (A $50.00 order results in a total bill of $60.15.)

Input = 99.99, Result = 113.8855 (A $99.99 order results in a total bill of $113.89)

Input = 100, Result = 107.5 (A $100.00 order results in a total bill of $107.50. Surprised? Remember all orders $100.00 or more result in the shipping charged being waived.)

Input = 149.99, Result = 161.23925 ($149.99 order yields a bill of $161.24)

f(x) = (sin x)/x

This program calculates the function f(x) = (sin x)/x for all real numbers. If we attempt to calculate f(0) directly, we would get a "division by zero" error. However, the calculus limit as f(x) approaches 0 is 1. Let's create a test condition that detects for an input of 0. If the input is anything else, f(x) computes normally.

Note: The test of equality requires two equal signs, ==. This can be typed directly from the keyboard via alpha or by the TEST submenu of the PROGRAM menu. This program uses TEST submenu method (4 keystrokes opposed to 6 keystrokes).

The Program SINX

The program takes X from Level 1 of the stack and stores it as the local variable X.

[RS] [ + ] (<< >>)
[RS] [ 0 ] (&rarr)
[ X ]

* Stores the contents of Level 1 in the local variable X
[RS] [ + ] (<< >>)
[LS] [EVAL] (PRG)
[F3] (BRCH)
[RS] [F1] (IF)

* Inserts the IF-THEN-ELSE-END structure
[ X ] [SPC] 0
[LS] [EVAL] (PRG) [F4] (TEST) [F1] (==)

* Inserts the double equals sign, ==, for the equality test. Test X = 0?
[ &darr ] [SPC] 1
* Enters the commands should X = 0 (THEN)
[ &darr ] [SPC]
[ X ] [SIN] [ X ] [ ÷ ]
[RS] [ENTER] (&rarr NUM)

* Enters the commands should X ≠ 0 (ELSE)
[ENTER]
* Terminates program entry

[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TOOL] (I) [EVAL] (N) [ X ] (X) [ENTER] [STO>]

The complete program:
<< &rarr X << IF X 0 == THEN 1 ELSE X SIN X / &rarr NUM END >> >>

Source: HP 48SX Scientific Expandable Calculator: Owner's Manual Volume II. Hewlett Packard, 3rd Edition, 1990

Instructions for SINX:
1. Enter X on the stack.
2. Run SINX.

Test Data - Assume the calculator is in Radians mode:

Input = -1, Result = 0.841470984808

Input = 0, Result = 1 (If you get this, then the IF-THEN-ELSE-END structure worked)

Input = 1, Result = 0.841470984808

Coming up, we'll take a look at the FOR-NEXT structure. See you next time in Part 4!

Eddie


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

RPL Programming Tutorial - Part 2 - HP 49g+/50g: Local Variables

Welcome!

Welcome to Part 2 of the RPL Programming Tutorial for the HP 49g+ and 50g calculators. To recap, Part 1 talked about how to create, save, run, and (if need be) delete a program.

In Part 2, introduces the concept of the local variable. Simply put, a local variable is a variable that is used in a program and is then purged (deleted) at the end of the program. Local variables are not stored outside of the program, which saves memory.

A general structure of declaring local variables goes like this:

<< commands needed to set up the local variables, if any &rarr Local Variables
<< main program >> >>

=================================================================
Hint: There are times that you want to store information outside of a program. To do so, just store the data in a variable enclosed in single quotes followed by a STO command. These variables are called global variables.
=================================================================

Fibonacci Sequence

The well known Fibonacci Sequence is:
1, 1, 2, 3, 5, 8, 13, 21, 34,...

After the first two numbers, each succeed number is the sum of the last two numbers.

F_n = F_(n-1) + F_(n-2) where F_1 = 1 and F_2=1.

You can quickly find the mth term of the Fibonacci Sequence by using this closed formula:

f(n) = (ø^(n + 2) - (1 - ø)^(n + 2)) ÷ √5

Where:
ø = (1 + √5) ÷ 2, the Golden Ratio
n = m - 2

There are two local variables used in this program:
N = M - 2, the user supplies where M is the desired mth term
H = ø

The Program FIBN

Comments will be italicized, starting with an asterisk *. This program starts with M on Level 1 of the stack.

[RS] [ + ] (<< >>)
* Start of the program
2 [ - ]
* Subtract 2 from M to get N
1 [SPC] 5 [ √ ] [ + ]
2 [ ÷ ]

* Set up the Golden Ratio Constant, to be stored in H
[RS] [ 0 ] (&rarr)
* Prepare to name the local variables
[ALPHA] [EVAL] (N) [SPC]
[ALPHA] [MODE] (H)
[RS] [ + ] (<< >>)

* Start the main program
[ALPHA] [MODE] (H) [SPC]
* Leave spaces in between H and N
[ALPHA] [EVAL] (N) [SPC]
2 [ + ] [y^x]

* H^(N + 2); [y^x] is shown as ^
1 [SPC] [ALPHA] [MODE] (H) [ - ]
[ALPHA] [EVAL] (N) [SPC] 2
[ + ] [y^x]

* (1 - H)^(N + 2)
[ - ]
* H^(N + 2) - (1 - H)^(N + 2)
5 [ √ ] [ ÷ ]
* ( H^(N + 2) - (1 - H)^(N + 2) ) ÷ √5
[EVAL]
* To simplify the answer
[ENTER]
* To terminate program entry

[ ' ] [ALPHA] [ALPHA]
[F6] (F) [TOOL] (I) [F2] (B) [EVAL] (N)
[ENTER] [STO>]


Here is the completed program:

<< 2 - 1 5 √ + 2 / → N H
<< H N 2 + ^ 1 H - N 2 + ^ - 5 √ / EVAL >> >>


How to run FIBN:
1. Enter M on the Stack
2. Run FIBN

Results:
FIBN(1) = 1
FIBN(2) = 1
FIBN(3) = 2
FIBN(4) = 3
FIBN(5) = 5
etc...

That wraps up Part 2. In Part 3, the IF-THEN-END and IF-THEN-ELSE-END program structures are introduced. Until then, Cheers! Eddie

Source Used: Math Formulas and Tables from Mobile Reference. SoundTells, LLC 2003-2010

This tutorial is created and is the property of Edward Shore. No mass reproduction without express permission of the author.


Edit: 10/20/11: correct an error in the formula in the text. The program is correct.

RPL Programming Tutorial - Part 1 - HP 49g+/50g: Your First Program


RPL Tutorial - Part 1

Introduction

This is a first in a series of RPL programming tutorial with the Hewlett Packard HP 50g Graphics Calculator. RPL stands for the Reverse Polish Lisp programming language. RPL is similar to RPN, since RPL is a combination of RPN (Reverse Polish Notation), Lisp, and Forth.

This series of tutorials cover basic RPL programming. There is a wide variety of mathematical programs, applications, and analysis that can be done with the HP 50g calculator. My goal of this series is to introduce readers to RPL programming so that they get a working knowledge of RPL programming.

The Hewlett Packard graphing series, starting with the HP 28C in the late 1980s, through the HP 48S series, the HP 48G series, the HP 49G at the turn of the century, and the 49g+ all operate on RPL. If you have a 50g or a 49g+, you can follow the programs in this tutorial keystroke by keystroke. Most of the programs shown in this series can be programmed on the 48S, 48G, and the 28 series - but please check your manual: these are older calculators that require different keystrokes.

For example, the programming commands are accessed on the 48S and 48G just by pressing the [PRG] key on the top row. However, on the 49g+ and 50g, you will need to press [LS] [EVAL] to access the program commands.

The author owns a 48SX, 48G, 49g+, and a 50g.

Getting Started

Shift and Soft Keys

For the HP 50g:

[LS] represents the Left Shift key. It is the third key up from the ON button on the left hand side of the keyboard. The key's color is white.

[RS] represents the Right Shfit key. if is the second key up from the ON button on the left hand side of the keyboard. The key's color is orange.

[ALPHA] is the Alpha Key, which allows the user to type alpha and other characters. This is important in naming programs and variables. For the programs in the tutorials, I will use one letter names for variables (i.e. A, B, C, etc..)

Pressing [ALPHA] twice will lock the keyboard into Alpha-Lock mode. This allows you to type more than one letter in succession. Press [ALPHA] again to leave Alpha-Lock mode.

The [ALPHA] key, which is yellow in color, is the fourth key from the ON button on the left hand side of the keyboard.

Finally, the top row of the keyboard consists of six soft keys, labeled F1 to F6. The functions of the soft keys change depending on which menu is currently active.

HP 49g+ Shfit Key Colors:
[ALPHA] - yellow
[LS] - green
[RS] - red

HP 50g Shift Key Colors:
[ALPHA] - yellow
[LS] - white
[RS] - orange

Setting the 50g to RPN Mode

All the programs shown in this tutorial series will be operated in RPN (Reverse Polish Notation) mode. To set the 50g in RPN mode:

1. Press the [MODE] key.
2, Press [F2] (CHOOSE) and select RPN.

Your calculator is set. (49g+ users follow the same instructions)

Setting Soft Menus

Personally I like using soft menus. The HP 49g+ and 50g gives a user a choice to operate using soft menus or scrolling menus. All of the keystrokes in the programs in this tutorial assume that you are using soft keys. To set the calculator for soft keys:

1. Press the [MODE] key
2. Press [F1] (FLAGS) to bring up the calcualtor's flags. Flags are binary operators that dictates the various modes of the calculator. Note: There are user flags that you can set for programming purposes.
3. Scroll up until you see "117" on the left side. If there is a check mark next the 117, the calculator will read "117: Soft MENU". Otherwise, the calculator will read "117: CHOOSE boxes". Press [F3] until 117 is checked. Then press [F6] (OK) twice.

We want the calculator to read "√ 117 Soft MENU". 49g+ users will follow the same instructions.
Programming Basics

Brackets

Programs are enclosed with "pointy" brackets. ( << >> ) All the programming instructions are included with a set of brackets. We can enclose programming instructions in many sets of brackets.

How to Name Programs

You can name a program almost any name you want. Program names are enclosed in single quotes. ( ' ' ) Named programs are shown in the Variables menu. You can simply access the Variables menu by press the [VAR] key. Variables most recently stored are listed first.

Note that:

1. Variable names can be used to name not only programs, but real numbers, complex numbers, constants, graphic databases, matrices, vectors, lists, and even quotes.

2. The Variables menu will only show the first five characters. When a menu is showing (this works with almost any soft menu), you can press [RS] [down arrow] to have the calculator list the full name of each of the commands shown in the current soft menu.
(Edited 4/12/2013: Thank you to Félix Hernández for correcting me on this step - much appreciated!) 

3. Variables must start with a letter and can be of any length. No spaces are allowed in variable names.

Acceptable: 'PROG1', 'MYPROG', 'CHANGE%'
Not Acceptable: '123', 'COOL PROGRAM'

How to Name a Program:

1. Press [ ' ].
2. Press [ALPHA] [ALPHA] and type the name. The alphabetic characters are the yellow letters on the keys. Lower case letters can be accessed by first pressing [LS] before the letter. You can access other characters as well by first pressing [RS] before the appropriate key. When done press [ENTER].
3. Press [STO>].

How to delete a Program:

1. Press [ ' ]
2. Press [VAR] and find the program (or variable) you want to delete. Press the appropriate soft key to recall the name.
3. Press [TOOL] then [F5] (PURGE). The program (variable) is deleted.

Your First Program

Finally! Now we get to the good stuff - programming! Programming is one of my favorite features of graphing calculators.

The first program we are going to do is a simple one: find the area of a circle of a given radius. This program takes the radius from Level 1 of the stack and returns a numeric approximation of the area.

Commands Used

&rarr NUM: Convert the contents of Level 1 to an approximate answer.

The Program ACIR

For each program, I will list a series of keystrokes. Following the keystrokes, any notes will be italicized .

Ready?

[RS] [ + ] (<< >>)
* Start the program
[LS] [ √ ] (x^2)
* Square function - labeled as SQ
[LS] [SPC] (π)
* Inserts π
[ x ]
[RS] [ENTER] (->NUM)
* Convert answer to an approximation
[ENTER]
* Terminate program

[ ' ] [ALPHA] [ALPHA]
[F1] (A) [F3] (C) [TOOL] (I) [ √ ] (R) [ENTER] [STO>]


Store the program in variable ACIR

The Program:

<< SQ π * ->NUM >>

Running ACIR

1. Type the radius.
2. Press [VAR] - find ACIR and press the appropriate soft key. Press [NXT] if necessary.

An alternative way:

1. Type the radius.
2. Press [ ' ] [ALPHA] [ALPHA] type ACIR and press [ENTER].
3. Press [EVAL] to run the program.

Results:

Radius = 6; Result = 113.09734
Radius = 4.08; Result = 52.29621

Hint: You can look at program (to edit, view, etc), by pressing [ ' ], typing the name, and pressing [LS] [STO>] (RCL) .

Be sure to check this blog for future tutorials on RPL Programming. Up next, local variables. See you next time! - Eddie

This tutorial is property of Edward Shore. Mass reproduction or distribution requires express permission by the author.

RPN HP 12C: Fibonacci and Lucas Sequences

  RPN HP 12C: Fibonacci and Lucas Sequences Golden Ratio, Formulas, and Sequences Let φ be the Golden Ratio: φ = (1 + √5) ÷ 2...