Re: LS fit to a plane
- To: mathgroup at smc.vnet.net
- Subject: [mg16854] Re: [mg16828] LS fit to a plane
- From: Maarten.vanderBurgt at icos.be
- Date: Thu, 1 Apr 1999 21:35:25 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Virgil, The distance from a point (x0,y0) to a line y = a + b x is D = ( a+ b x0 - y0)/sqrt(1 + b^2). For doing a least squares fit (lsq) to a line you can use this distance instead of the vertical distance (i.e. D = y0 - a - b x0; this last D is used in ordinary linear regression). This is worked out in the attached notebook (in ascii text at the bottom of this message; I deleted all output; the FullSimplify[] which is used to make the expressions more or less readable can take some time if you do not have a fast computer). If you execute everything in this notebook you will see, for a nearly vertical line, the lsq fit which uses minimal distances is much better than the ordinary lsq fit (I use Regression[] here, Fit[] would have done as well). You can work out something similar for a fit to a plane if you know that the distance from point (x0,y0,z0) to plane a x + b y + c z + d = 0 is D = (a x0 + b y0 + c z0 + d)/sqrt(a^2 + b^2 + c^2). hope this helps a bit Maarten Virgil Stokes <virgil.stokes at neuro.ki.se> on 30-03-99 05:46:38 PM cc: Subject: [mg16854] Re: [mg16828] LS fit to a plane Maarten.vanderBurgt at icos.be wrote: > Virgil, > > Have a look at the following: > > In[1]:= z[x_,y_]:= 3x+5y-9; > > In[2]:= xyzdata = > Flatten[Table[{u,v,z[u,v]+Random[]},{u,-1,+1},{v,-1,1}],1] > > Out[2]= > {{-1,-1,-16.9183},{-1,0,-11.6465},{-1,1,-6.13617},{0,-1,-13.8047},{0, > > 0,-8.3627},{0,1,-3.00167},{1,-1,-10.0657},{1,0,-5.43228},{1,1,-0.174832}} > > In[3]:= Fit[xyzdata,{1,x,y},{x,y}] > > Out[3]= -8.39366+3.17135 x+5.24602 y > > The equation of the plane is z = 3 x + 5 y - 9. > > In xyzdata I add some random noise to nine points in this plane. > > Then I fit the leas mean square plane using the Fit[] command. > > If you want more regression data have look in the package > Statistics`LinearRegression`. Thanks Maarten, But, I am not asking about a regression fit. I have a set of measured space coordinates where ALL contained measurement error and I would like to find the "best" fitting plane to this set of noisy 3D points data. It seems to me that a reasonable function to use for minimization is the perpendicular distances to the plane (to be determined) from each point. This is not ordinary regression! --V. Stokes --------------------------------------------------------------------------- ---------------------------------------------------------------- (*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info at wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 3609, 118]*) (*NotebookOutlinePosition[ 4258, 141]*) (* CellTagsIndexPosition[ 4214, 137]*) (*WindowFrame->Normal*) Notebook[{ Cell[BoxData[ \(Sdd = \((a + b\ x\_i\ - \ y\_i)\)^2/\((1 + b^2)\)\)], "Input"], Cell[BoxData[ \(Eq1\ = Together[Expand[D[Sdd, b]]]*\((1 + b\^2)\)\^2/2\)], "Input"], Cell[BoxData[ \(Eq2\ = \ D[Sdd, a]*\((1 + b\^2)\)/2\)], "Input"], Cell[BoxData[ \(\(Eqs\ = \ {Nn\ a\ + \ b\ Sx\ - \ Sy\ == 0, \n\t\t\t\t\t\t\t a \((1 - b^2)\) Sx + b\ Sxx\ + \ 2\ a\ b\ Sy\ - \((1 - b^2)\) Sxy - b\ Syy\ - Nn\ a^2\ b\ == \ 0}; \)\)], "Input"], Cell[BoxData[ \(Solution\ = \ FullSimplify[Solve[Eqs, {a, b}]]\)], "Input"], Cell[BoxData[ \(<< Statistics`LinearRegression`\)], "Input"], Cell[BoxData[ \(<< Statistics`DataManipulation`\)], "Input"], Cell[BoxData[ \(TTvert = Table[{\ \((Random[] - 0.5)\) + 10, \((Random[] - 0.5)\) + 3*i}, {i, 1, 10}]; \ndatplot\ = ListPlot[TTvert, PlotRange -> {{0, 35}, {0, 35}}]; \)], "Input"], Cell[BoxData[ \(regres\ = Regress[TTvert, {1, x}, x, \ RegressionReport -> BestFit]\)], "Input"], Cell[BoxData[ \(\(Sxx = Plus@@\((Column[TTvert, 1]^2)\); \)\)], "Input"], Cell[BoxData[ \(\(Syy = Plus@@\((Column[TTvert, 2]^2)\); \)\)], "Input"], Cell[BoxData[ \(\(Sx = Plus@@Column[TTvert, 1]; \)\)], "Input"], Cell[BoxData[ \(\(Sy = Plus@@Column[TTvert, 2]; \)\)], "Input"], Cell[BoxData[ \(\(Sxy = Plus@@\((Column[TTvert, 1]*Column[TTvert, 2])\); \)\)], "Input"], Cell[BoxData[ \(\(Nn = 10; \)\)], "Input"], Cell[BoxData[ \(Solution\)], "Input"], Cell[BoxData[ \(fit0\ = \ Plot[BestFit /. regres, {x, 0, 35}, PlotRange -> {{0, 35}, {0, 35}}] \)], "Input"], Cell[BoxData[ \(\(fit1\ = \ Plot[\((a /. Solution[\([1]\)])\)\ + \ \((b /. Solution[\([1]\)])\)\ x, {x, 0, 35}, PlotRange -> {{0, 35}, {0, 35}}]; \)\)], "Input"], Cell[BoxData[ \(Show[fit0, fit1, datplot]\)], "Input"] }, FrontEndVersion->"Microsoft Windows 3.0", ScreenRectangle->{{0, 1024}, {0, 712}}, WindowSize->{837, 581}, WindowMargins->{{0, Automatic}, {Automatic, 5}} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[1709, 49, 83, 1, 30, "Input"], Cell[1795, 52, 88, 1, 32, "Input"], Cell[1886, 55, 69, 1, 31, "Input"], Cell[1958, 58, 231, 4, 50, "Input"], Cell[2192, 64, 80, 1, 30, "Input"], Cell[2275, 67, 64, 1, 30, "Input"], Cell[2342, 70, 64, 1, 30, "Input"], Cell[2409, 73, 210, 4, 50, "Input"], Cell[2622, 79, 108, 2, 30, "Input"], Cell[2733, 83, 76, 1, 30, "Input"], Cell[2812, 86, 76, 1, 30, "Input"], Cell[2891, 89, 67, 1, 30, "Input"], Cell[2961, 92, 67, 1, 30, "Input"], Cell[3031, 95, 92, 1, 30, "Input"], Cell[3126, 98, 46, 1, 30, "Input"], Cell[3175, 101, 41, 1, 30, "Input"], Cell[3219, 104, 128, 3, 30, "Input"], Cell[3350, 109, 194, 4, 30, "Input"], Cell[3547, 115, 58, 1, 30, "Input"] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)