I've recorded random Cartesian coordinates from the surface of a sphere. I'm trying to extrapolate where the center x,y,z coordinate of this sphere would be based on the points below.
I'm trying to create a script to solve this, but the math has me stuck.
Appreciate any help. Thanks!
Coordinates x,y,z:
-1.190185,0.7824033,0.1691585
-1.152931,0.7811859,0.1401751
-1.110813,0.775885, 0.1348239
-1.078485,0.772419, 0.1545465
-1.101839,0.7839018,0.2090827
-1.120642,0.7859643,0.2231803
-1.136541,0.7895643,0.2040433
-1.135001,0.7879777,0.1715891
-1.111366,0.7854245,0.1738369
-1.115193,0.78763 , 0.1931185
-1.130671,0.7892017,0.1985596
-1.132487,0.7872546,0.2240417
-1.161051,0.7876478,0.171136
-1.114549,0.7809206,0.1476536
-1.083838,0.7764122,0.1626304
-1.06429, 0.769419,0.2177327
-1.142459,0.7805369,0.2528498
-1.162153,0.7863876,0.1668259
The equation of a sphere with center $(x_0, y_0, z_0)$ and radius $r$ is
$ (x - x_0)^2 + (y - y_0)^2 + (z - z_0)^2 = r^2 $
Expanding, we get,
$ x^2 + y^2 + z^2 + A x + B y + C z + D = 0 $
where
$ A = -2 x_0 , \ B = - 2 y_0, \ C = - 2 z_0, \ D = x_0^2 + y_0^2 + z_0^2 - r^2 $
To identify the parameters $A,B,C,D$, we build a linear regression model, where we set
$ A x_i + B y_i + C z_i + D = -( x_i^2 + y_i^2 + z_i^2) $
If the above model equation is repeated for $i = 1, 2,.., N $, then we end up with the following regression model
$ M X = Y $
where the $i$-th row of $M$ is
$M_i = [x_i , y_i, z_i, 1]$
and $X$ is the vector of parameters:
$ X = [A, B, C, D]^T$
and $Y$ is the data vector, whose $i$-th entry is
$Y_i = -( x_i^2 + y_i^2 + z_i^2) $
The least squares estimate of $X$ is given by
$ \hat{X} = (M^T M)^{-1} M^T Y $
The computation of $\hat{X}$ involves the inversion of the $4 \times 4$ matrix $M^T M$.
Once $\hat{X}$ is determined, then we know the estimate of $A, B, C, D$, and hence,
$x_0 = - \dfrac{1}{2} A$
$y_0 = - \dfrac{1}{2} B $
$z_0 = -\dfrac{1}{2} C $
$ r^2 = x_0^2 + y_0^2 + z_0^2 - D$