Newton Raphson Solver - J E Patterson

J E Patterson - jepspectro.com

Description

08082019

Newton-Raphson Solver


GSB A runs a Newton-Raphson 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 decimetre 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

For the box problem there are 3 solutions depending on the guesses.
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.

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.

Program Resources

Labels

Name Description
 A Newton-Raphson solve routine - needs one guess in x
 E Formula to be Solved, f(x) = 0
 1 loop

Storage Registers

Name Description
.1 x1
.2 f(x1)
.3 h

Program

Line Display Key Sequence Line Display Key Sequence
000 023 43 34 g RND
001 42,21,11 f LBL A 024 43,30, 0 g TEST x≠0
002 44 .1 STO . 1 025 22 1 GTO 1
003 42,21, 1 f LBL 1 026 45 .1 RCL . 1
004 45 .1 RCL . 1 027 43 32 g RTN
005 32 15 GSB E 028 42,21,15 f LBL E
006 44 .2 STO . 2 029 36 ENTER
007 45 .1 RCL . 1 030 36 ENTER
008 43 20 g x=0 031 36 ENTER
009 12 032 6 6
010 26 EEX 033 30
011 4 4 034 20 ×
012 10 ÷ 035 8 8
013 44 .3 STO . 3 036 40 +
014 45,40, .1 RCL + . 1 037 20 ×
015 32 15 GSB E 038 4 4
016 45,30, .2 RCL . 2 039 20 ×
017 45,10, .3 RCL ÷ . 3 040 7 7
018 45 .2 RCL . 2 041 48 .
019 34 x↔y 042 5 5
020 10 ÷ 043 30
021 44,30, .1 STO . 1 044 43 32 g RTN
022 45 .2 RCL . 2