# [✓] Differentiate a function defined via Piecewise[]?

Posted 5 months ago
577 Views
|
5 Replies
|
4 Total Likes
|
 Using the "default value" syntax of Piecewise[] one can define the function equal to x*sin(1/x) for non-zero x and equal to 0 for x=0 in the following compact form: y[x_] := Piecewise[{{x*Sin[1/x], x != 0}}] Now, if we try to calculate the value of its derivative at x=0, then Mathematica assumes that it depends only on the value of y[x] at x=0: y'[0] 0 But of course this is not true --- this function is not differentiable at x=0, because for x!=0 we have: y'[x] = Sin[1/x] - Cos[1/x]/x And the above expression has no limit as x approaches 0 and Mathematica knows this very well: Limit[Sin[1/x] - Cos[1/x]/x, x -> 0] Indeterminate Limit[y'[x],x->0] Indeterminate Also, just taking the definition of derivative (as a limit) at x=0 we would end up with Limit[Sin[1/h],h->0] which of course doesn't exist.So, I think there is a bug here: when one applies the differentiation operator D to something which has the head of Piecewise it shouldn't differentiate the expression for each condition independently, because the value of a derivative of a function at some point depends not only on the value of the function at that point, but also on all the values of the function in the infinitesimal neighbourhood of that point.
5 Replies
Sort By:
Posted 5 months ago
 You get a correct answer with this syntax: f[x_] := Piecewise[{{x*Sin[1/x], x < 0 || x > 0}}] 
Posted 5 months ago
 Thank you, yes, indeed it works if I define it the way you suggest.But can you please explain why it makes a difference? I still think that the fact that it doesn't work in the form in which I showed it above is a bug.
Posted 5 months ago
 I don't know the internals, so I am just guessing. With the x < 0 || x > 0 syntax, Mathematica knows that x is a real variable, that the derivative is in the real sense, and calls the recent, sophisticated algorithms for the real case. If the condition is x != 0, Mathematica may assume that the variable is complex, for which case nobody has written any code (yet?), and it defaults to the older dumb behaviour.