0
$\begingroup$

I am using ContourPlot to plot implicit equations. Plotting separate implicit equations seems to work fine, however I can't get ContourPlot to plot a piecewise function that returns implicit equations.

ClearAll["Global`*"];
a[p1_,p2_]:=(p1-30)^2+(2p2-60)^2==250
b[p1_,p2_]:=(p1-20)^2+(5p2-40)^2==300
pw[p1_,p2_]:=Piecewise[{{a[p1,p2],p1<p2}},b[p1,p2]]
Grid[{{
  ContourPlot[Evaluate@a[p1,p2],{p1,0,50},{p2,0,50}],
  ContourPlot[Evaluate@b[p1,p2],{p1,0,50},{p2,0,50}],
  ContourPlot[Evaluate@pw[p1,p2],{p1,0,50},{p2,0,50}]
}}]

Output

I am expecting the third figure to have 2 diagonal cut-off circles, like so: Expected output

$\endgroup$
  • $\begingroup$ Use RegionFunction for a workaround: Show[ ContourPlot[Evaluate@a[p1, p2], {p1, 0, 50}, {p2, 0, 50}, RegionFunction -> (#1 < #2 &)], ContourPlot[Evaluate@b[p1, p2], {p1, 0, 50}, {p2, 0, 50}, RegionFunction -> (#1 >= #2 &)]] $\endgroup$ – Bob Hanlon Jan 6 at 23:37
3
$\begingroup$

Try these two changes. First, use Boole instead of Piecewise, maybe like this

pw[p1_, p2_] :=  With[{b = Boole[p1 < p2]}, 
    (p1 - 20 - 10 b)^2 + (5 p2 - 40 - (3 p2 + 20) b)^2 == 300 - 50 b
]

Second, use pw[p1,p2] (without the underscores) in the ContourPlot command.

enter image description here

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thanks for picking up the underscore typo, I've edited the question. Interesting workaround, I wonder why Boole works and Piecewise does not? $\endgroup$ – John Smith Jan 5 at 22:56

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.