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.