Fitting a circle with least squares is easy once you get the trick for $c = r^2 - a^2 -b^2$ and you got a linear set of equations.
my problem is as follows:
$$z = \alpha x + \beta y + \alpha \beta$$
given a set of $x,y,z$, how can i fit(mse) the best $\alpha$ and $\beta$ ?
Thanks!
We'll try to minimize $\|c_1x+c_2y+c_1c_2 z-t\|^2$ (I use the notation of the code below to make it easier to make comparisons). Note that if we had a free variable $c_3$ instead of $c_1c_2$, there would be no problem (at least, for non-degenerate data). So, it is tempting to consider the conditional optimization $$ f(C)=\|c_1x+c_2y+c_3 z-t\|^2=(QC,C)-2(S,C)+\|t\|^2\to\min $$ $$ g(C)=2(c_1c_2-c_3)=(RC,C)-2(T,C)=0\,. $$ The Lagrange multiplier theorem for quadratic forms is extremely nice: every local conditional minimizer is a global minimizer of $f+qg$ for some $q$.
However the global minimization is easy. First, we need to ensure that $Q+qR$ is non-negative definite (otherwise no global minimum exists). In our case, $\det(Q+qR)$ is a quadratic polynomial in $q$, so finding the initial admissible interval $[q-2dq,q+2dq]$ is not a problem. Next, the minimizer $C(q)$ is unique and given by $$ C(q)=(Q+qR)^{-1}(S+qT)\,, $$ provided that the matrix $Q+qR$ is non-degenerate. The minimum itself equals $\|t\|^2-\left((Q+qR)^{-1}(S+qT),(S+qT)\right)=\|t\|^2-F(q)$. Note that this last expression is always a lower bound for the minimum in the original problem, so the true $q$ is easy to recognize: it should minimize $F(q)$ (the true minimal value must be attained). The function $F(q)$ is convex, so its minimizing is a piece of cake (I just used the regular bisection algorithm below). Once we know $q$, we know $C=C(q)$.
It looks neat and clean except for "provided that". It turns out that the Lagrange global minimization problem is degenerate and the algorithm fails exactly if the original problem has multiple solutions. The ugly part of the code handles that case by moving in the direction of the possibly degenerate line to reach the correct ellipsoid surface (I preferred it to the paraboloid because of the stability issues; if there is no degeneracy, there is no real movement either).