File Exchange

image thumbnail

Ellipsoid fit

version (4.01 KB) by Yury
Fits an ellipsoid / sphere / paraboloid / hyperboloid to data using linear least squares.


Updated 04 Dec 2015

View Version History

View License

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).

Cite As

Yury (2021). Ellipsoid fit (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (56)

Linas Minkevicius

Brett Diehl

Sahin Yildirim


Tom Fringand

yixing zhang

T Wong

Is it possible to add constraint on length of the axes? Or even just enforces the values of the axes?

Lucas Struber

math works

I am passing about 3k points from top cap of an approximately ellipsoid-shaped data; the x-y is regular grid while the z coordinate varies (2.5D). I am seeing the message that matrix is singular to machine precision. What could be the reason for that?

Ilja Verspohl

nice work! How do i plot the ellipsoid using the Matlab ellipsoid function?
Do I use the vaules in the "v" array? Do I have to convert it to cylindrical coordinates in order to plot it with the matlab function?

Kevin Galloway

Ilter Koksal

Timothy Darling

Shangha Lee


Lu Shien Lee

Awesome stuff thanks!

Xiangyu Gong

Very useful! Thank you!!

Carlos Eugenio

Ahmad Falahatpisheh

Jean-Baptiste RENAULT

Well Gerrie

Can you give a pdf which you referenced ?

Matthieu Chardon

nay zhao

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.


Thibaud Michel

Eomzi Yang

thank you!

Alexey Stankevich

Works good, very useful. Thank you.

Ho King

excellent job


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


good work!

Amy Estes

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.


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.


kassem kalo

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

Krisztian Szucher




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.

Jean-Yves Tinevez


very useful and smart!

Tucker McClure

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


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


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?

Liang Sun

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!


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

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

Daniel Lopes

Well done!


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).


This is a fine piece of code


Working very good

Kevin Shaw

Works great.

Hui Ma

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!

Hui Ma

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

MATLAB Release Compatibility
Created with R2015b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!