BisecnewtP3 - 4 Solvers for the hp15c with single entry of guesses - J E Patterson

J E Patterson - jepspectro.com - 20231119

Description


The four solvers in this program have their own particular strengths. GSB A Enters up to two guesses. GSB B runs a Bisection Solver. GSB C runs a Secant Solver. GSB D runs a Newton-Raphson Solver. GSB E runs SOLVE, the built-in solver. Guesses only need to be entered once to run alternative solvers.

Using the hp-15c Simulator by Torsten Manz, solutions to most root finding problems can be found. In this version, guesses are saved for use with alternative solvers.

Bisection Solver


GSB A saves guesses from the stack for use with any of the solvers in this program. Guesses don't need to be re-entered when trying another solver. They are recalled from memory instead.
GSB B runs a bisection solver which acts on f(x) = 0 under label 0.
This program was written to test the hp-15c Simulator by Torsten Manz.

Label 0 can hold any equation of the form f(x) = 0. Note that some of the hp-15C 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.

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

A 4 decimetres 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:

There are 3 solutions depending on the guesses.
Initial guesses can be recovered with RCL 1 and RCL 2


0 ENTER 1 GSB A GSB B gives x = 0.2974 decimetres or 29.74 mm - a flat box.
1 ENTER 2 GSB A GSB B gives x = 1.5 decimetres or 150 mm - a reasonable height box.
2 ENTER 3 GSB A GSB B can't find a root as there are none in this interval. Press any key to stop.
3 ENTER 4 GSB A GSB B can't find a root as there are none in this interval. Press any key to stop.
4 ENTER 5 GSB A GSB B gives x = 4.206 decimetres or 420.26 mm - an impossible box height.

Bisection Solver Procedure:

Enter two guesses x1 and x0.
Do
Set flag 1
Calculate f(x0)
Let x2 = (x1 - x0)/2
Calculate f(x2)
If f(x2)*f(x0) < 0 let x1 = x2, clear flag 1
If flag 1 is set let x0 = x2
Loop until f(x2) = 0 within an error tolerance determined by the FIX, SCI or ENG significant digits setting.
Display root x2.

Notes:

Choose guesses which bracket the root of interest.
There 3 roots of which two are useful.
The practical range, given the available metal, is 0 to 8 decimetres.

Secant Solver

GSB A saves guesses from the stack for use with any of the solvers in this program. Guesses don't need to be re-entered when trying another solver. They are recalled from memory instead.
GSB C runs a secant solver which acts on f(x) = 0 under label 0.

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.

Instructions:

There are 3 solutions depending on the guesses.
Initial guesses can be recovered with RCL 1 and RCL 2


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

Secant Solver Procedure:

Enter two guesses 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 until f(x1) = 0 within an error tolerance determined by the FIX, SCI or ENG significant digits setting.
Display root x1.

Newton Raphson solver

GSB A saves guesses from the stack for use with any of the solvers in this program. Guesses don't need to be re-entered when trying another solver. They are recalled from memory instead.
GSB D runs a Newton-Raphson solver which acts on f(x) = 0 under label E. Only one guess is required. If two guesses are entered only the last one is used.

Instructions

There are 3 solutions depending on the guesses.
The initial guess can be recovered with RCL 1


0 GSB A GSB D gives x = 0.2974 decimetres or 29.74 mm - a flat box
1 GSB A GSB D gives x = 1.5 decimetres or 150 mm - a reasonable height box.
2 GSB A GSB D gives x = 1.5 decimetres or 150 mm - a reasonable height box.
3 GSB A GSB D gives x = 0.2974 decimetres or 29.74 mm - a flat box. Here the secant line now intersects the x axis below the smallest root.
4 GSB A GSB D gives x = 4.2026 decimetres or 420.26 mm - an impossible box.

Newton-Raphson Solver Procedure:

Enter a guess x1
If two guesses are entered only the last one is used.
Do
Calculate f(x1)
If x1 = 0 use 1 instead to avoid a divide by zero from the derivative f'(x1)
define h = 1/10000
calculate f'(x1) ≈ (f(x1+h) - f(x1))/h
calculate f(x1)/f'(x1)
subtract from x1
Loop until f(x1) = 0 within an error tolerance determined by the FIX, SCI or ENG significant digits setting.
Display root x1.

SOLVE - the hp-15c solver

GSB A saves guesses from the stack for use with any of the solvers in this program. Guesses don't need to be re-entered when trying another solver. They are recalled from memory instead.
GSB E runs the internal hp-15c solver which acts on f(x) = 0 under label 0. Two guesses are required. A modified Secant method is used.

Instructions

There are 3 solutions depending on the guesses.
Initial guesses can be recovered with RCL 1 and RCL 2


0 ENTER 2 GSB A GSB E gives x = 0.2974 decimetres or 29.74 mm - a flat box.
1 ENTER 2 GSB A GSB E gives x = 1.5 decimetres or 150 mm - a reasonable height box.
2 ENTER 3 GSB A GSB E gives x = 1.5 decimetres or 150 mm - a reasonable height box.
3 ENTER 4 GSB A GSB E gives x = 4.2026 decimetres or 420.26 mm - outside the guess interval and an impossible box.

Notes:

A TEST=0 ex statement in the Newton-Raphson solver program is a way of entering 1 without it attaching to the following EEX statement. TEST=0 10x or TEST=0 COS could also be used. Also the obvious TEST=0 1 ENTER would also do.

There are useful discussions on Newton's solver for the hp-12C here and here.

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 RCL . 1. Alternatively change to FIX mode - e.g. FIX 4 before solving.

