# Seeking a closed-form solution of a system of nonlinear trigonometric equations (converting Euler angles to spherical)

Following this and this questions, I want to solve the system of nonlinear trigonometrical equations (as part of an inverse kinematics calculation):

\begin{align} \sin{\phi} \sin{\gamma} &= \sin\alpha \cos\theta_1 \cos\theta_2 + \cos\alpha \sin\theta_2\\ \cos{\phi} &= \cos\alpha \cos\theta_1 \cos\theta_2 - \sin\alpha \sin\theta_2 \\ \sin\phi \cos\gamma &= \sin\theta_1 \cos\theta_2 \end{align}

for $$\theta_1$$ and $$\theta_2$$ given the $$\gamma$$, $$\phi$$, and $$\alpha$$.

I think there should be closed form solution.

I tried Python-SymPy:

from sympy.solvers import solve
from sympy import symbols, sin, cos, tan

alpha, gamma, phi, theta1, theta2 = symbols('alpha gamma phi theta_1 theta_2')

eqs = (sin(alpha) * cos(theta1) + tan(theta2) * cos(alpha) - tan(gamma) * sin(theta1),
cos(alpha) * cos(theta1) * cos(theta2) - sin(alpha) * sin(theta2) - cos(phi))
solve(eqs, (theta1, theta2))


However, the interpreter never finishes.

I would appreciate if you could help me know if the above system of equations has explicit solution.

• If this is a numpy question, it would be best served on StackOverflow. – dantopa May 21 '19 at 22:00
• OK, try editing the question to start from the beginning, explaining exactly what you are trying to do, what each symbol means, where you got the expressions on each side of your equations, what those expressions mean, and why you think they should be equal. While you're at it you should comment the code you posted on StackOverflow; how is anyone supposed to guess what you think your "Rodrigues" function is supposed to do? Lack of explanation by you may have something to do with why you didn't get any answers there yet. – David K May 21 '19 at 22:03
• Dear @dantopa this is not a numpy question and the only reason I have used the python-sympy code is just to include what I have done so far. – Foad May 21 '19 at 22:07
• The equations at hand can be rewritten as $$\begin{bmatrix} \sin \phi \sin\gamma \\ \cos \phi\\ \sin \phi \cos\gamma \end{bmatrix} = R(\alpha) \begin{bmatrix} \sin\theta_2\\ \cos\theta_1 \cos\theta_1\\ \sin\theta_1 \cos\theta_2 \end{bmatrix} \quad\text{where}\quad R(\alpha) =\begin{bmatrix} \cos\alpha & \sin\alpha & 0\\ -\sin\alpha & \cos\alpha & 0\\ 0 & 0 & 1\\ \end{bmatrix}$$ Try multiply both sides on the left by $R(\alpha)^{-1} = R(-\alpha)$. – achille hui May 21 '19 at 23:43
• @achillehui That's so very nearly a complete answer it seems a shame not to put it in the answer section. (Although I'm sure you meant one of those $\theta_1$s to be $\theta_2$ instead.) – David K May 22 '19 at 0:32

Thanks to the great comment by achille hui we can write the system of equations as:

$$\begin{bmatrix} \sin \phi \sin\gamma \\ \cos \phi\\ \sin \phi \cos\gamma \end{bmatrix} = R(\alpha) \begin{bmatrix} \sin\theta_2\\ \cos\theta_1 \cos\theta_2\\ \sin\theta_1 \cos\theta_2 \end{bmatrix}$$

where $$R(\alpha)$$ is the rotation matrix of:

$$R(\alpha) =\begin{bmatrix} \cos\alpha & \sin\alpha & 0\\ -\sin\alpha & \cos\alpha & 0\\ 0 & 0 & 1\\ \end{bmatrix}$$

and its invert is

$$R^{-1}(\alpha) = R(-\alpha) = \begin{bmatrix} \cos\alpha & -\sin\alpha & 0\\ \sin\alpha & \cos\alpha & 0\\ 0 & 0 & 1\\ \end{bmatrix}$$

now the equation can be rewritten as:

$$\begin{bmatrix} \sin\theta_2\\ \cos\theta_1 \cos\theta_2\\ \sin\theta_1 \cos\theta_2 \end{bmatrix} = \displaystyle \left[\begin{matrix}- \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\gamma \right)} \sin{\left(\phi \right)} \cos{\left(\alpha \right)}\\\sin{\left(\alpha \right)} \sin{\left(\gamma \right)} \sin{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\\\sin{\left(\phi \right)} \cos{\left(\gamma \right)}\end{matrix}\right]$$

which is calculated through the python-sympy command of

Matrix([[cos(alpha), -sin(alpha), 0],
[sin(alpha), cos(alpha), 0],
[0, 0, 1]]) * Matrix([[sin(phi) * sin(gamma)], [cos(phi)], [sin(phi) * cos(gamma)]])


and as a result we get:

$$\tan{\theta_1} = \frac{\sin{\phi} \cos{\gamma}}{\sin{\alpha} \sin{\gamma} \sin{\phi} + \cos{\alpha} \cos{\phi}}$$

and

$$\sin{\theta_2} = \sin{\left(\gamma \right)} \sin{\left(\phi \right)} \cos{\left(\alpha \right)} - \sin{\left(\alpha \right)} \cos{\left(\phi \right)}$$

(too long for comment)

The Euler angles define the position of a 3D Euclidean reference system rotated wrt a base one.

The spherical coordinates define the position of one (unit vector), for instance the $$x'$$ axis of the rotated system: the position of the other two (rotation around $$x'$$) is not known.

So the correspondence is not univoque. You shall specify better what actually is your aim.

I saw now that you mention Rodrigues' formula: that is in fact relative to the rotation of a (unit) vector around another unit vector (axis of rotation). Practically the rotation of a point on earth around another point on earth. So how is your problem formulated in this "environment" ?

From your self-answer, and taking as reference the Geographic convention for Spherical Coordinates with $$\theta_1$$ as longitude and $$\theta_2$$ as latitude, it is now clear that you want to
- take a vector in such a reference system $${\bf v} =\left( {1,\theta _1 ,\theta _2 } \right) = \left( {\matrix{ {\cos \theta _2 \cos \theta _1 } \cr {\cos \theta _2 \sin \theta _1 } \cr {\sin \theta _2 } \cr } } \right)$$ - exchange its components according to the matrix $${\bf E} = \left( {\matrix{ 0 & 0 & 1 \cr 1 & 0 & 0 \cr 0 & 1 & 0 \cr } } \right)$$ which has a negative determinant and so includes a reflection;
- rotate it around the original reference polar axis ($$z$$) of an angle $$- \alpha$$ (in the right-hand convention, i.e. eastward) $${\bf R}_{\,{\bf z}} ( - \alpha ) = \left( {\matrix{ {\cos \alpha } & { + \sin \alpha } & 0 \cr { - \sin \alpha } & {\cos \alpha } & 0 \cr 0 & 0 & 1 \cr } } \right)$$ - exchange back its components $${\bf E}^{\, - \,{\bf 1}} = \left( {\matrix{ 0 & 1 & 0 \cr 0 & 0 & 1 \cr 1 & 0 & 0 \cr } } \right)$$ - rename the resulting longitude $${\theta '}_1$$ as $$\gamma$$, and take the azimut angle $$\phi$$ in place of the resulting latitude $${\theta '}_2$$ , i.e. $$\phi = \pi/2-{\theta '}_2$$.

• $\phi$ and $\gamma$ are the spherical angles of the $y$ unit vector. Anyway, I think the problem is solved now. – Foad May 22 '19 at 8:17