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.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityConsider 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.
This Print
s 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"]
]
]
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]
solves the problem!
==
to <=
$\endgroup$
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]]
Show[reg, Graphics@{Red, PointSize @ Large, Point @ Select[lattice, RegionMember[reg]]}]
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}}
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}]