Why does the plot of $f(x)=|\cos x|-|\sin x|$ look almost piecewise linear?

I recently stumbled upon an interesting plot that I - even until today - could not quite explain:

It's the plot of $$f(x) = \lvert \cos(x) \rvert - \lvert \sin(x) \rvert$$. I mean this is almost piecewise linear...

I tried to derive this shape from the Taylor series but I could not quite see it. Does anyone have some mathematical intuition for me concerning the shape of this plot?

• You could also try and find the fourier series of said function and compare it to the fourier of a piece wise linear? Jul 15, 2020 at 13:42

Of course, the function must have period at most $$2 \pi$$. Over $$[0,2 \pi]$$, we have $$f(x) = |\cos(x)| - |\sin(x)| = \begin{cases} \cos(x) - \sin(x) & 0 \leq x < \pi/2\\ -\cos(x) - \sin(x) & \pi/2 \leq x < \pi\\ -\cos(x) + \sin(x) & \pi \leq x < 3\pi/2\\ \cos(x) - \sin(x) & \pi \leq x \leq 2 \pi. \end{cases}$$ With the sum to product identities, we can rewrite this as $$f(x) = \begin{cases} \sqrt{2}\sin(\pi/4 - x) & 0 \leq x < \pi/2\\ \sqrt{2}\sin(x - 3 \pi/4) & \pi/2 \leq x < \pi\\ -\sqrt{2}\sin(\pi/4 - x) & \pi \leq x < 3\pi/2\\ -\sqrt{2}\sin(x - 3 \pi/4) & \pi \leq x \leq 2 \pi \end{cases} \implies\\ f(x) = \begin{cases} \sqrt{2}\sin(\pi/4 - x) & 0 \leq x < \pi/2\\ \sqrt{2}\sin(x - 3 \pi/4) & \pi/2 \leq x < \pi\\ \sqrt{2}\sin(\pi/4 - (x - \pi)) & \pi \leq x < 3\pi/2\\ \sqrt{2}\sin((x-\pi) - 3 \pi/4) & \pi \leq x \leq 2 \pi. \end{cases}$$ In other words, we see that $$f(x)$$ in fact has a period of $$\pi$$, and over the single period $$[0,\pi]$$ it can more simply be written in the form $$f(x) = \begin{cases} \sqrt{2}\sin(\pi/4 - x) & 0 \leq x < \pi/2\\ \sqrt{2}\sin(x - 3 \pi/4) & \pi/2 \leq x \leq \pi. \end{cases}$$ Now, we can conveniently answer the question at hand: why does $$f(x)$$ look piecewise linear? One answer is that because we have $$\sin(x) \approx x$$ when $$|x|$$ is sufficiently small, $$f(x)$$ is well approximated by $$f(x) \approx \begin{cases} \sqrt{2}(\pi/4 - x) & 0 \leq x < \pi/2\\ \sqrt{2}(x - 3 \pi/4) & \pi/2 \leq x \leq \pi. \end{cases}$$

Here is a plot of the two functions, for comparison.

• Phenomenal, thanks! Jul 15, 2020 at 10:02
• I don't think you can approximate sine in such a huge domain. For example $\sin(\frac{\pi}4-x)\neq\frac{\pi}4$ at $x=0$. Jul 15, 2020 at 10:12
• @SameerBaheti It's enough to give an overall impression of linearity. You'll notice that the deviation of the approximation from the true value is greatest at the transition points between piecewise functions Jul 15, 2020 at 10:14
• I Quadrant$$\rightarrow x\in\left(0,\frac{\pi}2\right], f(x) = \lvert \cos x \rvert - \lvert \sin x \rvert=\cos x-\sin x=-\sqrt2\sin\left(x-\frac{\pi}4\right)$$
• II Quadrant$$\rightarrow x\in\left(\frac{\pi}2,\pi\right], f(x) = \lvert \cos x \rvert - \lvert \sin x \rvert=-\cos x-\sin x=-\sqrt2\sin\left(x+\frac{\pi}4\right)$$
• IV Quadrant$$\rightarrow x\in\left(-\frac{\pi}2,0\right], f(x) = \lvert \cos x \rvert - \lvert \sin x \rvert=\cos x+\sin x=\sqrt2\sin\left(x+\frac{\pi}4\right)$$
• III Quadrant$$\rightarrow x\in\left(-\pi,-\frac{\pi}2\right], f(x) = \lvert \cos x \rvert - \lvert \sin x \rvert=-\cos x+\sin x=\sqrt2\sin\left(x-\frac{\pi}4\right)$$

Draw individual graphs in individual domains and you should get the desired graph. Also, it looks linear just because of the scale you have chosen. Actually, it is as sinusoidal as sine is in the intervals of $$\frac{\pi}2$$.