Getting that X with the Glog function and Lambert’s W

July 16, 2015

[This article was first published on Data Shenanigans » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Facing a simple, yet frustrating formula like this


and the task to solve it for x left me googling around for hours until I found salvation in Wolfram AlphaWikipedia, and a nice blog post with R-syntax to solve a similar equation.

Using the results from Wolfram Alpha I was able to find the solution with the ‘gsl’ library

# install.packages("gsl")

# create some example data
dat <- data.frame(a = 0.109861, x = 10)
# a is set so that b is roughly 30. 
# Lazy as I am I used Excel and its solver ability to find numbers

# to check if b is close to 30. Using the initial formula
dat$b <- dat$x * exp(dat$a * dat$x)

# solve for x2 and see if x and x2 are similar and close to 10
dat$x2 <- lambert_W0(dat$a * dat$b)/dat$a

#  a        x  b2       x2
#1 0.109861 10 29.99993 10.00001

# Hurray!

Sometimes life can be so easy (after a long time searching for the right results….).

To leave a comment for the author, please follow the link and comment on their blog: Data Shenanigans » R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)