Integration of sawtooth, square and triangle wave functions

Well, in principle there's nothing wrong with the definitions you have. Mathematically, functions are defined extensionally, so two different-looking functions that have the same output on all inputs are actually the same function, just written in different forms.

That said, when dealing with custom user-specified functions that are periodic, I think it's easier to define them on an interval spanning one period, and then extend them to the rest of the real line using periodicity. That is, you define $g\colon [0,T)\to\mathbb R$ whatever way you like, where $T$ is the period of your waveform, and then let $f(x) = g(x\bmod T)$ for any $x\in\mathbb R$. For your examples, I'd define $$\begin{align} g_{\text{sawtooth}}(x) &= \frac{2x}T-1, \\ g_{\text{square}}(x) &= \begin{cases}1&\text{if } x<T/2,\\-1&\text{if }x\ge T/2,\end{cases}\\ g_{\text{triangle}}(x) &= \begin{cases}\frac{4x}T-1&\text{if } x<T/2,\\3-\frac{4x}T&\text{if }x\ge T/2.\end{cases} \end{align}$$ These are slightly different from the ones in your question. They all have amplitude $1$ and mean $0$, and the square and triangle waves behave sort of like $\sin$ and $-\cos$ respectively.

Now you want to integrate these. There's a nice way to integrate a periodic function, by breaking the interval of integration into periods: $$\begin{align} \int_0^xf(t)\,\mathrm dt &= \int_0^Tf(t)\,\mathrm dt+\int_T^{2T}f(t)\,\mathrm dt+\cdots+\int_{(n-1)T}^{nT}f(t)\,\mathrm dt+\int_{nT}^xf(t)\,\mathrm dt \\ &= n\int_0^Tf(t)\,\mathrm dt+\int_0^{x-nT}f(t)\,\mathrm dt. \end{align}$$ If you pick $n=\lfloor x/T\rfloor$, and use the fact that $f(t)=g(t)$ when $t\in[0,T)$, this becomes $$\int_0^xf(t)\,\mathrm dt=nT\bar g+\int_0^{x\bmod T}g(t)\,\mathrm dt,$$ where $\bar g$ is the mean value of $g$ over $[0,T)$. (This is one reason why I made it zero in the above examples, so this term drops out.) So all you really need to find analytically is the indefinite integral of $g$ over $[0,T)$. I imagine you can do that, especially with the simple definitions above.


I am going to focus on your sawtooth example:

We usually write mod like this

$$ f(x)\equiv f_cx\mod{ 1 } $$

or this

$$ f(x)\equiv f_cx\pmod{ 1 } $$

in mathematics.

Another issue is that normally I would only use the mod operator for integers. I'm not saying what you are doing is strictly wrong, but it strikes me as unnatural mathematically, where the mod operator is used on integers to build arithmetics like rings. See Wikipedia for more on that. You will also run into problems on computers where the mod operator is usually defined for integers only.

I would say your functions don't have a particular "best" description, but some descriptions are better than others. The "best" for implementation on a computer would probably be something with a lot of conditions, like this:

$$ f(x)=\left\{ \begin{matrix} ...\\ c(x+1)&, -1<x\le0\\ cx&, 0<x\le1\\ c(x-1)&, 1<x\le2\\ ... \end{matrix}\right. $$

This representation also shows what you intend, so there is really nothing wrong with it.

Mathematically, other "good" representations would have to do with infinite expansions, either Taylor series or Fourier series. I'm not sure there would be a proper Taylor series expansion of these functions, and since these are waves, the Fourier series is appropriate anyway. It so happens that the Forier series expansion of the sawtooth function is given on Wikipedia:

$$ f(x)=2\sum_{n=1}^\infty {(-1)^{(n+1)} \over n} \sin(nx), x - \pi \notin 2 \pi \mathbb{Z} $$

If you wanted the peaks to apear at multiples of 1 instead of $\pi$, that would just be a matter of:

$$ f(x)=2\sum_{n=1}^\infty {(-1)^{(n+1)} \over n} \sin(n(2 \pi x + 1)), x \notin \mathbb{Z} $$

Now, both of this and the conditional form are pretty easy to integrate.

The square wave is also very easy to integrate: start by thinking of it as a constant function. You should get a triangle wave of the same period. Note that at the end of each "cycle" you should get 0.

The triangle wave is best integrated in sections in the conditional form. You should get segmented parabolas. Again, the end of each cycle should give you 0.