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'[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?
Answer
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 *)
Comments
Post a Comment