8
$\begingroup$

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.

$\endgroup$
2
  • 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
11
$\begingroup$

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

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

returns

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

Try

Plot[Evaluate[D[Sqrt[(5 - 2 x)^2], x]], {x, -10, 10}]
$\endgroup$
1
  • 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
7
$\begingroup$

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}]
$\endgroup$
4
  • 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
4
$\begingroup$

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.

$\endgroup$

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.