TITLE: Approximating sum of absolute vales by a quadratic function
QUESTION [7 upvotes]: Here's a really neat thing I just found:
Consider, for instance, $$\sum _{k=-20}^{20} \left| x-k\right|$$
It's plot (after adjusting scaling) looks like

This looks suspiciously quadratic, and we can in fact overlay $y = x^2 + 420$ to find that they line up quite nicely:
.
Moreover, for integer values of $x$ within the domain $[-21,21]$, we find that the values reached by these two functions are equal.
I suspect that if we changed the summation step size to be smaller, we would find that it began to approximate even for non-integer values of $x$ as well.
Can someone explain why this property occurs?

REPLY [3 votes]: Hint:  take an $\,x \in [-20, 21)\,$, then $\,-20 \le m = \lfloor x \rfloor \lt 21\,$, and:
$$
\;|x-k| = 
\begin{cases}
\;x - k \quad\text{for}\;\;-20 \le k \le m \\
\;k - x \quad\text{for}\;\;m+1 \le k \le 20
\end{cases}
$$
It follows that (for $\,-20\le m\le20\,$):
$$
\begin{align}
\sum _{k=-20}^{20} \left| x-k\right| &= \sum _{k=-20}^{m} (x-k) + \sum _{k=m+1}^{20} (k-x) \\[3px]
 &= (m+21)x - \sum _{k=-20}^{m} k + \sum _{k=m+1}^{20} k - (20-m)x \\[3px]
 &= (2m+1) x - (m+21)\cdot \frac{-20 + m}{2} + (20-m)\cdot\frac{m+1 + 20}{2} \\[3px]
 &= (2m+1)x + (m+21)(20-m) \\
 &= (2 \lfloor x \rfloor+1)x - \lfloor x \rfloor^2 - \lfloor x \rfloor + 420
\end{align}
$$
The latter expression explains the quadratic behavior, as well as the coincidence at integer $x\,$.
The quadratic behavior quickly degrades outside $\,(-21,21)\,$ as can be verified here.