How do I convert a vector field in Cartesian coordinates to spherical coordinates?

I have a vector field in terms of $\mathbf{\hat i}$, $\mathbf{\hat j}$, and $\mathbf{\hat k}$,

$$\mathbf{F} = x\mathbf{\hat i} + y\mathbf{\hat j} + z\mathbf{\hat k}$$

How do I convert it to the spherical coordinate system so that the unit vectors become $\mathbf{\hat r}$, $\boldsymbol{\hat \theta}$ and $\boldsymbol{\hat \phi}$?

I know that we have the following relations:

$$\begin{align} F_r &= \sqrt{{F_x}^2 + {F_y}^2 + {F_z}^2}\\ F_\theta &= \tan^{-1} \frac{F_y}{F_x}\\ F_\phi &= \cos^{-1} \frac{F_z}{F_r} \end{align}$$

And the inverses:

$$\begin{align} x &= r\cos{\theta}\sin{\phi}\\ y &= r\sin{\theta}\sin{\phi}\\ z &= r\cos{\phi} \end{align}$$

I want to have the vector in the form

$$\begin{align} \mathbf{F} &= F_{r}\mathbf{\hat r} + F_{\theta}\boldsymbol{\hat \theta} + F_{\phi}\boldsymbol{\hat \phi} \end{align}$$

Therefore, I will eventually get

$$\begin{align} \mathbf{F} &= \sqrt{x^2 + y^2 + z^2}\mathbf{\hat r} + \tan^{-1} \frac{y}{x}\boldsymbol{\hat \theta} + \cos^{-1} \frac{z}{\sqrt{x^2 + y^2 + z^2}}\boldsymbol{\hat \phi} \end{align}$$

Then express $x$, $y$ and $z$ in terms of $r$, $\theta$ and $\phi$.

Did I get the relations right? Or are there other operations (chain rule, etc) that I missed? OR is this simply out of whack?

I am looking for the process as to how it got to this (from Wikipedia):

$$\begin{bmatrix}\boldsymbol{\hat\rho} \\ \boldsymbol{\hat\theta} \\ \boldsymbol{\hat\phi} \end{bmatrix} = \begin{bmatrix} \sin\theta\cos\phi & \sin\theta\sin\phi & \cos\theta \\ \cos\theta\cos\phi & \cos\theta\sin\phi & -\sin\theta \\ -\sin\phi & \cos\phi & 0 \end{bmatrix} \begin{bmatrix} \mathbf{\hat x} \\ \mathbf{\hat y} \\ \mathbf{\hat z} \end{bmatrix}$$

so that $\mathbf{F} = x\mathbf{\hat i} + y\mathbf{\hat j} + z\mathbf{\hat k} = F_{r}\mathbf{\hat r} + F_{\theta}\boldsymbol{\hat \theta} + F_{\phi}\boldsymbol{\hat \phi}$.


First, $\mathbf{F} = x\mathbf{\hat i} + y\mathbf{\hat j} + z\mathbf{\hat k}$ converted to spherical coordinates is just $\mathbf{F} = \rho \boldsymbol{\hat\rho} $. This is because $\mathbf{F}$ is a radially outward-pointing vector field, and so points in the direction of $\boldsymbol{\hat\rho}$, and the vector associated with $(x,y,z)$ has magnitude $|\mathbf{F}(x,y,z)| = \sqrt{x^2+y^2+z^2} = \rho$, the distance from the origin to $(x,y,z)$.

You also asked about where

$$\begin{bmatrix}\boldsymbol{\hat\rho} \\ \boldsymbol{\hat\theta} \\ \boldsymbol{\hat\phi} \end{bmatrix} = \begin{bmatrix} \sin\theta\cos\phi & \sin\theta\sin\phi & \cos\theta \\ \cos\theta\cos\phi & \cos\theta\sin\phi & -\sin\theta \\ -\sin\phi & \cos\phi & 0 \end{bmatrix} \begin{bmatrix} \mathbf{\hat x} \\ \mathbf{\hat y} \\ \mathbf{\hat z} \end{bmatrix}$$

comes from. Let's look at the simpler 2D case first. For a point $(x,y)$ it helps to imagine that you're on a circle centered at the origin. In this case, the two fundamental directions you can move are perpendicular to the circle or along the circle. For the perpendicular direction we use the outward-pointing radial unit vector $\mathbf{\hat{r}}$. For the other direction, moving along the circle means (instantaneously) that you're moving tangent to it, and we take the unit vector in this case to be $\boldsymbol{\hat\theta}$, pointing counterclockwise. For example, suppose you're at the point $(1/\sqrt{2},1/\sqrt{2})$. Then, in the graph below, $\mathbf{\hat{r}}$ is in red and $\boldsymbol{\hat\theta}$ is in yellow.

enter image description here

Note that this means that, unlike the unit vectors in Cartesian coordinates, $\mathbf{\hat{r}}$ and $\boldsymbol{\hat{\theta}}$ aren't constant; they change depending on the value of $(x,y)$.

