Is this function differentiable at 0? I would like to know if this function is differentiable at the origin:

$$f(x) =
\left\{
 \begin{array}{cl}
  x+x^2  &amp; \mbox{if } x \in \mathbb{Q}; \\
  x &amp; \mbox{if } x \not\in \mathbb{Q}. \end{array}
\right.$$

Intuitively, I know it is, but I don't know how to prove it. 

Any ideas?

Thanks a lot.
$f$ can be either $x + x^2$, or $x$ (depending on the value of $x$), right?

Firstly, you can prove that $f$ is continuous at 0, by noticing that $0 &lt; |f(x)| \le |x| + x^2$

And, you know that these 2 functions ($x + x^2$, and $x$)' derivatives at 0 are both 1. So, in order to prove that the derivative at 0 is indeed 1, you should use Squeeze Theorem, like this:

Right derivative at 0

For $x &gt; 0$, we have the inequality: $x &lt; x + x^2$, so the upper bound for $f$ is $x + x^2$, and the lower bound is of course $x$. So we'll have:

$\begin{align} &amp; x \le f(x) \le x + x^2 \\
\Rightarrow &amp;\dfrac{x}{x} \le \dfrac{f(x)}{x} \le \dfrac{x + x^2}{x} \quad \mbox{since }x &gt; 0\mbox{, the inequality sign doesn't change}\\
\Rightarrow &amp;1 \le \dfrac{f(x) - 0}{x - 0} \le \dfrac{x + x^2}{x}\\
\Rightarrow &amp;1 \le \dfrac{f(x) - f(0)}{x - 0} \le \dfrac{x + x^2}{x}
\end{align}$

...

From here, you can apply Squezze Theorem, it should be easy, let's give it a try.



Left derivative at 0

Since $x \rightarrow 0^-$, we'll still have the inequality: $x &lt; x+x^2$, so the upper bound for $f$ is still $x + x^2$, and the lower bound is of course $x$. So we'll have:

$\begin{align} &amp; x \le f(x) \le x + x^2 \\
&amp;...\end{align}$

Can you take it from here? Remember that when dividing to $x$ in this case, you have to change the signs (from > to &lt;; and from &lt; to >), as $x &lt; 0$.
You need to show that if $h$ is near $0$, then $\displaystyle\frac{f(h)}h$ is near $1$ (which should be the value of $f'(0)$). 

But $\displaystyle \frac{f(h)}h$ is either $1$ or $1+h$.



In detail: We have that $f(0)=0$. 

We can anticipate that, if it exists, the derivative of $f$ at $0$ is $1$, since $g'(0)=j'(0)=1$ where $g(x)=x$ and $j(x)=x+x^2$. However, to see whether $f'(0)$ exists and what its value is, we simply use the definition of the derivative at $0$: 
 $$ \lim_{h\to0}\frac{f(0+h)-f(0)}h=\lim_{h\to0}\frac{f(h)}h. $$
From the definition of $f$, we see that the fraction $\displaystyle \frac{f(h)}h$ equals either $1$ or $1+h$, depending on whether $h$ is irrational or not. 

It should be clear now that the limit of this expression is $1$ as $h$ approaches $0$, but we can proceed to verify this from the definition of limit:

We have that $\displaystyle\lim_{h\to0}\frac{f(h)}h=1$ if and only if for every $\epsilon&gt;0$ there is a $\delta&gt;0$ such that if $0&lt;|h-0|&lt;\delta$, then 
 $$\left|\frac{f(h)}h-1\right|&lt;\epsilon.$$ 
That is, given $\epsilon&gt;0$, we must find a $\delta&gt;0$ such that $0&lt;|h|&lt;\delta$ implies that both $|1-1|&lt;\epsilon$ and $|(1+h)-1|&lt;\epsilon$. 

The first of these inequalities always holds, so we only need to look at the second one, which simplifies to $|h|&lt;\epsilon$. Now we see that if we take $\delta=\epsilon$, then what we need to verify is that $0&lt;|h|&lt;\epsilon$ implies $|h|&lt;\epsilon$, which is always the case, and we are done: We have shown that $f'(0)$ exists, and equals $1$.

(Of course, we could just as well take as $\delta$ anything that is both positive and less than $\epsilon$.)