I have the following problem:
Let $f:\mathbb{R}^n \rightarrow \mathbb{R}$, $f(x)=(\operatorname{dist}(x,D))^2$ where $D$ is a convex, close set in $\mathbb{R}^n$. Prove that $f$ is convex and $f'(x)=2(x-P_D(x))$. Here $P_D(x)$ is the projection of $x$ in $D$.
I prove convexity, but I can't demonstrate that differential (gradient) is this. I think that the way to go is to use the definition, proving that
$$\lim_{x\rightarrow x_0} \frac{|f(x)-f(x_0)-f'(x_0)^{\top}(x-x_0)|}{\|x-x_0\|}=0$$
I would do it by squeezing: if $f$ is squeezed between two differentiable functions that have the same value and the same gradient at $x_0$, then it also has that gradient at $x_0$. Let's write $p_0=P_D(x_0)$ for brevity. Then two natural functions to use are $$ g(x) = \left((x-p_0)\cdot \frac{x_0-p_0}{\|x_0-p_0\|}\right)^2\quad \text{and} \quad h(x) = \|x-p_0\|^2 $$ Indeed, $g$ is the squared distance to a halfspace containing $D$, while $h$ is the squared distance to a point in $D$. Hence $g(x)\le f(x)\le h(x)$ in a neighborhood of $x_0$.
The identities $g(x_0)=h(x_0)$ and $g'(x_0)=h'(x_0)=2(x_0-p_0)$ are easy to verify.