I'm getting ellipses as level curves of a fit dataset. After selecting a particular ellipse, I would like to report it as a center point, semi-major and minor axes lengths, and a rotation angle. In other words, I would like to transform (using mathematica) my ellipse equation from the form:
Ax^2 + By^2 + Cx + Dy + Exy + F = 0
to a more standard form:
((xCos[alpha] - ySin[alpha] - h)^2)/(r^2) + ((xSin[alpha] + yCos[alpha] - k)^2)/(s^2) = 1
where (h,k)
is the center, alpha
is the rotation angle, and r
and s
are the semi-axes
The actual equation I'm attempting to transform is
1.68052 x - 9.83173 x^2 + 4.89519 y - 1.19133 x y - 9.70891 y^2 + 6.09234 = 0
I know the center point is the fitted maximum, which is:
{0.0704526, 0.247775}
I posted a version of this answer on Math SE since it benefits a lot from proper mathematical typesetting. The example there is simpler as well, and there are some extra details.
The following description follows the German Wikipedia article Hauptachsentransformation. Its English counterpart, according to inter-wiki links, is principal component analysis. I find the former article a lot more geometric than the latter. The latter has a strong focus on statistical data, though, so it might be useful for you nevertheless.
Rotation
Your ellipse is described as
First you identify the rotation. You do this by identifying the eigenvalues and eigenvectors of this 2×2 matrix. These eigenvectors will form an orthogonal matrix describing your rotation: its entries are the
Sin[alpha]
andCos[alpha]
from your formula.With your numbers, you get
The first of the three factors is the matrix formed by the eigenvectors, each normalized to unit length. The central matrix has the eigenvalues on the diagonal, and the last one is the transpose of the first. If you multiply the vector
(x,y)
with that last matrix, then you will change the coordinate system in such a way that the mixed term vanishes, i.e. the x and y axes are parallel to the main axes of your ellipse. This is just what happens in your desired formula, so now you know thatTranslation
If you multiply the row vector
[C D]
in the above formula with the first of the three matrices, then this effect will exactly cancel the multiplication of(x, y)
by the third matrix. Therefore in that changed coordinate system, you use the central diagonal matrix for the quadratic term, and this product for the linear term.Now you have to complete the square independently for
x
andy
, and you end up with a form from which you can read the center coordinates.Note that
h
andk
describe the center in the already rotated coordinate system; to obtain the original center you'd multiply again with the first matrix:which fits your description.
Scaling
The completed squares above contributed some more terms to the constant factor
F
:Now you move this to the right side of the equation, then divide the whole equation by this number so that you get the
= 1
from your desired form. Then you can deduce the radii.Verifying the result
Now let's check that we didn't make any mistakes. With the parameters we found, you can piece together the equation
Move the
1
to the left side, and multiply by-6.7580
, and you should end up with the original equation. Expanding that (with the extra precision versions printed in parentheses), you'll getwhich is a perfect match for your input.
If you have
h
andk
, you can use Lagrange Multipliers to maximize / minimize the function(x-h)^2+(y-k)^2
subject to the constraint of being on the ellipse. The maximum distance will be the major radius, the minimum distance the minor radius, andalpha
will be how much they are rotated from horizontal.