# How does PGFplots calculate trig functions?

In plotting the graph from my last question I have discovered that the plots I'm getting from PGFplots are not entirely correct. I was attempting to plot (4.9/(w^2))*(cosh(w*x)-cos(w*x)) for multiple values of w. With the help of the users 1010011010 and Enthusiastic Student I was able to get good looking plots -- that is until I checked what I should be getting with WolframAlpha.

As w in my above formula goes to 0, the function should asymptote to 4.9*x^2 from above. That is not what the data is showing. Using this code, which is essentially just 1010011010's in the link above,

\documentclass{standalone}
\usepackage{pgfplots}
\def\mycolone{yellow}
\def\mycoltwo{green}
\pgfplotsset{every axis legend/.append style={at={(.5,-.2)}, anchor=north}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10,ymin=-0.5,ymax=100,no markers, grid=both, samples=100, restrict y to domain=0:1000]
\foreach \w in {5,10,...,100} {\edef\tmp{\noexpand\addplot[\mycolone!\w!\mycoltwo, domain=-10:10]}
\pgfmathparse{\w/100}
\edef\x{\pgfmathresult}
\tmp{(4.9/((\w/100)^2))*(cosh(\w*x/100)-cos(\w*x/100))};
\edef\legendentry{\noexpand\addlegendentry{$\omega = \noexpand\pgfmathprintnumber[fixed,fixed zerofill, precision=2]{\x}$}};
\legendentry}
\end{axis}
\end{tikzpicture}
\end{document}


I get the following plots. Here the red plot is (4.9)*x^2.

For reference, here's what WolframAlpha gives me for the highest and lowest values of w plotted above (w=0.05 and w=1) and the plot of (4.9)*x^2:

It's hard to tell, but if I remove the 4.9*x^2 plot you can see that the plot with w=0.05 follows it pretty much exactly.

This is very different behavior from the first image above. So my question is how does PGFplots compute things like exp, cosh, and cos, and how can I get a better approximation in the future?

• Try with cos(\w*x/100 r) to convert radians to degrees for the trig function. Apr 19 '15 at 0:55
• @PaulGessler Ah. OK. What a simple fix. I hadn't even considered that -- I don't ever use degrees. So thanks. If you want to write that as an answer, I'll accept it. Apr 19 '15 at 0:56

The trigonometric functions of pgf assume inputs in of degrees. To input an angle in radians, use the special r operator: replace cos(\w*x/100) with cos(\w*x/100 r).

\documentclass{standalone}
\usepackage{pgfplots}
\def\mycolone{yellow}
\def\mycoltwo{green}
\pgfplotsset{compat=1.12,every axis legend/.append style={at={(.5,-.2)}, anchor=north}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10,ymin=-0.5,ymax=100,no markers, grid=both, samples=100, restrict y to domain=0:1000]
\foreach \w in {5,10,...,100} {\edef\tmp{\noexpand\addplot[\mycolone!\w!\mycoltwo, domain=-10:10]}
\pgfmathparse{\w/100}
\edef\x{\pgfmathresult}
\tmp{(4.9/((\w/100)^2))*(cosh(\w*x/100)-cos(\w*x/100 r))};
\edef\legendentry{\noexpand\addlegendentry{$\omega = \noexpand\pgfmathprintnumber[fixed,fixed zerofill, precision=2]{\x}$}};
\legendentry}
\end{axis}
\end{tikzpicture}
\end{document}


Alternatively, with pgfplots v1.11 or newer, a new key trig format plots allows us to change the angle format for all \addplot commands in-scope of the key setting1. Here, I use trig format plots=rad at the top level to change the behavior for the whole document, but it could also be applied per-axis or per-plot. Note that this will only affect pgfplots' \addplot commands, not any plain TikZ code with trig functions. Additionally, this key is somewhat experimental and may not work properly with more exotic axis types such as polar and smithchart. The package manual mentions that it has been tested only for default axes.

\documentclass{standalone}
\usepackage{pgfplots}
\def\mycolone{yellow}
\def\mycoltwo{green}
\pgfplotsset{
compat=1.12,
every axis legend/.append style={at={(.5,-.2)}, anchor=north},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10,ymin=-0.5,ymax=100,no markers, grid=both, samples=100, restrict y to domain=0:1000]
\foreach \w in {5,10,...,100} {\edef\tmp{\noexpand\addplot[\mycolone!\w!\mycoltwo, domain=-10:10]}
\pgfmathparse{\w/100}
\edef\x{\pgfmathresult}
\tmp{(4.9/((\w/100)^2))*(cosh(\w*x/100)-cos(\w*x/100))};
\edef\legendentry{\noexpand\addlegendentry{$\omega = \noexpand\pgfmathprintnumber[fixed,fixed zerofill, precision=2]{\x}$}};
\legendentry}

1 Thanks to Christian Feuersänger, the pgfplots author himself, for pointing this new method out to me in a comment.
• Related: recent versions of pgfplots allow to say trig format plots=rad to switch the default format from degrees to radians. Apr 19 '15 at 16:59