Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an expression from which I'm trying to create a plot that I can manipulate. The expression contains Abs, and I only want to have it evaluated for reals. What happens when I manipulate the plot is that it is appears to render even when Abs is taking on a complex value, which is something I want to prevent.

Is there a way to restrict the rendering only when a Real number is produced, and ... I don't know ... show a blank plot otherwise.


example function

e1 = Abs[Sqrt[delta*Cos[theta] - b]]
e1 // Manipulate[Plot[#, {theta, 0, 2 Pi}], {b, 0, 2}, {delta, 0, 2}] &
share|improve this question
Dan, Would you include some code so we know the specific function you are working with? – DavidC Apr 9 '13 at 0:34
David, I can do that, but the function is fairly hairy... and I don't know how to reduce it to a point where it still illustrates the problem. – Dan G Apr 9 '13 at 1:07
I updated the question with a simplified function – Dan G Apr 9 '13 at 1:14
up vote 2 down vote accepted

The problem is that Abs[Sqrt[e]] automatically simplifies to Sqrt[Abs[e]]. Perhaps the easiest way is to define a real absolute value:

abs[x_Real] := If[x < 0, -x, x];
e1 = abs[Sqrt[delta*Cos[theta] - b]]
e1 // Manipulate[Plot[#, {theta, 0, 2 Pi}], {b, 0, 2}, {delta, 0, 2}] &


While the above abs works perfectly with Plot, where the argument x is always Real, the following works in more general situations:

abs[x_?(Element[#, Reals] &)] := If[x < 0, -x, x];

Note the common solution abs[x_?NumericQ] doesn't work with the Manipulate. It generates a Less::nord message about an invalid comparison with a complex number on some inputs.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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