2

Why the following two MWE codes, using pgfplots with and without gnuplot give different results (a phase shift)?

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [thick,mark=none,domain=0:6,samples=1000]  {cos(deg(2*x+atan(1)))};
\end{axis}
\end{tikzpicture}
\end{document}

and

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [thick,mark=none,domain=0:6,samples=1000]  gnuplot {cos(2*x+atan(1))};
\end{axis}
\end{tikzpicture}
\end{document}
  • 2
    atan gives its result already in degrees. I think you want to do cos(deg(2*x)+atan(1)). – Qrrbrbirlbel Sep 24 '13 at 23:48
  • 1
    I get the same result in both cases. – Jake Sep 25 '13 at 7:21
  • 1
    @Jake I get different results; what Qrrbrbirlbel proposes seems the right solution. – egreg Sep 25 '13 at 10:51
1

The PGFmath function atan (as well as all other trigonometrical functions) uses angles in degree (either as input or as output).

The deg function is used to convert an angle in radians in degrees. The correct way to write this function in PGFmath would be

cos(deg(2 * x) + atan(1))

or

cos(2 * deg(x) + atan(1)) = cos(2 * deg x + atan 1) = cos(2 * deg x + 45)

Code

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[thick,domain=0:6, samples=1000]
\addplot [ultra thick] gnuplot {cos(    2*x  + atan(1) )};
\addplot [red]                 {cos(deg(2*x  + atan(1)))};
\addplot [green]               {cos(deg(2*x) + atan(1) )};
\end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

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.