# Fitting ellipse to 5 given points on the plane

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.

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]]


• Thank you @Mark McClure! Can you tell me something more? Beside the Evaluate[eq] how can I plot an extra {x_6,y_6} point in the same ContourPlot? Or some extra points? – user153012 Sep 27 '14 at 17:24
• @user153012 If you'd like to plot more points in a list, say morePoints, simply add another Point primitive containing your list to the Epilog. – Mark McClure Sep 27 '14 at 18:53
• @user153012 For more than five points, see my comment to the original question: it becomes a fitting problem that has been asked about before. – Jens Sep 27 '14 at 19:05
• @Jens Thank you. The referred question is interesting. I know for $5$ points it is a special case of that. But this last question is a little bit different, because I wanted to plot extra points, after we determined the ellipse. – user153012 Sep 27 '14 at 19:07
• @Jens My assumption is that the OP simply wants to add more points that are know to be on the same ellipse, rather than find a best fit. – Mark McClure Sep 27 '14 at 19:08

The general equation of ellipse (here) is given by:

ellipse[x_, y_] = a x^2 + b x y + c y^2 + d x + e y + f == 0;


solving using 5 pintos result in:

SeedRandom[3];
pts = RandomReal[{-1, 1}, {5, 2}];
sol = Solve[ellipse @@@ pts];
ellipse[x, y] /. sol[[1]] // Simplify

(*a (-0.275185 + 1. x^2 + x (0.189022 + 0.566953 y) + 0.1281 y +
0.397124 y^2) == 0*)


all a values result in the same equation except when a=0.

• With MMA V9 I do not get the right answer, for example f (-0.275185 + 1. x^2 + x (0.189022 + 0.566953 y) + 0.1281 y + 0.397124 y^2) == 0 is the equation of the ellipse which is returned. The parameter f is not resolved. – Sigis K Oct 1 '14 at 12:02
• it is the same except a is replaced by f. – Algohi Oct 1 '14 at 13:47
• Your ellipse definition is in fact the general equation of a conics - it is only an ellipse if the discriminant is negative. How does your answer take this restriction into account? Thanks – Sigis K Oct 1 '14 at 22:08

Through 5 points we can pass a conic, an ellipse, hyperbola etc. After Algohi's solution coefficients are obtained we can determine choice of conic by sign of the second evaluated invariant $(b^2 - 4 a c)$ along with standard calculated expression for values of rotation/translation of central conic.A sign change test for a test point chosen inside or outside can be done,it should vanish on the arc.