Message Boards Message Boards

0
|
8785 Views
|
29 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Why doesn't RegionPlot plot the region?

Was my 7th grade teacher messing with us when she claimed the set of points that satisfy x^2+y^2=1 was a circle of radius 1? RegionPlot[] thinks so. When it comes to a point (0,0), ContourPlot and RegionPlot each have doubts about that.

More generally, what's the best way to plot a geometric object defined by an equation or by a system of equations in x,y or x,y,z? The parametric plots work reliably for me, but is there a reliable plotting function that displays the points that satisfy Cartesian equations. Am I doing something wrong with ContourPlot and RegionPlot.

enter image description here

POSTED BY: Jay Gourley
29 Replies

I am not sure if I understand the question. ContourPlot is designed to plot one-dimensional objects on the cartesian plane. You are asking RegionPlot to do the same thing--but it has to search for a set of area zero in the plane, and so has to do quite a bit of hunting and recursion to find the one dimensional region. If you are willing to accept a limit then:

Manipulate[
 RegionPlot[1 - e < x^2 + y^2 < 1 + e, {x, -1.2, 1.2}, {y, -1.2, 1.2},
   MaxRecursion -> 5, PlotPoints -> 60], {e, 0, .1}]
POSTED BY: W. Craig Carter
Posted 2 years ago

From the documentation.

RegionPlot initially evaluates pred at a grid of equally spaced sample points specified by PlotPoints. Then it uses an adaptive algorithm to subdivide at most MaxRecursion times, attempting to find the boundaries of all regions in which pred is True.

and

You should realize that since it uses only a finite number of sample points, it is possible for RegionPlot to miss regions in which pred is True. To check your results, you should try increasing the settings for PlotPoints and MaxRecursion.

It works for x == y and the given ranges because the sample points include that line. If the range is changed to an irrational then RegionPlot is empty

RegionPlot[x == y, {x, -Sqrt[2], Sqrt[2]}, {y, -3/2, 3/2}]
POSTED BY: Rohit Namjoshi

Thanks, Rohit Namjoshi and W. Craig Carter.

I appreciate your explaining why Mathematica didn't render the defined sets as I entered them. That leaves the second part of my question.

I'll try to focus it better: What is the most convenient way to visualize the solution to a set of real-valued equations in four or fewer variables? Take a simple example that doesn't need a graphic to visualize.

POSTED BY: Jay Gourley

I'm not sure about the best way, but these are methods I would employ:

Epilog:

solutions = Solve[{x^2 + y^2 == 1, x == y}, {x, y}];
ParametricPlot[ {{Cos[t], Sin[t]}, {t, t}}, {t, -Pi, Pi}, 
Epilog -> {Red, PointSize[0.05], Point[{x, y} /. solutions]}]

Show:

plot = ParametricPlot[ {{Cos[t], Sin[t]}, {t, t}}, {t, -Pi, Pi}]
points = Graphics[{Red, PointSize[0.05], Point[{x, y} /. solutions]}]
Show[plot, points] 
POSTED BY: W. Craig Carter

Thanks, W. Craig Carter.

That code nicely produces the desired result. But it skirts the motive behind my question.

I hope (maybe in vain) for an easy way to visualize a set or a geometric object defined by a system of equations. It's to help me with a book I'm reading on algebraic geometry.

I picked examples x^2 + y^2 = 1 and x = y to avoid extraneous issues. They might have been bad choices since they are so simple they don't need visualization. Sets defined by more complicated systems of equations -- the ones that need visualization -- are often difficult to parameterize.

I hope someone has ideas on a general and practical way to visualize sets defined by systems of equations. Or if someone can explain why that's impossible in Mathematica, I can at least abandon this effort.

POSTED BY: Jay Gourley
Posted 2 years ago

Hi Jay,

Can you give an example of

Sets defined by more complicated systems of equations -- the ones that need visualization -- are often difficult to parameterize.

POSTED BY: Rohit Namjoshi

Yes, Rohit Namjoshi. Here are some examples. I hope they are good examples. I was in a hurry to respond. So I haven't thought too much about them. It would be helpful to see how you would visualize any one of these. Please keep in mind they are just examples. My question is about the easiest way to streamline visualization of real sets like these. I'm just an undergraduate math student. So systems like these are difficult for me to visualize. Thanks in advance for any help you can give.

