2
$\begingroup$

Is there any way to mark integer points after plotting region in Mathematica? For example, if I:

RegionPlot[x >= 4 y && x <= 4 y + 3 , {x, 0, 63}, {y, 0, 15}]

Mathematica graphics

then it highlights the region but I want to see Y value corresponding to integer X value. Can I do it with Mathematica?

$\endgroup$
  • $\begingroup$ I added a graphic to your post to help illustrate. Could you describe in more detail what you expect to see, please? $\endgroup$ – Mr.Wizard Dec 11 '12 at 16:39
  • $\begingroup$ Like in above image I want to mark (x,y) where both x and y are integers. $\endgroup$ – username_4567 Dec 11 '12 at 16:44
8
$\begingroup$

Use the inequality to sow all integer coordinates that are inside the boundary of the region when iterating through all integer pairs of the full range:

pts = First@Last@Reap@Do[If[x >= 4 y && x <= 4 y + 3, Sow@{x, y}], {x, 0, 63}, {y, 0, 15}]
RegionPlot[x >= 4 y && x <= 4 y + 3, {x, 0, 63}, {y, 0, 15}, Epilog -> {Red, Point@pts}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Amazing..Thanks a lot.. $\endgroup$ – username_4567 Dec 11 '12 at 16:58
8
$\begingroup$

Another way to generate all the points is by using Reduce:

points = {x, y} /. 
 List@ToRules@
   Reduce[x >= 4 y && x <= 4 y + 3 && 0 < x < 63 && 0 < y < 15, {x, y}, Integers]

If you give bounds (and thus constrain the possible solutions to a finite set), Reduce will typically list all solutions.

Then just plot them with Point or ListPlot:

ListPlot[points]

Show them together with the RegionPlot:

Show[ListPlot[points], RegionPlot[...]]

Thanks to Mr.Wizard to pointing me to the following relevant note in the documentation:

Mathematica enumerates the solutions explicitly only if the number of integer solutions of the system does not exceed the maximum of the $p^{\text{th}}$ power of the value of the system option DiscreteSolutionBound, where $p$ is the dimension of the solution lattice of the equations, and the second element of the value of the system option ExhaustiveSearchMaxPoints.

$\endgroup$
  • 2
    $\begingroup$ Might want to mention ExhaustiveSearchMaxPoints. +1 $\endgroup$ – Mr.Wizard Dec 11 '12 at 18:29
  • $\begingroup$ @Mr.Wizard Thanks for the pointer. I didn't know about this option. $\endgroup$ – Szabolcs Dec 11 '12 at 23:42
  • $\begingroup$ I changed your documentation link to an anchor I thought was more appropriate. If you disagree change it back, or perhaps add a second one. $\endgroup$ – Mr.Wizard Dec 12 '12 at 19:30
  • $\begingroup$ @Mr.Wizard I agree, but I am usually too lazy to dig out the precise anchor (if it is not linked to and I can't just copy the link). Is there an easier way than using the dev tools of the browser or looking at the web page source? $\endgroup$ – Szabolcs Dec 13 '12 at 0:23
  • $\begingroup$ There's a Meta post about that. I still use View Selection Source myself; it really doesn't take long. If your browser only lets you view the source of the entire page I can see the problem. $\endgroup$ – Mr.Wizard Dec 13 '12 at 1:36
2
$\begingroup$

Alternatively you can generate just the points you want and then plot them :

data = DeleteCases[Flatten[Outer[Boole[4 #2 <= #1 <= 4 #2 + 3] {#1, #2} &, 
    Range[0, 63], Range[0, 15]], 1], {0, 0}];

Show[RegionPlot[x >= 4 y && x <= 4 y + 3, {x, 0, 63}, {y, 0, 15}],  ListPlot[data]]

plot

$\endgroup$
2
$\begingroup$

Another, using smart and fast functions like Array and Tuples, thus a bit more recommended way :

RegionPlot[ x >= 4 y && x <= 4 y + 3, {x, 0, 63}, {y, 0, 15}, Epilog -> { 
            Red, PointSize[0.005], 
            Point[ Join @@ Tuples /@ Array[ {Range[4 #, 4 # + 3], {#}} &, {16}, 0]]}, 
            AspectRatio -> 15/63 ]

enter image description here

$\endgroup$
  • $\begingroup$ By the way, you can just do AspectRatio -> Automatic... $\endgroup$ – Rahul Dec 12 '12 at 2:30
  • $\begingroup$ @RahulNarain That's all ? $\endgroup$ – Artes Dec 12 '12 at 19:04
  • $\begingroup$ I mean, instead of manually specifying 15/63. $\endgroup$ – Rahul Dec 12 '12 at 21:49

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.