# Derivative of piecewise function

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}}]
f'


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?

• 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]. – IPoiler Nov 30 '15 at 19:20
• 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. – John McGee Nov 30 '15 at 19:34
• @JohnMcGee In some cases the derivative still exists, if the interval of width 0 makes the function continuous. – GambitSquared Nov 30 '15 at 19:49
• To get the correct answer, evaluate (f[h] - f)/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}]. – murray Nov 30 '15 at 20:16

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}}];
pwf1'
pwf2'
(* 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)/h, h -> 0]
Limit[(pwf2[0 + h] - pwf2)/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}
}]
f'
(* Indeterminate *)


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

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


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

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

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

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

(* Indeterminate *)

• Indeed, I found the same. Strange that defining the regions more explicit will generate different results. – GambitSquared Dec 1 '15 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.

• 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). – GambitSquared Nov 30 '15 at 19:52
• Well... the g[x] suggested above is continuous, as you can see by taking Limit[g[x], x -> 0], which of course gives 0. – bill s Nov 30 '15 at 21:10
• 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. – GambitSquared Nov 30 '15 at 21:33