POSTED BY: Jay Gourley

Here is my workflow for your last example. It is not the best workflow, but how I approached it. Visualize the first two equations:

planes = 
 ContourPlot3D[{x + y + z == 1, x - 2 y - z == 3}, {x, -2, 2}, {y, -2,
    2}, {z, -2, 2}]

Look for a location where I might visualize the third equation:

FindInstance[(y - x^2)^2 + (z - x^3)^2 == 0, {x, y, z}, 6] // N

See if I can find conditions that there are real solutions to the last equation:

Reduce[(y - x^2)^2 + (z - x^3)^2 == 0, {x, y, z}, Reals]

Continue to see if there might be any real solutions other than 0:

Reduce[-x^4 + 2 x^2 y - y^2 > 0, {x, y}, Reals]

I conclude there is no intersection between the three surfaces.

For your second example:

eq1 = x y - z^2 - x;
eq2 = x y z - 2;
ineq3 = x^2 + y^2  + z^2 <= 5;

Sanity check to see if there are any intersections:

FindInstance[{eq1 == 0, eq2 == 0, ineq3}, {x, y, z}, Reals, 6] // N

Reduce[{eq1 == 0, eq2 == 0, ineq3}, {x, y, z}, Reals] // N

It looks like there is a narrow band around x=-1 that may have solutions in it.

Plot the first two surfaces in the region of interest:

surfaces = 
 ContourPlot3D[{eq1 == 0, eq2 == 0}, {x, -1.5, -1}, {y, -10, 
   10}, {z, -10, 10}]

plot the inequality region:

region = 
 RegionPlot3D[ineq3, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
  PlotStyle -> {Opacity[0.5], Orange}] (*I added Opacity on a second pass, not on the first work flow*)

VIsualize the intersection:

Show[surfaces, region]

Look for the curve. Copying and pasting as this is a one-time-only:

