Post Reply 
(42S) Numerical Derivative
07-11-2016, 05:03 AM
Post: #1
(42S) Numerical Derivative
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/n...-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:
R00 = the numerical derivative
R01 = X
R02 = h

Radians mode is automatically set.

Code:
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:
Code:
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:
Code:
39 LBL 00
40 ENTER
41 E^X
42 *
43 RTN
44 END

1.75 [ENTER] 1 [E] -5 [XEQ] {DERVX}
Result: 15.8252
Visit this user's website Find all posts by this user
Quote this message in a reply
07-11-2016, 06:39 AM
Post: #2
RE: (42S) Numerical Derivative
if the function to be derived is defined in the complex plane, there is a much simpler, faster and more accurate way.
The program below is my version of what was originally a 15C program by Valentin Albillo.

Code:
Usage:
In:  ALPHA: function name
     X: x
Out: X: f'(x)

00 { 30-Byte Prgm }
01>LBL "DER"
02 ENTER
03 ABS
04 X#0?
05 LOG
06 IP
07 9
08 -
09 10^X
10 STO "h"
11 COMPLEX
12 ASTO ST L
13 XEQ IND ST L
14 COMPLEX
15 RCL/ "h"
16 END
Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)