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.

I am using the general form of a second-degree plane curve:

$$Ax^2+2Bxy + Cy^2+2Dx + 2Ey + F = 0$$

I want to randomly generate plane curves of this form, so I am using RandomReal[{-1,1},6] to
generate the coefficients. I made the above equation into a function:

SecondDegreeCurve[{a_, b_, c_, d_, e_, f_}, x_, y_] := 
   a x^2 + 2 b x y + c y^2 + 2 d x + 2 e y + f == 0

However, when I try to plug the composition

ContourPlot[
 SecondDegreeCurve[RandomReal[{5}, 6], x, y], 
 {x, -2, 2}, {y, -2, 2}]

into ContourPlot, the image comes back blank.

When I run SecondDegreeCurve[RandomReal[{5}, 6], x, y], I get something like the following output:

1.4557 + 5.20582 x + 1.29609 x^2 + 9.37565 y + 6.73248 x y + 1.84528 y^2 == 0

and when I plug this into ContourPlot, the curve is displayed.

My question is, what is it about the initial composed expression that doesn't display the curve?

share|improve this question

3 Answers 3

up vote 14 down vote accepted

This has to do with the HoldAll attribute of ContourPlot. Try it with Evaluate inserted, like this:

ContourPlot[
   SecondDegreeCurve[RandomReal[{5}, 6], x, y] // Evaluate,
   {x, -2, 2}, {y, -2, 2}
]

you get ouput like this:

Mathematica graphics

or

Mathematica graphics

share|improve this answer
    
That did the trick! So, since I ran the composed function and got the output, that was the evaluation step that I was missing initially? –  Tobi Lehman Feb 5 '12 at 22:12
    
Yeah, if you don't use Evaluate your function is evaluated completely anew for every point the ContourPlot tries. That means the RandomReal is executed every time, yielding a hopeless mess of values. With Evaluate the RandomReal disappears from the resulting function, as only the numerical results remain. –  Sjoerd C. de Vries Feb 5 '12 at 22:16

Evaluate works. I prefer Function:

SecondDegreeCurve[{a_, b_, c_, d_, e_, f_}, x_, y_] := 
  a x^2 + 2 b x y + c y^2 + 2 d x + 2 e y + f == 0

ContourPlot[#, {x, -2, 2}, {y, -2, 2}] & @
  SecondDegreeCurve[RandomReal[{5}, 6], x, y]

Mathematica graphics

share|improve this answer

Use Evaluate, i.e.

ContourPlot[Evaluate @ SecondDegreeCurve[RandomReal[{5}, 6], x, y] == 0, 
   {x, -2, 2}, {y, -2, 2}]
share|improve this answer

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.