Post Reply 
HP49-50G, VER 5.03: Newton-Raphson algorithm to find f(X)=0, f non differentiable
01-09-2024, 04:27 PM (This post was last modified: 01-15-2024 04:29 PM by Gil.)
Post: #1
HP49-50G, VER 5.03: Newton-Raphson algorithm to find f(X)=0, f non differentiable

Version 5.03


A few days ago I published 3 programs for the Newton's algorithm (to find a zero value of a function with an initial guess value).

They depended on whether the function f was in the stack or in the directory, and whether f was differentiable or not.

Beside the fact that the programs failed in case of errors cases, the above distinctions were not so handy in comparison with the easy to use and very fast built-in ROOT command of the HP50G.

Consequently, I decided to suppress the post.

Now I present a single, new version of Newton's algorithm, with always two inputs: a) the function (with the variable X) or program containing the function (with the variable X) in stack level 2, and b) the initial Xo guess value in stack level 1.

For the above, like the built-in command ROOT.

The function has not to be differentiable.

Contrarily to built-in command ROOT, however, if, during the process, |f(x)|=infinity or |f(x+h)|=infinity or |f(x-h)|=infinity or f'(x)=0, a corresponding message, with x value and loop number, will indicate the problem and stop the algorithm.

Therefore, in case of failure, the program will not try with another initial guess value Xo.

And, as already said, the program is much slower than the built-in command ROOT.

Code main program NEWTON

NEWTON "\\<< \"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
. \\<<\\>>
\\|> 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
\\>>
\\>>" "


" msg "\\<< \"subroutine
 not to delete!
\" DROP \"After n=\" \\<-n 1 - R\\->I + \",
X=\" + \\<-X DTAG \\->STR DUP '\\<-X' STO SIZE DUP '\\<-s' STO 10 >
IF
THEN \\<-X 1 10 SUB \"
\" + \\<-X 11 \\<-s SUB + \", \" +
ELSE \\<-X
END + \"
\" + \"|f(X\" ROT + + \")|=+\\oo
\\-> Newton imp
\\-> other seed!\" + \\<-fg STOF DOERR
\\>>"


Attached File(s)
.hp  NEWTON.05.03.hp (Size: 1.96 KB / Downloads: 0)
Find all posts by this user
Quote this message in a reply
Post Reply 




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