2

I would like to plot a sampled sine wave. MWE is below. The error is that \hoek and \sinus are not defined.

\documentclass[12pt]{article}

\usepackage{tikz} \usepackage{pgfplots}

\begin{document}

\begin{tikzpicture} \begin{axis}[] \addplot [domain=0:360,mark=none,samples=201,red]{0.5*sin(x)+0.5}; \foreach \count in {0.0,1.0,...,16.0}{%

\pgfmathsetmacro{\hoek}{\count*360.0/16.0};
\pgfmathsetmacro{\sinus}{0.5*sin(\count*360.0/16.0)+0.5};
\draw (\hoek,\sinus) circle[radius=2pt];

} \end{axis} \end{tikzpicture} \end{document}

1 Answers1

6
  • \foreach loop doesn't work (well) in the pgfplots diagrams.
  • Samples can be simply drawn by use ycomb option:
\documentclass[12pt]{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture} \begin{axis} \addplot [red, domain=0:360,mark=none,samples=201] {0.5sin(x)+0.5}; \addplot [ycomb, red, domain=0:360, mark=, samples=18] {0.5*sin(x)+0.5}; \end{axis} \end{tikzpicture} \end{document}

enter image description here

Edit: A bit more fancy diagram, For fun and exercise ...

enter image description here

\documentclass[12pt, margin=3mm]{standalone}
\usepackage{pgfplots}

\begin{document} \begin{tikzpicture} \begin{axis}[ x = 0.8, xtick={0,30,...,360}, extra y ticks={0, 0.5}, extra y tick style={grid=major, dashed}, ytick={0,0.25,...,1}, ticklabel style={font=\footnotesize}, domain=0:360, ] \addplot [red, thick, mark=none, samples=201] {0.5sin(x)+0.5}; \addplot [ycomb, red, samples=13, mark=, mark options={scale=0.75, fill=white}] {0.5*sin(x)+0.5}; \end{axis} \end{tikzpicture} \end{document}

Zarko
  • 296,517