Differentiability/continuity of piecewise defined functions Let $$f(x)=\begin{cases}x^2\sin(\frac{1}{x}), &x\not= 0,\\ 0, &x = 0.\end{cases}$$
Since I can differentiate both parts of this, technically, $f$ is differentiable for all $x$. However I have written down in my notes that $f'(x)$ is not even continuous at $0$ and thus not differentiable. However, I am confused about this because isn't my original function not continuous?
 A: There's a few misconceptions here; firstly, the fact that $0$ and $x^2\sin(\frac{1}x)$ are differentiable does not imply that $f$ is - yes, in the interior of those domains, that's true, but we still need to consider the boundary - that is, if $f$ is differentiable at $0$. This must be directly established via evaluating the limit:
$$\lim_{h\rightarrow 0}\frac{h^2\sin(\frac{1}h)}{h}$$
which happens to exist and equal $0$. This is why $f$ is differentiable there. (For instance, setting $f(x)=x$ if $x$ is non-negative and $f(x)=-x$ if $x$ is negative is differentiable everywhere except at $0$, though both pieces are everywhere differentiable).
Moreover, $f$ is continuous at $0$. In general, differentiability must imply continuity because, otherwise the limit $$\lim_{h\rightarrow 0}\frac{f(x+h)-f(x)}h$$
would have that $f(x+h)$ does not go to $f(x)$, so the numerator doesn't tend to zero, while the denominator does - which would imply divergence. In the particular case of $f$, notice that $-x^2\leq f(x) \leq x^2$ and, by use of the squeeze theorem, we can show that $f$ is continuous at $0$ since both $x^2$ and $-x^2$ are and because they are equal at $0$.
What is curious about $f$ is the form of its derivative:
$$f'(x)=\begin{cases}2h\sin(\frac{1}h)-\cos(\frac{1}h)&&\text{if }h\neq 0\\0&&\text{if }h=0\end{cases}$$
which is not continuous at $0$, since $f$ oscillates arbitrarily quickly near $0$ between $-1$ and $1$ due to the $\cos(\frac{1}h)$ term. So the interesting bit here is that $f'$ existing everywhere does not imply continuity. Here's a graph of $f'$ which aptly shows why it's not continuous:

A: Just because two pieces of a function are individually continuous (there is a name for this: we say $f$ is piecewise continuous), that does not mean they come together in a continuous way, much less a differentiable way.
For example, consider
$$f(x)=\begin{cases}-1, &x<0\\ \phantom{-}1, &x\geq 0.\end{cases}$$ The pieces of $f$ are each continuous (and differentiable), but $f$ as a whole is not continuous (and thus not differentiable) at $x=0$.
Another, different type of example would be
$$g(x)=\begin{cases} 0, &x<0,\\ x, &x\geq 0.\end{cases}$$ $g$ is continuous for all $x$. The pieces of $g$ are continuous (in fact differentiable), but $g$ is not differentiable at $x=0$ because the pieces of $g$ do not come together in a smooth way so that the derivative at $x=0$ exists.
Finally, even if a function is differentiable (so $f'(x)$ exists) that does not mean that $f'(x)$ is is itself continuous. Yours is an example. Can you think of another?
More generally, there are examples where $f$ is $k$-times continuously differentiable (meaning $f$ has $k$ derivatives and those derivatives are continuous) but where $f$ is not $k+1$-times continuously differentiable, i.e. $f^{(k+1)}(x)$ is not continuous.
