Sphere Fit

Obtain the radius and center of a sphere based on solving a set of linear equations
969 Downloads
Updated 17 Mar 2014

View License

Given 4+ points in 3D space, this function attempts to find the center of a sphere and its radius.
Consider that there are 4 points or more on the surface of a sphere, then the sphere equation may then be represented by (x-a)^2 + (y-b)^2 + (z-c)^2 = r^2, where a,b,c and r are the unknowns and x,y,z are known data points.
For each data point (x,y,z), the above equation may be reduced to a linear equation that has a,b,c and r as variables.
-2ax -2by -2cz + (a^2+b^2+c^2-r^2) = -(x^2 + y^2 + z^2).

Let D = (a^2+b^2+c^2-r^2),, then the above equation will reduce to

-2ax -2by -2cz + D = -(x^2 + y^2 + z^2)

For each data point, the above equation may be reduced to AA*X = BB and solved for X using the mldivide() or '\' operator.

The solution is not a least-squares solution in its truest sense, since the residuals aren't being minimized. This solution may be used as an initial guess to the lsqnonlin() function which may implement a Gauss-Newton or a Levenberg–Marquardt algorithm.

Cite As

Prem Rachakonda (2026). Sphere Fit (https://www.mathworks.com/matlabcentral/fileexchange/45910-sphere-fit), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Linear Algebra in Help Center and MATLAB Answers
Tags Add Tags
Version Published Release Notes
1.2.0.0

Changed description.

1.1.0.0

Changed descripton.

1.0.0.0