5
$\begingroup$

Consider an equation $(x-1)^2/25+(y+2)^2/9=1$.

(x-1)^2/25+(y+2)^2/9==1

How can one print all the integer points on and inside the graph of the equation?

I tried many times but failed to solve it.

$\endgroup$
1
  • $\begingroup$ There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please do your part. $\endgroup$
    – rhermans
    Jul 1, 2018 at 8:31

4 Answers 4

7
$\begingroup$

This Prints the numbers, as you asked.

Module[
 {
  eqn = (x - 1)^2/25 + (y + 2)^2/9 <= 1,
  region,
  pnts
  },
 region = ImplicitRegion[eqn, {x, y}];
 pnts = ({x, y} /. Solve[{x, y} \[Element] region, Integers]);
 Print[pnts];
 ]
{{-4,-2},{-3,-3},{-3,-2},{-3,-1},{-2,-4},{-2,-3},{-2,-2},{-2,-1},{-2,0},{-1,-4},{-1,-3},{-1,-2},{-1,-1},{-1,0},{0,-4},{0,-3},{0,-2},{0,-1},{0,0},{1,-5},{1,-4},{1,-3},{1,-2},{1,-1},{1,0},{1,1},{2,-4},{2,-3},{2,-2},{2,-1},{2,0},{3,-4},{3,-3},{3,-2},{3,-1},{3,0},{4,-4},{4,-3},{4,-2},{4,-1},{4,0},{5,-3},{5,-2},{5,-1},{6,-2}
}

But probably you actually want to do something with them, not just Print them.

Module[
 {
  eqn = (x - 1)^2/25 + (y + 2)^2/9 <= 1,
  region, pnts
  },
 region = ImplicitRegion[eqn, {x, y}];
 pnts = ({x, y} /. Solve[{x, y} \[Element] region, Integers]);
 Show[
  Region[region],
  ListPlot[
   pnts
   , PlotStyle -> Red
   , PlotTheme -> "Scientific"]

  ]
 ]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ @Alan thanks for checking and warning me. I had two versions, and pasted the wrong one. It's fixed now. $\endgroup$
    – rhermans
    Jul 1, 2018 at 8:31
3
$\begingroup$
sol=NSolve[(x - 1)^2/25 + (y + 2)^2/9 <=  1, {x, y}, Integers]
Show[{ RegionPlot[(x - 1)^2/25 + (y + 2)^2/9 <= 1, {x, -5, 7}, {y, -6,2}],ListPlot[{x, y} /. sol]}, PlotRange -> All,AspectRatio -> Automatic]

enter image description here

solves the problem!

$\endgroup$
4
  • 1
    $\begingroup$ To answer the question as stated, I think you need to change == to <= $\endgroup$
    – mikado
    Jun 28, 2018 at 19:37
  • $\begingroup$ Thanks, I edited my answer. $\endgroup$ Jun 29, 2018 at 6:37
  • $\begingroup$ @UlrichNeumann you should have plotted the ellipse for completeness, +1 $\endgroup$ Jun 29, 2018 at 10:12
  • $\begingroup$ @José Antonio Díaz Navas: Done, thanks. $\endgroup$ Jun 29, 2018 at 10:33
0
$\begingroup$
reg = Region @ ImplicitRegion[(x - 1)^2 / 25 + (y + 2)^2 / 9 <= 1, {x, y}];
lattice = Tuples[Range[Ceiling @ #, Floor @ #2]& @@@ RegionBounds[reg]]; 

Show[reg, RegionIntersection[reg, Point @ lattice]]

enter image description here

Show[reg, Graphics@{Red, PointSize @ Large, Point @ Select[lattice, RegionMember[reg]]}] 

enter image description here

coords = Pick[lattice , RegionMember[reg, lattice]]

 {{-4, -2}, {-3, -3}, {-3, -2}, {-3, -1}, {-2, -4}, {-2, -3}, {-2, -2}, {-2, -1}, {-2, 0}, {-1, -4}, {-1, -3}, {-1, -2}, {-1, -1}, {-1,   0}, {0, -4}, {0, -3}, {0, -2}, {0, -1}, {0,   0}, {1, -5}, {1, -4}, {1, -3}, {1, -2}, {1, -1}, {1, 0}, {1,   1}, {2, -4}, {2, -3}, {2, -2}, {2, -1}, {2,   0}, {3, -4}, {3, -3}, {3, -2}, {3, -1}, {3,   0}, {4, -4}, {4, -3}, {4, -2}, {4, -1}, {4,   0}, {5, -3}, {5, -2}, {5, -1}, {6, -2}}

$\endgroup$
0
$\begingroup$

If you stick to graphics primitives, than you can just use RegionIntersection. For example, the disk is:

disk = Disk[{1,-2}, {5,3}];

and a lattice that encompasses the disk is:

lattice = Point @ Tuples @ Replace[
    CoordinateBounds[disk],
    {a_, b_} :> Range[Floor[a], Ceiling[b]],
    {1}
];

The intersection is then:

inter = RegionIntersection[disk, lattice]

Point[{{-4, -2}, {-3, -3}, {-3, -2}, {-3, -1}, {-2, -4}, {-2, -3}, {-2, -2}, {-2, -1}, {-2, 0}, {-1, -4}, {-1, -3}, {-1, -2}, {-1, -1}, {-1, 0}, {0, -4}, {0, -3}, {0, -2}, {0, -1}, {0, 0}, {1, -5}, {1, -4}, {1, -3}, {1, -2}, {1, -1}, {1, 0}, {1, 1}, {2, -4}, {2, -3}, {2, -2}, {2, -1}, {2, 0}, {3, -4}, {3, -3}, {3, -2}, {3, -1}, {3, 0}, {4, -4}, {4, -3}, {4, -2}, {4, -1}, {4, 0}, {5, -3}, {5, -2}, {5, -1}, {6, -2}}]

Visualization:

Graphics[{FaceForm[LightBlue], disk, Black, lattice, Red, inter}]

enter image description here

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.