Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Fit an ellipse to a list of 2D points
ResourceFunction["EllipseFit"][{{x1,y1},{x2,y2},…,{xn,yn}},{x,y}] returns an equation in x and y of an ellipse that fits the given 2D data. |
TimeConstraint | 1 | the maximum time spent computing exact Eigenvectors before switching to precision specified by the WorkingPrecision option |
WorkingPrecision | MachinePrecision | the precision used within Eigenvectors after surpassing the TimeConstraint |
Make some 2D data approximating an ellipse:
In[1]:= |
Out[2]= |
EllipseFit returns the equation of the ellipse that best approximates the data:
In[3]:= |
Out[4]= |
Plot the ellipse and the data:
In[5]:= |
Out[5]= |
EllipseFit works well when the data is over only a part of an ellipse:
In[6]:= |
Out[9]= |
This data has 50 digits of precision and EllipseFit finds the equation of an ellipse with all the precision that can be justified:
In[10]:= |
Out[11]= |
The above curve has 45.2 digits of precision. Almost five digits of precision were lost in finding the equation of this ellipse:
In[12]:= |
Out[12]= |
In this case the data is exact and we get the equation of an ellipse with exact coefficients. Whenever all the data is exact and not all the data is exactly on an ellipse EllipseFit returns an equation with exact coefficients:
In[13]:= |
Out[15]= |
This data is exact and all the points are exactly on an ellipse:
In[16]:= |
Out[16]= |
With the default setting TimeConstraint→1, this example resorts to numeric computation:
In[17]:= |
Out[18]= |
Plot the ellipse and the data:
In[19]:= |
Out[19]= |
With the setting TimeConstraint → ∞, EllipseFit finds an exact solution after several seconds. This would take an extremely long time and require huge amounts of memory if the data had many samples:
In[20]:= |
Out[20]= |
This data is exact and all the points are exactly on an ellipse:
In[21]:= |
Out[21]= |
In this case, EllipseFit switches to arithmetic with 25 digits of precision after exact arithmetic took longer than the TimeConstraint value:
In[22]:= |
Out[22]= |
When the data is used for the left side of the equation above we get a value within 10-24 of zero. If the equation had MachinePrecision coefficients, the values below would be within about 10-16 of zero:
In[23]:= |
Out[23]= |
With the setting WorkingPrecision→ ∞, EllipseFit finds an exact solution after several seconds. This would take an extremely long time and require huge amounts of memory if the data had many samples:
In[24]:= |
Out[24]= |
This data would perfectly fit a hyperbola:
In[25]:= |
Out[26]= |
EllipseFit finds a curve to fit the data:
In[27]:= |
Out[28]= |
Whenever EllipseFit finds a curve to fit the data, the curve is guaranteed to be an ellipse. The plot below shows that the above curve is an ellipse, and it fits the data about as well as an ellipse can fit this data:
In[29]:= |
Out[29]= |
EllipseFit fails if the points in the data are nearly along a line or exactly along a line:
In[30]:= |
Out[31]= |
In this case the data has three distinct points, so an infinite collection of ellipses pass perfectly through all the data. When that happens EllipseFit displays a Message and returns unevaluated:
In[32]:= |
Out[33]= |
Plot an ellipse:
In[34]:= |
Out[34]= |
Make a list of 200,000 MachinePrecision points near the above ellipse:
In[35]:= |
Out[36]= |
EllipseFit finds the ellipse that fits the large list of points in less than 0.1 second:
In[37]:= |
Out[38]= |
A plot of the ellipse found looks exactly like the ellipse above:
In[39]:= |
Out[39]= |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License