4
$\begingroup$

I want to plot contours of the signed distance function of a region defined by an implicit equation: $R = \{ (x,y) : F(x,y) \le 0 \}$. As a trivially simple concrete example, we could take the region to be the elliptical shape where $F(x,y) = x^2 + 4y^2-1$.

I define Gxy = SignedRegionDistance[R], and then I pass Gxy to the ContourPlot function. It works, but it’s pretty slow -- around a minute on a decent laptop.

What can I do to speed it up? I hope the answer is not "buy a better laptop" :-)

Also, I get mysterious error messages that I don't understand:

Max::nord: Invalid comparison with 1.71429 -1.3924 I attempted. >>

Here is my code:

Fxy = x^2 + 4*y^2 - 1;
R = ImplicitRegion[Fxy <= 0, {x, y}];
Gxy = SignedRegionDistance[R, {x, y}];
levels = {-0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3};
ContourPlot[Gxy, {x, -2, 2}, {y, -1, 1}, Contours -> levels, 
 ContourShading -> None, AspectRatio -> Automatic]
$\endgroup$
0

2 Answers 2

4
$\begingroup$
  • DiscretizeRegion the region at first.
Clear[R];
Fxy = x^2 + 4*y^2 - 1;
R = DiscretizeRegion@ImplicitRegion[Fxy <= 0, {x, y}];
Gxy = SignedRegionDistance[R, {x, y}];
levels = {-0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3};
ContourPlot[Gxy, {x, -2, 2}, {y, -1, 1}, Contours -> levels, 
 ContourShading -> None, AspectRatio -> Automatic]

enter image description here

  • If we do not want to discrete the region, we could define dist = SignedRegionDistance[R] at first, then act on the point {x,y}.
Clear[R, dist];
Fxy = x^2 + 4*y^2 - 1;
R = ImplicitRegion[Fxy <= 0, {x, y}];
levels = {-0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3};
dist = SignedRegionDistance[R]; 
ContourPlot[
 dist@{x, y}, {x, -2, 2}, {y, -1, 1}, Contours -> levels, 
 ContourShading -> None, AspectRatio -> Automatic, PlotPoints -> 100, 
 MaxRecursion -> 0, Exclusions -> None]

enter image description here

$\endgroup$
4
  • $\begingroup$ Fabulous. Down to 4 seconds from around 50. Did you get the same error messages that I got? What are those trying to tell me?? $\endgroup$ Commented Oct 8 at 10:18
  • $\begingroup$ Discretizing the region doesn't seem to do any harm. I was afraid that it would make the final plot look discretized in some way, but it doesn't seem to. Your two solutions produce exactly the same output, don't they? Is one better than the other in some way? $\endgroup$ Commented Oct 8 at 10:21
  • $\begingroup$ To answer "is one solution better than the other" -- the second solution takes several minutes on my computer, while the first one takes only 4 seconds. Big difference. $\endgroup$ Commented Oct 8 at 10:32
  • $\begingroup$ @bubba On my computer( Mac M4) with the Mathematica version 14.3.0, the second code get the result only within 8 second. $\endgroup$ Commented Oct 8 at 11:15
4
$\begingroup$

It seems that the error message comes from some underlying calculation of discontinuities (similar issues #1 and #2 in older versions). If you set the option Exclusions -> None, you will not get the error. Also, you may want to manually decrease PlotPoints to get better performance (perhaps together with slightly higher MaxRecursion).

Fxy = x^2 + 4*y^2 - 1;
R = ImplicitRegion[Fxy <= 0, {x, y}];
Gxy = SignedRegionDistance[R, {x, y}];
levels = {-0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3};

ContourPlot[Gxy, {x, -2, 2}, {y, -1, 1}, Contours -> levels, 
 ContourShading -> None, AspectRatio -> Automatic, Exclusions -> None, 
 PlotPoints -> 5, MaxRecursion -> 3]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks. The error messages have mysteriously disappeared. Adjusting PlotPoints and MaxRecursion produced some further speedup. $\endgroup$ Commented Oct 8 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.