Matrix form of least squares fitting

52 Views Asked by At

I need to fit some 3D $\left(x_{i},y_{i},z_{i}\right)$ data to a spherical surface. I tried doing this:

Assuming that not all points are coplanar, one has to minimize the function:

\begin{equation} F\left(a,b,c,r\right)=\sum_{i=1}^{m}\left(r_{i}-r\right)^2, \label{spherefunctional} \end{equation} where $r$ is the sphere radius and

\begin{equation} r_{i}=\sqrt{\left(x_{i}-a\right)^2+\left(y_{i}-b\right)^2+\left(z_{i}-c\right)} \label{defri} \end{equation}

Taking the partial derivative of $F$ with respect to $r$:

\begin{equation} \frac{\partial F}{\partial r}=-2\sum_{i=1}^{m}\left(r_{i}-r\right) \end{equation} and making it equal to zero: \begin{equation} r=\frac{1}{m}\sum_{i=1}^{m}r_{i} \end{equation}

Similarly, taking the other partial derivatives and making them equal to zero: \begin{align} \begin{split} a&=\frac{1}{m}\sum_{i=1}^{m}x_{i}+\frac{r}{m}\sum_{i=1}^{m}\frac{\partial r_{i}}{\partial a}\\ b&=\frac{1}{m}\sum_{i=1}^{m}y_{i}+\frac{r}{m}\sum_{i=1}^{m}\frac{\partial r_{i}}{\partial b}\\ c&=\frac{1}{m}\sum_{i=1}^{m}z_{i}+\frac{r}{m}\sum_{i=1}^{m}\frac{\partial r_{i}}{\partial c} \end{split} \end{align}

Since: \begin{align} \begin{split} \frac{\partial r_{i}}{\partial a}&=\frac{\left(a-x_{i}\right)}{r_{i}}\\ \frac{\partial r_{i}}{\partial b}&=\frac{\left(b-y_{i}\right)}{r_{i}}\\ \frac{\partial r_{i}}{\partial c}&=\frac{\left(c-z_{i}\right)}{r_{i}} \end{split} \end{align}

Then:

\begin{align} \begin{split} a&=\frac{1}{m}\sum_{i=1}^{m}x_{i}+\frac{1}{m^{2}}\sum_{i=1}^{m}r_{i}\sum_{i=1}^{m}\frac{\left(a-x_{i}\right)}{r_{i}}\\ b&=\frac{1}{m}\sum_{i=1}^{m}y_{i}+\frac{1}{m^{2}}\sum_{i=1}^{m}r_{i}\sum_{i=1}^{m}\frac{\left(b-y_{i}\right)}{r_{i}}\\ c&=\frac{1}{m}\sum_{i=1}^{m}z_{i}+\frac{1}{m^{2}}\sum_{i=1}^{m}r_{i}\sum_{i=1}^{m}\frac{\left(c-z_{i}\right)}{r_{i}} \end{split} \end{align}

However, solving the previous equations is cumbersome. So I did some more research and found this report, which shows how to fit some data solving linear equations like so:

\begin{align} Ax &= B\\ A^TA\hat{x} &= A^TB\\ \hat{x} &= (A^TA)^{-1}A^TB \end{align}

Where $A$ and $B$ are shown in the report. That approach is much simpler and yields good results. However, I can't derive the expressions for those matrices. Are they compatible with the optimization function?