How To Fit A Plane To Multiple Points? I need help with plane fit. I am using this as a reference.
I calculated centroid as mean value of each coordinate.
Then I calculated matrix $M$:
$$
M = \begin{vmatrix}
    x_1 - x_c & y_1 - y_c & z_1 - z_c\\
x_2 - x_c & y_2 - y_c & z_2 - z_c\\
    \vdots & \vdots & \vdots\\
    x_n - x_c & y_n - y_c & z_n - z_c \\
    \end{vmatrix}
$$
Then I calculate my covariance matrix:
$$A = M^T * M$$
and I divide each value in matrix $A$ by $N$ - number of points
then I calculate eigenvector using $eig()$ function in Octave (if that is relevant). My problem is that it leaves me with vector $x$ that has three values so I cannot use this formula to calculate distances:
$$ d = \frac{Ax_i + By_i + Cz_i +D}{\sqrt{A^2 + B^2 + C^2} } $$
I tried assuming that one  of $ABCD$ is equal $1$ but it did not work. 
Can you point out my mistake? Or perhaps there is some other, similar algorithm using SVD instead? 
 A: I had a quick look at the eig and eigs function documentation for octave, I have to admit that the eig one is a little confusing to me. 
Regardless, in the code you linked, you used 
[x l] = eigs(B);, which by default returns the six eigenvalues (and corresponding eigenvectors) with largest magnitude. In this particular case, it should yield all three eigenvalues, but the documentation doesn't seem to specify in what order these eigenvalues/eigenvectors are returned.
Maybe the returned eigenvalues are sorted from largest to smallest, but you probably want to make sure of that somehow. Alternatively, you can just inspect the matrix l of eigenvalues to find the good one. Or tweak the arguments of eigs to ask for the smallest eigenvalue directly:
[x, l]=eigs(B, 1, "sm");
With the selection of the proper eigenvalue/eigenvector out, your code has one more mistake. To quote the documentation:

[V, d] = eigs (A, …)
...
With two return arguments, V is a n-by-k matrix whose columns are the k eigenvectors corresponding to the returned eigenvalues.

However in your code you've extracted a line of "V" (which you've called x):
[x l] = eigs(B);

A = x(1,1);
B = x(1,2);
C = x(1,3);

