[Tex/LaTex] How to plot Lambert W function with pgfplots

asymptotepgfplotstikz-pgf

I would like to plot the two main branches of the Lambert W function with different colors using pgfplots like here but I don't know how to do it. Is there some way to reflect a function with respect to a line? I am using LuaLaTeX.

Best Answer

Since the Lambert W function is a multivalued non-elementary function, then PGFplots can't plot it by just typing \addplot {LambertW(x)};.

On the other hand, the inverse function, y e^y is elementary and can easily be plotted and as a result, a simple parametric plot will do the trick. If you want to have the -1th and 0th branch of the Lambert W function drawn in different colours; you'll need to plot them separately. The turning point happens at (-1/e, -1), hence splitting the domain at -1 in the following example.

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      samples=1001,
      enlarge y limits=false,
      axis lines=middle,
    ]
    \addplot [red!80!black, domain=-5:-1] (x * exp(x), x);
    \addplot [blue!80!black, domain=-1:2] (x * exp(x), x);
  \end{axis}
\end{tikzpicture}
\end{document}

output