[Tex/LaTex] How to draw a sinewave in Tikz

tikz-pgf

I am an absolute newbie in LaTeX in general and Tikz in particular and am in the process of exploring various aspects of the ecosystem. During the course of looking Tikz related information I came across this response: Sinewave in Tikz.

This works but I am unable to figure out how this works. Can someone point me or explain how these commands combine to generate a sine wave?

Best Answer

The Last Error has given the snippet from manual and there shouldn't be any error anymore. But still some illustration would be nice. This answer serves that purpose.

Let use consider the construct:

\draw[ultra thick, red] (3,0) sin (4,1)

in

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
    \begin{tikzpicture}
    \draw (0,0) -- (12,0);
    \draw (0.2,1)node[left,font=\tiny] {$y=1$} -- (11.8,1);
    \draw (0.2,-1)node[left,font=\tiny] {$y=-1$} -- (11.8,-1); 
    \foreach \x in {0,0.5,...,12}{
    \draw (\x,-0.2)node [below,font=\tiny,] {\x} -- (\x,0.2) ;
    }
    \draw[ultra thick, red] (3,0) sin (4,1);    %% the real business in this line
    \end{tikzpicture}
\end{document}

It says that starting from the point (3,0) draw a sine curve and end the curve at the point (4,1):

enter image description here

Please note that the sin and cos commands draw only a quarter sine/cos curve and the y coordinate of two points should be different. For example, if you draw

(3,0) sin (11,0)     %%% same y-coordinate

you will get a straight line like:

enter image description here

Now add the line

\draw[ultra thick, blue] (4,1) cos (5,0);    %% the real business in this line

This says that start a cosine curve at (4,1) and end it at (5,0):

enter image description here

The blue curve is the cosine curve. You add sin and cos curves like this continuously and alternatively to get a continuous sine wave:

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
    \begin{tikzpicture}
    \draw (0,0) -- (12,0);
    \draw (0.2,1)node[left,font=\tiny] {$y=1$} -- (11.8,1);
    \draw (0.2,-1)node[left,font=\tiny] {$y=-1$} -- (11.8,-1); 
    \foreach \x in {0,0.5,...,12}{
    \draw (\x,-0.2)node [below,font=\tiny,] {\x} -- (\x,0.2) ;
    }
    \draw[ultra thick, red] (3,0) sin (4,1);    %% the real business in this line
    \draw[ultra thick, blue] (4,1) cos (5,0);    %% the real business in this line
    \draw[ultra thick, red] (5,0) sin (6,-1);    %% the real business in this line
    \draw[ultra thick, blue] (6,-1) cos (7,0);    %% the real business in this line
    \draw[ultra thick, red] (7,0)  sin (8,1);    %% the real business in this line
    \draw[ultra thick, blue] (8,1) cos (9,0);    %% the real business in this line
    \draw[ultra thick, red] (9,0) sin (10,-1);    %% the real business in this line
    \draw[ultra thick, blue] (10,-1) cos (11,0);    %% the real business in this line
    \end{tikzpicture}

\end{document}

enter image description here

All red curves are sine curves and the blue ones are cosines. Instead of putting many separate \draw commands like this, you can stuff all of them in one \draw command:

\draw[ultra thick, red]
    (3,0) sin (4,1) cos (5,0) sin (6,-1) cos (7,0)
          sin (8,1) cos (9,0) sin (10,-1) cos (11,0);

as Peter Grill did in his linked answer