7
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$?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
gaazkam
  • 817
  • 5
  • 15

5 Answers5

9

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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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? – gaazkam Mar 08 '15 at 13:35
  • 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. – gaazkam Mar 08 '15 at 13:36
  • 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? – user106860 Feb 08 '20 at 19:22
3

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}]
ubpdqn
  • 60,617
  • 3
  • 59
  • 148
3

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.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
3

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
1

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

Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78