How to find out the global minimum of the following expression

You can in principle write out the function in a lot of intervals. But it would probably take too long. However, I will use this fact, without doing it explicitly. We know that if we do write this function, it is going to be linear on each interval (sum of linear functions is a linear function), and it's going to be continuous (sum of continuous functions is a continuous function). We also know that on a line you get minimum at one end, the other, or both (constant line). So all you need to do is to calculate your function at $1,2,5,6,...$ and find the minimum.


Unfortunately I needed some minutes to think about the problem before finding a solution that can be calculated very quickly:

Imagine the graph of the function $f_a(x)=|x-a|$. Having the graph in mind you see that the derivation $f'(x)=-1$ for $x<a$ and $f'(x)=1$ for $x>a$.

For the intervals: $(-\infty,1)$, $(1,2)$, $(2,5)$, ..., $(32,\infty)$ we can now easily calculate the derivative $f'(x)=f'_1(x)+f'_2(x)+f'_5(x)+...-f'_{32}(x)$:

In the range $(-\infty,1)$ it is $f'(x)=-1-1-1-...-1+1=-11$.
In the range $(1,2)$ it is $f'(x)=+1-1-1-...-1+1=-9$.
In the range $(2,5)$ it is $f'(x)=+1+1+1-...-1+1=-7$.
...

In every step we simply have to invert one sign so "-1" becomes "+1". This means the derivative is changing by 2 at the points x=1,2,5,...

We start by calculating the derivative for $x<1$; it is -11.

Now we simply go through the ranges:

<1: -11
1..2: -9
2..5: -7
5..6: -5
6..8: -3
8..9: -1
9..10: +1
10..11: +3
11..12: +5
12..17: +7
17..24: +9
24..31: +11
31..32: +13
>32: +11

At $x=32$ the derivation decreases by 2 because of the minus sign before $|x-32|$; you could of course adapt this method for sums of elements of the form $b|x-a|$.

We see that for $x<9$ the derivation is negative and for $x>9$ the derivation is positive. We also know that the function is continuous. (This is important because the derivation is not defined at x=1,2,5,...) This means that the function is strictly decreasing respectively increasing for $x<9$ and for $x>9$.

So we know that the global minimum must be at $x=9$.


The answer (minimizer) in this case is $10$, the median of the sequence $$(1,2,5,6,8,9,10,11,12,17,24,31,32).$$

You can plug in $x=10$ in the function and you would find that the minimum value is $96$. In general, the solution to the following minimization problem

$$\min\{|x-a_1| + |x-a_2| + \cdots + |x-a_n|\}$$ is the median of $(a_1,\ldots,a_n)$. To see why, consider first when $n=2$, and without loss of generality assume $a_1<a_2$. Then $|x-a_1|+|x-a_2|$ is the distance between $x$ and $a_1$ plus the distance between $x$ and $a_2$. It is easy to see that only when $x$ is in the middle of $a_1$ and $a_2$ should the sum of distances be minimal, which equals $|a_2-a_1|$ in this case. In this case the minimizer is not unique. Any points in $[a_1,a_2]$ is a minimizer.

When $n=3$, the function is $|x-a_1|+|x-a_2|+|x-a_3|$, and we order the parameters again so that $a_1<a_2<a_3$. When $x$ coincides with $a_2$, i.e. $x=a_2$, the value becomes $|a_2-a_1|+|a_2-a_3|=|a_3-a_1|$, the distance between $a_3$ and $a_1$. But when $x\in[a_1,a_3], x\neq a_2$, the value of the function is $$|x-a_2|+|x-a_1|+|x-a_3| = |a_3-a_1| + |x-a_2|,$$ which is largar than $|a_3-a_1|$, the distance between $a_3$ and $a_1$. Similarly the value would become larger when $x$ is outside $[a_1,a_3]$. So in this case, the minimizer is unique and is equal to $a_2$, the median of $(a_1,a_2,a_3)$.

In general, when $n$ is odd, there exists a unique minimizer, which is equal to the (unique) median of the parameters $(a_1,\ldots,a_n)$. When $n$ is even, the function is minimal and constant over the range $[a_i,a_j]$, where $a_i$ and $a_j$ are the two middle values.

Tags:

Calculus