# Plot and calculate subgradient of f(x)

I want to calculate the subgradient of $$f(x) = \begin{cases} 0& |x|\le 1\\ |x|-1& 1<|x|\le2\\ +\infty& x > 2 \end{cases}$$

but I do not know how to start and want to plot $f(x)$ first. Then, I think it is easier to imagine? Do you admit with me or should I start different?

• Sure, visualization is almost certain to help. Go for it! What do you get when you look at it that way? – Michael Grant May 6 '18 at 19:41
• Subgradients really aren't that hard. After all, if a function is convex, it's going to have a gradient at all but a countable number of points. So if you can differentiate, you can find a subgradient, almost everywhere. Don't be surprised that it seems easy. – Michael Grant May 6 '18 at 20:09
• if I take the point x=-1 then the intervallis between [-1,0]? x=1: [0,1]. x=-2: [-inf,-1]. x=2: [1,inf]... – Peter May 7 '18 at 20:33