Re: fit plane to xz axis of data

• To: mathgroup at smc.vnet.net
• Subject: [mg89522] Re: [mg89515] fit plane to xz axis of data
• From: "Szabolcs HorvÃt" <szhorvat at gmail.com>
• Date: Thu, 12 Jun 2008 02:55:38 -0400 (EDT)
• References: <200806110720.DAA15072@smc.vnet.net>

```On Wed, Jun 11, 2008 at 10:20, will parr <willpowers69 at hotmail.com> wrote:
> Dear Math Forum,
>
> I am having problems fitting a plane to some data. I am using the following procedure to fit the plane to my data (data is pasted at the bottom of this message):
>
> In[2]:= plane = Fit[data, {1, x, y}, {x, y}]
>
> Out[2]= 3.28723- 0.189001 x - 0.0874557 y
>
> Then the following to display the points and plane:
>
> Show[ListPointPlot3D[data, Boxed -> False, Axes -> True,
>  AxesEdge -> {{-1, -1}, {-1, -1}, {-1, -1}},
>  AxesLabel -> {"x", "y", "z"}],
>  Plot3D[plane, {x, Min[data[[All, 1]]], Max[data[[All, 1]]]}, {y,
>   Min[data[[All, 2]]], Max[data[[All, 2]]]},
>  PlotStyle -> Directive[Green, Opacity[0.5]],
>  Mesh -> None]]
>
> This works fine at fitting the plane in the xy axis, but I want to fit the plane to the xz axis. Does anyone know how this is done?

You just need to permute the coordinates:

Replace[data, {x_, y_, z_} :> {x, z, y}, 1]

Or, if necessary, you can do an "isotropic" fitting (that prefers no
axis) by calculating the distances from the plane.  The original
fitting attempt showed that the plane will not pass through {0,0,0} so
we can use the equation a x + b y + c z == 1.  The distance-squared of
point {x,y,z} from this plane is (a x + b y + c z - 1)^2/(a^2 + b^2 +
c^2), so the fitting can be done like this:

In[2]:= planei =
a x + b y + c z /.
Last@NMinimize[
Expand@Total[
Function[{x, y, z}, (a x + b y + c z - 1)^2] @@@ data]/(
a^2 + b^2 + c^2), {a, b, c}]

Out[2]= 0.100048 x + 0.0136288 y + 0.0112116 z

In[3]:= plane = Fit[data, {1, x, y}, {x, y}]

Out[3]= 3.28723- 0.189001 x - 0.0874557 y

In[4]:= plane2 =
Fit[Replace[data, {x_, y_, z_} -> {x, z, y}, 1], {1, x, z}, {x, z}]

Out[4]= 8.12817- 0.817828 x - 0.225109 z

In[5]:= {{xa, xb}, {ya, yb}, {za, zb}} = {Min[#], Max[#]} & /@
Transpose[data]

Out[5]= {{7.08299, 11.4614}, {-5.66284, 4.59986}, {-1.83404,
4.49073}}

In[6]:= Show[
ContourPlot3D[planei == 1, {x, xa, xb}, {y, ya, yb}, {z, za, zb},
ContourStyle -> Directive[Blue, Opacity[.5]], Mesh -> None],
Plot3D[plane, {x, xa, xb}, {y, ya, yb},
PlotStyle -> Directive[Green, Opacity[0.5]], Mesh -> None],
ContourPlot3D[plane2 == y, {x, xa, xb}, {y, ya, yb}, {z, za, zb},
ContourStyle -> Directive[Red, Opacity[.5]], Mesh -> None],
ListPointPlot3D[data, PlotStyle -> Black],
PlotRange -> All, AxesLabel -> {"x", "y", "z"}
]

Your points don't seem to lie on a plane, so the three approaches give
give different results.

```

• Prev by Date: Re: Persistent addition to recently opened? Was Re: Notebooks location
• Next by Date: Re: fit plane to xz axis of data
• Previous by thread: fit plane to xz axis of data
• Next by thread: Re: fit plane to xz axis of data