3
$\begingroup$

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]
$\endgroup$
  • $\begingroup$ Yeah every ones got the sheet fit, the plane is pretty rare to find $\endgroup$ – Jethro Devøn Nov 13 '14 at 15:51
  • $\begingroup$ Define fit = Fit[t1, {1, x, y}, {x, y}], then use Plot3D. $\endgroup$ – Rahul Nov 13 '14 at 16:18
  • $\begingroup$ 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. $\endgroup$ – rhermans Nov 13 '14 at 17:05
  • $\begingroup$ 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) $\endgroup$ – rhermans Nov 13 '14 at 17:06
10
$\begingroup$

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

enter image description here

The RMS error is the third singular value:

S[[3, 3]]

which produces

25.8873
$\endgroup$
  • $\begingroup$ +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. $\endgroup$ – ubpdqn Nov 15 '14 at 11:56
  • $\begingroup$ @ubpdqn: Dang, you're right, sorry 'bout that! Fixing it. $\endgroup$ – DumpsterDoofus Nov 15 '14 at 14:40
  • $\begingroup$ Why don't you plot the plane against the actual data t1? $\endgroup$ – Taiki Oct 15 '15 at 18:51
6
$\begingroup$
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]]]}
  ]]

Mathematica graphics

$\endgroup$
  • $\begingroup$ That's very beautiful graph, mathematica is very powerful thank you for your help, this problem is solved :) $\endgroup$ – Jethro Devøn Nov 13 '14 at 17:05
  • 1
    $\begingroup$ Am I correct in thinking that the plane of best fit is also the sum of least squares? $\endgroup$ – Jethro Devøn Nov 13 '14 at 17:06
  • 3
    $\begingroup$ 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. $\endgroup$ – Daniel Lichtblau Nov 13 '14 at 20:59
  • $\begingroup$ really?? uh oh, so is there a more effective function than fit?? $\endgroup$ – Jethro Devøn Nov 13 '14 at 21:22
  • 1
    $\begingroup$ @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). $\endgroup$ – DumpsterDoofus Nov 14 '14 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.