Given a set of points $ P_i = (x_i,y_i,z_i), \ i =1,2,\dots, N $. I want to find the best fitting plane to these points. The plane has the equation
$$ n^T ( r - r_0 ) = 0 $$
where $n$ is a unit vector. I want to find $r_0$ and $n$ such that the sum of squared perpendicular distances from $(x_i, y_i, z_i)$ to the plane is minimized. That is, I want to minimize
$ f = \displaystyle \sum_{i=1}^N d_i^2 $
where $d_i = | n^T (P_i - r_0) | $
My Attempt: is detailed in my answer below.
Your comments, hints, and solutions are highly appreciated.
The sum of squared distance function is given by
$ f = \displaystyle \sum_{i=1}^N n^T (P_i - r_0) (P_i - r_0)^T n \hspace{15pt}(1) $
This is also equal to
$ f = \displaystyle \sum_{i=1}^N (P_i - r_0)^T {n n}^T (P_i - r_0) \hspace{15pt}(2) $
Using form $(2)$, the gradient of $f$ with respect to $r_0$ is
$ \nabla_{r_0} f = -2 \displaystyle \sum_{i=1}^N {n n}^T (P_i - r_0) = - 2 {nn}^T \sum_{i=1}^N (P_i - r_0) \hspace{15pt}(3) $
If this is to be zero, then it is sufficient to take
$ \displaystyle \sum_{i=1}^N (P_i - r_0) = \mathbf{0} \hspace{15pt}(4)$
Solving for $r_0$, we get
$ r_0 = \dfrac{1}{N} \displaystyle \sum_{i=1}^N P_i \hspace{15pt}(5) $
That is, $r_0$ is the centroid of the given points.
From $(1)$, we have
$ f = \displaystyle n^T \left( \sum_{i=1}^N {(P_i - r_0) (P_i - r_0)}^T \right) n \hspace{15pt}(6) $
The $3 \times 3 $ matrix $Q = \displaystyle \sum_{i=1}^N {(P_i - r_0) (P_i - r_0)}^T $ is symmetric and positive definite or positive semi-definite. The minimum of the quadratic form $(6)$ is achieved when $n$ is chosen to be the unit eigenvector corresponding to the minimum eigenvalue of $Q$.
This completes the specification of the best fit plane.