Guesses can be tested with GSB 0. For the Bisection solver look for a sign change. For the Secant solver with multiple roots try choosing both guesses to be on the same side of a root. This can alter the direction of the iteration. For the Newton-Raphson solver try moving the guess closer to the expected root, or to the other side.

Equations are entered under label 0. For example f(x) = 3x2 - 5x + 1 = 0 is entered using GTO 0, changing to program mode with g P/R and deleting any entered function. X is placed on the stack twice as it is needed in two terms.

ENTER ENTER g x2 3 X X<>Y 5 X - 1 +

The roots are 0.2324 and 1.4343, using guesses 0,1 and 1,2.

An alternative form of this equation is: f(x) = x(3x-5)+1 = 0

The program is shorter: ENTER ENTER 3 X 5 - X 1 +.

Program Resources

Labels

Name Description Name Description
 A Enter two guesses GSB A  1 Bisection loop updates x2 and either x0 to x1 or x1 to x0
 B Bisection solve routine  2 Bisection - Store x0 to x1 and x2 to x2
 C Secant solve routine  3 Bisection - Store x1 to x0 and x2 to x1
 D Newton-Raphson Solve routine  4 Secant - Iteration loop
 E SOLVE - internal solver  5 Newton - iteration loop
 0 Formula to be Solved, f(x) = 0  6 If two guesses are the same, add 1E-7 to R1.

Storage Registers

Name Description
 1 Initial guess 1
 2 Initial guess 2
.0 x0
.1 x1
.2 Bisection - x2 = (x0+x1)/2, Secant x0-x1, Newton - h
.3 Bisection - f(x0), Secant - f(x0)
.4 Bisection f(x2), Secant - f(x1), Newton - f(x1)

Flags

Number Description
1 Bisection - If flag 1 is set x0 = x2 else x1 = x2

Program

Line Display Key Sequence Line Display Key Sequence Line Display Key Sequence
000 044 43 32 g RTN 088 10 ÷
001 42,21,11 f LBL A 045 42,21,13 f LBL C 089 44 .2 STO . 2
002 44 .0 STO . 0 046 45 2 RCL 2 090 45,40, .1 RCL + . 1
003 44 2 STO 2 047 45 1 RCL 1 091 32 0 GSB 0
004 34 x↔y 048 42 36 f RAN # 092 45,30, .4 RCL . 4
005 43,30, 5 g TEST x=y 049 44 .3 STO . 3 093 45,10, .2 RCL ÷ . 2
006 32 6 GSB 6 050 42,21, 4 f LBL 4 094 45 .4 RCL . 4
007 44 .1 STO . 1 051 45 .0 RCL . 0 095 34 x↔y
008 44 1 STO 1 052 45,30, .1 RCL . 1 096 10 ÷
009 43 32 g RTN 053 44 .2 STO . 2 097 44,30, .1 STO . 1
010 42,21,12 f LBL B 054 45 .1 RCL . 1 098 45 .4 RCL . 4
011 45 2 RCL 2 055 32 0 GSB 0 099 43 34 g RND
012 45 1 RCL 1 056 44 .4 STO . 4 100 43,30, 0 g TEST x≠0
013 42,21, 1 f LBL 1 057 45 .1 RCL . 1 101 22 5 GTO 5
014 43, 4, 1 g SF 1 058 34 x↔y 102 45 .1 RCL . 1
015 45 .0 RCL . 0 059 45,20, .2 RCL × . 2 103 43 32 g RTN
016 32 0 GSB 0 060 45 .3 RCL . 3 104 42,21,15 f LBL E
017 44 .3 STO . 3 061 45,30, .4 RCL . 4 105 45 2 RCL 2
018 45 .1 RCL . 1 062 43 20 g x=0 106 45 1 RCL 1
019 45,40, .0 RCL + . 0 063 42 36 f RAN # 107 42,10, 0 f SOLVE 0
020 2 2 064 10 ÷ 108 43 32 g RTN
021 10 ÷ 065 30 109 42,21, 6 f LBL 6
022 44 .2 STO . 2 066 45 .1 RCL . 1 110 26 EEX
023 32 0 GSB 0 067 44 .0 STO . 0 111 16 CHS
024 44 .4 STO . 4 068 34 x↔y 112 7 7
025 45,20, .3 RCL × . 3 069 44 .1 STO . 1 113 40 +
026 43,30, 2 g TEST x<0 070 45 .4 RCL . 4 114 43 32 g RTN
027 32 3 GSB 3 071 44 .3 STO . 3 115 42,21, 0 f LBL 0
028 43, 6, 1 g F? 1 072 43 34 g RND 116 36 ENTER
029 32 2 GSB 2 073 43,30, 0 g TEST x≠0 117 36 ENTER
030 45 .4 RCL . 4 074 22 4 GTO 4 118 36 ENTER
031 43 34 g RND 075 45 .1 RCL . 1 119 6 6
032 43,30, 0 g TEST x≠0 076 43 32 g RTN 120 30
033 22 1 GTO 1 077 42,21,14 f LBL D 121 20 ×
034 45 .2 RCL . 2 078 45 2 RCL 2 122 8 8
035 43 32 g RTN 079 42,21, 5 f LBL 5 123 40 +
036 42,21, 2 f LBL 2 080 45 .1 RCL . 1 124 20 ×
037 45 .2 RCL . 2 081 32 0 GSB 0 125 4 4
038 44 .0 STO . 0 082 44 .4 STO . 4 126 20 ×
039 43 32 g RTN 083 45 .1 RCL . 1 127 7 7
040 42,21, 3 f LBL 3 084 43 20 g x=0 128 48 .
041 45 .2 RCL . 2 085 12 129 5 5
042 44 .1 STO . 1 086 26 EEX 130 30
043 43, 5, 1 g CF 1 087 4 4 131 43 32 g RTN