1
$\begingroup$

There is some strange behaviour in Mathematica which I don't understand. If you call a function like ContourPlot on two identical expressions (one a named expression, and the other the same expression literal typed out), then one gets different outputs.

Calling it on a named expression:

mysoln = y^2 == C*(y + x)*(y - x)^2;
myC = C /. Solve[mysoln /. {x -> 2, y -> 1}, C][[1, 1]];
myFinalSoln = mysoln /. {C -> myC}

produces

y^2 == 1/3 (-x + y)^2 (x + y)

Then we attempt to use it inside a ContourPlot, and it produces an empty plot:

ContourPlot[myFinalSoln, {x, xmin, xmax}, {y, ymin, ymax}]

But, if we instead insert the equation in "typed out" form into ContourPlot, then it works:

ContourPlot[y^2 == 1/3 (-x + y)^2 (x + y), {x, xmin, xmax}, {y, ymin, ymax}]

What is going on here?

$\endgroup$
3

1 Answer 1

1
$\begingroup$

Your problem comes from the attribute "HoldAll" of ContourPlot. This attribute is responsible that Contour only gets the name and not the value. Why ContourPlot does not dereference the name itself, I do not know. However, to force that ContourPlot gets the value, you need Evaluate like:

mysoln = y^2 == C*(y + x)*(y - x)^2;
myC = C /. Solve[mysoln /. {x -> 2, y -> 1}, C][[1, 1]];
myFinalSoln = mysoln /. {C -> myC}

ContourPlot[Evaluate[myFinalSoln], {x, 0, 1}, {y, -1, 1}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you, much appreciated. $\endgroup$ Commented Feb 7, 2023 at 21:55

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.