![atan2 no funciona con pgfplots](https://rvso.com/image/298786/atan2%20no%20funciona%20con%20pgfplots.png)
Al intentar trazar la amplitud y el argumento de dos ondas sinusoidales en términos de sus amplitudes y fases usando pgfplots
, aparece un error al compilar la función atan2 (compatible con pgfmath
). Aquí está el ejemplo
\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}
Los errores son:
(line 31) Illegal unit of measure (pt inserted) {atan2(-sin(y),x-cos(y)}
y
(line 31) Missing = inserted for \ifdim. {atan2(-sin(y),x-cos(y)}
repetido varias veces
Respuesta1
Como ya mencionaron otros, la unidad de punto flotante (FPU) enviada con PGF actualmente no tiene implementación de atan2 (debería tener una; esto es un error).
Una solución alternativa es configurar pgfplots
que no utilice la FPU. Esto funciona para la imagen en cuestión (pero, en general, no se recomienda ya que limita tanto el rango de datos como la precisión).
\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}
Respuesta2
Puede fingirlo recurriendo a atan2
la función PGF haciéndose pasar por una implementación de fpu.
\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}
La salida es la misma que la imagen de John Kormylo.
Respuesta3
Creo que acerté en los cuadrantes.
\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}