For example, the non-differentiable point of the function $f(x)=|x|$ is at $x=0$.
How to find the non-differentiable points of a continuous function that is defined numerically?
If we define f[x]
e.g. like this:
f[x_] := Abs[x]
the following returns interesting points:
Reduce[
Limit[(f[x + h] - f[x])/h, h -> 0, Assumptions -> x ∈ Reals, Direction -> -1] !=
Limit[(f[x + h] - f[x])/h, h -> 0, Assumptions -> x ∈ Reals, Direction -> 1], x]
x == 0
Let's try another function defined with Piecewise
, e.g.
g[x_] := Piecewise[{{x^2, x < 0}, {0, x == 0}, {x, 1 > x > 0},
{1, 2 >= x >= 1}, {Cos[x - 2] + x - 2, x > 2}}]
then we needn't use Assumptions
in Limit
:
Reduce[ Limit[ (g[x + h] - g[x])/h, h -> 0, Direction -> -1] !=
Limit[ (g[x + h] - g[x])/h, h -> 0, Direction -> 1], x]
x == 0 || x == 1 || x == 2
pts = {x, g[x]} /. {ToRules[%]};
Plot[ g[x], {x, -5/4, 3}, PlotStyle -> Thick,
Epilog -> {Red, PointSize[0.023], Point[pts]}]
One should be careful when working with Piecewise
since Reduce
may produce errors when weak inequalities (LessEqual
) are involved. For this reason we added {0, x == 0}
in the definition of the function g
.
-
$\begingroup$ But when
f[x_] := Abs[x]
, I run your code and get false, not 0(12.1.1). $\endgroup$ – A little mouse on the pampas Jul 29 '20 at 3:12 -
1$\begingroup$ @Ordinaryusers68 This answer had provided correct solution in version $9.0.1$ and formerly. And it should work correctly until now. However
RealAbs
would have been introduced in $11.1$ and consequentlyAbs
changed as well asLimit
in version $11.2$. Example with functionf
should be changed usingAssumptions -> x>=0
andAssumptions -> x < 0
to avoid misleading results. However example withg
(usingPiecewise
) works correctly. $\endgroup$ – Artes Jul 29 '20 at 9:36
Here is an approach that you can use for numerical functions that at least have a left and right derivative. If such a function isn't differentiable in a point that is equivalent to the left and right derivatives being unequal, so look at the left and right finite difference approximation of the derivative, and see where they disagree.
rightd[f_, h_, x_] := (f[x + h] - f[x])/h
leftd[f_, h_, x_] := (f[x] - f[x - h])/h
f[x_?NumericQ] := If[x < .13, 0 , x - .13 ]
Plot[Abs[leftd[f, 0.1, x] - rightd[f, 0.1, x]], {x,-1,1}, PlotRange->All]
Using this you can use some numerical maximization on Abs[leftd[f, h, x] - rightd[f, h, x]]
, perhaps with successively smaller h
to avoid false positives.