Secant Solver - J E Patterson

Description

Secant Solver

20191010

GSB A runs a secant solver which acts on f(x) = 0 under label E. This program was written to test the hp-15c Simulator by Torsten Manz.

Label E can hold any equation to be solved. Note that some of the Owner's Handbook examples may require some extra ENTER statements at the beginning as the stack is expected to be filled with x. The open box example on page 189 requires three ENTER statements at the beginning. RAN# is substituted for f(x0)-f(x1) if a divide by zero error would occur. If the equation to be solved has a square root term an error will occur for negative inputs. Use g TEST 2, 0 as the first two equation program statements. This is a test for a negative input and sets it to zero as the lowest valid input. Negative inputs may occur during the iteration.

A problem from the hp-15c Owner's Handbook - page 189:

A 4 decimetre by 8 decimetre metal sheet is available, i.e. 400 mm by 800 mm.
The box should hold a volume V of 7.5 cubic decimetres, i.e. 7.5 litres.
How should the metal be folded for the tallest box in decimetres.
We are using decimetres rather than mm because equation entry is simplified.
Let x be the height.
Volume V = (8 - 2x)(4-2x)x. There are two sides and two ends of height x.
Rearrange to f(x) = 4((x - 6)x + 8)x - V = 0 and solve for the height x.

Instructions:

The Secant solver uses the box problem to display two roots outside the bracketed guesses.
There are 3 solutions depending on the guesses.

0 ENTER 1 GSB A gives x = 0.2974 decimetres or 29.74 mm - a flat box.
1 ENTER 2 GSB A gives x = 1.5 decimetres or 150 mm - a reasonable height box.
2 ENTER 3 GSB A gives x = 1.5 decimetres or 150 mm - a reasonable height box.
3 ENTER 4 GSB A gives x = 4.2026 decimetres or 420.26 mm - an impossible box.
4 ENTER 2 GSB A points to the correct answer of 1.5 decimetres in spite of a potential divide by zero error.

Solver:

Choose two starting points x0 and x1.
If x0 = x1 add 1E-7 to x1.
Set f(x0) = RAN# a non-zero, non-integer starting value to avoid possible divide by zero. f(x0) is updated by f(x1) after one iteration.
Do
Calculate f(x1)
Let x2 = x1 - f(x1)*(x0 - x1)/(f(x0) - f(x1))
if f(x0)-f(x1) = 0 then substitute RAN#
If f(x1) = 0 let root = x1, display x1 and exit
else
x0 = x1
f(x0) = f(x1)
x1 = x2
Loop.

The root x1 is found if f(x1) = 0 within an error tolerance determined by the FIX, SCI or ENG significant digits settng.

Notes:

SOLVEkey.pdf has a good explanation of the additional tricks used to solve difficult equations using the built-in Solver.

In SCI and ENG modes on the DM15 and a real hp-15C some roots are not always found. This is because RND is implemented slightly differently in the hp-15C Simulator. Just stop the iteration by pressing any key and examine the register where x is held. This can be done with GSB D. Alternatively change to FIX mode - e.g. FIX 4 before solving.

Guesses can be tested with GSB E. For the Secant solver with multiple roots try choosing both guesses to be on the same side of a root. This may allow a particular root to be found. Otherwise just bracket as usual.

Program Resources

Labels

Name Description
 A Secant solve routine - needs two guesses in x and y
 D Recover root after program stop
 E Formula to Solve = 0
 1 Iteration loop
 2 If two guesses are the same, add 1E-7 to R.1.

Storage Registers

Name Description
.0 x0
.1 x1
.2 x0-x1
.3 f(x0)
.4 f(x1)
.5 x2

Program

Line Display Key Sequence Line Display Key Sequence Line Display Key Sequence
000 022 42 36 f RAN # 044 42,21,14 f LBL D
001 42,21,11 f LBL A 023 10 ÷ 045 45 .1 RCL . 1
002 44 .0 STO . 0 024 30 046 43 32 g RTN
003 34 x↔y 025 44 .5 STO . 5 047 42,21,15 f LBL E
004 43,30, 5 g TEST x=y 026 45 .1 RCL . 1 048 36 ENTER
005 32 2 GSB 2 027 44 .0 STO . 0 049 36 ENTER
006 44 .1 STO . 1 028 45 .4 RCL . 4 050 36 ENTER
007 42 36 f RAN # 029 44 .3 STO . 3 051 6 6
008 44 .3 STO . 3 030 45 .5 RCL . 5 052 30
009 42,21, 1 f LBL 1 031 44 .1 STO . 1 053 20 ×
010 45 .0 RCL . 0 032 45 .4 RCL . 4 054 8 8
011 45,30, .1 RCL . 1 033 43 34 g RND 055 40 +
012 44 .2 STO . 2 034 43,30, 0 g TEST x≠0 056 20 ×
013 45 .1 RCL . 1 035 22 1 GTO 1 057 4 4
014 32 15 GSB E 036 45 .1 RCL . 1 058 20 ×
015 44 .4 STO . 4 037 43 32 g RTN 059 7 7
016 45 .1 RCL . 1 038 42,21, 2 f LBL 2 060 48 .
017 45 .4 RCL . 4 039 26 EEX 061 5 5
018 45,20, .2 RCL × . 2 040 16 CHS 062 30
019 45 .3 RCL . 3 041 7 7 063 43 32 g RTN
020 45,30, .4 RCL . 4 042 40 +
021 43 20 g x=0 043 43 32 g RTN