I want to draw a custom function using pgfplots
, and add an annotation node:
\begin{tikzpicture}[
>=stealth,
evaluate={
function f(\x) {
return cos(2*\x);
};
}
]
\begin{axis}[
axis lines=middle,
xtick=\empty,
ytick=\empty,
xmin=-200,
xmax=200,
ymin=-1.5,
ymax=1.5
]
\addplot[
domain=-180:180,
samples=361
]{f(x)};
% \node[coordinate,pin=above right:{$\cos 2x$}] at (axis cs:45,{f(45)}) {};
\node[coordinate,pin=above right:{$\cos 2x$}] at (axis cs:45,0) {};
\end{axis}
\end{tikzpicture}
The result is:
My question is about the commented line above, which cannot be compiled correctly. The reason, IMHO, is probably related to the function evaluation process (say, function f
above). I am not quite understand the mechanism of it, so is there any way to solve this problem? An extra explanation is quite appreciated!
BTW: Instead of evaluate
, I have also tried declare function
, which works fine in this scenario; yet the actual custom function involves a complicated expression (defined in a recursive manner), which might not be handled by declare function
.