Bisection, Secant and Newton Raphson Solver - J E Patterson

Description

08072018

Introduction

The three solvers in this program have their own particular strengths. With the additional choice of the built-in solver, in the hp-15c Simulator by Torsten Manz, solutions to most root finding problems can be found.

Bisection Solver


GSB A runs a self contained 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.

To compare with the built-in solver press 1 ENTER 2 f SOLVE E.

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:

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

Note:

Choose guesses which bracket the root of interest. Adjust for other roots.

Secant Solver

GSB B runs a self contained 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:

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

0 ENTER 1 GSB B gives x = 0.2974 decimeters or 29.74 mm - a flat box.
1 ENTER 2 GSB B gives x = 1.5 decimeters or 150 mm - a reasonable height box.
2 ENTER 3 GSB B gives x = 1.5 decimeters or 150 mm - a reasonable height box.
3 ENTER 4 GSB B gives x = 4.2026 decimeters or 420.26 mm - an impossible box.
4 ENTER 2 GSB B points to the correct answer of x = 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.

Newton Raphson solver

GSB C runs a self contained Newton Raphson solver which acts on f(x) = 0 under label E. Only one guess is required.

Instructions

For the box problem there are 3 solutions depending on the guesses.
0 GSB C gives x = 0.2974 decimeters or 29.74 mm - a flat box
1 GSB C gives x = 1.5 decimeters or 150 mm - a reasonable height box.
2 GSB C gives x = 1.5 decimeters or 150 mm - a reasonable height box.
3 GSB C gives x = 0.2974 decimeters 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 decimeters 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 - exit if f(x1) = 0 within an error tolerance determined by the FIX, SCI or ENG significant digits settng.
Display the root x1

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 GSB D. 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 root or to the other side.

Program Resources

Labels

Name Description Name Description
 A Bisection solve routine - needs two guesses in x and y  1 Bisection loop updates x2 and either x0 to x1 or x1 to x0
 B Secant solve routine - needs two guesses in x and y  2 Bisection - Store x0 to x1 and x2 to x2
 C Newton - Raphson Solve routine - needs one guess in x  3 Bisection - Store x1 to x0 and x2 to x1
 D Recover root after program stop  4 Secant - Iteration loop
 E Formula to be Solved, f(x) = 0  5 Newton - iteration loop

Storage Registers

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