How to use atan2 to solve this matrix equation?

72 Views Asked by At

I have the following matrix equation:

enter image description here

I'm trying to solve for x and for y. I know that y = atan2(Q,R) and that x = atan2(-P*sin(y), Q) Can someone please explain how those 2 atan2 equations are obtained?

1

There are 1 best solutions below

0
On BEST ANSWER

I think part of the difficulty is that the definitions of $\mathrm{atan2}$ that you find in places like the "definition and computation" section of the Wikipedia page are computationally correct but conceptually separated from the usage of $\mathrm{atan2}$ that you are trying to understand. In particular, any "definition" that explains $\mathrm{atan2}$ by an equation involving the arc tangent function is necessarily going to get bogged down in computational details that explain nothing.

The actual definition in Wikipedia is almost what you want: $\mathrm{atan2}(y,x)$ gives you the principal value of the argument of the complex number $x + iy.$ In your context, however, I say an even better definition is:

The function $\mathrm{atan2}(y,x)$ is defined as the correct value in radians of the angle $\theta$ in the conversion of the Cartesian coordinates $(x,y)$ to polar coordinates $(r,\theta)$, with the constraint that $-\pi < \theta \leq \pi.$

In other words, if we have $x$ and $y$ coordinates (not both zero) and we want $r$ and $\theta$ such that \begin{align} x &= r \cos \theta, \\ y &= r \sin \theta, \end{align} with $r > 0$ and $-\pi < \theta \leq \pi,$ there is exactly one $r$ and one $\theta$ that satisfy all these conditions, namely \begin{align} r &= \sqrt{x^2 + y^2}, \\ \theta &= \mathrm{atan2}(y,x). \end{align}

Graphically, if you plot $P(x,y)$ in Cartesian coordinates and draw a line segment from the origin to $P,$ then $\mathrm{atan2}(y,x)$ is the angle of that line segment counterclockwise from the $x$ axis.

enter image description here

Your problem is a very well-known one, but it is presented in a way that can cause some confusion if you try to look it up elsewhere, because all the variables have been given unusual names. The variables that you name $P,Q,R$ would usually be named $z,y,x$ respectively, the variables that you name $x$ and $y$ would have names such as $\theta,$ $\phi,$ or $\lambda$ (conventions in different fields of study differ on this), and the variable that you name $z$ would be named $r$ or $\rho$ for "radius." It's also unusual to have a negative sign like the one in $-\sin(x)\cdot z,$ but that's a relatively minor issue.

Now consider these two equations implied by your matrix equation: \begin{align} R &= (\cos(x)\cdot z)\cdot \cos(y), \\ Q &= (\cos(x)\cdot z)\cdot \sin(y). \end{align} This is the same as $x = r\cos\theta,$ $y = r\sin\theta$ if you replace $x$ and $y$ with $R$ and $Q$ respectively, replace $r$ with $\cos(x)\cdot z,$ and replace $\theta$ with $y.$ So just as we used $\mathrm{atan2}$ to find $\theta$ before, we use $\mathrm{atan2}$ to find $y$ now: \begin{align} \theta &= \mathrm{atan2}(y,x) && \text{(old variables),} \\ y &= \mathrm{atan2}(Q,R) && \text{(new variables).} \end{align}

There is one thing to beware of: the equations with the old variables required $r$ to be positive, so in the new equations we need $\cos(x)\cdot z.$ If what you are doing is actually a conversion between Cartesian coordinates and any of various kinds of spherical coordinates, this condition will almost always be satisfied, because we would want $\cos(x)$ to be non-negative and $z$ to be positive. The only time the new equation will not work is when $\cos(x) = 0,$ in which case you can see that the matrix equation is satisfied for any value of $y$ that you might choose.

Your matrix equations also imply the following two equations: \begin{align} Q &= (\sin(y)\cdot z)\cdot \cos(x), \\ -P \sin(y) &= (\sin(y)\cdot z)\cdot \sin(x). \end{align} Again we refer to $x = r\cos\theta,$ $y = r\sin\theta,$ but this time we replace $x$ and $y$ with $Q$ and $-P \sin(y)$ respectively, replace $r$ with $\sin(y)\cdot z,$ and replace $\theta$ with $x$: \begin{align} \theta &= \mathrm{atan2}(y,x) && \text{(old variables),} \\ x &= \mathrm{atan2}(-P \sin(y),Q) && \text{(new variables).} \end{align}

There is a problem with this last equation, however. What if $\sin(y)=0$? In that case $Q = 0$ as well, and you will be looking at $\mathrm{atan2}(0,0),$ which is the one place where $\mathrm{atan2}$ is not useful, yet unlike the case earlier where $\cos(x) = 0$ and you can set $y$ to anything you like, in this case there is only one correct value of $x,$ you just have no way to find it with this equation.

A better solution for $x$ can be found by observing that \begin{align} \sqrt{Q^2 + R^2} &= z \cos(x), \\ -P &= z \sin(x), \end{align} and therefore if we assume $z > 0,$ we have $$x = \mathrm{atan2}\left(-P, \sqrt{Q^2 + R^2}\right).$$

Another solution comes from the observation that $$ \sqrt{P^2 + Q^2 + R^2} = z, $$ and therefore $$ -P = \sqrt{P^2 + Q^2 + R^2} \sin(x) $$ and $$ x = -\arcsin\left(\frac{P}{\sqrt{P^2 + Q^2 + R^2}}\right). $$