# pgfplots: More efficient way for declaring piecewise function (sawtooth with rising and falling edge)

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,
]
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. • 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.       • Well the regular sawtooth is this 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
• @percusse I appreciate the help! – Dr. Manuel Kuehner Feb 12 '16 at 12:02

Here is one way of implementing the function. The parameters are the frequency period of the teeth and the percentage that defines how much of the tooth is spend on rising.

The function is defined to map into [0,1] so you can shift it by adding and scale it by multiplication.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[
declare function={func(\x,\a,\b) = (mod(\x,\a)/\a<\b? % If
mod(\x,\a)/\b/\a: % Yes
(\a-mod(\x,\a))/(\a-\b*\a));} % No
]
\begin{axis}[axis x line = middle,axis y line = middle,
samples = 301,grid,ymax=1.1,ymin=0,domain=0:4, no marks,thick] 