# Given a set of points, how do I find the plane that best fits those points?

Given a set of points in three-dimensional space, how do I find the plane that best fits those points? More specifically, I want to find the plane that minimizes the sum of the square of the distance between each point and the plane.

For a set of points $X = \{\mathbf x_1,\mathbf x_2,\mathbf x_3...\mathbf x_k\}$, a unit vector $\mathbf n$ normal to the plane, and the distance from the origin to the plane $r$, the fit of the plane to the set of points is defined as follows: $$\text{Error} =\sum_{i=1}^k{((\mathbf x_i - r\mathbf n) \cdot \mathbf n)^2}$$ Or, equivilantly: $$\text{Error} =\sum_{i=1}^k{(\mathbf x_i \cdot \mathbf n - r)^2}$$ Assuming that the best-fitting plane passes through the origin, this can be expressed using matrix algebra as $$\text{Error}=X\mathbf n\cdot X\mathbf n \ \ \text{where} \ X=\begin{bmatrix} \mathbf x_1\\ \mathbf x_2\\ \vdots\\ \mathbf x_k \end{bmatrix}$$

First: Will the best-fitting plane always pass through the origin if the points are shifted so that the center of all the points is at the origin?

Second: What's the best way to find the plane that best fits these points?

• 1. the best fit plane pass through the centroid of the points. 2. Assume the centroid is located at origin. Compute the $3 \times 3$ matrix $\sum_{i=1}^k \mathbf{x}_i \otimes \mathbf{x}_i = \begin{bmatrix} \sum_{i=1}^k x_i^2 & \sum_{i=1}^k x_i y_i & \sum_{i=1}^k x_i z_i \\ \sum_{i=1}^k y_i x_i & \sum_{i=1}^k y_i^2 & \sum_{i=1}^k y_i z_i \\ \sum_{i=1}^k z_i x_i & \sum_{i=1}^k z_i y_i & \sum_{i=1}^k z_i^2 \\ \end{bmatrix}$ and find its eigenvector for lowest eigenvalue. It will be the best $\mathbf{n}$ you seek. – achille hui Oct 28 '16 at 6:59