15
$\begingroup$

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?

$\endgroup$

4 Answers 4

19
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – tlehman
    Feb 5, 2012 at 22:12
  • $\begingroup$ 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. $\endgroup$ Feb 5, 2012 at 22:16
5
$\begingroup$

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

$\endgroup$
3
$\begingroup$

Use Evaluate, i.e.

ContourPlot[Evaluate @ SecondDegreeCurve[RandomReal[{5}, 6], x, y] == 0, 
   {x, -2, 2}, {y, -2, 2}]
$\endgroup$
-1
$\begingroup$
ContourPlot[Evaluate@tab, {x, -1, 1}, {y, -1, 1}]
$\endgroup$
1
  • 1
    $\begingroup$ As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. $\endgroup$
    – Community Bot
    Feb 25 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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