BisecnewtP2 - 4 Solvers for hp-15c programmable calculators - J E Patterson

J E Patterson - jepspectro.com

Description

08082019

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.

Solver:

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.

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 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.

Solver

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.

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

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)
.5 Secant x2

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