atan2 不適用於 pgfplots

atan2 不適用於 pgfplots

嘗試使用 來繪製兩個正弦波的振幅和相位的振幅和幅角pgfplots,編譯 atan2 函數(由 提供支援)時出現錯誤pgfmath。這是例子

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

錯誤是:

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

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

重複幾次

答案1

正如其他人已經提到的,PGF 附帶的浮點單元 (FPU) 目前沒有實現 atan2(它應該有一個;這是一個錯誤)。

解決方法是配置pgfplots它不應使用 FPU。這適用於有問題的圖像(但通常不建議這樣做,因為它限制了數據範圍和精度)。

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

在此輸入影像描述

答案2

您可以透過回退到 PGFatan2函數來假裝它是一個 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}

輸出與 John Kormylo 的影像相同。

答案3

我想我的象限是正確的。

阿坦2

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

相關內容