TITLE: why minimum of these functions happen at a special place?
QUESTION [6 upvotes]: why minimum of these functions happen at a special place? how to use derivative to find the minimum of these functions?
$$|x-1| + |x-2| + \dots + |x-9|$$ minimum is for $x = 5$
$$|x-1| + |x-2| + \dots + |x-99|$$ minimum is for $x = 50$
$$|x-1| + |x-2| + \dots + |x-999|$$ minimum is for $x = 500$ $$ \dots$$ why it happens?

REPLY [7 votes]: Suppose that $a_1\lt a_2\lt a_3\lt \cdots\lt a_9$. Consider the function 
$$f(x)=|x-a_1|+|x-a_2|+\cdots +|x-a_9|.$$
Then $f(x)$ is the sum of the distances from $x$ to the $a_i$.
Imagine the points $a_1,a_2,\dots,a_9$ are on the $x$-axis. A tiny bug is on the $x$-axis, well to the left of $a_1$, and starts walking in the rightward direction. 
For every tiny step $s$ that she takes, the sum of her distances from the $a_i$ decreases by $9s$.  This continues until she hits $a_1$.  Now for every tiny step $s$ that she takes, her distance from each of $a_2,a_3,\dots,a_9$ decreases by $s$, and her distance from $a_1$ increases by $s$, for a total decrease of $7s$.
This continues until she hits $a_2$.  Then for every step $s$ that she takes, her distance from each of $a_3$ to $a_9$ decreases by $s$, and her distance from each of $a_1$ and $a_2$ increases by $s$, for a total decrease of $5s$.
When she hits $a_3$, for each a tiny step to the right, there is a total decrease of $3s$.  When she hits $a_4$, there is a total decrease of $s$ for each tiny step to the right. When she hits $a_5$ and takes a tiny step, there is an increase of $s$ for each tiny step to the right. And things get worse as she continues to the right. So the minimum value of $f(x)$ was reached at $x=a_5$.
Exactly the same idea shows that if we have $a_1\lt a_2\lt\cdots  \lt a_{2m-1}$, and $f(x)=|x-a_1|+\cdots+|x-a_{2m-1}|$, then $f(x)$ reaches a minimum at $x=a_m$.
Remark: The situation is only slightly different is we have an even number of points $a_1\lt a_2\lt \cdots\lt a_{2m}$. Then the minimum value of $f(x)$ is reached at all points $x$ such that $a_m\le x\le a_{m+1}$.     
To put it in other words, if we have an odd number of terms, then $f(x)$ is minimized at the (unique) median of the $a_i$. The same is true with an even number of terms, except that there are infinitely many medians.
Note that by contrast $g(x)=(x-a_1)^2+(x-a_2)^2+\cdots +(x-a_n)^2$ is minimized at the mean $\frac{a_1+a_2+\cdots+a_n}{n}$ of the $a_i$.