BisecnewtP2 - 4 Solvers for the hp-15c - J E Patterson

Description

20191013

The four solvers in this program have their own particular strengths. SOLVE, the built-in solver, is now accessed with GSB D. Using the hp-15c Simulator by Torsten Manz, solutions to most root finding problems can be found.

Bisection Solver


GSB A runs a bisection 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 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 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 can't find a root as there are none in this interval. Press any key to stop.
3 ENTER 4 GSB A can't find a root as there are none in this interval. Press any key to stop.
4 ENTER 5 GSB A gives x = 4.206 decimetres or 420.26 mm - an impossible box height.

Bisection Solver Procedure:

Choose 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.
This solver can find multiple roots in the example f(x) = 0, coded under label E.
0 ENTER 1 GSB A, Answer is 0.2974.
2 GSB A, Answer is 1.5.
3 GSB A, Answer is 1.5.
4 GSB A, answer is 1.5.
5 GSB A, answer is 4.2026.
There 3 roots of which two are useful.
The previous root is the new first guess which gets pushed onto the stack by the second guess.
If not enough roots are found choose smaller intervals or widen the search.
The practical range, given the available metal, is 0 to 8 decimetres.
Secant solvers will not behave so neatly.


Secant Solver

GSB B runs a secant solver which acts on f(x) = 0 under label E.

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

Secant Solver Procedure:

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 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 C runs a Newton-Raphson solver which acts on f(x) = 0 under label E. Only one guess is required.

Instructions

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


0 GSB C gives x = 0.2974 decimetres or 29.74 mm - a flat box
1 GSB C gives x = 1.5 decimetres or 150 mm - a reasonable height box.
2 GSB C gives x = 1.5 decimetres or 150 mm - a reasonable height box.
3 GSB C 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 C gives x = 4.2026 decimetres or 420.26 mm - an impossible box.

Newton-Raphson Solver Prcedure:

Choose a starting point x1
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 D runs the internal hp-15c solver which acts on f(x) = 0 under label E. 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 1 GSB D gives x = 0.2974 decimetres or 29.74 mm - a flat box.
1 ENTER 2 GSB D gives x = 1.5 decimetres or 150 mm - a reasonable height box.
2 ENTER 3 GSB D gives x = 1.5 decimetres or 150 mm - a reasonable height box.
3 ENTER 4 GSB D gives x = 4.2026 decimetres or 420.26 mm - outside the guess interval and an impossible box.
4 ENTER 2 GSB B gives a correct answer of x = 1.5 decimetres which is also outside the guess interval.

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 E. 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 E. For example f(x) = 3x2 - 5x + 1 = 0 is entered using GTO E, 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 Bisection solve routine - two guesses in x and y  1 Bisection loop updates x2 and either x0 to x1 or x1 to x0
 B Secant solve routine - two guesses in x and y  2 Bisection - Store x0 to x1 and x2 to x2
 C Newton-Raphson Solve routine - one guess in x  3 Bisection - Store x1 to x0 and x2 to x1
 D SOLVE - internal solver - two guesses in x and y  4 Secant - Iteration loop
 E Formula to be Solved, f(x) = 0  5 Newton - iteration loop
 0 Enter two guesses  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 32 15 GSB E 088 45 .4 RCL . 4
001 42,21,11 f LBL A 045 44 .4 STO . 4 089 43 34 g RND
002 32 0 GSB 0 046 45 .1 RCL . 1 090 43,30, 0 g TEST x≠0
003 42,21, 1 f LBL 1 047 34 x↔y 091 22 5 GTO 5
004 43, 4, 1 g SF 1 048 45,20, .2 RCL × . 2 092 45 .1 RCL . 1
005 45 .0 RCL . 0 049 45 .3 RCL . 3 093 43 32 g RTN
006 32 15 GSB E 050 45,30, .4 RCL . 4 094 42,21,14 f LBL D
007 44 .3 STO . 3 051 43 20 g x=0 095 32 0 GSB 0
008 45 .1 RCL . 1 052 42 36 f RAN # 096 42,10,15 f SOLVE E
009 45,40, .0 RCL + . 0 053 10 ÷ 097 43 32 g RTN
010 2 2 054 30 098 42,21, 0 f LBL 0
011 10 ÷ 055 45 .1 RCL . 1 099 44 .0 STO . 0
012 44 .2 STO . 2 056 44 .0 STO . 0 100 44 2 STO 2
013 32 15 GSB E 057 34 x↔y 101 34 x↔y
014 44 .4 STO . 4 058 44 .1 STO . 1 102 43,30, 5 g TEST x=y
015 45,20, .3 RCL × . 3 059 45 .4 RCL . 4 103 32 6 GSB 6
016 43,30, 2 g TEST x<0 060 44 .3 STO . 3 104 44 .1 STO . 1
017 32 3 GSB 3 061 43 34 g RND 105 44 1 STO 1
018 43, 6, 1 g F? 1 062 43,30, 0 g TEST x≠0 106 43 32 g RTN
019 32 2 GSB 2 063 22 4 GTO 4 107 42,21, 6 f LBL 6
020 45 .4 RCL . 4 064 45 .1 RCL . 1 108 26 EEX
021 43 34 g RND 065 43 32 g RTN 109 16 CHS
022 43,30, 0 g TEST x≠0 066 42,21,13 f LBL C 110 7 7
023 22 1 GTO 1 067 44 .1 STO . 1 111 40 +
024 45 .2 RCL . 2 068 44 1 STO 1 112 43 32 g RTN
025 43 32 g RTN 069 42,21, 5 f LBL 5 113 42,21,15 f LBL E
026 42,21, 2 f LBL 2 070 45 .1 RCL . 1 114 36 ENTER
027 45 .2 RCL . 2 071 32 15 GSB E 115 36 ENTER
028 44 .0 STO . 0 072 44 .4 STO . 4 116 36 ENTER
029 43 32 g RTN 073 45 .1 RCL . 1 117 6 6
030 42,21, 3 f LBL 3 074 43 20 g x=0 118 30
031 45 .2 RCL . 2 075 12 119 20 ×
032 44 .1 STO . 1 076 26 EEX 120 8 8
033 43, 5, 1 g CF 1 077 4 4 121 40 +
034 43 32 g RTN 078 10 ÷ 122 20 ×
035 42,21,12 f LBL B 079 44 .2 STO . 2 123 4 4
036 32 0 GSB 0 080 45,40, .1 RCL + . 1 124 20 ×
037 42 36 f RAN # 081 32 15 GSB E 125 7 7
038 44 .3 STO . 3 082 45,30, .4 RCL . 4 126 48 .
039 42,21, 4 f LBL 4 083 45,10, .2 RCL ÷ . 2 127 5 5
040 45 .0 RCL . 0 084 45 .4 RCL . 4 128 30
041 45,30, .1 RCL . 1 085 34 x↔y 129 43 32 g RTN
042 44 .2 STO . 2 086 10 ÷ 130 43 32 g RTN
043 45 .1 RCL . 1 087 44,30, .1 STO . 1