Least Squares Plane using Matricies For a Least Squares solution to a 2D set of coordinates, the formula is:
$X^T\,X\,\vec b = X^Ty$ (where $X^T$ denotes $X$ transpose)
(for: $y = B_0 + B_1x + B_2x^2$)
where:
$X$:        $b$:       $y$:
[$1$  $x_1$]  [$B_0$]  [$y_1$]
[$1$  $x_2$]  [$B_1$]  [$y_2$]
[$1$  $x_3$]  [$B_2$]  [$y_3$]
[...  ...]           [... ]
My question is:
What do I need to change in order for me to use least squares to solve for a 3d surface?
Also, what would my equation look like? $y = B_0 + B_1x_1 + B_2x_2 + B_3{x_1}^2 + B_4{x_2}^2$ ?
Thanks a million. 
 A: So to clarify, you're looking for the best approximation of the form $z=f(x,y)=a_xx+a_yy+a_0$ in $xyz$-space for a set of $x,y,z$ data points.  In order to do that, we begin by supposing that there is an exact solution.  If that were the case, our system of equations would look like this:
$$
a_0+a_xx_1+a_yy_1=z_1\\
a_0+a_xx_2+a_yy_2=z_2\\
\vdots
$$
and so forth.  We can rewrite this in matrix-form as follows:
$$
\left[
\begin{array}{ccc}
1 & x_1 & y_1 \\ 
1 & x_2 & y_2 \\ 
\, & \vdots & \, \\ 
1 & x_n & y_n
\end{array}
\right]
\,\left[
\begin{array}{c}
a_0\\
a_x\\
a_y
\end{array}
\right]=
\left[
\begin{array}{c}
z_1\\
z_2\\
\vdots\\
z_n
\end{array}
\right]
$$
From there, getting the least squares solution simply a matter of applying the same logic as before.  Setting
$$
A=
\left[
\begin{array}{ccc}
1 & x_1 & y_1 \\ 
1 & x_2 & y_2 \\ 
\, & \vdots & \, \\ 
1 & x_n & y_n
\end{array}
\right];
\vec x=
\left[
\begin{array}{c}
a_0\\
a_x\\
a_y
\end{array}
\right];
\vec y =
\left[
\begin{array}{c}
z_1\\
z_2\\
\vdots\\
z_n
\end{array}
\right]
$$
we have
$$
A^TA \vec x = A^T\vec y
$$
And must solve for $\vec x$.
