# Mathematica is unable to compute the derivative of the function $\frac{\sin^2(x)}x$

Consider the function $$f: \mathbb R\to\mathbb R, x\mapsto\begin{cases}\frac{\sin^2(x)}x, & x\neq 0, \\ 0, & x=0.\end{cases}$$

It is true that $$f\in\mathcal C^1(\mathbb R)$$ and that $$f'(x)=\begin{cases}\frac{2 \sin (x) \cos (x)}{x}-\frac{\sin ^2(x)}{x^2}, & x\neq 0, \\ 1, & x=0.\end{cases}$$

(The fact that $$f'(0)=1$$ can be derived directly from the definition of the derivative.)

However, when I enter $$f'(0)$$ into Mathematica (in this case Wolfram Alpha but it makes no difference), I get $$f'(0)=0$$. Even more astonishingly, if I define $$g:\mathbb R\to\mathbb R, x\mapsto\begin{cases} \frac{\sin^2(x)}x, & x\neq0, \\ x, & x=0,\end{cases}$$

then $$f=g$$. However, Mathematica gives $$g'(0)=1\color{red}\neq f'(0)$$. It seems that Mathematica has a massive programming error when it comes to differentiating piecewise functions!

Is this a known error?

Note: This bug was originally discovered by Micah Windsor in a discussion on $$f'(0)$$ here.

• if you do Limit[D[f[x], x], x -> 0] it gives 1 I do not know why it does not work on the Piecewise function itself. Apr 10, 2020 at 20:49
• SeriesCoefficient[f[x], {x, 0, 1}] works (12.1.0), but SeriesCoefficient[f[$x], {$x, x, 1}, Assumptions -> x \[Element] Reals] gives a rather interesting answer. Apr 11, 2020 at 3:49
• It's not a bug (or an error). Substituting 0 for x in the derivative gives an indeterminate form that happens to be at a removable singularity. Series can be quite useful in such cases (and so can Limit, but that's a heavier hammer). Apr 11, 2020 at 17:18
• This is a task usually given in the introductory course of mathematical analysis. It is for proving considerations an example working for small values around a point, x=0. The students are requested to make use of the Taylor expansion Series and an idea of limit introduced in the lessons. It is not an example of Piesewise or D or f'. It is an easy hand or head task. The function is not in need for Piecewise at all. Taylor series multiplication may be another context. Apr 12, 2020 at 10:02
• Limit[D[Sinc[x] Sin[x], x], x -> 0] works perfectly fine. Jan 30, 2021 at 10:11

See the possible issues section of the documentation for Piecewise. The proper method for defining f is

Clear["Global*"]

f[x_] := Piecewise[{{Sin[x]^2/x, x < 0 || x > 0}}]

f[0]

(* 0 *)

f'[0]

(* 1 *)

• Side note: in v9.0.1 f'[x] still gives the undesired result Piecewise[{{(2*Cos[x]*Sin[x])/x - Sin[x]^2/x^2, x > 0 || x < 0}}, 0], but D[f[x], x] gives the desired Piecewise[{{(2*Cos[x]*Sin[x])/x - Sin[x]^2/x^2, x < 0}, {1, x == 0}}, (2*Cos[x]*Sin[x])/x - Sin[x]^2/x^2]`, seems like a bug in v9. Apr 11, 2020 at 3:23