8

How to use sin function in pgfplots? In this post Timing and integration with TikZ PGFlots i need [/pgf/declare function={f=sin(x);}] but generate a line, and [/pgf/declare function={f=sin(x r);}] not work.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
    integral segments/.code={\pgfmathsetmacro\integralsegments{#1}},
    integral segments=3,
    integral/.style args={#1:#2}{
        ybar interval,
        domain=#1+((#2-#1)/\integralsegments)/2:#2+((#2-#1)/\integralsegments)/2,
        samples=\integralsegments+1,
        x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\integralsegments)/2}
    }
}
\begin{tikzpicture}[/pgf/declare function={f=sin(x);}]
\begin{axis}[
    domain=0:10,
    samples=100,
    axis lines=middle
]
\addplot [
    fill=yellow,
    integral segments=20,
    integral=-6:6
] {f};
\addplot [thick] {f};
\end{axis}
\end{tikzpicture}
\end{document}
Regis Santos
  • 14,463
  • Can you elaborate and let us know what you mean by "not work". Ideally you should compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it. And details exactly what you have a problem with. – Peter Grill Oct 08 '11 at 03:40

1 Answers1

8

If you replace /pgf/declare function={f=sin(x);} with /pgf/declare function={f=sin((x)r);}, the function plots as expected, with the x values behaving like radians. If you also change the integral domain to 0:2*pi, you get the following output:

Jake
  • 232,450