I tried to find $f'(0)$ of this function:

$$f(x) = \begin{cases} x\cdot\sin(\frac{1}{x}) & \text{if $x\ne0$} \\ 0 & \text{if $x=0$} \end{cases}$$

This is what I tried in Mathematica:

f[x_] = Piecewise[{{x*Sin[1/x], x != 0}, {0, x == 0}}]

Mathematica gives the answer $0$, while the answer should be $undefined$, also see this discussion: https://math.stackexchange.com/questions/1551257/derivative-of-piece-wise-function-at-x-0

Any idea why Mathematica doesn't give the correct anwer?

  • $\begingroup$ Mathematica takes symbolic derivatives and intergrals of Piecewise functions in a piecewise fashion, i.e. D gets applied to each part of Piecewise and then kicks back the derivatives of each condition. Examine the output of D[f[x],x]. $\endgroup$
    – IPoiler
    Nov 30, 2015 at 19:20
  • $\begingroup$ If Mathematica must work piece by piece on the derivative of a Piecewise function, it seems that a better choice for intervals of width 0, would be undefined, since the derivative is undefined for a function that exists only at a single point. $\endgroup$
    – John McGee
    Nov 30, 2015 at 19:34
  • $\begingroup$ @JohnMcGee In some cases the derivative still exists, if the interval of width 0 makes the function continuous. $\endgroup$ Nov 30, 2015 at 19:49
  • $\begingroup$ To get the correct answer, evaluate (f[h] - f[0])/h // Simplify[#, h != 0] & so as to obtain Sin[1/h]. Then either look at a plot of that output function of h or else evaluate Limit[Sin[1/h], h -> 0] to obtain Interval[{-1,1}]. $\endgroup$
    – murray
    Nov 30, 2015 at 20:16

2 Answers 2


Mathematica is being inconsistent in how it is treating the derivative for a piecewise function (this seems like a bug to me). We can look at a simpler example to see this, which will point towards a workaround,

pwf1[x_] := Piecewise[{
    {3 x, x != 0},
    {5 x, x == 0}}];
pwf2[x_] := Piecewise[{
    {3 x, x < 0},
    {5 x, x == 0},
    {3 x, x > 0}}];
(* 5 *)
(* 3 *)

These are both the same function, and if we take the derivative manually, then we get the same answer:

Limit[(pwf1[0 + h] - pwf1[0])/h, h -> 0]
Limit[(pwf2[0 + h] - pwf2[0])/h, h -> 0]
(* 3 *)
(* 3 *)

So apparently it is better to define the piecewise regions more explicitly,

f[x_] := Piecewise[{
   {x*Sin[1/x], x > 0},
   {0, x == 0},
   {x*Sin[1/x], x < 0}
(* Indeterminate *)

This is the same answer you get when you take the analytic derivative and substitute x=0,

func[x_] := x Sin[1/x];

During evaluation of In[149]:= Power::infy: Infinite expression 1/0 encountered. >>

During evaluation of In[149]:= Power::infy: Infinite expression 1/0 encountered. >>

During evaluation of In[149]:= Power::infy: Infinite expression 1/0 encountered. >>

During evaluation of In[149]:= General::stop: Further output of Power::infy will be suppressed during this calculation. >>

(* Indeterminate *)
  • $\begingroup$ Indeed, I found the same. Strange that defining the regions more explicit will generate different results. $\endgroup$ Dec 1, 2015 at 10:00

Your function is perhaps clearer without the Piecewise. Let

g[x_] = x*Sin[1/x]

Then D[g[x], x]

-(Cos[1/x]/x) + Sin[1/x]

To find what is happening as x approaches the discontinuity:

Limit[D[g[x], x], x -> 0]

Interval[{-∞, ∞}]

which is pretty much saying that the derivative doesn't exist at this point.

  • 1
    $\begingroup$ I am specifically interested in what happens with the derivative if we make the original function continuous. By saying f(x)=0 the original function becomes continuous, but still the derivative cannot be defined at f'(0). $\endgroup$ Nov 30, 2015 at 19:52
  • $\begingroup$ Well... the g[x] suggested above is continuous, as you can see by taking Limit[g[x], x -> 0], which of course gives 0. $\endgroup$
    – bill s
    Nov 30, 2015 at 21:10
  • 1
    $\begingroup$ For it to be continuous, both the limit and the value of f at x should be equal. In your function f(0) is not defined. $\endgroup$ Nov 30, 2015 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.