$f$ continuous on $(a,b)$ and $|f|$ differentiable on $(a,b)$; is $f$ differentiable in $(a,b)$?

Let $f:(a,b) \to \mathbb R$ be a continuous function such that $|f|$ is differentiable in $(a,b)$ ; then is $f$ differentiable in $(a,b)$ ?


The only "problematic points" may be the $x_0$'s where $f$ becomes $0$ (if any), i.e the points with $x_0$ with $f(x_0)=0$. In such a point $|f|$ has a local minimum (why?) and therefore $|f|'(x_0)=0$ (since $|f|$ is differentiable by assumption). Now, using the limit-definition of the derivative $$|f|'(x_0)=\lim_{h\to 0}\frac{|f|(x_0+h)-|f|(x_0)}{h}=\lim_{h\to 0}\frac{|f|(x_0+h)}{h}=\begin{cases}\lim_{h\to 0^-}\frac{-f(x_0+h)}{h}\\\lim_{h\to 0^+}\frac{f(x_0+h)}{h}\end{cases}$$ Since $|f|$ is differentiable at $x_0$ with $|f|'(x_0)=0$ these two limits exist and are equal to $0$, i.e $$-\lim_{h\to 0^-}\frac{f(x_0+h)}{h}=\lim_{h\to 0^+}\frac{f(x_0+h)}{h}=0$$ which implies that $f$ is also differentiable at $x_0$ with $f'(x_0)=0$. (All these imply in particular that $x_0$ is a saddle point). For all other points, the implication is fairly straightforward. So, the answer is yes.


Let's see what happens at points where $f(x)\ne0$. There is a neighborhood of $x$ where $f$ has the same sign as $f(x)$, so for $y$ in this neighborhood, $|f(y)|=f(y)$, if $f(x)>0$, or $|f(y)|=-f(y)$, if $f(x)<0$.

In the case where $f(x)>0$, we have $$ \lim_{y\to x}\frac{f(y)-f(x)}{y-x}= \lim_{y\to x}\frac{|f(y)|-|f(x)|}{y-x} $$ which exists finite by assumption. In the case where $f(x)<0$, we have $$ \lim_{y\to x}\frac{f(y)-f(x)}{y-x}= \lim_{y\to x}\frac{-|f(y)|+|f(x)|}{y-x}= -\lim_{y\to x}\frac{|f(y)|-|f(x)|}{y-x} $$ which again exists finite by assumption.

Suppose $f(x)=0$. Then, by assumption $$ \lim_{y\to x}\frac{|f(y)|}{y-x} $$ exists finite. Note that $$ \lim_{y\to x^-}\frac{|f(y)|}{y-x}\le0, \qquad \lim_{y\to x^+}\frac{|f(y)|}{y-x}\ge0, $$ so we conclude that this limit is $0$.

Now, for $y>x$, $$ -\frac{|f(y)|}{y-x}\le\frac{f(y)}{y-x}\le\frac{|f(y)|}{y-x} $$ and, by taking the limit for $y\to x^+$, we get that $$ \lim_{y\to x^+}\frac{f(y)}{y-x}=0 $$ Similarly for the limit $y\to x^-$ because, for $y<x$, $$ \frac{|f(y)|}{y-x}\le\frac{f(y)}{y-x}\le-\frac{|f(y)|}{y-x} $$


We can deal easily with the points $x_0$ where $f(x_0)\not = 0$.

Take a point $x_0$ such that $ f(x_0)=0$.

lemma: $\lim _{x\rightarrow x_0} g(x) = 0 \Leftrightarrow \lim _{x\rightarrow x_0}| g(x)|=0$

First notice that if $|f(x_0)|=0$ then $x_0$ is a local minimum for $|f|$so $|f|'(x_0)=0$. Therefore using the lemma we have $$\lim _{ x \rightarrow x_0} \frac{|f(x)|}{x-x_0} =0 \Leftrightarrow \lim _{ x \rightarrow x_0}\left | \frac{f(x)}{x-x_0} \right | =0 \Leftrightarrow \lim _{ x \rightarrow x_0}\frac{f(x)}{x-x_0} =0$$


Let's write $g(x)=\left|f(x)\right|$ to save typing.

First, let us get rid of the easy case: If $f(x_0)\ne 0$, then due to continuity, there's an open interval $I$ around $x_0$ such that $f(x)\ne 0$ for any $x\in\mathbb I$. Since $f(x)$ is real, this implies either $f(x)=g(x)$ or $f(x)=-g(x)$ for all $x\in I$; in both cases $f$ is obviously differentiable in $x_0$.

So let's now consider the case $f(x_0)=0\iff g(x_0)=0$.

We know by definition that $g(x)\ge 0$, and $g(x)$ is differentiable by assumption. We will now show that for any $x_0$ with $g(x_0)=0$ we also have $g'(x_0)=0$:

On one hand, $$g'(x_0) = \lim_{x\to x_0+0}\frac{g(x)-g(x_0)}{x-x_0} = \lim_{x\to x_0+0}\frac{g(x)}{x-x_0}$$ Now since $g(x)\ge 0$, for $x>x_0$, clearly $\frac{g(x)}{x-x_0}\ge 0$ and thus $g'(x_0)\ge 0$.

On the other hand, $$g'(x_0) = \lim_{x\to x_0-0}\frac{g(x)-g(x_0)}{x-x_0} = \lim_{x\to x_0-0}\frac{g(x)}{x-x_0}$$ The analogous argumentation as above gives $g'(x_0)\le 0$. Therefore $g'(x_0)=0$.

Now $$\lim_{x\to x_0} \frac{f(x)-f(x_0)}{x-x_0} = \lim_{x\to x_0}\frac{f(x)}{x-x_0}$$ Consider any sequence $(x_n)$ converging to $x_0$. Then obviously $$\frac{-g(x_n)}{x_n-x_0} \le \frac{f(x_n)}{x_n-x_0} \le \frac{g(x_n)}{x_n-x_0}$$ But the left and right side both converge to $0$, and thus does also the sequence in the center. Since this holds for any $(x_n)$, it follows that also $$\lim_{x\to x_0}\frac{f(x)}{x-x_0}=0$$ in other words, $f'(x_0)$ exists and is $0$.