0
$\begingroup$

Suppose we have a function that does something with its parameters, and then returns an expression containing an equality. I want to then use that expression in (Contour)Plot to visualize it. The returned expression is correct, but plotting does not work - ContourPlot appears to evaluate something else and does not draw anything. I have tried Defer and Hold*, without any change.

The actual use case is a bit more involved, but a minimum working example is this:

ellipse[a_, b_, c_] := a x^2 + b y^2 == c^2;
(* does not work *)
ContourPlot[ellipse[1, 0.42, 3], {x, -10, 10}, {y, -10, 10}]

(* manually transfer the expression - works *)
Print[ellipse[1, 0.42, 3]]
ContourPlot[x^2 + 0.42 y^2 == 9, {x, -10, 10}, {y, -10, 10}]

I have frequently run into similar problems with Plot, but this time my usual workaround of just unrolling the function manually would be very awkward, hence the question.

$\endgroup$
  • $\begingroup$ Another way ContourPlot[ a x^2 + b y^2 == c^2 /. Thread[{a, b, c} -> {1, 0.42, 3}] // Evaluate, {x, -10, 10}, {y, -10, 10}] $\endgroup$ – cvgmt Sep 11 at 11:35
1
$\begingroup$
ellipse1[a_, b_, c_] := a x^2 + b y^2 == c^2;
ContourPlot[Evaluate[ellipse1[1, 0.42, 3]], {x, -10, 10}, {y, -10, 10}]

or more explicitly as suggested by @Kuba

ellipse1[a_, b_, c_, x_, y_] := a x^2 + b y^2 == c^2;
ContourPlot[Evaluate[ellipse1[1, 0.42, 3, x,y]], {x, -10, 10}, {y, -10, 10}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Huh. I tried that too, and it did not work either. But it works in the example... Are there any caveats that I might have hit? $\endgroup$ – Martok Sep 10 at 13:34
  • $\begingroup$ It won't work if x or y have values @martok so you can define it as a function of five variables. $\endgroup$ – Kuba Sep 10 at 13:37
  • $\begingroup$ Maybe that was it. I did try a lot of things without a Clear in between... $\endgroup$ – Martok Sep 10 at 13:54
0
$\begingroup$

I' m not quite sure of the details, but apparently you cannot define a function with an equality. A solution to your problem may be writing in the following way:

ellipse[a_, b_, c_] := a x^2 + b y^2 - c^2;
ContourPlot[ellipse[1, 0.42, 3] == 0, {x, -10, 10}, {y, -10, 10}];
| improve this answer | |
$\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.