Tuesday, October 18, 2011

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.