# How to rotate 3D ellipsoid with built-in functions?

I have 3D graphics with an ellipsoid and three arrows which represent its axes. However, not all arrows are inside the ellipsoid; two are outside. How can I rotate the ellipsoid with the use of built-in functions to have the arrows inside the ellipsoid? I have tried three different solutions; however, none give the result I want to get. What did I do wrong?

Here is an example:

Module[{rotZ, a1, a2, a3, b1, b2, b3, l1, l2, l3},
{a1, a2, a3} = {{1.5, 0., 0.}, {0., 1, 0.}, {0., 0., 2}};
rotZ[θ_] := {{Cos[θ], Sin[θ], 0}, {-Sin[θ], Cos[θ], 0}, {0, 0, 1}};
{b1, b2, b3} = {rotZ[30. Degree].a1, rotZ[30. Degree].a2, a3};
{l1, l2, l3} = Map[Sqrt[#[[1]]^2 + #[[2]]^2 + #[[3]]^2] &, {a1, a2, a3}];
Graphics3D[{
(*coodrinate system*)
{Arrowheads[Small], Arrow[{{0., 0., 0.}, {1., 0., 0.}}]},
Text[Style["X", 12, Black],  {1., 0., 0.} 1.1],
{Arrowheads[Small], Arrow[{{0., 0., 0.}, {0., 1., 0.}}]},
Text[Style["Y", 12, Black], {0., 1., 0.} 1.1],
{Arrowheads[Small], Arrow[{{0., 0., 0.}, {0., 0., 1.}}]},
Text[Style["Z", 12, Black], {0., 0., 1.} 1.1],
{Dotted, Line[{{0., 0., 0.}, {-1., 0., 0.}}]}, {Dotted,
Line[{{0., 0., 0.}, {0., -1., 0.}}]}, {Dotted,
Line[{{0., 0., 0.}, {0., 0., -1}}]},
(*ellipsoid*)
(*1*)
{Opacity[0.3], Gray, Ellipsoid[{0., 0., 0.}, {l1, l2, l3}]},
(*2*)
(*{Opacity[0.3],Gray,Ellipsoid[{0.,0.,0.},{b1,b2,b3}]},*)
(*3*)
(*{Opacity[0.3],Gray,Rotate[Ellipsoid[{0.,0.,0.},{l1,l2,l3}],
30. Degree,{0.,0.,0.}]},*)
(*ellipsoid axes*)
Text[Style["a1", 12, Black], b1 1.1],
Text[Style["a2", 12, Black], b2 1.1], {Arrowheads[Small],
Arrow[{{0., 0., 0.}, b3}]}, Text[Style["a3", 12, Black], b3 1.1]
}, ViewPoint -> Top
]]


TransformedRegion[] can quickly determine the rotated ellipsoid:

er[θ_] = TransformedRegion[Ellipsoid[{0, 0, 0}, {4, 3, 2}],
RotationTransform[θ, {0, 0, 1}]]


which yields

   Ellipsoid[{0, 0, 0}, {{16 Cos[θ]^2 + 9 Sin[θ]^2, 7 Cos[θ] Sin[θ], 0},
{7 Cos[θ] Sin[θ], 9 Cos[θ]^2 + 16 Sin[θ]^2, 0},
{0, 0, 4}}]


Alternatively, you can directly construct the rotated Ellipsoid[] with RotationMatrix[]:

er[θ_] := Ellipsoid[{0, 0, 0}, RotationMatrix[θ, {0, 0, 1}].DiagonalMatrix[{4, 3, 2}^2].
RotationMatrix[-θ, {0, 0, 1}]]


Note the arrangement of the matrices in the second argument of Ellipsoid[]; in this, you are in effect performing a similarity transformation on the diagonal matrix containing the squares of the semiaxis lengths. Similar arrangements will work for other rotation (or orthogonal in general) matrices.

Thus,

With[{θ = -30 ​°},
Graphics3D[{Ellipsoid[{0, 0, 0}, {4, 3, 2}],
{Opacity[2/3, Red], er[θ]}}]]


As a more elaborate example, let's rotate an ellipsoid about an arbitrary axis:

(* https://mathematica.stackexchange.com/a/111693 *)
rodrigues[th_, axis_?VectorQ] :=
First[LinearAlgebraMatrixPolynomial[{{1, Sin[th], 2 Sin[th/2]^2}},
-LeviCivitaTensor[3, List].Normalize[axis]]]

ela[θ_, ax_] := With[{m = rodrigues[θ, ax]},
Ellipsoid[{0, 0, 0}, m.DiagonalMatrix[{4, 3, 2}^2].Transpose[m]]]

With[{ax = {3, -2, 4}},
Animate[Graphics3D[{Ellipsoid[{0, 0, 0}, {4, 3, 2}],
{Opacity[2/3, Red],
Arrow[Tube[{{0, 0, 0}, 5 Normalize[ax]}, 0.05]],
ela[θ, ax]}}, Boxed -> False, PlotRange -> 5],
{θ, 0, 2 π - π/20, π/20}]]


{Opacity[0.3], Gray, Rotate[Ellipsoid[{0., 0., 0.}, {l1, l2, l3}], -30 Degree, {0, 0, 1}]}

I get your ellipsoid axes (a1, a2, a3`) to align with the ellipse.