(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 Code: X1,X2 = 1,2 X1,X2 = 1,2 Code: X1,X2 = 3,4 X1,X2 = 3,4 |
|||
« Next Oldest | Next Newest »
|
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)