Can someone be so nice to provide some help for this particular situation? I try to plot a graph of a derivative which contains modulus. Without it the graph is drawn correctly:

correct graph

But when I wrap the expression in the Abs Mathematica outputs an empty graph:

empty graph

I looked for several answers related to empty graphs, but nothing seems to fit to my case. Thanks in advance.

  • 1
    $\begingroup$ Workaround: With[{a = Piecewise[{{#, # >= 0}, {-#, # < 0}}, 0] &}, Plot[Evaluate[D[a[5 - 2 x], x]], {x, -1*^3, 1*^3}, Axes -> None, PlotRange -> {-1*^3, 1*^3}]] $\endgroup$
    – J. M.'s torpor
    May 8 '13 at 15:25
  • $\begingroup$ @J. M, thank you, it works $\endgroup$ May 8 '13 at 15:27

Might it be the way Mathematica deals with the derivative of Abs[]? For example,

D[Abs[5 - 2 x], x]


-2*Derivative[1][Abs][5 - 2*x]


Plot[Evaluate[D[Sqrt[(5 - 2 x)^2], x]], {x, -10, 10}]
  • 2
    $\begingroup$ I was about to say that, so I added the relevant example to your answer. +1 $\endgroup$
    – rcollyer
    May 8 '13 at 15:36

I'm also wondering why Mathematica doesn't treat the derivative of Abs as normal way. But here is a solution.

Plot[Evaluate@ComplexExpand[D[Abs[5 - 2 x], x]], {x, -10, 10}]
  • 1
    $\begingroup$ "I'm also wondering..." - that Mathematica considers variables to take complex values by default is a hint... $\endgroup$
    – J. M.'s torpor
    May 9 '13 at 3:44
  • 1
    $\begingroup$ Perhaps not everyone knows that as a function of a complex variable, Abs[z] is not differentiable at any z. $\endgroup$
    – Michael E2
    May 9 '13 at 3:49
  • $\begingroup$ @J.M. Yes. Sometimes if plotting a complex function carelessly , Mathematica will return a empty graphic. I think in this occasion, it will be helpful if Mathematica give some warnings. $\endgroup$
    – luyuwuli
    May 9 '13 at 4:11
  • $\begingroup$ @MichaelE2 Mathematica often tells me to rethink about the complex variables:) $\endgroup$
    – luyuwuli
    May 9 '13 at 4:14

As of V11.1, another available approach is to use RealAbs instead of Abs:

Plot[Evaluate[D[RealAbs[5 - 2 x], x]], {x, -10, 10}]

Note that Abs, as a function of a complex variable, is not differentiable, which I mentioned in a comment earlier.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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