Lambert W Subroutine

I recently had an interesting assignment in which I had to create a LambertW solution in Fortran95. While it isn’t the hardest thing to code once you understand how to do it, it still took a little to figure this one out. Here’s a piece of code in Fortran and this is newtons method. There are links below describing exactly how it works, and an example of Newtons method in Python as well. enjoy.

subroutine Lambert(n,a,finalp)
  implicit none
    real :: n, a, y, z, firstW, lastW, CurrentW, expW
    real :: eps = 0.00000000001
    integer :: finalp
        expW = exp(lastW)
        z = (-a)*(exp(-a))
        CurrentW = LastW - ((LastW * expW - z) / ((expW * (LastW + 1)) - ((LastW + 2) * (LastW * expW) / (2 * LastW + 2 -z))))
        if(lastW - CurrentW < eps) exit loop
        lastW = CurrentW
        end do loop
 end subroutine Lambert

and here are some useful Links:

Lambert W Function Explained with solutions

Python Solution

Leave a Reply

Your email address will not be published. Required fields are marked *