## Linear least squares fitting of a sphere

The equation of a sphere with centre in $\left[ x_c, y_c, z_c \right]$ and radius $r$ is

$(x - x_c)^2 + (y - y_c)^2 + (z - z_c)^2 = r^2$

or

$x^2 - 2 x x_c + x_c^2 + y^2 - 2 y y_c + y_c^2 + z^2 - 2 z z_c + z_c^2 = r^2$

that is

(1)  $\alpha x + \beta y + \gamma z + \delta = \epsilon$

with

$\alpha = - 2 x_c$

$\beta = - 2 y_c$

$\gamma = - 2 z_c$

$\delta = x_c^2 + y_c^2 + z_c^2 - r^2$

$\epsilon = - x^2 - y^2 - z^2$

Writing (1) for four points on the sphere, $\left[ x_i \hspace{1 mm} y_i \hspace{1 mm} z_i \right], i=1 \cdots 4$, not belonging to the same plane, one gets a linear system of rank 4 in the four unknowns $\alpha, \beta, \gamma, \delta$; then of course

$x_c = - \dfrac{1}{2} \alpha$

$y_c = - \dfrac{1}{2} \beta$

$z_c = - \dfrac{1}{2} \gamma$

$r = \sqrt{x_c^2 + y_c^2 + z_c^2 - \delta}$

If more than four points on the sphere are given, and they don’t belong all to the same plane, one gets an overdetermined linear system of rank 4 whose solution, eg with the Singular Value Decomposition method (SVD), is an approximation of the sphere in the least squares sense.