HP Forums
Finding Min/Max of a function for HP-41C - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP-41C Software Library (/forum-11.html)
+--- Thread: Finding Min/Max of a function for HP-41C (/thread-879.html)



Finding Min/Max of a function for HP-41C - Namir - 03-10-2014 05:56 PM

Finding the minimum/maximum of a function using Newton's method.

Memory Map

R00 = tolerance
R01 = x
R02 = h
R03 = f(x), f''(x)
R04 = f(x+h)
R05 = f(x-h)

HP-41C Implementation

Code:
1 LBL "MINMAX"
2 LBL A
3 "X?"
4 PROMPT
5 STO 01
6 "TOLER?"
7 CF 22
8 PROMPT
9 FC? 22
10 1E-6
11 STO 00
12 LBL 00
13 RCL 01
14 PSE
15 ABS
16 +
17 0.001
18 *
19 STO 02     # calculate and store h
20 RCL 01
21 XEQ E
22 STO 03     # Calculate and store f(x)
23 RCL 01
24 RCL 02
25 +
26 XEQ E
27 STO 04     # Calculate and store f(x+h)
28 RCL 01
29 RCL 02
30 -
31 XEQ E
32 STO 05     # Calculate and store f(x-h)
33 RCL 04
34 +
35 RCL 03
36 2
37 *
38 -
39 RCL 02
40 X^2
41 /
42 STO 03     # calculate and store f''(x)
43 RCL 04
44 RCL 05
45 -
46 RCL 02
47 /
48 2         
49 /         # calculate f'(x)
50 RCL 03
51 /         # calculate diff
52 STO- 01    # x = x - diff
53 ABS
54 RCL 00
55 X<=Y?
56 GTO 00
57 RCL 01
58 "X="
59 ARCL X
60 PROMPT
61 RTN
62 LBL E
63 EXP
64 LASTX
65 X^2
66 3
67 *
68 -
69 RTN

Usage

LBL E has the code for the function whose minimum or maximum you seek.

1) Start the program by pressing [A].
2) The program prompts you for a guess using "X?". Enter a guess for the minimum/maximum and press [R/S].
3) The program prompts you for he tolerance using "TOLER?". Enter the tolerance and press [R/S].
4) The program pauses showing intermediate values for the refined min/max values. When the program has reached the tolerance level it displays the value of the X for the min/max value.