Thursday, January 10, 2019

HP 42S Polynomial Operations

HP 42S Polynomial Operations

Note: These programs presented in today's blog are can also be programmed on the Free42 app and the DM 42.  The default setting, 25 memory registers, is assumed to be set.  The programs are written use R24 as a counter. 

This program uses indirect storage (R24).  The maximum order is 23.

HP 42S Program SPOLY

This program asks for the order of a polynomial and then asks the user to store the coefficients.  The order of registers are coefficients of powers of x in descending order from n to 0. 

For example, for a cubic polynomial, the coefficients are stored as such:
R01 = coefficient of x^3
R02 = coefficient of x^2
R03 = coefficient of x
R04 = constant coefficient

Program:

00 { 67-Byte Prgm }
01▸LBL "SPOLY"
02 CLX
03 "ORDER?"
04 PROMPT
05 STO 00
06 IP
07 1
08 +
09 1ᴇ-3
10 ×
11 1
12 +
13 STO 24
14▸LBL 00
15 "X↑"
16 RCL 00
17 RCL 24
18 IP
19 -
20 1
21 +
22 ARCL ST X
23 ├" ="
24 CLX
25 PROMPT
26 STO IND 24
27 ISG 24
28 GTO 00
29 "DONE"
30 AVIEW
31 RTN
32 END

Example:  Store the polynomial p(x) = 2*x^3 - x^2 + 5*x + 7

Keystrokes:  [ XEQ ] (SPOLY)
"ORDER?" 3 [ R/S ]
"X↑3.0000=" 2 [R/S]
"X↑2.0000=" 1 [ +/- ] [R/S]
"X↑1.0000=" 5 [R/S]
"X↑0.0000=" 7  [R/S]
"DONE"

Results
R01 = 2.0000
R02 = -1.0000
R03 = 5.0000
R04 = 7.0000

HP 42S Program HORNER

The HORNER evaluates the polynomial p(x).  The assumes that the order of registers are coefficients of powers of x in descending order from n to 0.  The user is asked about the order of the polynomial and the value of x.

Program:

00 { 65-Byte Prgm }
01▸LBL "HORNER"
02 CLX
03 "X?"
04 PROMPT
05 STO 00
06 CLX
07 "ORDER?"
08 PROMPT
09 IP
10 1ᴇ-3
11 ×
12 1
13 +
14 STO 24
15 RCL 00
16 RCL× IND 24
17 ISG 24
18▸LBL 01
19 RCL+ IND 24
20 RCL× 00
21 ISG 24
22 GTO 01
23 RCL+ IND 24
24 "P(X)="
25 AVIEW
26 RTN
27 .END.

Example:  Let p(x) be the polynomial, p(x) = 2*x^3 - x^2 + 5*x + 7
Calculate p(-3).

The coefficients are have already been stored (from last example).
R01 = 2.0000
R02 = -1.0000
R03 = 5.0000
R04 = 7.0000

Keystrokes:  [XEQ] (HORN)
"X?" 3 [ +/- ] [ R/S ]
"ORDER?" 3  [ R/S ]

Result:  -71.0000

HP 42S Program DX_PX

This program calculates coefficients of the derivative of the polynomial p(x), using the form:

d/dx x^n = n * x^(n-1)

The order of registers are coefficients of powers of x in descending order from n to 0. 

Program:

00 { 79-Byte Prgm }
01▸LBL "DX_PX"
02 CLX
03 "ORDER?"
04 PROMPT
05 STO 00
06 IP
07 1ᴇ-3
08 ×
09 1
10 +
11 STO 24
12▸LBL 02
13 RCL IND 24
14 RCL 00
15 RCL 24
16 IP
17 -
18 1
19 +
20 ×
21 STO IND 24
22 "X↑"
23 RCL 00
24 RCL 24
25 IP
26 -
27 ARCL ST X
28 X<>Y
29 AVIEW
30 STOP
31 ISG 24
32 GTO 02
33 1
34 RCL+ 00
35 STO 24
36 CLX
37 STO IND 24
38 "DONE"
39 AVIEW
40 RTN
41 .END.

Example:  Calculate the derivative of p(x) = 2*x^3 - x^2 + 5*x + 7.  Assume that coefficients from the previous example.

Keystrokes:  [ XEQ ] (DX_PX)
"ORDER?" 3 [R/S]

Results:
"X↑2.0000=" 6.0000 [R/S]
"X↑1.0000=" -2.0000 [R/S]
"X↑0.0000=" 5.0000  [R/S]
"DONE"

dp/dx = 6*x^2 - 2*x + 5

Registers:
R1 = 6.0000
R2 = -2.0000
R3 = 5.0000
R4 = 0.0000

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.