Is a distance function differentiable on $\Bbb R^n$? Let $C\subset \Bbb R^n$ be closed and convex. Define $d_X\colon \Bbb R^n\to \Bbb R$ by the formula $d_X(x):=\mathrm{dist}(x,C)$. Is it differentiable outside $ C$? How to prove it? What's the derivative?
Moreover, if it's true that it is not differentiable for any closed convex set $C$ and any $x\in\partial C$? (this isn't covered by my proof below and I'll be happy to see the answer to this question).
Context:
In the question the derivative of $d_X$ is considered. In the discussion there was a doubt if this function is always differentiable and the smooth boundary is assumed to force differentiability. While trying to answer the question I realized that the question of differentiability is interesting on its own and putting my proof as the answer to that question may obfuscate both the proof of differentiability of $d_X$ and the answer of that question.
Short googling gave the following statement in
an article from Transactions of the AMS: 'For convex $C$, the
differentiability of $d_C$ everywhere outside of $C$ is well known' (page 1 line -4). So it turns out that the condition on $\partial X$ is not needed!
Of course if $x\in \mathrm{int}\,C$ then $\nabla d_X(x)=0$.
 A: I'm going to prove the first question.
Preliminaries.

*

*Recall that if $C$ is closed and convex, for any $a\in \Bbb R^n$ there's a unique $x_0\in C$ such that $d_C(a)=\|a-x_0\|$ and $(x-x_0)\cdot (a-x_0)\leq 0$ for any $x\in C$. Put $\pi_C(a):=x_0$. We know that $\pi_C$ is 1-Lipschitz.

*For a fixed $w\notin X$ define $p:=\pi_X(w)$ and $e:=\frac{w-p}{\|w-p\|}$.

*Define $Y:=\{x:(x-p)\cdot e\leq 0\}$ (a hyperspace). From 1. and since $p=\pi_X(w)$ is the closest point from $X$ and $X$ is convex then $X\subset Y$.

Theorem. The function $d_X$ is differentiable outside $X$ and $\nabla d_X(w)=\frac{w-\pi_X(w)}{\|w-\pi_X(w)\|}$.
Proof. Take any $u$ such that $\|u\|<d_X(w)$. Then $u=u_\bot+ce$ where $u_\bot\bot e$ and $$d_X(w+u)\geq d_Y(w+u) = \|(w+u)-(p+u_\bot)\|=\|(w-p)+ce\|$$ and
$$d_X(w+u)\leq \|w+u-p\| = \|(w-p)+u\|.
$$
Therefore
$$ \|(w-p)+ce\|^2-\|w-p\|^2\leq d_X^2(w+u)-d_X^2(w) \leq \|(w-p)+u\|^2-\|w-p\|^2.
$$
Using the definition of $e$ and the formula $\|a+b\|^2=\|a\|^2+\|b\|^2+2a\cdot b$ we get
$$ c^2+2c\|w-p\|
\leq d_X^2(w+u)-d_X^2(w) \leq 2(w-p)u + \|u\|^2.\tag{1}
$$
Since $(w-p)u = (w-p)u_\bot+(w-p)\cdot ce = 0+c\|w-p\|$ and we have from ($1$) that
$$ 0\leq \frac{c^2}{\|u\|}\leq
\frac{d_X^2(w+u)-d_X^2(w)-2(w-p)u}{\|u\|} \leq  \|u\|.
$$
This shows that the function $w\mapsto d_X^2(w)$ is differentiable outside $X$ and $\nabla d_X^2(w)=2(w-\pi_X(w))$. Now using the chain rule and the formula $(\sqrt{x})'=1/(2\sqrt x)$ for $x\in\Bbb R\setminus\{0\}$ we get
$$\nabla d_X(w)=\nabla\left(\sqrt{d_X^2}\right)(w) = \frac{1}{2\sqrt{d_X^2(x)}}\cdot 2(w-\pi_X(w)) = \frac{w-\pi_X(w)}{\|w-\pi_X(w)\|}.$$
Remark If we know already (as is stated in the article) that $d_X$ is differentiable, we can calculate the derivative much simpler. Namely

*

*Since $X\subset Y$ and $\pi_Y(p+te)=p\in X$, we have $d_X(p+te)=t$.

*This shows that
$$ \nabla d_X(w)\cdot e = \lim_{t\to 0}\frac{d_X(w+te)-d_X(w)}{t} = \lim_{t\to 0}\frac{\|w-p\|+t-\|w-p\|}{t}=1$$

*Since $d_X$ is 1-Lipschitz, we have $\|\nabla d_X(w)\|\leq 1$.

*Points 2. and 3. and the fact that $\|e\|=1$ gives $\nabla d_X(w)=e=\frac{w-p}{\|w-p\|}$. $\square$
