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

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.

• Possibly a bug in the R code for LambertW? Can you try integrating on other intervals to see for what value of x the "non-finite" value is occurring? – Robert Israel Dec 13 '18 at 13:39
• @RobertIsrael I figured it out. It seems there is a LambertW-function and a lambertW-function that are slightly different. – Johanna Dec 13 '18 at 14:32

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.