Now, what about a formula for $\mathbf{\hat{r}}$? If we move perpendicular to the circle we're keeping $\theta$ fixed in the polar coordinate representation $(r \cos \theta, r \sin \theta)$. The vector $\mathbf{\hat{r}}$ is the unit vector in the direction of this motion. If we interpret $r$ as time, taking the derivative with respect to $r$ will give us the velocity vector, which we know points in the direction of motion. Thus we want the unit vector in the direction of $\frac{d}{dr} (r \cos \theta, r \sin \theta) = (\cos \theta, \sin \theta)$. This is already a unit vector, so $\mathbf{\hat{r}} = \cos \theta \mathbf{\hat{x}} + \sin \theta \mathbf{\hat{y}} $. Similarly, moving counterclockwise along the circle entails keeping $r$ fixed in the polar coordinate representation $(r \cos \theta, r \sin \theta)$. Thus to find $\boldsymbol{\hat\theta}$ we take $\frac{d}{d\theta} (r \cos \theta, r \sin \theta) = (-r \sin \theta, r \cos \theta)$. This is not necessarily a unit vector, and so we need to normalize it. Doing so yields $\boldsymbol{\hat\theta}= -\sin \theta \mathbf{\hat{x}} + \cos \theta \mathbf{\hat{y}} $. In matrix form, this is $\begin{bmatrix} \mathbf{\hat{r}} \\ \boldsymbol{\hat\theta}\end{bmatrix} = \begin{bmatrix} \cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} \mathbf{\hat{x}} \\ \mathbf{\hat{y}} \end{bmatrix}$.

Moving up to spherical coordinates, for a given point $(x,y,z)$, imagine that you're on the surface of a sphere. The three fundamental directions are perpendicular to the sphere, along a line of longitude, or along a line of latitude. The first corresponds to $\boldsymbol{\hat\rho}$, the second to $\boldsymbol{\hat\theta}$, and the third to $\boldsymbol{\hat{\phi}}$. (This is using the convention in the Wikipedia page, which has $\theta$ and $\phi$ reversed from what you have.) Thus to find $\boldsymbol{\hat\rho}$, $\boldsymbol{\hat\theta}$, and $\boldsymbol{\hat{\phi}}$, we take the derivative of the spherical coordinate representation $(\rho \sin \theta \cos \phi, \rho \sin \theta \sin \phi, \rho \cos \theta)$ with respect to $\rho$, $\theta$, and $\phi$, respectively, and then normalize each one. That's where the matrix

$$\begin{bmatrix}\boldsymbol{\hat\rho} \\ \boldsymbol{\hat\theta} \\ \boldsymbol{\hat\phi} \end{bmatrix} = \begin{bmatrix} \sin\theta\cos\phi & \sin\theta\sin\phi & \cos\theta \\ \cos\theta\cos\phi & \cos\theta\sin\phi & -\sin\theta \\ -\sin\phi & \cos\phi & 0 \end{bmatrix} \begin{bmatrix} \mathbf{\hat x} \\ \mathbf{\hat y} \\ \mathbf{\hat z} \end{bmatrix}$$

comes from.

As Henning Makholm points out, one way to view what we're doing here is that we're rotating the $\mathbf{\hat{x}}, \mathbf{\hat{y}}, \mathbf{\hat{z}}$ vectors. The transformation matrix can thus be considered a change-of-basis matrix. This means you could also (and more generally) convert $\mathbf{F} = x\mathbf{\hat i} + y\mathbf{\hat j} + z\mathbf{\hat k}$ to spherical coordinates via \begin{align} \mathbf{F} &= \begin{bmatrix} F_{\rho}\\ F_{\theta}\\ F_{\phi}\end{bmatrix} \\ &= \begin{bmatrix} \sin \theta \cos \phi & \sin \theta \sin \phi & \cos \theta \\ \cos \theta \cos \phi & \cos \theta \sin \phi & - \sin \theta \\ - \sin \phi & \cos \phi & 0 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \end{bmatrix} \\ &= \begin{bmatrix} \sin \theta \cos \phi & \sin \theta \sin \phi & \cos \theta \\ \cos \theta \cos \phi & \cos \theta \sin \phi & - \sin \theta \\ - \sin \phi & \cos \phi & 0 \end{bmatrix} \begin{bmatrix} \rho \sin \theta \cos \phi \\ \rho \sin \theta \sin \phi \\ \rho \cos \theta \end{bmatrix} \\ &= \begin{bmatrix} \rho \sin^2 \theta \cos^2 \phi + \rho \sin^2 \theta \sin^2 \phi + \rho \cos^2 \theta \\ \rho \sin \theta \cos \theta \cos^2 \phi + \rho \sin \theta \cos \theta \sin^2 \phi - \rho \sin \theta \cos \theta \\ - \rho \sin \theta \sin \phi \cos \phi + \rho \sin \theta \sin \phi \cos \phi + 0 \end{bmatrix} \\ &= \begin{bmatrix} \rho \\ 0 \\ 0\end{bmatrix}. \end{align} So we get $\mathbf{F} = \rho \boldsymbol{\hat\rho} + 0 \boldsymbol{\hat\theta} + 0 \boldsymbol{\hat{\phi}} = \rho \boldsymbol{\hat{\rho}}$, just as we argued at the beginning.


Spherical coordinates do not work like this: $$\mathbf{F} = F_{r}\mathbf{\hat r} + F_{\theta}\mathbf{\hat \theta} + F_{\phi}\mathbf{\hat \phi}$$ That would be true if the coordinate system is linear but spherical coordinates aren't. Your formulas are right (except that you need to add $\pi$ to the arctangent if $x$ is negative, and add some specific mechanism to make $\theta=\pm \pi/2$ when $x=0$), but it makes no sense to express the result as a linear combination of basis vectors. There is no such thing as "spherical-coordinate unit vectors".

Instead, write the result simply as a triple $$(r,\theta,\phi)=\left(\sqrt{x^2 + y^2 + z^2},\;\; \tan^{-1} \frac{y}{x},\;\; \cos^{-1} \frac{z}{\sqrt{x^2 + y^2 + z^2}}\right)$$