This example shows a NLREG program that fits a plane in 3-dimensional
space to a set of data points whose X,Y,Z coordinates are specified.

In this example, the plane is fitted so as to minimize the sum of the
squared distances of the points from the plane along lines
perpendicular to the plane. For an NLREG example that minimizes the *Z*
distances from the plane to the points,
click here

The implicit equation for a plane in 3D space is

ax + by + cz + d = 0

We can simplify this equation and eliminate the *c* parameter by
dividing by *c*.
(Note, this assumes *c* is not zero which would occur only if the plane is
vertical.)

ax + by + z + d = 0

The distance of a point whose coordinates are (Xp,Yp,Zp) to the plane along a
line perpendicular (normal) to the plane, is:

Distance = |a*Xp + b*Yp + Zp + d| / sqrt(a^2 + b^2 + 1)

The following NLREG program will minimize the sum of the squared distances.

Title "Fit plane to data points along normal lines";
Variables X, Y, Z;
Parameters a, b, d;
Double Distance;
/*
* Compute distance from this point to the plane.
*/
Distance = (a*X + b*Y + Z + d) / sqrt(a^2 + b^2 + 1);
/*
* Minimize the squared distances.
*/
Function Distance;
Data;
[data values go here]

For an example NLREG program that performs orthogonal linear regression,
please click here.