Rectangular to Spherical/Spherical to Rectangular Case Not Consistent

40 Views Asked by At

I found the following equations to converting between Rectangular and Spherical coordinates on the site https://byjus.com/maths/spherical-coordinates/#:~:text=In%20three%20dimensional%20space%2C%20the,used%20to%20determine%20these%20coordinates.

Rectangular to Spherical: r = sqrt(pow(x,2 + pow(y,2) + pow(z,2)) theta = arccos (x/r) phi = arccos (x/(r sin theta))

Spherical to Rectangular: x = r sin (theta) cos (phi) y = r sin (theta) sin (phi) z = r cos (theta)

If I use the first set to convert (0, 10, 10) to spherical I get r= 10 sqrt(2), theta = 90 deg, phi = 90 deg.

But if I use the second set to convert (10 sqrt(2), 90, 90) to rectangular, I get x = 0, y = 10 sqrt(2), z = 0.

You would think that one should be the inverse of the other and return the original set.

2

There are 2 best solutions below

1
On BEST ANSWER

$$\phi=\cos^{-1}(\frac{x}{r \sin \theta})$$ is just true, but not a transformation formula, because angular functions occur on both sides.

by $$\frac{y}{x}=\frac {r \sin \phi}{r \cos \phi}$$

$$\phi=\tan^{-1}\left(\frac{y}{x}\right) + if(x>0,0,\pm \pi) $$

where the conventions in the x-negative half plane vary. In numerical languages there exists the $\tan^{-1}(x,y)$ of two arguemnts as a continuous function to $(-\pi,\pi)$

4
On

There are so many defects in the formulas given on that page that it is hard to figure out what formulas they meant to write there.

$\theta = \cos^{-1}(x / r)$

If you solve this for $x$, it says that $x = r \cos\theta$. But if you look at the diagram at the top of the page that supposedly says what the angles mean in spherical coordinates, this makes no sense. Here is the diagram with a few added annotations:

enter image description here

The triangle with blue edges is a right triangle. I have named the vertices of this triangle $O$, $P$, and $Q$ in this version of the diagram. The point $P$ is the point with spherical coordinates $(r, \theta, \phi)$. The point $O$ is the origin of the coordinate system, with rectangular coordinates $(0,0,0)$. The point $Q$ is in the $x,y$ plane directly below $P$ and is the right-angled vertex of the triangle.

The hypotenuse of the triangle is $OP = r$, as shown in the original diagram. Clearly $\angle OPQ = \phi$ and therefore $PQ = r\cos(\phi)$ and $OQ = r\sin(\phi)$. I have labeled the lengths of those two sides in the modified diagram.

If the rectangular coordinates of $P$ are $(x,y,z)$, we can work out via the Pythagorean Theorem that $r = \sqrt{x^2 + y^2 + z^2}$. So the formula that the web page gives for $r$ is correct.

Since the side $OQ$ of the triangle makes the angle $\theta$ with the $x$ axis, $Q$ is at rectangular coordinates $$(r\sin(\phi)\cos(\theta), r\sin(\phi)\sin(\theta),0).$$

Now already we have found a problem on the web page. The $x$ and $y$ coordinates of $P$ are the same as the $x$ and $y$ coordinates of $Q$. So when the web page says that we can get the $x$ coordinate of the point shown in the diagram by writing $$ x = r \sin(\theta)\cos(\phi), $$ it is clearly incorrect. They have swapped the names of the two angles.

Granted, not everyone measures $\phi$ from the $z$ axis and $\theta$ from the $x$ axis. Some people would swap the labels $\phi$ and $\theta$ on the angles in the diagram. But the web page showed the diagram they showed, and then they gave an inconsistent conversion formula. Which way did they intend to define spherical coordinates?

I am going to suppose that the diagram is correct. Therefore the formulas are wrong. The formulas for converting spherical to rectangular should be

\begin{align} x &= r\sin(\phi)\cos(\theta), \\ y &= r\sin(\phi)\sin(\theta), \\ z &= r\cos(\phi). \end{align}

Now we know for sure that $r\cos(\theta)$ is not the correct formula for $x$, despite the implication of the formula $\theta = \cos^{-1}(x / r)$. So that formula also is wrong.

But if we solve for $\phi$ in the formula $z = r\cos(\phi)$, assuming $0 \leq \phi \leq 180^\circ$ (which is a reasonable assumption for the angle measured from the $z$ axis in spherical coordinates), then we get

$$ \phi = \cos^{-1}\left(\frac zr\right). $$

That is, take the incorrect formula $\theta = \cos^{-1}(x / r)$ from the web page, replace $\theta$ with $\phi$ and replace $x$ with $z$, and you have a correct formula to compute the angle $\phi$.

Now that we have $\phi$ we can try to use the fact that $x = r\sin(\phi)\cos(\theta)$ to solve for $\theta$ in terms of the known quantities $x$, $r$, and $\phi$. If we assume that $0 \leq \theta \leq 180^\circ$ the result is

$$ \theta = \cos^{-1}\left(\frac x{r\sin(\phi)}\right). $$

That's almost like one of the formulas on the web page, except that the names $\phi$ and $\theta$ are swapped.

But there is a problem with this formula, because in spherical coordinates, if you always have $0 \leq \theta \leq 180^\circ$ then you always have $y \geq 0$. And we want to be able to convert all rectangular coordinates in the entire three-dimensional space, not just the ones with non-negative $y$ coordinates.

In order to get spherical coordinates for points with negative $y$ coordinates you need to change the formula. One convention is to simply reverse the sign of $\theta$ when $y < 0$, so when converting $(x,y,z) = (0,-10,10)$ to spherical coordinates we would get $\theta=-90^\circ$. Another convention is to subtract $\cos^{-1}\left(\frac x{r\sin(\phi)}\right)$ from $360^\circ$, so when $(x,y,z) = (0,-10,10)$ we find that $\theta=270^\circ$. Either way is acceptable and will result in the correct values of the $x$, $y$, and $z$ coordinates when you convert back to rectangular coordinates.

So here's a correct set of formulas to convert from rectangular to spherical coordinates using the angles as labeled in the diagram:

\begin{align} r &= \sqrt{x^2 + y^2 + z^2}, \\ \phi &= \cos^{-1}\left(\frac zr\right), \\ \theta &= \begin{cases} \cos^{-1}\left(\dfrac x{r\sin(\phi)}\right) & \text{ if $y \geq 0$}, \\ -\cos^{-1}\left(\dfrac x{r\sin(\phi)}\right) & \text{ if $y < 0$}. \\ \end{cases} \end{align}

So the final score for the conversion formulas on the web page is $1$ correct (the formula for $r$), $5$ incorrect according to their diagram.