how to find subdifferential of a function $x^2+ |x-1|+|x-2|$ 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|&gt;1 and |x-2|&gt;2
$f(x) = x^2+ x-1+x-2$ and $f'(x) = 2x+2$

For |x-1|&lt;1 and |x-2|&lt;2
$f(x) = x^2-(x-1)-(x-2)$ and $f'(x) = 2x-2$

For |x-1|&gt;1 and |x-2|&lt;2
$f(x) = x^2+(x-1)-(x-2)$ and $f'(x) = 2x$

For |x-1|&lt;1 and |x-2|&gt;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) &amp;= \partial f_1(x) + \partial f_2(x) + \partial f_3(x) \\
&amp;= 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) &amp;= \{f_1'(x) \} + \partial f_2(x) + \{f_3'(x)\} \\
&amp;= \{f_1'(x)\} + [-1,1] + \{f_3'(x)\} \\
&amp;= \{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) &amp;= \{f_1'(x)\} + \{f_2'(x)\} + \partial f_3(x) \\
&amp;= \{f_1'(x)\} + \{f_2'(x)\} + [-1,1] \\
&amp;= \{ f_1'(x) + f_2'(x) + g \mid -1 \leq g \leq 1\}.
\end{align}
First, I have to admit I am not familiar with a "subdifferential".  It look to me like you are just taking the derivative.  Second, dividing with things like "|x- 1|> 1 and |x- 2|> 2" is confusing and probably not what you want.  Instead divide the real line into three intervals: $x\le 1$, $1&lt; x\le 2$, and $x&gt; 2$.

For $x\le 1$ both x-1 and x-2 are negative: $x^2+ |x- 1|+ |x- 2|= x^2- (x- 1)- (x- 2)= x^2- 2x+ 3$.  The derivative is $2x- 2$.

For $1&lt; x\le 3$ x-1 is positive but x-2 is still negative:$x^2+ |x- 1|+ |x- 2|= x^2+ (x- 1)- (x- 2)= x^2+ 10$.  The derivative is $2x$.

For $x&gt; 2$ both x-1 and x-2 are positive: $x^2+ |x- 1|+ |x- 2|= x^2+ (x- 1)+ (x- 2)= x^2+ 2x- 3$.  The derivative is $2x+ 2$.