Given a function
$f(x) = x^2+ |x-1|+|x-2| $
find it's subdifferential.
My approach to solving this was to divide the answer into 5 parts:
For |x-1|>1 and |x-2|>2
$f(x) = x^2+ x-1+x-2$ and $f'(x) = 2x+2$
For |x-1|<1 and |x-2|<2
$f(x) = x^2-(x-1)-(x-2)$ and $f'(x) = 2x-2$
For |x-1|>1 and |x-2|<2
$f(x) = x^2+(x-1)-(x-2)$ and $f'(x) = 2x$
For |x-1|<1 and |x-2|>2
$f(x) = x^2-(x-1)+(x-2)$ and $f'(x) = 2x$
For |x-1|=1 and |x-2|=2
$ f(x) = x^2$ and $f'(x) = 2x$
Does this look right? Is this the correct approach?
Here's an approaching using the subdifferential sum rule: \begin{align} \partial f(x) &= \partial f_1(x) + \partial f_2(x) + \partial f_3(x) \\ &= f_1'(x) + \partial f_2(x) + \partial f_3(x) \end{align} where $f_1(x) = x^2, f_2(x) = |x - 1|$, and $f_3(x) = |x - 2|$.
The function $f$ is differentiable everywhere except at $x = 1$ and $x = 2$. If $x \neq 1$ and $x \neq 2$, then we have $$ \partial f(x) = \{f_1'(x) + f_2'(x) + f_3'(x)\}. $$ If $x = 1$, we have \begin{align} \partial f(x) &= \{f_1'(x) \} + \partial f_2(x) + \{f_3'(x)\} \\ &= \{f_1'(x)\} + [-1,1] + \{f_3'(x)\} \\ &= \{f_1'(x) + f_3'(x) + g \mid -1 \leq g \leq 1 \}. \end{align} If $x = 2$, we have \begin{align} \partial f(x) &= \{f_1'(x)\} + \{f_2'(x)\} + \partial f_3(x) \\ &= \{f_1'(x)\} + \{f_2'(x)\} + [-1,1] \\ &= \{ f_1'(x) + f_2'(x) + g \mid -1 \leq g \leq 1\}. \end{align}