# Plotting function in TikZ

I am trying to plot the following 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):

• Maybe post the errors? Also consider giving us a minimal working example so we know what documentclass you are using. Any other information like which version of LaTeX are you working with would be helpful as well! – Paul Stiverson Jun 22 '15 at 5:08
• As the function is not smooth, you probably better draw the three arcs one by one by restricting the domains. Otherwise, you will get a "smooth" result without nice kinks. – JohnB Jun 22 '15 at 5:12
• \frac, \cos, \pi are typesetting commands, they are not supported in pgfmath expressions. Instead use operator / and functions cos and pi. without backslash. Also the default for trigonometric functions is degrees, not radians. – Heiko Oberdiek Jun 22 '15 at 6:02

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:

• Thank you very much! This was more than I could hope for :) – fluxmodel Jun 22 '15 at 8:03
• Don't forget that you can and should accept the answer that best solves the question! Our reputations depend on it. – Paul Stiverson Jun 24 '15 at 3:17

Drawing functions with pgfplots (should be the same with TikZ) uses a different syntax than what you are used to when writing functions in LaTeX. Have a look at section 4.3.3 in the pgfplots manual.

So here is an example of your function with pgfplots. Not perfect yet, but you should be able to finish it yourself. (I canceled the pi and the 3 by the way.)

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0.45, ymax=1.05]