# How to draw a sinewave in Tikz?

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?

-
Good Q: But to what frequency and amplitude you wish to know is another Q due to phase difference of learning :) I am sure texdoc tikz would be too exhaustive may be start with csweb.ucc.ie/~dongen/LAF/TikZ.pdf and Related Links at tex.stackexchange.com/questions/15779/… – texenthusiast Feb 27 '14 at 4:41
I must say, this SE site is the friendliest of the lot. This question has been received amazingly. Thanks. – Vaibhav Garg Feb 27 '14 at 7:25
Someone downvoted, can he\she please explain why? – Vaibhav Garg Mar 1 '14 at 2:11
Once any one edits the Q for eg: due to the current edit here anyone after the edit of Q they can retract their upvotes/downvotes. So someone would have taken back their upvotes since i see no downvotes on your Q – texenthusiast Mar 1 '14 at 19:33

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):

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:

\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):

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}


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);


-
The expository aspects of this answer trump the other one. I'll wait for some time, and accept this one. Also, this was a question was born out of curiosity, not out of an error. – Vaibhav Garg Feb 27 '14 at 5:43

According to Karl's students (who don't care about almost everything in TikZ) on page 30 of TikZ documentation (by invoking texdoc tikz in your terminal, shell, DOS prompt), they said that

## Sine

Remember 3 important behaviors:

• sin (x,y) draws only the first 1/4 of a complete sine curve. In other words, the curve in the first quadrant is drawn.

• the previous point is used as the starting point.

• if the previous point is lower than (x,y) then it draws sine with positive amplitude. Otherwise it draws with negative amplitude.

If you still get confused with these 3 rules, the progressive examples as follows should help you understand its behavior.

\begin{tikzpicture}
\draw[gray] (0,0) grid (5,5);
\draw[red] (0,0) sin (1,2);
\end{tikzpicture}


\begin{tikzpicture}
\draw[gray] (0,0) grid (5,5);
\draw[red] (0,0) sin (1,2) sin (3,5);
\end{tikzpicture}


\begin{tikzpicture}
\draw[gray] (0,0) grid (5,5);
\draw[red] (0,0) sin (1,2) sin (3,5) sin (5,0);
\end{tikzpicture}


## Cosine

Cosine is the "complement" of sine. The following examples should make it clearer.

\begin{tikzpicture}
\draw[gray] (0,0) grid (5,5);
\draw[red] (0,0) sin (1,2);
\draw[blue] (0,0) cos (1,2);
\end{tikzpicture}


\begin{tikzpicture}
\draw[gray] (0,0) grid (5,5);
\draw[red] (0,0) sin (1,2) sin (3,5);
\draw[blue] (0,0) cos (1,2) cos (3,5);
\end{tikzpicture}


\begin{tikzpicture}
\draw[gray] (0,0) grid (5,5);
\draw[red] (0,0) sin (1,2) sin (3,5) sin (5,0);
\draw[blue] (0,0) cos (1,2) cos (3,5) cos (5,0);
\end{tikzpicture}


## Summary

If you want to create a complete sine wave then you need to use both sin and cos alternately as follows,

\begin{tikzpicture}
\draw[gray] (0,-3) grid (5,3);
\draw[green] (0,0) sin (1,2) cos (2,0) sin (3,-2) cos (4,0);
\end{tikzpicture}


because using sin alone does not produce what you want to get as follows.

\begin{tikzpicture}
\draw[gray] (0,-3) grid (5,3);
\draw[red] (0,0) sin (1,2) sin (2,0) sin (3,-2) sin (4,0);
\end{tikzpicture}


-
why does your sine shake/break is it resolution problems – texenthusiast Feb 27 '14 at 4:46
@texenthusiast: No. It is not about resolution but by design feature of sin and cos implemented by TikZ. – kiss my armpit Feb 27 '14 at 4:51
I keep my tutorial above as simple as possible because in my experience reading complicated code just increases confusion. – kiss my armpit Feb 27 '14 at 5:14