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$
18
$\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$
  • $\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 '12 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$ – Sjoerd C. de Vries Feb 5 '12 at 22:16
4
$\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$

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.