HP 42S Programming Part III: Numerical Derivative, Recurring
Sequences, Fractions
Click here for Part I: Matrix Column Sum, GCD, Error Function
Click here for Part II: Dew Point, Ellipse Area and Eccentricity, Easy Transverse
HP 42S: Numerical Derivative
The program DERVX is based off my numerical derivative
program for the HP 15C. To see the HP
15C program, click here: http://edspi31415.blogspot.com/2011/11/numerical-derivatives-in-part-9-we-will.html
DERVX calculates numerical derivatives of f(x),
given a small increment h. Generally,
the smaller h is,
the better the calculation. However with certain methods, if h is
too small, the final result may be unexpected.
This program uses a five-point formula:
f'(x) ≈ (f(x - 2h) - 8 * f(x - h) + 8 * f(x + h) - f(x + 2h)) / (12h)
The error is of the order of h^4.
Source: Burden, Richard L. and J. Douglas Faires. "Numerical Analysis 8th Edition" Thomson Brooks/Cole Belton, CA 2005
Labels Used:
Label DERVX: Main routine
Label 00: f(X). This function starts with X on the X register.
Numerical memory registers will be used, and they are the following:
This program uses a five-point formula:
f'(x) ≈ (f(x - 2h) - 8 * f(x - h) + 8 * f(x + h) - f(x + 2h)) / (12h)
The error is of the order of h^4.
Source: Burden, Richard L. and J. Douglas Faires. "Numerical Analysis 8th Edition" Thomson Brooks/Cole Belton, CA 2005
Labels Used:
Label DERVX: Main routine
Label 00: f(X). This function starts with X on the X register.
Numerical memory registers will be used, and they are the following:
R00 = the numerical derivative
R01 = X
R02 = h
R01 = X
R02 = h
Radians mode is automatically set.
HP 42S: Program DERVX
00 {“70+”-Byte Prgm}
\\ number varies depends on what is f(X)
01 LBL “DERVX”
02 RAD
03 STO 02 \\ store h
04 R↓ \\ roll down
05 STO 01 \\ store x
06 2
07 RCL* 02
08 –
09 XEQ 00 \\ subroutine
10 STO 00
11 RCL 01
12 RCL- 02
13 XEQ 00
14 8
15 +/-
16 *
17 STO+ 00
18 RCL 01
19 RCL+ 02
20 XEQ 00
21 8
22 *
23 STO+ 00
24 RCL 01
25 2
26 RCL* 02
27 +
28 XEQ 00
29 +/-
30 STO+ 00
31 RCL 00
32 RCL÷ 02
33 12
34 ÷
35 STO 00
36 “d/dX =” \\ lower
case d: [shift] { D } in ALPHA
37 ARCL ST X
38 RTN \\ main program ends
39 LBL 00
…
NN RTN \\ end f(X) with RTN
NN+1 END
How to run DERVX:
1. If desired, edit
the function by [shift] [XEQ] (GTO) 00, [shift] [R/S] (PRGM). Remember you start with x in the X
stack. End f(x) with RTN.
2. Input x, press
[ENTER].
3. Input h, press
[XEQ] {DERVX}.
Test 1: f(x) = x^2 +
x*sin x, where x = π/4, h = 1E-5
The code for f(x) would look like this:
39 LBL 00
40 X^2
41 LASTX
42 ENTER
43 SIN
44 *
45 +
46 RTN
47 END
π [ENTER] 4 [÷] 1 [E]
-5 [XEQ] {DERVX}
Result: 2.8333
Test 2: f(x) = x*e^x,
where x = 1.75, h = 1E-5
The code for f(x) would look like this:
39 LBL 00
40 ENTER
41 E^X
42 *
43 RTN
44 END
1.75 [ENTER] 1 [E] -5 [XEQ] {DERVX}
Result: 15.8252
HP 42S: Recurring Sequence Matrix
The program RECUR builds a table for the sequence f(n,
u(n-1)). The table is stored in the
matrix MAT. At the completion of the
program, you will be to see the elements of MAT in matrix edit mode.
LBL 00 holds the sequence function.
R00 = u(n-1)
Specify as many entries as you like, up to 999.
To use the counter in the sequence function, use RCL 02, IP.
Other registers that are used are:
R01 = n
R02 = counter, in the form of 2.nnn
HP 42S Program RECUR
00 {“95+” Byte Prgm}
\\ varies depending on the sequence function
01 LBL “RECUR”
02 “U(1)=”
03 PROMPT
04 STO 00 \\ store
the initial condition
05 “# TERMS:”
06 PROMPT
07 STO 01 \\ store n
08 2
09 DIM “MAT” \\ create the matrix MAT (n x 2)
10 1E-3
11 RCL* 01
12 2
13 +
14 STO 02 \\ set up counter
15 INDEX “MAT” \\ index the matrix
16 1
17 ENTER
18 STOIJ \\ set index
counters at I=1 (row), J=1 (col)
19 1
20 STOEL \\ store 1 into MAT(1,1)
21 J+ \\ XEQ “J+”
22 RCL 00
23 STOEL \\ store
U(1) into MAT(1,2)
24 J- \\ XEQ “J-“,
set column pointer back to 1
25 LBL 10 \\ main
loop
26 I+ \\ XEQ “I+”, go to the next row
27 RCL 02
28 IP
29 STOEL
30 J+ \\ XEQ “J+”
31 RCL 00 \\ recall
u(n-1)
32 XEQ 00 \\ calculate f(n, u(n-1))
33 STO 00 \\ store u(n) for the next loop
34 STOEL
35 J- \\ XEQ “J-“
36 ISG 02 \\ increase
counter
37 GTO 10
38 RCL “MAT”
39 EDIT \\ put MAT in viewing/editing mode
40 RTN \\ main
program ends
41 LBL 00
…
NN RTN \\ end sequence function with RTN
NN+1 END
How to run RECUR:
1. If desired, edit
the function by [shift] [XEQ] (GTO) 00, [shift] [R/S] (PRGM). Remember you start with u(n-1) in the X
stack. End f(x) with RTN.
2. Press [XEQ] {RECUR}. You will be prompted for U(1) and the number
of rows desired.
Test 1: u(n) =
2*u(n-1), u(1) = 1, 5 rows wanted
41 LBL 00
42 2
43 *
44 RTN
45 END
[XEQ] {RECUR} 1 [XEQ] 5 [XEQ]
MAT =
1
|
1
|
2
|
2
|
3
|
4
|
4
|
8
|
5
|
16
|
Test 2: u(n) =
u(n-1)^2 – u(n-1)^3/6, u(1) = 1.5, 5 rows wanted
41 LBL 00
42 ENTER
43 X^2
44 X<>Y
45 3
46 Y^X
47 6
48 ÷
49 –
50 RTN
51 END
MAT =
1
|
1.5000
|
2
|
1.6875
|
3
|
2.0468
|
4
|
2.7602
|
5
|
4.1138
|
The program FRACT provides three calculations involving
fractions:
ADD: add two
fractions
W/X + Y/Z = (W*Z + X*Y)/(X*Z)
MULT: multiply two fractions
W/X * Y/Z = (W*Y)/(X*Z)
POWER: take a fraction to a power
(W/X)^Y = W^Y/X^Y
The end of the program stores the resulting numerator in N
and the resulting denominator in D.
FRACT simplifies the fraction after calculation. Note:
FRACT sets the HP 42S to FIX 0 mode. Mixed fractions are not included.
HP 42S: Program FRACT
00 {255-Byte Prgm}
01 LBL “FRACT”
02 FIX 00
03 LBL 99 \\ start menu
04 CLMENU
05 LBL 00
06 “ADD”
07 KEY 1 GTO 01 \\
KEYG
08 “MULT”
09 KEY 2 GTO 02
10 “POWER”
11 KEY 3 GTO 03
12 MENU
13 STOP
14 GTO 99 \\ end menu
15 LBL 01 \\ addition
routine
16 CLMENU
17 “W/X + Y/Z”
18 AVIEW
19 PSE
20 INPUT “W”
21 INPUT “X”
22 INPUT “Y”
23 INPUT “Z”
24 RCL “W”
25 RCL* “Z”
26 RCL “X”
27 RCL* “Y”
28 +
29 STO “N”
30 RCL “X”
31 RCL* “Z”
32 STO “D”
33 GTO 10 \\ go to GCD routine
34 LBL 02 \\ multiply routine
35 CLMENU
36 “W/X x Y/Z”
37 AVIEW
38 PSE
39 INPUT “W”
40 INPUT “X”
41 INPUT “Y”
42 INPUT “Z”
43 RCL “W”
44 RCL* “Y”
45 STO “N”
46 RCL “X”
47 RCL* “Z”
48 STO “D”
49 GTO 10 \\ go to GCD routine
50 LBL 03 \\ power routine
51 CLMENU
52 “(W/X)/\Y” \\ the
power symbol is made of two symbols: / and \
53 AVIEW
54 PSE
55 INPUT “W”
56 INPUT “X”
57 INPUT “Y”
58 RCL “W”
59 RCL “Y”
60 Y^X
61 STO “N”
62 RCL “X”
63 RCL “Y”
64 Y^X
65 STO “D”
66 GTO 10 \\ go to
GCD routine
67 LBL 10 \\ GCD routine
68 RCL “N”
69 RCL “D”
70 X<Y?
71 X<>Y
72 STO 01
73 X<>Y
74 STO 00
75 LBL 11
76 RCL 01
77 ENTER
78 RCL÷ 00
79 IP
80 RCL* 00
81 –
82 X=0?
83 GTO 12
84 X<>Y
85 STO 01
86 X<>Y
87 STO 00
88 GTO 11
89 LBL 12 \\ ending and display
90 CLA \\ clear ALPHA register
91 RCL “N”
92 RCL÷ 00 \\ GCD = R00
93 STO “N”
94 ARCL ST X
95 ├
“/” \\ ├
is the attach symbol, press [shift] [ENTER] (ALPHA) [ENTER]
96 RCL “D”
97 RCL÷ 00
98 STO “D”
99 ARCL ST X
100 AVIEW
101 END
Test 1: Addition
(ADD)
12/19 + 8/17 = 356/323
W = 12, X = 19, Y = 8, Z = 17
Result: “356./323.” Y: 356, X: 323
Test 2: Multiplication (MULT)
12/19 * 8/17 = 96/323
W = 12, X = 19, Y = 8, Z = 17
Result:
“96./323.” Y: 96, X: 323
Test: Power (POWER)
(2/7)^3 = 8/343
W = 2, X = 7, Y = 3
Result:
“8./343.”, Y: 8, X: 343
That is Part III, I am happy I was able to post this a lot sooner than I thought.
Eddie
This blog is property of Edward Shore, 2016.