# How do I display the plane of best fit in 3D?

I'm trying to display the plane of best fit in the same 3D-box. Currently, I have the following code which spits out a plot of the points in 3D. I want to have a plane of best fit, can Mathematica do this automatically?

t1 = {{10.26, 43., 330.}, {985.69, 28., 120.}, {1445.56, 35.,
360.}, {1188.19, 33., 270.}, {574.51, 44., 220.}, {568.95,
19., 170.}, {471.81, 20., 70.}, {537.35, 22.,
210.}, {514.07, 21., 200.}, {174.09, 40., 300.}, {1720.81,
32., 290.}, {611.48, 20., 70.}, {251.19, 24.,
150.}, {97.97, 38., 190.}, {406.81, 24., 240.}, {265.4,
25., 100.}, {1323.29, 35., 250.}, {196.65, 36.,
210.}, {1326.6, 27., 280.}, {1380.69, 33., 230.}};

ListPointPlot3D[t1]

• Yeah every ones got the sheet fit, the plane is pretty rare to find – Jethro Devøn Nov 13 '14 at 15:51
• Define fit = Fit[t1, {1, x, y}, {x, y}], then use Plot3D. – Rahul Nov 13 '14 at 16:18
• Hi, welcome to Mathematica.SE, please consider taking the tour so you learn the basics of the site. Once you gain enough reputation by making good questions you will be able to vote up and down both questions and answers. When you see good ones, please vote them up by clicking the grey triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. As you receive help, try to give it too, by answering questions in your area of expertise. – rhermans Nov 13 '14 at 17:05
• There are things to do after your question has been answered, but don't rush, you may want stay vigilant some time after you get the first answer as its is likely that the best approaches may come later improving over a previous reply. Therefore, its a good idea to wait a few hours before voting the deserving answers and accepting the best one for you. (Links contain useful information) – rhermans Nov 13 '14 at 17:06

Here's a way to get the plane of best fit: subtract the centroid of the data, and then plot the plane generated by the first two left singular vectors of the singular value decomposition of the resulting data:

Y = # - Mean /@ # &[t1\[Transpose]]
{U, S, V} = SingularValueDecomposition[Y];
Graphics3D[{InfinitePlane[{{0, 0, 0}, U[[;; , 1]], U[[;; , 2]]}],
Point /@ (Y\[Transpose])}, BoxRatios -> {1, 1, 1}]


which produces

The RMS error is the third singular value:

S[[3, 3]]


which produces

25.8873

• +1, I think there may be a syntax error as Y is not defined. Copy and paste does not run. Should it be: {U, S, V} = SingularValueDecomposition[Y = # - Mean /@ # &[t1\[Transpose]]]; Graphics3D[{InfinitePlane[{{0, 0, 0}, U[[;; , 1]], U[[;; , 2]]}], Point /@ (Y\[Transpose])}, BoxRatios -> {1, 1, 1}] or alternatively plotting original points by translating plane. – ubpdqn Nov 15 '14 at 11:56
• @ubpdqn: Dang, you're right, sorry 'bout that! Fixing it. – DumpsterDoofus Nov 15 '14 at 14:40
• Why don't you plot the plane against the actual data t1? – Taiki Oct 15 '15 at 18:51
Show[
ListPointPlot3D[t1, PlotStyle -> Red, Filling -> Bottom],
Plot3D[
Evaluate@Fit[t1, {1, x, y}, {x, y}]
, {x, Min[t1[[All, 1]]], Max[t1[[All, 1]]]}
, {y, Min[t1[[All, 2]]], Max[t1[[All, 2]]]}
]]


• That's very beautiful graph, mathematica is very powerful thank you for your help, this problem is solved :) – Jethro Devøn Nov 13 '14 at 17:05
• Am I correct in thinking that the plane of best fit is also the sum of least squares? – Jethro Devøn Nov 13 '14 at 17:06
• This particular fit minimizes sums of squares of vertical discrepancies. For total least squares (sums of squares of shortest rather than vertical distances to plane), one would have to do this a bit differently. – Daniel Lichtblau Nov 13 '14 at 20:59
• really?? uh oh, so is there a more effective function than fit?? – Jethro Devøn Nov 13 '14 at 21:22
• @rhermans: Not quite, the plane of best fit is different from the plane of best vertical fit, although for point clouds which nearly outline a horizontal plane the difference will be fairly small. One way to get the true plane of best fit (and the associated RMS error) is to use principal value decomposition of the data (see my answer for details). – DumpsterDoofus Nov 14 '14 at 16:00