|
(50G) Ver 6 Aitken-Steffensen Delta-2 & Newton's algorithms +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html) +--- Forum: General Software Library (https://www.hpmuseum.org/forum/forum-13.html) +--- Thread: (50G) Ver 6 Aitken-Steffensen Delta-2 & Newton's algorithms (/thread-24979.html) |
(50G) Ver 6 Aitken-Steffensen Delta-2 & Newton's algorithms - Gil - 2026-04-21 17:28 1) Aitken-Steffensen algorithm Suppose that you have to solve X=f(X)='0.01+0.9*SIN(X)'. Put, in stack level 2, the expression '0.01+0.9*SIN(X)' <containing just the variable X>. Put then, in stack level 1, an approximate value of X, say 0.01. Run Aitken program. New Aitken-Steffensen code \<< "Sol Aitken-Steffensen X=f(X) (not f(X)=0) 2 ARG: .f [in Stack 2] .initial guess # Xo for X [in Stack 1] .Xo & X better not complex # \|> f is expr: ' X ' \|> f with var X (no other var/letter) \|> f may be NOT differentiable " DROP DTAG \-> f X \<< 'X' 'f' RCL = 'f(X)' 'f' RCL = DEFINE X "With Xo" \->TAG 1. 200. FOR i 'f(X)' \->NUM 'f(f(X))' \->NUM 'f(X)' \->NUM - DUP ABS .000000000001 < IF THEN DROP2 X "X" \->TAG 'f(X)' \->NUM X - "f(X)-X" \->TAG i 1. - "Steps 3*" \->TAG UNROT KILL END INV 'f(X)' \->NUM X - INV - DUP ABS .000000000001 < IF THEN DROP2 X "X" \->TAG 'f(X)' \->NUM X - "f(X)-X" \->TAG i 1. - "Steps 3*" \->TAG UNROT KILL END INV + 'X' STO NEXT X "last X NOT sol" \->TAG 'f(X)' \->NUM X - "f(X)-X" \->TAG \>> \>> 2) Just, as comparison, Newton's algorithm Put here, in stack level 2, the modified expression '0.1+0.9*SIN(X) —X' <that must be equal to zero> and again, in stack level 1, the same approximate value of X, ie 0.01. Run Newton program. Corrected code for Newton's program \<< "Sol f(X)=0 (not X=f(X)) 2 ARG: .f [in Stack 2] .initial guess # for X or z [Stack 1] \|> f with var X (no other var/letter) \|> f like: . ' X ' or . \<< \-> X ' X' \>> or . \<< \-> X \<< X \>> \>> or . \<<\>> \|> inside '' or \<<\>> =0 \|> f may be NOT differentiable " DROP DTAG DUP2 1. * DUP IM 0. == { RE } IFT DUP TYPE 1. == IF THEN DUP RE ELSE DUP END ABS LOG IP 9. - ALOG 0. 0. 999. 0. RCLF .00000000001 \-> f \<-X h \<-n f\180 max \<-s \<-fg \Ge \<< "With Xo" \->TAG -22. CF -103. SF -3. CF -105. SF 'f' RCL TYPE 9. == IF THEN 'f(X)' 'f' RCL = DEFINE END DO 1. '\<-n' STO+ \<-X h + f IFERR EVAL \->NUM THEN DROP DTAG "+h" msg END \<-X h - f IFERR EVAL \->NUM THEN DROP2 DTAG "-h" msg END - \->NUM h / 2 . / DUP 'f\180' STO 0. == IF THEN \<-n 1. - R\->I \<-X DUP f \->NUM DUP ABS \Ge > IF THEN ROT "Stop at n" \->TAG UNROT SWAP "& no sol; last X" ELSE ROT "Iterations n" \->TAG UNROT SWAP "Solution X" END \->TAG SWAP "f(X)" \->TAG \<-fg STOF DUP ABS \Ge > IF THEN 0. R\->I "as f'(X)" \->TAG "f'(X)=0: Impossible to continue Newton's alg" DOERR ELSE KILL END END '\<-X-f(\<-X)/f\180' IFERR EVAL \->NUM THEN 3. DROPN DTAG "" msg END DUP \<-X - ABS SWAP '\<-X' STO \Ge < \<-n max == OR f\180 0. == OR UNTIL END \<-n R\->I "Iterations" \<-n max == "=max" "" IFTE + \->TAG \<-X \<-n max == IF THEN ROT DTAG UNROT "No sol, last X" ELSE "Solution X" END \->TAG DUP f \->NUM "f(X)" \->TAG \<-fg STOF \>> \>> 3) When dealing with converging series above all alternate series, Aitk2 simple program — that uses Aitken Delta-2 — might help find, with three given partial sums S[n], S[n+1] and S[n+2], an approximate value of the final result (S[infinity]). Example ln(2) = sum from n=1 to n=infinity of (-1)^(n+1)/n. Let's calculate the partial sum S1, S2 and S3: 1 for S1, .5 for S2, .833333333333 for S3, and put the above found values respectively in stack level 3, 2 and 1. Run Aitk2, to find 0.7 (vs 0.693147, the real value), a much better value than the initial given partial sums S1, S2 and S3. If we want a still much better value than 0.7, we could calculate for instance S100, S101 and S102: .688172179304 for S100, .698073169403 for S101, .688269247834 for S102, and put again those values respectively in stack level 3, 2 and 1. Run Aitk2, to find now 0.693147. Note that, to get the same value (0.693147) with partial sums, we should go up to S[1000 000], this example showing the interest of that very simple method. Code Aitk2 \<< "3 Arg/partial sums: S[n] S[n+1] S[i+2] of conv. series works better in oscillat.series " DROP \-> S0 S1 S2 \<< '(S0*S2-S1^2)/(S0-2*S1+S2)' \->NUM "A[n]" \->TAG \>> \>> Idea of the program when reading the reference: https://fr.wikipedia.org/wiki/Delta-2#/languages given by Albert Chan in one of his last posts. Thanks to Thomas Klemm for having spotted an import error in Newton's code. |