6
$\begingroup$

I have the following code to show a red area defined by inequalities:

ClearAll["Global`*"];
p = Reduce[y <= 3/10 x + 18 && y > x^2/8, {x, y}]
r = RegionPlot[p, {x, -15, 18}, {y, -5, 25}, 
   GridLines -> {Table[i, {i, -15, 18}], Table[j, {j, -5, 25}]}, 
   PlotStyle -> Directive[{Opacity[0.5], Red}]];
bg = Graphics[{Opacity[0.2], Yellow, Rectangle[{-16, -6}, {19, 26}]}];
range = First /@ 
  Differences /@ (PlotRange /. Options[r]); target = 1; Show[{r, bg}, 
 AspectRatio -> (Last[range]/First[range]/target)]

which shows:

enter image description here

How can I count those integer grids and highlight them with colored dots?

$\endgroup$
3
  • 1
    $\begingroup$ Closely related: How to find lattice points on a line segment?. If there are many solutions Reduce can't be efficient however a neat approach with FrobeniusSolve can yield all interesting solutions as in one of the answers in the link. For benchmarks see this answer Finding the number of solutions to a diophantine equation. $\endgroup$
    – Artes
    Aug 22, 2014 at 13:02
  • $\begingroup$ Thank you! This link and the new command are very useful! $\endgroup$ Aug 22, 2014 at 13:05
  • 1
    $\begingroup$ @Artes Those links show efficient methods, as such problems go. But they require polytope regions (i.e. linear inequalities). $\endgroup$ Aug 22, 2014 at 13:18

3 Answers 3

9
$\begingroup$

You can do :

p = ImplicitRegion[y <= 3/10 x + 18 && y > x^2/8, {x, y}]

points = Reduce[Element[{x, y}, p], {x, y}, Integers]

pp = Cases[points, x == xx_ && y == yy_ -> {xx, yy}]

pp // Length
(* 286 *) 

Show[RegionPlot[p], ListPlot[pp]]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ You can directly to pp using Reduce with pp = {x, y} /. {Reduce[Element[{x, y}, p], {x, y}, Integers] // ToRules}; $\endgroup$
    – Bob Hanlon
    Aug 22, 2014 at 14:56
  • $\begingroup$ @BobHanlon Thanks, I knew there must have been a better way. $\endgroup$ Aug 22, 2014 at 15:07
8
$\begingroup$
eqn = y <= 3/10 x + 18 && y > x^2/8;
sol = Reduce[eqn, {x, y}, Integers];
Length @ sol
(* 286 *)
points = {x, y} /. {ToRules[sol]}; (* thanks: BobHanlon *)
RegionPlot[eqn, {x, -15, 18}, {y, -5, 25}, 
           GridLines -> {Range[-15, 18], Range[-5, 25]}, 
           PlotStyle -> Directive[{Opacity[0.5], Red}], 
           Epilog -> {PointSize[Medium], Point[points]}]

enter image description here

$\endgroup$
2
  • $\begingroup$ You can go directly to points using Reduce with points = {x, y} /. {Reduce[eqn, {x, y}, Integers] // ToRules}; $\endgroup$
    – Bob Hanlon
    Aug 22, 2014 at 15:03
  • $\begingroup$ @BobHanlon, thank you; will update with you suggestion. $\endgroup$
    – kglr
    Aug 22, 2014 at 15:32
7
$\begingroup$

Here is another solution using V10 functionalities:

region = ImplicitRegion[y <= 3/10 x + 18 && y > x^2/8, {{x, -15, 18}, {y, -5, 25}}];

lis = Tuples[{Range[-15, 18], Range[-5, 25]}];

We create a RegionMemberFunction

rm = RegionMember[region];

Now we select from lis the points that are in the region:

in = Select[lis, rm];

Length @ in

286

Visualize:

out = Complement[lis, in];

Show[RegionPlot[region], Graphics[{Red, Point[in],Green, Point[out]}], 
                         PlotRange -> {{-15, 18}, {-5, 25}}]

Mathematica graphics

$\endgroup$

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.