Piecewise linear function and absolute value 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?

 A: 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.
A: The basic mechanism at work is that functions such like $|.|$ or $\min$ or $\max$ or $\Theta$ realize case decisions depending on their argument, together with the closedness of linear functions regarding linear combinations.
This can be exploited to combine these functions in such a way that a decision with at least $n$ cases can be realized to deal with the $n$ continous pieces of the piecewise linear function. 
Update: I tried a different decomposition for $f$, but the "dynamic" approach that user Carsten Schultz posted turned out to be easier so far.
Here is a proof:
$$
\max(x, y) = \frac{x + y + |x - y|}{2}
$$
so we can write
$$
\begin{align}
\frac{a_{i+1}-a_i}{2} \left(\left|x - x_i\right| + x - x_i\right)
&= 
(a_{i+1}-a_i)
\frac{\left|x - x_i - 0\right| + x - x_i + 0}{2} \\
&=
(a_{i+1}-a_i) \max(x-x_i,0)
\end{align}
$$
and therefore for $n$ pieces $f_i(x) = a_i x + b_i$ (we assume $n \ge 2$) and $n-1$ points of change $x_i$ (we assume $x_i < x_j$ for $i < j$) we get:
$$
f(x) := a_1 x + b_1 + 
\sum_{i=1}^{n-1} (a_{i+1}-a_i) \max(x-x_i,0)
$$
We have three cases: 


*

*left: $x \in (-\infty, x_1]$,  

*inner: $x \in [x_i, x_{i+1}]$ and 

*right: $x \in [x_{n-1}, \infty)$.


Inner case: For $x \in [x_j, x_{j+1}]$ we have $x \le x_{j+1} \le x_{i}$ for $i\ge j+1 \iff i > j$ and thus $x - x_i \le 0$, so the maximum terms for $i > j$ vanish and the equation above reduces to
$$
\begin{align}
f(x) 
&= 
a_1 x + b_1 + 
\sum_{i=1}^j (a_{i+1}-a_i) (x-x_i) \\
&= 
a_1 x + b_1 
+ x \sum_{i=1}^j (a_{i+1}-a_i)
- \sum_{i=1}^j (a_{i+1}-a_i) x_i \\
&= 
a_1 x + b_1 
+ x \left(a_{j+1}-a_1\right)
- \sum_{i=1}^j (a_{i+1}-a_i) x_i \\
&= 
a_{j+1} x + b_1 
- \sum_{i=1}^j (a_{i+1}-a_i) x_i \\
\end{align}
$$
From the continuity requirement at the point of change $x_i$ we get
$$
a_{i+1} x_i + b_{i+1} = a_i x_i + b_i \iff
(a_{i+1} - a_i) x_i = b_i - b_{i+1}
$$
this gives
$$
\begin{align}
f(x) 
&= 
a_{j+1} x + b_1 
- \sum_{i=1}^j (a_{i+1}-a_i) x_i \\
&= 
a_{j+1} x + b_1 
- \sum_{i=1}^j b_i - b_{i+1} \\
&= 
a_{j+1} x + b_1 
- (b_1 - b_{j+1}) \\
&= a_{j+1} x + b_{j+1}
\end{align}
$$
Right case: All $n-1$ maximum terms turn into $x - x_i$.
$$
\begin{align}
f(x) 
&= 
a_1 x + b_1 + 
\sum_{i=1}^{n-1} (a_{i+1}-a_i) (x-x_i) \\
&= 
a_n x + b_1 
- \sum_{i=1}^{n-1} (a_{i+1}-a_i) x_i \\
&= 
a_n x + b_1 
- \sum_{i=1}^{n-1} b_i - b_{i+1} \\
&= a_n x + b_n
\end{align}
$$
Left case: All $n-1$ maximum terms vanish.
$$
f(x) 
= 
a_1 x + b_1 
$$
