Trying to take a numerical integral involving the Lambert W function in R

89 Views Asked by At

I'm trying to get an approximate answer to the following integral:

$\lim_{n \to \infty} \frac{n+1}{2} \int_1^{\ln(n)}xe^{-x}(1-\exp(-x - W(-xe^{-x})))^2 dx $

I'm currently just interested in if it converges to anything, so I want to just plug in a few values of $n$ and see what happens. My R 'code' looks like this:

>W <- function(x){(1001/2)*x*exp(-x)*(1-exp(-x-LambertW(-x*exp(-x))))^2}

>integrate(W, lower=1, upper=log(1000))

Unfortunately, R tells me that I have a non-finite function value. It's my understanding that LambertW calculates the principal branch of the function, which does not do anything strange for values between $-\frac 1e$ and $0$, which is where I am. I tried shifting the lower bound slightly up to avoid the branching point at $-\frac 1e$, but I got the same error message then.

1

There are 1 best solutions below

2
On BEST ANSWER

It seems capital letters REALLY matter with R. The function LambertW only takes positive values, while the function lambertW takes values at least $-\frac 1e$. Switching to the correct Lambert W function-function solved the problem.