Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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 at 4:41
1  
I must say, this SE site is the friendliest of the lot. This question has been received amazingly. Thanks. –  Vaibhav Garg Feb 27 at 7:25
    
Someone downvoted, can he\she please explain why? –  Vaibhav Garg Mar 1 at 2:11
1  
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 at 19:33

2 Answers 2

up vote 23 down vote accepted

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

share|improve this answer
    
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 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

enter image description here

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}

enter image description here

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

enter image description here

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

enter image description here

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}

enter image description here

\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}

enter image description here

\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}

enter image description here

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}

enter image description here

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}

enter image description here

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.