Derivative of piecewise function
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'[0]
pwf2'[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}
}]
f'[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];
func'[0]
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 *)
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.