Secant Solver - J E Patterson

Description

Secant Solver

08072018

GSB A runs a self contained 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 decimeter by 8 decimeter metal sheet is available, i.e. 400 mm by 800 mm.
The box should hold a volume V of 7.5 cubic decimeters, i.e. 7.5 litres.
How should the metal be folded for the tallest box in decimeters.
We are using decimeters 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 decimeters or 29.74 mm - a flat box.
1 ENTER 2 GSB A gives x = 1.5 decimeters or 150 mm - a reasonable height box.
2 ENTER 3 GSB A gives x = 1.5 decimeters or 150 mm - a reasonable height box.
3 ENTER 4 GSB A gives x = 4.2026 decimeters or 420.26 mm - an impossible box.
4 ENTER 2 GSB A points to the correct answer of 1.5 decimeters in spite of a potential divide by zero error.

Solver:

Choose two starting points x0 and 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

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 019 43 20 g x=0 038 43 32 g RTN
001 42,21,11 f LBL A 020 42 36 f RAN # 039 42,21,15 f LBL E
002 44 .0 STO . 0 021 10 ÷ 040 36 ENTER
003 33 R⬇ 022 30 041 36 ENTER
004 44 .1 STO . 1 023 44 .5 STO . 5 042 36 ENTER
005 42 36 f RAN # 024 45 .1 RCL . 1 043 6 6
006 44 .3 STO . 3 025 44 .0 STO . 0 044 30
007 42,21, 1 f LBL 1 026 45 .4 RCL . 4 045 20 ×
008 45 .0 RCL . 0 027 44 .3 STO . 3 046 8 8
009 45,30, .1 RCL . 1 028 45 .5 RCL . 5 047 40 +
010 44 .2 STO . 2 029 44 .1 STO . 1 048 20 ×
011 45 .1 RCL . 1 030 45 .4 RCL . 4 049 4 4
012 32 15 GSB E 031 43 34 g RND 050 20 ×
013 44 .4 STO . 4 032 43,30, 0 g TEST x≠0 051 7 7
014 45 .1 RCL . 1 033 22 1 GTO 1 052 48 .
015 45 .4 RCL . 4 034 45 .1 RCL . 1 053 5 5
016 45,20, .2 RCL × . 2 035 43 32 g RTN 054 30
017 45 .3 RCL . 3 036 42,21,14 f LBL D 055 43 32 g RTN
018 45,30, .4 RCL . 4 037 45 .1 RCL . 1