# Math Help - Solve for rotations {rx,ry,rx} given before and after vectors

1. ## Solve for rotations {rx,ry,rx} given before and after vectors

We can use the 3x3 rotation matrix R to determine the effect of rotations about the principle axes:

Vafter = R.Vbefore

That's straightforward.

Does anyone know what the strategy should be for solving for unknown rotations r={rx,ry,rx} radians, given we know Vbefore and Vafter?

2. Originally Posted by BPW
We can use the 3x3 rotation matrix R to determine the effect of rotations about the principle axes:

Vafter = R.Vbefore

That's straightforward.
Then presumably you know that a rotation about the x-axis though angle rx is given by the matrix with rows [1 0 0], [0 cos(rx) -sin(rx)], [0 sin(rx) cos(rx)], that a rotation about the y-axis through angle ry is given by the matrix [cos(ry) 0 -sin(ry)], [0 1 0], [sin(ry), 0 cos(ry)], and that the rotation around the z-axis through an angle rz is given by the matrix [cos(rz) -sin(rz) 0], [sin(rz) cos(rz) 0], [0 0 1].

And the rotation matrix R is the product of those matrics.

Does anyone know what the strategy should be for solving for unknown rotations r={rx,ry,rx} radians, given we know Vbefore and Vafter?
Go ahead and multiply the matrices above by the Vbefore vector, and set the result equal to the Vafter vector. That gives you three equations to solve for vx, vy, and vz. (I presume you meant {rx,ry,rz}, not {rx,ry,rx}.)

3. Hi Halls.....
yes I realize Rzyx = Rx.Ry.Rz and that I can multiply out the equations. You get three simultaneous equations with anglex, angley and anglez as the unknowns - the problem is these equations involve Sin and Cos terms and are rather tricky to solve. For example, and there are multiple solutions. I've also read there are problems with 'ambiguities' - by that, I think they mean the solutions are:

angle = C x Pi + function

where C is a member of the class of integers. The solutions seem to have branching. I do wonder if I have stumbled over the rationale for using quaternions!

Originally Posted by HallsofIvy
Then presumably you know that a rotation about the x-axis though angle rx is given by the matrix with rows [1 0 0], [0 cos(rx) -sin(rx)], [0 sin(rx) cos(rx)], that a rotation about the y-axis through angle ry is given by the matrix [cos(ry) 0 -sin(ry)], [0 1 0], [sin(ry), 0 cos(ry)], and that the rotation around the z-axis through an angle rz is given by the matrix [cos(rz) -sin(rz) 0], [sin(rz) cos(rz) 0], [0 0 1].

And the rotation matrix R is the product of those matrics.

Go ahead and multiply the matrices above by the Vbefore vector, and set the result equal to the Vafter vector. That gives you three equations to solve for vx, vy, and vz. (I presume you meant {rx,ry,rz}, not {rx,ry,rx}.)

4. The vectors $\mathbf{v}_{\text{after}}$ and $\mathbf{v}_{\text{before}}$ obviously have to have the same length, or you can't rotate one to coincide with the other. Let's assume that they are both unit vectors.

I'm going to start with one unit vector $\mathbf{v} = (a,b,c)$, with $a^2+b^2+c^2 = 1$. Let $\theta =\arccos{a}$ and let $d = b/\sin\theta,\ e = c/\sin\theta$. (That can't be done if $\sin\theta=0$, but we'll come back to that later.) Then $\mathbf{v} = (\cos\theta,d\sin\theta,e\sin\theta)$, and $d^2+e^2=1$. So we can find $\phi$ such that $\cos\phi =d,\ \sin\phi = e$, and $\mathbf{v} = (\cos\theta,\sin\theta\cos\phi,\sin\theta\sin\phi)$.

Let $\mathbf{u} = (1,0,0)$. Then it is easy to check that $\mathbf{v} = X_\phi Z_\theta\mathbf{u}$, where $X_\phi$ is a rotation of $\phi$ radians about the x-axis and $Z_\theta$ is a rotation of $\theta$ radians about the z-axis. The inverse transformation is given by $\mathbf{u} = Z_{-\theta}X_{-\phi}\mathbf{v}$.

Now if you are given two vectors $\mathbf{v_1}$ and $\mathbf{v_2}$, you can rotate $\mathbf{v_1}$ to $\mathbf{v_2}$ by rotating it first to $\mathbf{u}$ and then on to $\mathbf{v_2}$.

Thus $\mathbf{v_2} = X_{\phi_2}Z_{\theta_2-\theta_1}X_{-\phi_1}\mathbf{v_1}$. That is more or less what you wanted. I seem to have ended with rotations {rx,rz,rx} rather than the {rx,ry,rx} that you wanted, so you'll have to switch the axes round to get that.

Finally, what happens if $\sin\theta=0$? In that case, $\cos\theta = \pm1$ and the vector $\mathbf{v}$ will be $(\pm1,0,0)$. If the sign is +, then $\mathbf{v}$ is already equal to $\mathbf{u}$. If it is -, then the rotation $Z_\pi$ will swing it round to $\mathbf{u}$.