[Tex/LaTex] atan2 does not work with pgfplots

math-operatorspgfplotstikz-pgf

Trying to plot the amplitude and argument of two sine waves in terms of their amplitudes and phases using pgfplots, An error appears while compiling the atan2 function (supported by pgfmath). Here is the example

\documentclass[]{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
title={$\sqrt{x^2+1-2x\cos(y)}$},
xlabel=$x$, ylabel=$y$,
]
\addplot3[surf,domain=0:1,domain y=0:360,]
{sqrt(x^2+1-2*x*cos(y))};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
  title={$atan2(-\sin(y),x-cos(y)$},
 xlabel=$x$, ylabel=$y$,
]
\addplot3[surf,domain=0:1,domain y=0:360,]
{atan2(-sin(y),x-cos(y)};
  %{sqrt(x^2+1-2*x*cos(y))};
\end{axis}
\end{tikzpicture}

\end{document}

The errors are:

    (line 31) Illegal unit of measure (pt inserted) {atan2(-sin(y),x-cos(y)}

and

    (line 31) Missing = inserted for \ifdim. {atan2(-sin(y),x-cos(y)}

repeated several times

Best Answer

As others already mentioned, the floating point unit (FPU) shipped with PGF currently has no implementation of atan2 (it should get one; this is a bug).

A workaround is to configure pgfplots that it should not use the FPU. This works for the image in question (but is, in general, not recommended since it limits both data range + precision).

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
  title={$atan2(-\sin(y),x-cos(y)$},
 xlabel=$x$, ylabel=$y$,
 use fpu=false
]
\addplot3[surf,domain=0:1,domain y=0:360,]
{atan2(-sin(y),x-cos(y))};
  %{sqrt(x^2+1-2*x*cos(y))};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here