I want to draw a repeating function that is similar to a sawtooth function. So far I started with this post and manually defined three tooths:
% starting https://tex.stackexchange.com/questions/132476/piecewise-function-using-pgfplots
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[
declare function={
func(\x) =
% 1st tooth
% shifted by 0
(\x<=1) * (3*(\x-0)) +
% shifted by 1
and(\x>1, \x<=4) * (-1*(\x-1)+3) +
% 2nd tooth
% shifted by 4
and(\x>4, \x<=5) * (3*(\x-4)) +
% shifted by 5
and(\x>5, \x<=8) * (-1*(\x-5)+3) +
% 3rd tooth
% shifted by 8
and(\x>8, \x<=9) * (3*(\x-8)) +
% shifted by 9
and(\x>9, \x<=12) * (-1*(\x-9)+3);
}
]
\begin{axis}[
axis x line = middle,
axis y line = middle,
samples = 1200, % I need sharp edges
grid,
]
\addplot[red,
thick,
domain=0:12,
mark=none,
sharp plot
]
{func(x)-1}; % y shift by -1
\end{axis}
\end{tikzpicture}
\end{document}
Here are my questions:
1st Question (the most important one)
Is there a clever way to define the function for an arbitrary number of tooths without manually defining every tooth?
I do not get modulo
stuff in Jake's answer here - maybe this is the key.
2nd Question (nice to have)
The rising slope is +1 and the fallimg slope is -3. The period in the example is +4 and teh amplitude is +3. Can those somehow be parameters of the function? Of course, the four parameters are connected.
3nd Question (also nice to have)
I would like to number the maxima and minima like in the picture. But this is really a "first world problem".
Update
Until I have a LaTeX solution I have made a poor mans solution using Excel.
The text is German and means something like torque-angle-diagram.
Additional information regarding the answer of percusse
- With a as period and b as the fraction of the rising edge (0.1 --> 10 %).
- I have a German system therefore the decimal separator is a comma (,) in the following pictures.
func(\x,\myfreq) = mod(\x,\myfreq);
with frequency. The rising and falling edge requires an additional if condition and a second parameter for the percentage of the tooth. I'll check when I access a computer unless someone else chimes in – percusse Feb 12 '16 at 11:40