Numerical Derivative
Description
By Eddie Shore (Eddie's Math and Calculator Blog)
Calculate numerical derivatives of f(x).
Computing accurate numerical derivatives can present a challenge. Often, calculation involves a small increment, usually named 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 h4.
Source: Burden, Richard L. and J. Douglas Faires. "Numerical Analysis 8th Edition" Thomson Brooks/Cole Belton, CA 2005
Instructions:
- Enter the main program (Label B)
- Enter the function f(R1) (Label 0)
- Enter X, press ENTER
- Enter h, press f B
- The approximate numerical derivative is displayed.
Caution: Remember to end the function with the RTN command (g RTN).
Example 1:
Let f(x) = x·ex
Estimate f'(2) with h = 0.0001
Line Key Code Key
001 42 21 0 LBL 0
002 36 ENTER
003 12 ex
004 20 ×
005 43 32 RTN
To find the derivative press:
2 ENTER .0001 f B
Result: f(2) ≈ 22.1672
Example 2:
Let f(x) = -x2 + 2x + 3
Estimate f'(1.5) with h = 0.0001
We can rewrite f(x) as:
f(x) = -x2 + 2x + 3
f(x) = -1 (x2 - 2x - 3)
f(x) = -1 (x (x - 2) - 3)
Use the last form as the function:
Line Key Code Key
001 42 21 0 LBL 0
002 36 ENTER
003 36 ENTER
004 2 2
005 30 -
006 20 ×
007 3 3
008 30 -
009 16 CHS
010 43 32 RTN
To find the derivative::
1.5 ENTER .0001 f B
Result: f'(1.5) ≈ -1
Program Resources
Labels
Name |
Description |
|
B |
Main routine |
|
0 |
The function f(R1). The function starts with R1 loaded on the X register. |
|
Storage Registers
Name |
Description |
|
0 |
The numerical derivative |
|
1 |
X |
|
2 |
h |
|
Program
Line |
Display |
Key Sequence |
|
Line |
Display |
Key Sequence |
|
000 |
|
|
|
019 |
32 0 |
GSB 0 |
|
001 |
42,21,12 |
f LBL B |
|
020 |
8 |
8 |
|
002 |
44 2 |
STO 2 |
|
021 |
20 |
× |
|
003 |
33 |
R⬇ |
|
022 |
44,40, 0 |
STO + 0 |
|
004 |
44 1 |
STO 1 |
|
023 |
45 1 |
RCL 1 |
|
005 |
2 |
2 |
|
024 |
2 |
2 |
|
006 |
45,20, 2 |
RCL × 2 |
|
025 |
45,20, 2 |
RCL × 2 |
|
007 |
30 |
− |
|
026 |
40 |
+ |
|
008 |
32 0 |
GSB 0 |
|
027 |
32 0 |
GSB 0 |
|
009 |
44 0 |
STO 0 |
|
028 |
16 |
CHS |
|
010 |
45 1 |
RCL 1 |
|
029 |
44,40, 0 |
STO + 0 |
|
011 |
45,30, 2 |
RCL − 2 |
|
030 |
45 0 |
RCL 0 |
|
012 |
32 0 |
GSB 0 |
|
031 |
45,10, 2 |
RCL ÷ 2 |
|
013 |
8 |
8 |
|
032 |
1 |
1 |
|
014 |
16 |
CHS |
|
033 |
2 |
2 |
|
015 |
20 |
× |
|
034 |
10 |
÷ |
|
016 |
44,40, 0 |
STO + 0 |
|
035 |
44 0 |
STO 0 |
|
017 |
45 1 |
RCL 1 |
|
036 |
43 32 |
g RTN |
|
018 |
45,40, 2 |
RCL + 2 |
|
|
|
|
|