While writing a solution to homeworks for my students, I had to write the function
$$f(x)=\left\{\begin{array}{ll} \frac{x+2}{2}, & x\leqslant -4\\ \frac{x}{4}, & -4\leqslant x\leqslant 4 \\ \frac{x-2}{2}, & x\geqslant 4 \end{array}\right.$$
using one single formula and absolute values. After trials and errors, I obtained that $f(x)=\frac{4x+|x-4|-|x+4|}{8}$.
My questions:
- Can every continuous piecewise linear function be written as a linear combination of linear functions and absolute values of linear functions?
- If possible, is there a systematic way to do it?
Certainly. To change the slope from $m_1$ to $m_2$ at $a$ without changing the function to the left of $a$ add $$\frac{m_2-m_1}2\cdot (|x-a|+x-a).$$ In your case this leads to \begin{multline*} \frac{x+2}2 + \frac12\left(\frac14-\frac12\right)(|x+4|+x+4) + \frac12\left(\frac12-\frac14\right) (|x-4|+x-4) =\\= \frac12 x-\frac18|x+4|+\frac18|x-4|, \end{multline*} which agrees with what you have obtained.