8

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

  • Welcome to TeX.SX! – user31729 Sep 30 '14 at 12:39
  • It runs using {atan(-sin(y)/(x-cos(y)))}; I tried using ifthenelse to handle the quadrants but got errors about badly formed numbers. I suspect the problem is that atan2 is not implemented in the fpu library. – John Kormylo Sep 30 '14 at 17:07
  • Even {ifthenelse(x > .5, 1, 0)}; doesn't work. Weird! \pgfkeys{/pgf/fpu=false} doesn't help eiher. – John Kormylo Sep 30 '14 at 18:00
3

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

5

You can fake it by falling back to PGF atan2 function pretending to be a fpu implementation.

\documentclass[]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\makeatletter
\pgfmathdeclarefunction{myatan2}{2}{%
\begingroup%
  \pgfmathfloattofixed{#1}\edef\tempa{\pgfmathresult}%
  \pgfmathfloattofixed{#2}%
  \pgfkeys{pgf/fpu=false}%
  \pgfmathparse{atan2(\tempa,\pgfmathresult)}\pgfkeys{/pgf/fpu}%
  \pgfmathfloatparsenumber{\pgfmathresult}%
  \pgfmath@smuggleone\pgfmathresult%
\endgroup
}
\makeatother

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

Output is the same as John Kormylo's image.

2

I think I got the quadrants right.

atan2

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  title={$atan2(-\sin(y),x-cos(y))$},
 xlabel=$x$, ylabel=$y$,
]
\addplot3[surf,domain=0:1,domain y=0:360,]
{x == cos(y) ? ( -sin(y) > 0 ? 90: -90) :
 (x > cos(y) ? atan(-sin(y)/(x-cos(y))): 
 (-sin(y) > 0 ? 180+atan(-sin(y)/(x-cos(y))): atan(-sin(y)/(x-cos(y)))-180))};
\end{axis}
\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.