atan2 funktioniert nicht mit pgfplots

atan2 funktioniert nicht mit pgfplots

Beim Versuch, die Amplitude und das Argument zweier Sinuswellen in Bezug auf ihre Amplituden und Phasen mithilfe von darzustellen pgfplots, tritt beim Kompilieren der Funktion atan2 (unterstützt von pgfmath) ein Fehler auf. Hier ist das Beispiel

\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}

Die Fehler sind:

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

Und

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

mehrmals wiederholt

Antwort1

Wie andere bereits erwähnt haben, verfügt die mit PGF gelieferte Gleitkommaeinheit (FPU) derzeit über keine Implementierung von atan2 (sie sollte eine bekommen; dies ist ein Fehler).

Eine Problemumgehung besteht darin, die FPU so zu konfigurieren pgfplots, dass sie nicht verwendet wird. Dies funktioniert für das betreffende Bild (wird aber im Allgemeinen nicht empfohlen, da es sowohl den Datenbereich als auch die Genauigkeit einschränkt).

\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}

Bildbeschreibung hier eingeben

Antwort2

Sie können es vortäuschen, indem Sie auf eine PGF-Funktion zurückgreifen, atan2die vorgibt, eine FPU-Implementierung zu sein.

\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}

Die Ausgabe ist dieselbe wie das Bild von John Kormylo.

Antwort3

Ich glaube, ich habe die Quadranten richtig.

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}

verwandte Informationen