Best 3D plane fit to a set of points

kepler

New member
Joined
Jul 14, 2015
Messages
26
Hi,

I'm trying to develop a code (in C or vb6) that may calculate the best plane fit, let's say of 3 or 4 points, that passes also in the center (0,0,0) - imperative fixed point.

So the equation will be of the type Ax + By + Cz = 0 (D=0)

I thought on calculating the several combinations of 2 points (along with the condition of the center beeing 0,0,0) and then do the average of the coefficients...

Any ideas - or implementation - are very welcomed.

Kind regards,

Kepler
 
Hi,

I'm trying to develop a code (in C or vb6) that may calculate the best plane fit, let's say of 3 or 4 points, that passes also in the center (0,0,0) - imperative fixed point.

So the equation will be of the type Ax + By + Cz = 0 (D=0)

I thought on calculating the several combinations of 2 points (along with the condition of the center beeing 0,0,0) and then do the average of the coefficients...

Any ideas - or implementation - are very welcomed.

Kind regards,

Kepler
You could try least squares: Assume you have a set of points from a plane but there are measurement errors so
\(\displaystyle \epsilon_i\, =\, A\, x_i\, +\, B\, y_i\, +\, C\, z_i\)
and choose A, B, and C to minimize
E(A, B, C) = \(\displaystyle \Sigma_{i=1}^{i=n}\, \epsilon_i^2\)
You will need to solve a system of equations.
 
You could try least squares: Assume you have a set of points from a plane but there are measurement errors so
\(\displaystyle \epsilon_i\, =\, A\, x_i\, +\, B\, y_i\, +\, C\, z_i\)
and choose A, B, and C to minimize
E(A, B, C) = \(\displaystyle \Sigma_{i=1}^{i=n}\, \epsilon_i^2\)
You will need to solve a system of equations.

Hi Ishuda,

Thanks for the reply :) Can you please give me an example of such system of equations, for instance for 2 or 3 points?... I'm a little bit lost here... How do I fit the coeficients?

Sorry for the trouble.

Kind regards,

Kepler
 
Can you please give me an example of such system of equations, for instance for 2 or 3 points?... I'm a little bit lost here... How do I fit the coeficients?
If you're not familiar with this method, then your instructor or textbook must have taught you some other method. What method are you expected to use for this homework project? Thank you! ;)
 
Hi Ishuda,

Thanks for the reply :) Can you please give me an example of such system of equations, for instance for 2 or 3 points?... I'm a little bit lost here... How do I fit the coeficients?

Sorry for the trouble.

Kind regards,

Kepler
You will need to assume that one of your coefficients isn't zero. For simplicity assume it is the z coefficient and divide through by C so that the equation becomes
\(\displaystyle \epsilon_i\, = z_i\, -\,( a\, x_i\, +\, b\, y_i)\)
where a = \(\displaystyle -\frac{A}{C}\) and b = \(\displaystyle -\frac{B}{C}\)

Let \(\displaystyle \Sigma\) be the sum from 1 to n. The equation you need to solve is
\(\displaystyle \begin{pmatrix}
\Sigma\, x_i^2& \Sigma\, x_i\, y_i\\
\Sigma\, x_i\, y_i& \Sigma\, y_i^2
\end{pmatrix}
\begin{pmatrix}a\\b\end{pmatrix}\, =\,
\begin{pmatrix}\Sigma\, x_i\, z_i\\\Sigma\, y_i\, z_i\end{pmatrix}
\)
 
You will need to assume that one of your coefficients isn't zero. For simplicity assume it is the z coefficient and divide through by C so that the equation becomes
\(\displaystyle \epsilon_i\, = z_i\, -\,( a\, x_i\, +\, b\, y_i)\)
where a = \(\displaystyle -\frac{A}{C}\) and b = \(\displaystyle -\frac{B}{C}\)

Let \(\displaystyle \Sigma\) be the sum from 1 to n. The equation you need to solve is
\(\displaystyle \begin{pmatrix}
\Sigma\, x_i^2& \Sigma\, x_i\, y_i\\
\Sigma\, x_i\, y_i& \Sigma\, y_i^2
\end{pmatrix}
\begin{pmatrix}a\\b\end{pmatrix}\, =\,
\begin{pmatrix}\Sigma\, x_i\, z_i\\\Sigma\, y_i\, z_i\end{pmatrix}
\)
xyzx^2y^2xyxzyz
02300529000
20240040
01001000
10110010
5530050
50a5
0530b0
So b=0 and a = 1. Choosing C=1 we get the plane
x-z==-x+z=0
 
xyzx^2y^2xyxzyz
02300529000
20240040
01001000
10110010
5530050
50a5
0530b0
So b=0 and a = 1. Choosing C=1 we get the plane
x-z==-x+z=0


Hi,

You know what Ishuda? Your a kind and nice guy :)

Thanks for the help. Really.

Kind regards,

Kepler
 
Hi Ishuda,

I'm having some problems regarding the normalization of the best fit plane... :/

Using this pseudo code I can get more or less accurate results:

Code:
    SX2 = x1*x1+x2*x2+x3*x3;
    SY2 = y1*y1+y2*y2+y3*y3;
    SXY = x1*y1+x2*y2+x3*y3;
    SXZ = x1*z1+x2*z2+x3*z3;
    SYZ = y1*z1+y2*z2+y3*z3;

    c1 = SX2;
    c2 = SXY;
    c3 = SXZ;
    c4 = SXY;
    c5 = SY2;
    c6 = SYZ;

    cx = (c2*c6-c3*c5) / (c2*c4-c1*c5);
    cy = (c3*c4-c1*c6)/(c2*c4-c1*c5);
    cz = -(cx*x2+cy*y2)/z2;

Note: if I put cz=1, I get wrong results (even normalizing cx,cy and cz). Each will be normalized by:

Code:
    w3[0] = cx/sqrt(cx*cx+cy*cy+cz*cz);
    w3[1] = cy/sqrt(cx*cx+cy*cy+cz*cz);
    w3[2] = cz/sqrt(cx*cx+cy*cy+cz*cz);

Note 2: if instead of putting cz = -(cx*x2+cy*y2)/z2; I put cz=1, I get again wrong results. In the above, all is correct. Just one problem: if z2=0 I get an error...(division by zero)

I'm getting there, but not quite yet.

Can you help me out again?

Kind regards,

Kepler
 
Top