Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Mr.Wizard Feb 21 '13 at 18:08
1  
@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. –  Oleksandr R. Feb 21 '13 at 18:35
    
@OleksandrR. Could you explain (or refer to a proper doc) for those of us still living on previous versions of the Universe? –  belisarius Feb 21 '13 at 18:58
1  
@Mr.Wizard Yep. V8.0 also gives the "unexpected" result –  belisarius Feb 22 '13 at 0:19
1  
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). –  Simon Woods Feb 22 '13 at 21:10
show 4 more comments

2 Answers

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

share|improve this answer
1  
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. –  Max Feb 21 '13 at 16:52
add comment

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.

share|improve this answer
    
Do you need to use Evaluate if there's no deferred assignment? –  Max Feb 22 '13 at 11:17
    
@Max. Yes, it is necessary. Not even Evaluated -> True works. –  m_goldberg Feb 22 '13 at 16:18
add comment

Your Answer

 
discard

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.