Five points are required to define a unique ellipse. An ellipse has five degrees of freedom: the $x$ and $y$ coordinates of each focus, and the sum of the distance from each focus to a point on the ellipse, or alternatively, the $x$ and $y$ coordinates of the center, the length of each radius, and the rotation of the axes about the center.
I need a function, that fits an ellipse, for given five $(x,y)$ pairs. Is there a function in Mathematica to do that? If it's possible I need a plot with the ellipse and the given points, and also the equation of the fitted ellipse.
I need an other function, that could check that if a point is on an ellipse. For example on an ellipse, that we just fitted with the previous function.
Answer
The following is based on the fact that the determinant of a matrix is equal to zero when two rows are the same. Thus, if you plug any of the points in, you get a true statement.
SeedRandom[3];
pts = RandomReal[{-1, 1}, {5, 2}];
row[{x_, y_}] := {1, x, y, x*y, x^2, y^2};
eq = Det[Prepend[row /@ pts, row[{x, y}]]] == 0
(* Out:
0.0426805-0.0293168x-0.155097x^2-0.019868y-0.087933x*y-0.061593y^2 == 0
*)
ContourPlot[Evaluate[eq], {x, -1, 1}, {y, -1, 1},
Epilog -> Point[pts]]
Comments
Post a Comment