9
$\begingroup$

Bug introduced in 7.0 and fixed in 9.0.1


The following line works fine:

ContourPlot3D[2 x - y == 0, {x, -10, 10}, {y, -10, 10}, {z, -10, 10}, 
             ContourStyle -> Opacity[0.5], Mesh -> False]

But when I bind the equation 2 x - y == 0 to a variable eq:

eq = 2 x - y == 0
ContourPlot3D[eq, {x, -10, 10}, {y, -10, 10}, {z, -10, 10}, 
              ContourStyle -> Opacity[0.5], Mesh -> False]

it fails. I also noticed that values are assigned to variables x and y after executing ContourPlot3D. There's something that I don't understand about workings of ContourPlot. Could anyone explain what's going on and how should I pass the equation in a variable?

UPDATE

As m_goldberg wrote in his answer, in a bug-free Mathematica it is not necessary to defer evaluation so eq = 2 x - y == 0 works fine. The call to Evaluate is still required. But it seems to me that eq and 2x - y == 0 should evaluate to the same expression:

In[33]:= eq === 1 + 2 x == 0
Out[33]= True

So what's difference between these two expressions, which causes them to behave differently in ContourPlot3D? How can I see this difference in Mathematica environment?

$\endgroup$
  • $\begingroup$ I am proactively affixing the bugs tag in regard to the latter part of the question. Anyone who disagrees (with reason) is free to remove it. $\endgroup$ – Mr.Wizard Feb 21 '13 at 18:08
  • 1
    $\begingroup$ @Mr.Wizard I think this is fair enough. Evidently WRI agreed since this is fixed in version 9--no more assignments on x and y. $\endgroup$ – Oleksandr R. Feb 21 '13 at 18:35
  • $\begingroup$ @OleksandrR. Could you explain (or refer to a proper doc) for those of us still living on previous versions of the Universe? $\endgroup$ – Dr. belisarius Feb 21 '13 at 18:58
  • 1
    $\begingroup$ @Mr.Wizard Yep. V8.0 also gives the "unexpected" result $\endgroup$ – Dr. belisarius Feb 22 '13 at 0:19
  • 1
    $\begingroup$ Regarding your penultimate question, eq only becomes 2x-y==0 when it is evaluated. Before evaluation eq has head Symbol (and 2x-y==0 has head Equal). ContourPlot3D has the HoldAll attribute, so eq is passed to it in unevaluated form. My guess is that when ContourPlot3D tests its first argument to see if it's of the form a==b, it does so without evaluating it, and so ends up treating eq as a numerical function rather than an equality. Certainly a Trace shows eq being sampled at various points in the volume (almost all of which evaluate to False of course). $\endgroup$ – Simon Woods Feb 22 '13 at 21:10
7
$\begingroup$

The problem is very simple :)

  eq = 2 x - y == 0

evaluates to False.

To make it work, simply defer the evaluation:

  eq := 2 x - y == 0
  ContourPlot3D[Evaluate@eq, {x, -10, 10}, {y, -10, 10}, {z, -10, 10}, 
                ContourStyle -> Opacity[0.5], Mesh -> False]

Mathematica graphics

$\endgroup$
  • 2
    $\begingroup$ Thank you! That worked. But I still don't understand something. First, why does eq = 2 x - y == 0 evaluate to false? And second, how do values from ContourPlot3D get assigned to x and y? I assume these two questions are related, but cannot figure out how. $\endgroup$ – Max Feb 21 '13 at 16:52
  • 1
    $\begingroup$ I disagree with the saying that eqn evaluates to False. Not like SameQ, it will keep the form as it is when kernel doesn't know the answer. The problem is the attributes of ContourPlot3D; it seems hold the arguments, but in v11.0.0, it doesn't have HoldAll. I haven't checked other versions. Is this a bug? $\endgroup$ – luyuwuli Dec 21 '16 at 3:50
2
$\begingroup$

I think it should be noted that in V9.0.1 this problem has been fixed; that is,

Clear@eq

eq = 2 x - y == 0;
ContourPlot3D[Evaluate@eq, {x, -10, 10}, {y, -10, 10}, {z, -10, 10}, 
   ContourStyle -> Opacity[0.5], Mesh -> False]

works as expected.

$\endgroup$
  • $\begingroup$ Do you need to use Evaluate if there's no deferred assignment? $\endgroup$ – Max Feb 22 '13 at 11:17
  • $\begingroup$ @Max. Yes, it is necessary. Not even Evaluated -> True works. $\endgroup$ – m_goldberg Feb 22 '13 at 16:18

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.