4
$\begingroup$

I have an issue that the plot below drops the contour at 0. I suspect this case is special because my function is lower bounded by 0. As a work-around, I changed 0 to 0.01, but it looks jagged, any suggestions?

ContourPlot[0.5 (x y - 4)^2, {x, 0, 3}, {y, 0, 3}, 
 Contours -> {0.001, 1, 2, 3, 4}, ContourShading -> None]

enter image description here

$\endgroup$
2

2 Answers 2

5
$\begingroup$

You can add a mesh line using the options MeshFunctions and Mesh:

ContourPlot[0.5 (x y - 4)^2, {x, 0, 3}, {y, 0, 3}, 
 Contours -> {0, 1, 2, 3, 4}, 
 PlotRange -> All, 
 ContourShading -> None, 
 MeshFunctions -> {# #2 &}, 
 Mesh -> {{4}}, 
 MeshStyle -> Black]

enter image description here

$\endgroup$
1
  • $\begingroup$ I always struggle with getting my head around MeshFunctions[ ] $\endgroup$
    – MikeY
    Commented Dec 19, 2019 at 0:35
3
$\begingroup$

The square term and it being the minimum throws it off.

I think your best choice is to just solve your equation for when it is zero separately.

Solve[0.5 (x y - 4)^2 == 0, y]
(*  {{y -> 4/x}, {y -> 4/x}} *)

Then plot and combine.

lp = Plot[4/x, {x, 0, 3}, PlotRange -> {{0, 3}, {0, 3}}, PlotStyle -> Black]

cp = ContourPlot[0.5 (x y - 4)^2, {x, 0, 3}, {y, 0, 3}, Contours -> {1, 2, 3, 4, 5, 6, 7}]

Show[cp,lp]

enter image description here

You can also generate just the 0 contour with the CounterPlot[ ] function, taking advantage of your knowledge of the problem. You can merge this with the rest of the problem as per above.

ContourPlot[ (x y - 4) == 0, {x, 0, 3}, {y, 0, 3}]

enter image description here

$\endgroup$

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.