7
$\begingroup$
Plot[D[Abs[x], x], {x, -10, 10}, Exclusions -> {0}]

gives out several lines of error messages and an empty plot.

Plot[Derivative[1][Abs[#1] & ][x], {x, -10, 10}, Exclusions -> {0}]

just gives out an empty plot.

How do I plot $\left(\left|x\right|\right)^\prime$?

$\endgroup$
2
  • 2
    $\begingroup$ derivative of |x| is x/|x|, so you can use that. Plot[x/Abs[x], {x, -10, 10}] $\endgroup$ – Nasser Mar 8 '15 at 11:29
  • $\begingroup$ Also related: 8188 $\endgroup$ – Michael E2 Apr 8 '15 at 13:06
9
$\begingroup$

Using PiecewiseExpand[Abs[x], x \[Element] Reals] instead of Abs[x]:

Plot[Evaluate@ D[PiecewiseExpand[Abs[x], x \[Element] Reals], x], {x, -10, 10}, 
 PlotStyle -> Thick]

enter image description here

You can convert Abs to Piecewise for real arguments using PiecewiseExpand:

absToPW[x_] := PiecewiseExpand[Abs[x], x \[Element] Reals]
absToPW[z]

enter image description here

which you can differentiate

D[absToPW[z], z] 

enter image description here

and plot

Plot[Evaluate@{absToPW[x], D[absToPW[x], x]}, {x, -4, 4}, PlotStyle -> Thick]

enter image description here

$\endgroup$
3
  • $\begingroup$ In that case, I'm curious why won't this: Plot[D[Piecewise[{{x, x > 0}, {-x, x < 0}}, Indeterminate], x], {x, -10, 10}] work? Especially since D[Piecewise[{{x, x > 0}, {-x, x < 0}}], x] gives out Piecewise[{{-1, x < 0}, {1, x > 0}}, Indeterminate], and Plot[Piecewise[{{-1, x < 0}, {1, x > 0}}, Indeterminate], {x, -10, 10}] works nicely? $\endgroup$ – gaazkam Mar 8 '15 at 13:35
  • $\begingroup$ Ah, right. Having seen the other answer I now know that I should've typed Plot[Evaluate[D[Piecewise[{{x, x > 0}, {-x, x < 0}}, Indeterminate], x]], {x, -10,10}] instead, which works neatly. Sorry for troubling you. $\endgroup$ – gaazkam Mar 8 '15 at 13:36
  • $\begingroup$ Is there still a reason to define absToPW as you do here instead of using the command RealAbs (recently introduced)? If not, then perhaps it is worth updating this answer to mention that one can just use RealAbs? $\endgroup$ – user106860 Feb 8 '20 at 19:22
3
$\begingroup$

I suspect the simplest way is just define the absolute value function yourself, e.g.

f[x_] := Piecewise[{{x, x > 0}, {-x, x < 0}}]
Plot[Evaluate[D[f[x], x]], {x, -1, 1}, Exclusions -> {0}]
$\endgroup$
1
  • $\begingroup$ +1 the problem is that mathematica doesn't knows how to differentiate Abs $\endgroup$ – k_v Mar 8 '15 at 11:34
3
$\begingroup$

To extend the other answers, if you're going to be using the derivative of Abs often in your computations and do not need the complex absolute value, then you can define the Derivative of Abs once and for all, using whichever formula for the derivative of Abs you find convenient.

Derivative[1][Abs][x_] = Piecewise[{{1, x > 0}, {-1, x < 0}}, Indeterminate];
Plot[Evaluate@D[Abs[x], x], {x, -10, 10}, Exclusions -> {0}]

Mathematica graphics

Note that one must use Evaluate on the derivative. (see, for instance, General::ivar is not a valid variable when plotting - what actually causes this and how to avoid it?).

To Unset the definition, do the following:

Derivative[1][Abs][x_] =.

You can also localize the definition of the derivative to a block of code as follows:

Internal`InheritedBlock[{Derivative},
 Derivative[1][Abs][x_] = Piecewise[{{1, x > 0}, {-1, x < 0}}, Indeterminate];
 ...
 <code>
 ...
 ]

The definition of Derivative is automatically reset in this case.

$\endgroup$
3
$\begingroup$

You could rewrite it as

Plot[Evaluate @ ComplexExpand @ D[Abs[x], x], {x, -10, 10}, Exclusions -> {0}]

But I would rewrite it as

dAbs[x_] = ComplexExpand @ D[Abs[x], x];
Plot[dAbs[x], {x, -10, 10}, Exclusions -> {0}]

Both produce

plot

$\endgroup$
1
$\begingroup$

you can also use UnitStep:

f[a_] := (x - a) (UnitStep[x - a] - UnitStep[-(x - a)])
Plot[Evaluate@D[f[5], x], {x, -10, 10}, Exclusions -> 0]

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.