HP Forums
(71B) Newton and Halley's methods with enhanced derivatives estimation - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (71B) Newton and Halley's methods with enhanced derivatives estimation (/thread-9203.html)



(71B) Newton and Halley's methods with enhanced derivatives estimation - Namir - 09-30-2017 08:43 PM

I present versions of Newton's method and Halley's method that use enhanced estimations for the first and second derivatives, using five point central difference schemes. Each listing will prompt you for the initial guess for the root and its tolerance value. Line 20 in each listing defines the function whose root you seek.

Here is the enhanced Newton's method

Code:
10 REM NEWTON'S METHOD WITH ENHANCED DERIVATIVE
15 REM NEXT IS THE DEFINITION OF F(X)=0
20 DEF FNX(X)=EXP(X)-3*X^2
30 INPUT "GUESS? ";X
40 INPUT "TOLER? ";T
50 H = 0.001*(1+ABS(X))
60 F0 = FNX(X)
70 F1 = FNX(X+H) @ F2 = FNX(X+2*H)
80 F9 = FNX(X-H) @ F8 = FNX(X-2*H)
90 D1 = (-F2 + 8 * F1 - 8 * F9 + F8) / 12 / H 
100 D = F0 / D1
110 X = X - D
120 DISP X @ WAIT 1
130 IF ABS(D)>=T THEN 50
140 DISP "ROOT = ";X
150 END

Here is the enhanced Halley's method

Code:
10 REM HALLEY'S METHOD WITH ENHANCED DERIVATIVE
15 REM NEXT IS THE DEFINITION OF F(X)=0
20 DEF FNX(X)=EXP(X)-3*X^2
30 INPUT "GUESS? ";X
40 INPUT "TOLER? ";T
50 H = 0.001*(1+ABS(X))
60 F0 = FNX(X)
70 F1 = FNX(X+H) @ F2 = FNX(X+2*H)
80 F9 = FNX(X-H) @ F8 = FNX(X-2*H)
90 D1 = (-F2 + 8 * F1 - 8 * F9 + F8) / 12 / H 
100 D2 = (-F2 + 16 * F1 - 30 * F0 + 16 * F9 - F8) / 12 / H ^ 2
110 D = F0 / D1 / (1 - F0 * D2 / D1 / 2 / D1)
120 X = X - D
130 DISP X @ WAIT 1
140 IF ABS(D)>=T THEN 50
150 DISP "ROOT = ";X
160 END

You are welcome to add iteration counters to also view the number of iterations needed. Moreover, these counters can also help prevent indefinite iterations in case the guess for the function you are using fails to converge.

Enjoy!