pp = ParametricPlot3D[{x, Root[-4 - x^3 #1^2 + x^3 #1^3 &, 1], 
   2/(x Root[-4 - x^3 #1^2 + x^3 #1^3 &, 1])}, {x, -1.15, -1.06}, 
  PlotStyle -> {Red, Thickness[0.02]}]

Visualize:

Show[surfaces, region, pp]

result

POSTED BY: W. Craig Carter

I made an error in my first example: There are real solutions:

FindInstance[(y - x^2)^2 + (z - x^3)^2 == 0, {x, y, z}, Reals, 6] // N

Solve[(y - x^2)^2 + (z - x^3)^2 == 0, z]

Reduce[-x^4 + 2 x^2 y - y^2 >= 0, {x, y}, Reals]

Subspace of real solutions:

((y - x^2)^2 + (z - x^3)^2) /. y -> x^2

Therefore z -> x^3

pp = ParametricPlot3D[{x, x^2, x^3}, {x, -10, 10}]

Show[
 ContourPlot3D[{x + y + z == 1, x - 2 y - z == 3}, {x, -2, 2}, {y, -2,
    2}, {z, -2, 2}],
 pp
 ]

Resolve[Exists[{x, y, z}, {(y - x^2)^2 + (z - x^3)^2 == 0, 
   x + y + z == 1, x - 2 y - z == 3} ], {x, y, z}, Reals]
POSTED BY: W. Craig Carter

Thanks, W. Craig Carter. This is the best idea I've seen. I'll come back to this thread when I've had a chance to apply it more broadly. It will take me some time to see how well it generalizes for me. I'm still Mathematica challenged and mathematics challenged.

POSTED BY: Jay Gourley

You can try the combination Region[ImplicitRegion[]]:

Region[ImplicitRegion[{x^2 + y^2 == 1, x == y}, {x, y}],
 Frame -> True]
Region[ImplicitRegion[{x + y + z == 1, x - 2 y - z == 3},
  {{x, -2, 2}, {y, -2, 2}, {z, -2, 2}}], Boxed -> True]
POSTED BY: Gianluca Gorni

Thank you Gianluca Gorni. Your approach made fast work of all but one of the examples I posted. See notebook at the end. I'm always amazed at the level of help I can get from this community. I'm sorry I couldn't give you more than one thumbs up. Your answer deserves more.

Region[] and ImplicitRegion[] are amazing. You don't even need to tell them how many dimensions to return. I spent many hours pouring through the Visualization & Graphics section of the documentation. I should have been looking in the Geometry section. I posted an earlier question just asking what function(s) were most direct, but the moderators pulled that question because it didn't have any specific code questions.

I'm not sure what the conditionals are telling me in some of the results. I'll keep working on that. As for the system of equations that failed to render, Reduce[] gives an answer, but I'll have to figure out how to put it in a form ImplicitRegion[] can handle, if that's possible.

POSTED BY: Jay Gourley

I am new to Mathematica. What is the standard way to alter presentation of Region[]? Options seem to be different from the plotting presentation options. For example, how would you make the region shown below (two points and a line segment) stand out, say, by making them red and bolder/thicker? Also, should I care that Subscript[[ScriptCapitalR], 1] doesn't turn black when defined?

POSTED BY: Jay Gourley
Posted 2 years ago

You could use Style

Show[
    Region[Style[ImplicitRegion[Subscript[\[ScriptCapitalR], 1], {x, y}], Red, PointSize@Large]],
    Region[Style[ImplicitRegion[Subscript[\[ScriptCapitalR], 2], {x, y}], Red, Thick]],
    PlotRange -> rng1, Axes -> True
 ]
POSTED BY: Hans Milton

Thanks, Hans Milton. That works. It's simple and tells me how to do lots of over things. I love this community.

POSTED BY: Jay Gourley
Posted 2 years ago

Hi Jay,

You may be interested in this article from the Mathematica Journal. The article is based on this book by the same author. There is a link to download the notebooks used in the book.

POSTED BY: Rohit Namjoshi

Thanks, Rohit Namjoshi. I've downloaded it. It will take a while to read, but your recommendation is enough for me.

The generalized introductory documentation for Mathematica is great but never detailed enough to do anything useful. Finding the specialized literature that focuses on the details needed for a special application is hard. So it's great that you shared this.

POSTED BY: Jay Gourley

Jay,

your basic question was perfectly answered by Hans Milton: Style in combination with Region - this is great (+1)!

Here I just have a remark:

When I started to try understanding Mathematica, (well, I am still trying!) I found this discussion on SE extremely useful. There you also will find some background regarding your question:

Also, should I care that Subscript[\[ScriptCapitalR], 1] doesn't turn black when defined?

The reason is that \[ScriptCapitalR] is not defined by this - and stays blue. What is done here instead is adding some additional definitions to Subscript.

So, Subscript[\[ScriptCapitalR], 1] is not a symbol, OwnValues[Subscript[\[ScriptCapitalR], 1]] does not work! The general advice is, not to use Subscript at all, except maybe at the very end of a calculation for displaying reasons.

POSTED BY: Henrik Schachner

Thanks, Henrik Schachner. Looks like that link flags some likely pitfalls.

POSTED BY: Jay Gourley

Am I doing something wrong here? I think the two plots should be identical. If I'm not doing anything wrong, is there a workaround?

POSTED BY: Jay Gourley

I suppose it is because the function (x - y^2)^2 does not change sign around the region, and the numerical methods miss the zero completely. With some symbolic pre-processing things get back to normal:

Region[ImplicitRegion[Reduce[(x - y^2)^2 == 0 && -1 < x < 1, Reals],
  {x, y, z}]]
POSTED BY: Gianluca Gorni
Posted 2 years ago

Using Simplify on the region condition is also a fix

reg2 = (y - x^2)^2 == 0 && (z - x^3)^2 == 0;

Region[
    Style[ImplicitRegion[reg2 // Simplify, {x, y, z}], Red, Thick],
    PlotRange -> 5, Axes -> True, AxesOrigin -> {0, 0, 0}
 ]
POSTED BY: Hans Milton
Posted 2 years ago

Jay, after working a bit with your 3D visualizations I have seen that the graphs appear somewhat small, compared to the area taken up in the notebook. By using the SphericalRegion option the graphs can be made to appear larger:

reg2 = (y - x^2)^2 == 0 && (z - x^3)^2 == 0;

Region[
  Style[ImplicitRegion[reg2 // Simplify, {x, y, z}], Red, Thick],
  PlotRange -> 5, Axes -> True, AxesOrigin -> {0, 0, 0},
  SphericalRegion -> Sphere[{0, 0, 0}, 5]
]
POSTED BY: Hans Milton

Why doesn't this work? The points {0,0,1} and {0,0,-1} are missing. I think I've tried all tricks in this thread that solved earlier problems work. So I may need a new trick.

Either half of the disjunction by itself renders as expected. The whole disjunction will render as expected If I delete one of the x or y equalities in the three-set conjunction. Adding parentheses to make the three-set conjunction into two two-set conjunctions doesn't work either. Nor does removing the parentheses and relying on order of operation.

I know there are several other ways to display the correct set. But I'm trying to find a easy general way to visualize varieties. And ImplicitRegion is a perfect solution. I just need to know how make it work as expected.

POSTED BY: Jay Gourley
Posted 2 years ago

The only trick I can come up with is splitting the region condition in two parts and then using Show

Show[
    Region[
     Style[ImplicitRegion[eq1 && x == 0 && y == 0, {x, y, z}], Red, PointSize@Large],
     PlotRange -> 2
    ],
    Region[
     Style[ImplicitRegion[eq1 && eq2, {x, y, z}], Blue, Thick],
     PlotRange -> 2
    ],
    Axes -> True, AxesOrigin -> {0, 0, 0}
 ]
POSTED BY: Hans Milton

Thanks, Hans Milton. That's a good way to display a graphic I already know. But my goal is to find a generalized way for Mathematica to help me quickly understand sets defined by polynomial equations that would be hard to visualize.

POSTED BY: Jay Gourley

My general impression is that computational geometry is a part of Mathematica that was left half-baked. Some things work brilliantly, others fail miserably, without an apparent reason.

POSTED BY: Gianluca Gorni

You expressed my experience, perfectly, Gianluca. Thanks. It's awsome when it works and would be even more awsome if you could count on it to work. I still wonder if there is some hidden option or setting I could use every time that would always find the red dots in examples like the one I posted.

POSTED BY: Jay Gourley

It seems the OR statement in the Implicit region is not behaving as expected. As far as I can see plotting a single implicit region works. The solution of Hans allows for plotting them both, however you want something that works more general correct?

One option would be to wrap everything in a function that generates the code Hans provided.

r1 = eq1 && x == 0 && y == 0;
r2 = eq1 && eq2;
dom = {x, y, z};

RegionsPlot[eq_, dom_, opts : OptionsPattern[]] := 
 RegionsPlot[eq, dom, Automatic, opts]

RegionsPlot[eq_, dom_, rng_, opts : OptionsPattern[]] := 
  Show[ImplRegions[eq, dom, rng], opts,
   (*default plot options*)
   Axes -> True, AxesOrigin -> {0, 0, 0}, 
   AxesLabel -> {"x", "y", "z"}, 
   AxesStyle -> Directive[Medium, Black, Bold]
   ];

ImplRegions[eq_, dom_, rng_] /; ListQ[eq] := 
 ImplRegions[#, dom, rng] & /@ eq

ImplRegions[eq_, dom_, rng_] /; Head[eq] == Style := 
 Block[{eqp, style},
  {eqp, style} = eq /. Style[e_, st___] -> {e, {st}};
  Region[Style[ImplicitRegion[eqp, dom], style,
    (*Default region styling*)
    PointSize@Large, Thick
    ], PlotRange -> rng]
  ]

ImplRegions[eq_, dom_, rng_] := ImplRegions[Style[eq], dom, rng]

Then this should work, with the function propagating all options given to Show.

RegionsPlot[r2, dom, rng]
RegionsPlot[Style[r1, Green], dom, rng]
RegionsPlot[{Style[r2, Red], Style[r1, Blue]}, dom, rng]

enter image description here

POSTED BY: Martijn Froeling
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract