[Tex/LaTex] Plotting function in TikZ

plottikz-pgf

I am trying to plot the following function:

function

Where x=(x-floor(x))

This is what I have written in my .tex-file:

\draw[color=blue,smooth] plot({\x-floor(\x)},{\frac{\cos(\frac{\pi}{6})}{ \cos(\frac{\pi}{3}\{\frac{3*\x}{\pi}\}-\frac{\pi}{6})}});

But this returns a vast amount of errors I quite frankly doesnt understand.

It should look like something like this (curve to the right):
enter image description here

Best Answer

For starters you should think about the function, it can be reduced considerably. Specifically, cos(pi/6) = 1; also in the denominator you have pi/3 * (3*x/pi), that term simplifies to x.

Additionally, since you are not trying to typeset the equation, but rather evaluate the equation you can do away with the \frac and \cos macros.

I've used a \foreach loop to draw the curve as many times as you want. I've also automated drawing the axis ticks on the x axis. Note that by including an 'r' within the cos I've told the function to see the units as radians.

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}


\begin{document}
    \begin{tikzpicture}

        \draw (0, 1.4cm ) -- (0,0) -- (4cm, 0);
        \draw [dotted] (0,1cm) -- (3.5 cm, 1cm);
        \draw [dotted] (0,1.155cm) -- (3.5 cm, 1.155cm);
        \draw (0, 0.15cm) --++(0,-0.30cm) node [below] {$0^\circ$};

        \foreach \y in {0,...,2}{
            \draw ({(pi/3) * (\y+1)}, 0.15cm) --++(0,-0.30cm) node [below] {$\number\numexpr(\y + 1)* 60\relax^\circ$};
            \begin{scope}
                [domain=0:(pi / 3)]
                \draw plot({\x + (pi/3) * \y},{ 1 /  cos(\x r - (pi r)/6)});
            \end{scope}
        }
    \end{tikzpicture}
\end{document}

Running the above gives the following output:

repeating plot

Related Question