View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Ellipsoid fit

4.9 | 24 ratings Rate this file 90 Downloads (last 30 days) File Size: 4.01 KB File ID: #24693 Version: 1.3
image thumbnail

Ellipsoid fit


Yury (view profile)


10 Jul 2009 (Updated )

Fits an ellipsoid / sphere / paraboloid / hyperboloid to data using linear least squares.

| Watch this File

File Information

Fits an ellipsoid or other conic surface into a 3D set of points approximating such a surface, allows some constraints, like orientation constraint and equal radii constraint. E.g., you can use it to fit a rugby ball, or a sphere. 'help ellipsoid_fit' says it all. Returns both the algebraic description of the ellipsoid (the nine coefficients of the quadratic form) and the geometric description (center, radii, principal axes).

MATLAB release MATLAB 8.6 (R2015b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (33)
18 Jan 2017 nay zhao

06 Jan 2017 Timothy Wilson

Lots of questions below about how to use the output to correctly map points onto a unit sphere. Here's the answer:

corrected_point = C * (raw_point - center)

where corrected_point, raw_point, center are 3x1 vectors (x,y,z) and C = evecs*diag(1./radii)*evecs' is a symmetric 3x3 matrix.

If you'd rather have the points mapped onto a sphere of radius k (rather than unit sphere), then use k*C in the above instead of just C.

Comment only
08 Dec 2016 MA AG

MA AG (view profile)

24 Nov 2016 Thibaud Michel

26 Oct 2016 Eomzi Yang

thank you!

19 Oct 2016 Alexey Stankevich

Works good, very useful. Thank you.

13 Sep 2016 Ho King

excellent job

07 Aug 2016 Zhang

Zhang (view profile)

very nice.
But I have the same question that is how to get three rotation angles,or how to calibrate the values? help for your help

22 Jul 2016 zhang

zhang (view profile)

good work!

29 Apr 2016 Amy Estes

06 Jan 2016 Dorian Depriester

Very nice work!
Is there any hack to constrain the conic type? I would like to fit my data on an ellipsoid, not on a paraboloid nor an hyperboloid.

04 Jan 2016 Stefan

Stefan (view profile)

Very useful, thank you! Is there a paper available which describes the math behind this code?

I also think it would be convenient to have an 'yz' constraint included.

17 Dec 2015 Chani

Chani (view profile)

04 Nov 2015 kassem kalo

hello, how to apply this code to a csv (x,y,z) file data

Comment only
06 Aug 2015 Krisztian Szucher

17 Sep 2014 Akeel

Akeel (view profile)

08 Sep 2014 jerry

jerry (view profile)

30 Aug 2014 Corlan

Corlan (view profile)

Very useful piece of code!

evecs is eigenvectors matrix. Following the principal axis theorem, eigenvectors represent principal axes of ellipsoid. Regarding use with magnetometer calibration, those are needed to perform so called "soft iron" calibration, in order to properly shrink the ellipsoid into sphere. I would recommend taking a look at freescale's AN4246 & AN4248.

11 Mar 2014 Jean-Yves Tinevez

15 Oct 2013 Tongtong

very useful and smart!

19 Jul 2013 Tucker McClure

Tucker McClure (view profile)

This is well done. I especially appreciate the multiple input methods.

The flag=0 method can easily return imaginary results for noisy data. Note that the flag=0 method is also trying to find the primary axes of the ellipsoid. For those getting imaginary results when using flat=0 (the default), if your ellipsoid is expected to already be aligned with x, y, and z, then just use flag=1. This works quite well even when flag=0 falls apart.

I see some folks are using this for magnetometer data, which has a "hard iron" offset. To convert from raw data to points on a unit sphere:

[c, r] = ellipsoid_fit(raw, 1);

% For a single point, k:
unit_sphere(:, k) = (raw(:, k) - c)./r

% For multiple points, vectorized:
unit_sphere = diag(1./r) * (m - c * ones(1, size(m, 2)));

% Just for kicks using bsxfun:
unit_sphere = bsxfun(@times, 1./r, bsxfun(@minus, m, c))

24 Jun 2013 CrazyMaths_VM

Excellent code! however, for some ellipsoid data the radii values are imaginary what is the problem?. CAN ANYONE HELP PLEASE!!

Comment only
07 May 2013 kostas

kostas (view profile)

I used your code but the results are not correct with my data. The eigen values are very big. Should you normalise the Covariance matrix? I would also want to ask how did you derive from the 9 parameters of ellipsoid, the covariance matrix, center etc.. which book, notes did u use?

Comment only
06 May 2013 Liang Sun

Liang Sun (view profile)

If the ellipsoid passes through the origin (0,0,0), how can the default equation "Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz + 2Fyz + 2Gx + 2Hy + 2Iz = 1" represent that? Thanks!

Comment only
20 Nov 2012 Monica

Monica (view profile)

Hi, did you find hot to determine those rotation angles?

20 Sep 2012 avcs asfasfa

Nice code, however
center--> Magnetometer offset
radii --> scaling factor
v---> the 9 parameters,
what is evecs?

what do i do next to get the calibrated values??? need help.....

Comment only
27 Aug 2012 Daniel Lopes

Daniel Lopes (view profile)

Well done!

06 Aug 2012 Vincent

Hi, nice and efficient piece of code.
Nevertheless, I believe that the polynomial expression that you compute in a first step is not guaranteed to be one of an ellipsoid. I think that it could be any quadric (hyperboloid, paraboloid, etc), right?
It is probably no problem if you have much data (corresponding to a real ellipsoid) to fit your quadric on.
But in case of sparse data (for instance, data points only on a couple of planes), you could have bad surprises (and in particular negative eigenvalues).

Comment only
26 Apr 2012 Brian

Brian (view profile)

This is a fine piece of code

10 Feb 2012 Simone

Simone (view profile)

Working very good

09 Sep 2011 Kevin Shaw

Works great.

21 Jun 2011 Hui Ma

Hui Ma (view profile)

I have a question: we can find the center, the radii of the ellipsoid and also the 3*3 direction matrix "evecs" using this program. Then how do you determine the three rotation angles using "evecs"?
Thanks for your help!

Comment only
21 Jun 2011 Hui Ma

Hui Ma (view profile)

I am sorry, I meant how do you determine three rotation angles using "evecs" then?

Comment only
17 Jul 2009 1.1

Just a better description.

25 Sep 2015 1.2

Fixed instability arising for data points near zero.
Added equal radii constraints for an ellipsoid of an arbitrary orientation.

20 Nov 2015 1.3

Updated the function to give correct radii and chi2 when fitting other conics beside ellipsoids.

04 Dec 2015 1.3

Indicated that the code fits surfaces, not volumes.

Contact us