HP Forums
(34S) Function minimization (golden section) - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (34S) Function minimization (golden section) (/thread-8018.html)



(34S) Function minimization (golden section) - nsg - 03-24-2017 02:56 AM

This program finds minimum of a given function on a given interval with given precision.

The function must be unimodal (first strictly decrease then strictly increase) on that interval.
If function is not unimodal, program signals "Root not found".
The program uses golden section method to narrow down on the minimum point.


Input:
LBL 99 -- user function
eps [ENTER] a [ENTER] b XEQ 'UMΦ'
(eps is required precision, a and b -- ends of the interval)
Result:
r01, r04 -- new interval (size less than eps) containing minimum

Used registers:
r00..r09
flag 00

Example:
function at lbl 99 is x^2-5x
1e-6 [ENTER] 0 [ENTER] 5 XEQ 'UMΦ'
Output:
R01~R04~2.5


Code:
0001 LBL'UMΦ'
0002 x<? Y
0003 X↔Y
0004 STO 04
0005 R↓
0006 STO 01
0007 R↓
0008 STO 00
0009 # 2
0010 # Φ
0011 -
0012 STO 09
0013 RCl 04
0014 RCL-01
0015 *
0016 RCL+01
0017 STO 02
0018 XEQ 99
0019 STO 06
0020 RCL 01
0021 RCL-04
0022 RCL*09
0023 RCL+04
0024 STO 03
0025 xeq 99
0026 STO 07
0027 RCL 01
0028 xeq 99
0029 STO 05
0030 RCL 04
0031 xeq 99
0032 STO 08
0033 RCL 04
0034 RCL-01
0035 x<? 00
0036 skip 42
0037 RCL 06
0038 x>=? 07
0039 skip 16
0040 RCL 03
0041 STO 04
0042 RCL 07
0043 STO 08
0044 RCL 02
0045 STO 03
0046 RCL 06
0047 STO 07
0048 RCL 04
0049 RCL-01
0050 RCL*09
0051 RCL+01
0052 STO 02
0053 xeq 99
0054 STO 06
0055 skip 15
0056 RCL 02
0057 STO 01
0058 RCL 06
0059 stp 05
0060 RCL 03
0061 STO 02
0062 RCL 07
0063 STO 06
0064 RCL 01
0065 RCL-04
0066 RCL*09
0067 RCL+04
0068 STO 03
0069 xeq 99
0070 STO 07
0071 SF 00
0072 x<? 05
0073 CF 00
0074 x<? 08
0075 CF 00
0076 FS?C 00
0077 SKIP 2
0078 BACK 45
0079 RTN
0080 ERR 20
0081 LBL 99
0082 X^2
0083 RCL L
0084 5
0085 *
0086 -
0087 RTN
0088 END



RE: (34s) Function minimization (golden section) - Dieter - 03-24-2017 07:40 AM

(03-24-2017 02:56 AM)nsg Wrote:  This program finds minimum of a given function on a given interval with given precision.

May I add a hint that can be useful for this and other programs?

The sequence starting at line 71 seems to use flag 0 as an error flag that is only cleared if X<R05 or X<R08. If the flag is still set an error message is generated.

You can do "or"-tests like these in a much more simple and elegant way by combining two consecutive test commands. Have the the inverse of the first condition followed by the second one. So if you want to test if X<R05 or X<R08, simply write

Code:
0071 X≥? 05
0072 X<? 08
0073 BACK 40
0074 ERR 20

Or if you want to test if flag 1 or flag 2 is set, that's just two lines:

Code:
0001 FC? 01
0002 FS? 02
0003 ...

I assume this trick is as old as programmable calculators (> 40 years), but it still works today. ;-)

Dieter