# Best Fitting Plane given a Set of Points

Nothing more to explain. I just don't know how to find the best fitting plane given a set of N points in a 3D space. I then have to write the corresponding algorithm. Thank you ;)

-
There is plenty more to explain. There are many different measures of how well a plane fits given data, and different measures give rise to different "best" fitting planes. So you had best tell us what you have in mind as your measure of how well a given plane fits some given data. – Gerry Myerson Jan 15 '12 at 21:02
I'm sorry but I wish I could tell you more. But I know just a bit. Let's say that the set of Points I have (over a 100) already look like a plane, I mean, they are displayed as a plane but not perfectly. "Obtain the symmetry plane A by fitting it on the set of points B.." That's all I have to do. They don't say anything more. – G4bri3l Jan 16 '12 at 9:20
You hadn't mentioned the symmetry part before -- do you know what that's referring to? – joriki Jan 16 '12 at 9:53
The "symmetry plane" may confuse you. I have this set of points that represents the symmetry plane (but it could be any plane), but I actually don't know the equation of this plane (and I need it). So I presumed that the only way I can find this equation is finding the best fitting plane given this set of points. I'm sorry if I'm not explaining the whole thing properly. – G4bri3l Jan 16 '12 at 10:23
Let's look at a simpler problem. Say you have a bunch of points in 2 dimensions that almost lie along a line, but not quite, and you want to find the line that fits those points the best. You could draw a line, then draw vertical line segments from each point to the line, and add up the lengths of all those line segments, and ask for the line that makes that sum as small as possible. But you could draw horizontal line segments instead, and you might get a different answer by minimizing the sum of those lengths. Or you could draw line segments perpendicular to the line. Continued... – Gerry Myerson Jan 16 '12 at 11:23

Subtract out the centroid, form a $3\times N$ matrix $\mathbf X$ out of the resulting coordinates and calculate its singular value decomposition. The normal vector of the best-fitting plane is the left singular vector corresponding to the least singular value. See this answer for an explanation why this is numerically preferable to calculating the eigenvector of $\mathbf X\mathbf X^\top$ corresponding to the least eigenvalue.
@G4bri3l: I'm not sure I understand that question. The way I've defined $\mathbf X$, its right-singular vectors are $N$-dimensional, so I don't see how they could be used to find the best-fitting line. It's the left singular vectors that are $3$-dimensional, and indeed the left singular vector $u$ corresponding to the largest singular value gives the direction of the best-fitting line. Remember that $\mathbf X$ contains the coordinates with the centroid $c$ subtracted out, so the equation for the best-fitting line is $c+\lambda u$. – joriki Jan 17 '12 at 17:18