Thursday, 10 September 2015

Sketch $|1-3x|+|2-2x|+|3-x|$

Sketch the function $$f(x)=|1-3x|+|2-2x|+|3-x|,x\in \Bbb R.$$


$$f(x)=|1-3x|+|2-2x|+|3-x|=|1-3x|+2|1-x|+|3-x|$$First notice that when $x<0$,$f(x)=(1-3x)+(2-2x)+(3-x)=6-6x$.
Looking at the first term (within the absolute sign), we know when $x\leq \dfrac{1}{3}$,$f(x)=6-6x$.
Now, look at the other two terms, when $1\leq x\leq 3,f(x)=-(1-3x)-2(1-x)+(3-x)=4x$.
When $x\geq 3, f(x)=-(1-3x)-2(1-x)-(3-x)=-6+6x$.
It remains to check the interval $[\dfrac{1}{3},1]$. There $f(x)=-(1-3x)+2(1-x)+(3-x)=4$.

We conclude that $$f(x)=\begin{align}\begin{cases}6-6x,\quad &x\leq \dfrac{1}{3}\\ 4,\quad &\dfrac{1}{3}\leq x\leq 1\\ 4x,\quad &1\leq x\leq 3\\ -6+6x,\quad &x\geq 3. \end{cases}\end{align}$$

Remark: If we set $f(x)$ to be zero, we can have the 'critical points' $\dfrac{1}{3},1,3$. This naturally forms four regions. $x\leq \dfrac{1}{3}, \dfrac{1}{3}\leq x\leq 1, 1\leq x\leq 3, x\geq 3$.

In fact, this problem is related to distance. The Manhattan distance $d_M$ between points $P(x_1,y_1)$ and $Q(x_2,y_2)$ in the plane is defined by $$d_M(P,Q)=|x_1-x_2|+|y_1-y_2|.$$ The $d_M$ distance is the sum of the horizontal and vertical distances between the points like 'three blocks west and two blocks north'.

Here's an extra problem that can be solved using the notion of distance.

If $a<b<c<d$ and $x\in \Bbb R$, find the least value of the function $$f(x)=|x-a|+|x-b|+|x-c|+|x-d|.$$


It turns out we don't need to use derivatives for this type of problems. The value of $f(x)$ is the sum of the distances from $x$ to $a,b,c,d$. Imagine we start $x$ at the left of $a$, and move it to the right. Initially all the four terms are decreasing since $x$ is now closer to the other points. When $x$ passes $a$ and starts moving to $b$, the distance from $x$ to $a$ increases while that from $x$ to $b$ decreases at the same rate, so $|x-a|+|x-b|$ is constant. The distances from $x$ to $c$ and $d$ are decreasing, so $f(x)$ is still decreasing. We can treat the other intervals similarly.

Overall, the function is decreasing to the left of b, constant on $[b,c]$, and increasing to the right of $c$, so we know the minimum value is on $[b,c]$. When $b\leq x\leq c$, the sum of distances from $x$ to $a$ and $d$ is $d-a$, and that from $x$ to $b$ and $c$ is $c-b$. Therefore, the total distance is $c+d-a-b$.

More to read
The median minimises the sum of absolute deviations

No comments:

Post a Comment