Post Reply 
(HP71B) Newton's method
11-21-2024, 12:08 AM (This post was last modified: 11-24-2024 05:57 PM by Albert Chan.)
Post: #9
RE: (HP71B) Newton's method
Same as previous post, but with machine figure out negated reciprocal slope S
Since exit condition based on estimated relative error, output H replaced by H/X

10 DEF FNF(X)=EXP(X)-3*X*X
20 INPUT "X1,X2 = ";X,X2 @ H=FNF(X) @ H2=FNF(X2) @ C=2
30 S=(X2-X)/(H-H2) @ H=H*S @ X=X+H @ R=H/X @ DISP X,R,C
40 FOR I=1 TO 30 @ IF 1+R*R=1 THEN EXIT I
50 H=FNF(X)*S @ X2=X+H @ R=H/X2 @ DISP X2 @ IF 1+R*R=1 THEN EXIT I
60 S2=H-FNF(X2)*S @ IF S2 THEN S2=H/S2 @ S=S*S2 @ H=H*S2
70 X=X+H @ R=H/X @ C=C+2 @ DISP X,R,C
80 NEXT I

I name this Aitken solver, because it is based on same principle.
Although, Aitken and Secant are really one and the same

{X1, X2 = X1+S*f(X1), X3 = X2+S*f(X2)} --> Aitken for converged X

S = negated reciprocal slope of previous 2 points {x1,f(x1)}, {x2,f(x2)}
X1 is secant root of previous 2 points
X2 is newton of {X1,f(X1)}, but use (-1/S) for f'(X1)
X3 is newton of {X2,f(X2)}, but use (-1/S) for f'(X2)

X = aitken(X1,X2,X3) = secant(X1,f(X1) , X2,f(X2)). Let X1=X, rinse and repeat

It is easy to see why we need to multiply by S2 each time, to get the right S
From X2 = X + H*S, below 2 versions are equivalent.

< 30 S=(X2-X)/(H-H2) @ ...
> 30 S=(X2-X)/H @ S2=H/(H-H2) @ S=S*S2 @ ...



This is very close to Secant's method

10 DEF FNF(X)=EXP(X)-3*X*X
20 INPUT "X1,X2 = ";X,X2 @ Y=FNF(X) @ C=1
30 FOR I=1 TO 30 @ Y2=FNF(X2) @ C=C+1
40 H=-(X2-X)/(Y2-Y)*Y @ X=X+H @ H=H/X @ DISP X,H,C
50 VARSWAP X,X2 @ Y=Y2 @ IF 1+H*H=1 THEN EXIT I
60 NEXT I

Secant's method is not quite quadratic convergence. φ ≈ 1.61804
If we group iterations as f pairs, like Aitken, φ² = φ+1 ≈ 2.61804.

I should have adjusted exit condition to match this rate.
With same exit condition, Secant may need 1 more iteration to match Aitken.
Also, Secant X1, X2 order matters here. Ideally, we want X2 closer to root.

Aitken solver is more Newton like. {X1,X2} arguments are to setup {X,S}



Update:

Aitken code showed X2 too, and now also used to test for exit.
This allowed better comparison between Aitken and Secant's method

Below is side-by-side comparsion (left = Aitken, right = Secant)
Code:
X1,X2 = -1,0                                        X1,X2 = -1,0
-.275321257596       -2.63212055884        2        -.275321257596       -2.63212055884        2
-.421770900672                                      -.588196207259        1                    3
-.46545523822         .408490366015        4        -.43936481325         .373365255266        4
-.457556913325                                      -.457108107413       -.286777017778        5
-.458955061365       -1.41629919827E-2     6        -.458991546782        4.27605555479E-2     6
-.45896223854                                       -.45896222444         4.03980312106E-3     7
-.458962267537        1.57010121882E-5     8        -.458962267536       -6.37944512844E-5     8
-.458962267537                                      -.458962267537        9.39007171672E-8     9
Code:
X1,X2 = 1,2                                         X1,X2 = 1,2
 .934926430466       -6.96028772038E-2     2         .934926430466       -6.96028772038E-2     2
 .91754775157                                        .91725948208        -1.18040809506        3
 .910115652211       -2.72611268631E-2     4         .910111544594       -2.72657632129E-2     4
 .910009586479                                       .910008015202       -7.96857473484E-3     5
 .910007572616       -1.18767797141E-4     6         .910007572514       -1.14254082314E-4     6
 .910007572488                                       .910007572487       -4.86495824593E-7     7
Code:
X1,X2 = 3,4                                         X1,X2 = 3,4
 3.51170436248        .145713963836        2         3.51170436248        .145713963836        2
 3.77004656428                                       3.68065825616       -8.67621282961E-2     3
 3.72474392675        5.71957612275E-2     4         3.74559850253        6.24450644924E-2     4
 3.73454113046                                       3.73246065052        1.38788855967E-2     5
 3.73306774577        2.22975300337E-3     6         3.73307193242       -3.35556623094E-3     6
 3.73307910026                                       3.73307903269        1.65649364591E-4     7
 3.73307902864        3.02240229434E-6     8         3.73307902864        1.9009014104E-6      8
 3.73307902862
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(HP71B) Newton's method - Albert Chan - 10-01-2023, 03:59 PM
RE: (HP71B) Newton's method - Albert Chan - 10-01-2023, 04:34 PM
RE: (HP71B) Newton's method - Albert Chan - 10-01-2023, 09:47 PM
RE: (HP71B) Newton's method - Albert Chan - 10-02-2023, 04:04 PM
RE: (HP71B) Newton's method - Albert Chan - 11-12-2024, 12:12 PM
RE: (HP71B) Newton's method - Albert Chan - 11-13-2024, 01:12 PM
RE: (HP71B) Newton's method - Albert Chan - 11-13-2024, 08:38 PM
RE: (HP71B) Newton's method - Albert Chan - 11-18-2024, 01:22 PM
RE: (HP71B) Newton's method - Albert Chan - 11-21-2024 12:08 AM



User(s) browsing this thread: 1 Guest(s)