Post Reply 
Lambert W Function (hp-42s)
12-29-2022, 04:17 AM (This post was last modified: 12-29-2022 09:08 PM by Albert Chan.)
Post: #51
RE: Lambert W Function (hp-42s)
I discovered a bug, for termination condition.

We used y' == y' + dy^2 termination test, but it is wrong
Assuming Newton's method have quadratic convergence, it should be:

1 == 1 + (relative error)^2
1 == 1 + (dy/y')^2
y'^2 == y'^2 + dy^2

If y' is big, this does not matter, our test implied relative error test too.
It may terminate later than needed, but that's OK.

Problem is if we use this for e^W-1(-ε), which results in y' even smaller than ε
Now, the flawed termination test quit early.

Example, for ε = 1E-99:

-1e-99 XEQ "eW"    → W0(-ε) = 1.
- - + * R/S        → 2.201582623716450630677604534034768e-102

This quit too early (not even 1 correct digit!), we use this guess for next iteration:

CLX + R/S          → 4.281027759337267571527542476904921e-102

We now have 3 correct digits. It will take a few more iterations for convergence.

We use this formula, which is very accurate close to x = -1/e

e^W(x) ≈ 1/e + (x+1/e)/3 + sqrt ((2/e)*(x+1/e))

I am too lazy to code relative error test, and just hard coded right eps for the job.
If y and y' matched 17 digits, this implied y' converged to 34 digits.
Worse case, we have 17+ correct digits.

Code:
00 { 59-Byte Prgm }
01▸LBL "eW"
02 3
03 1/X
04 -1
05 E↑X
06 RCL+ ST Z
07 STO× ST Y
08 STO+ ST X
09 LASTX
10 STO+ ST Z
11 ×
12 SQRT
13 +
14 X<>Y
15 +/-
16 X<>Y     @   y    -x    x    x

17▸LBL 01
18 X=Y?
19 RTN
20 STO ST Y @   y     y     x    x
21 LN
22 1
23 +
24 R↑       @   x  LN(y)+1  y    x
25 RCL+ ST Z
26 X<>Y
27 ÷        @   y'    y     x    x
28 -
29 LASTX
30 X<>Y     @   dy   y'     x    x
31 1ᴇ-17
32 ×
33 X<>Y     @   y'   eps    x    x
34 +
35 LASTX    @   y'  y'+eps  x    x
36 GTO 01
37 END

With this updated eW, we get (all digits correct)

e^W-1(-1E-99) = 4.284330166764374654650589938202028e-102
→ W-1(-1E-99) = -233.4087152660372939791972288026527
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Lambert W Function (hp-42s) - Juan14 - 05-16-2020, 04:07 PM
RE: Lambert W Function (hp-42s) - Werner - 05-17-2020, 07:56 AM
RE: Lambert W Function (hp-42s) - Werner - 05-17-2020, 08:15 AM
RE: Lambert W Function (hp-42s) - Gerald H - 05-17-2020, 09:29 AM
RE: Lambert W Function (hp-42s) - Werner - 05-18-2020, 08:04 AM
RE: Lambert W Function (hp-42s) - Juan14 - 05-17-2020, 12:12 PM
RE: Lambert W Function (hp-42s) - Juan14 - 05-18-2020, 10:51 PM
RE: Lambert W Function (hp-42s) - Juan14 - 05-21-2020, 12:09 AM
RE: Lambert W Function (hp-42s) - Werner - 05-22-2020, 11:39 AM
RE: Lambert W Function (hp-42s) - Werner - 05-23-2020, 04:20 AM
RE: Lambert W Function (hp-42s) - Werner - 06-11-2020, 05:17 AM
RE: Lambert W Function (hp-42s) - Werner - 06-11-2020, 09:20 AM
RE: Lambert W Function (hp-42s) - lyuka - 09-28-2020, 04:06 PM
RE: Lambert W Function (hp-42s) - Werner - 09-30-2020, 09:12 AM
RE: Lambert W Function (hp-42s) - Werner - 10-02-2020, 03:02 PM
RE: Lambert W Function (hp-42s) - Werner - 09-30-2020, 07:08 AM
RE: Lambert W Function (hp-42s) - lyuka - 09-29-2020, 09:21 AM
RE: Lambert W Function (hp-42s) - lyuka - 09-29-2020, 11:17 PM
RE: Lambert W Function (hp-42s) - lyuka - 09-30-2020, 11:04 AM
RE: Lambert W Function (hp-42s) - lyuka - 09-30-2020, 07:16 PM
RE: Lambert W Function (hp-42s) - Werner - 10-01-2020, 09:37 AM
RE: Lambert W Function (hp-42s) - Werner - 10-01-2020, 01:39 PM
RE: Lambert W Function (hp-42s) - lyuka - 10-01-2020, 06:25 PM
RE: Lambert W Function (hp-42s) - lyuka - 10-02-2020, 05:44 AM
RE: Lambert W Function (hp-42s) - lyuka - 10-03-2020, 07:56 PM
RE: Lambert W Function (hp-42s) - Werner - 10-05-2020, 08:03 AM
RE: Lambert W Function (hp-42s) - lyuka - 10-05-2020, 06:09 PM
RE: Lambert W Function (hp-42s) - Werner - 10-06-2020, 06:16 AM
RE: Lambert W Function (hp-42s) - lyuka - 11-09-2020, 08:30 AM
RE: Lambert W Function (hp-42s) - Albert Chan - 12-29-2022 04:17 AM



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