TITLE: Divergence in spherical coordinates vs. cartesian coordinates
QUESTION [6 upvotes]: The divergence in spherical coordinates is derived as
$$\nabla\vec A=\frac{1}{r^2}\frac{\partial(r^2A_r)}{\partial r}+\frac{1}{r\sin\theta}\frac{\partial}{\partial\theta}(A_\theta\sin\theta)+\frac{1}{r\sin\theta}\frac{\partial A_\varphi}{\partial\varphi}$$
However, this seems to fail at a simple example. Let's take the identity $\vec F(\vec r):=\vec r$ for example. The divergence of the vector field $\vec F(\vec r)=\begin{pmatrix}x\\y\\z\end{pmatrix}$ in cartesian coordinates is
$$\nabla\vec F(\vec r)=\frac{\partial}{\partial x}F_x(\vec r)+\frac{\partial}{\partial y}F_y(\vec r)+\frac{\partial}{\partial z}F_z(\vec r)=\frac{\partial}{\partial x}x+\frac{\partial}{\partial y}y+\frac{\partial}{\partial z}z=3$$
But in spherical coordinates the vector field would be fined as $\vec F(\vec r)=\begin{pmatrix}r\\\theta\\\varphi\end{pmatrix}$, so the divergence would be:
$$\nabla\vec F(\vec r)=\frac{1}{r^2}\frac{\partial}{\partial r}(r^2\cdot r)+\frac{1}{r\sin\theta}\frac{\partial}{\partial\theta}(\theta\sin\theta)+\frac{1}{r\sin\theta}\frac{\partial}{\partial\varphi}(\varphi)=3+\frac{\sin\theta+\theta\cos\theta}{r\sin\theta}+\frac{1}{r\sin\theta}$$
Which certainly isn't 3:

(Graphic from WolframAlpha)
So what am I doing wrong here?

Edit I successfully managed to completely confuse myself. I think I misunderstood the meaning of $A_r, A_\theta, A_\varphi$ - could anybody explain it to me? In my understanding the following should be true:
$$A_r=\vec A\cdot\hat e_r\qquad A_\theta=\vec A\cdot\hat e_\theta\qquad A_\varphi=\vec A\cdot\hat e_\varphi$$
With the unit vectors for spherical coordinates being as follows:
$$\hat {e} _{r}={\begin{pmatrix}\sin \theta \cos \varphi \\\sin \theta \sin \varphi \\\cos \theta \end{pmatrix}},\qquad \hat {e} _{\theta }={\begin{pmatrix}\cos \theta \cos \varphi \\\cos \theta \sin \varphi \\-\sin \theta \end{pmatrix}},\qquad \hat {e} _{\varphi }={\begin{pmatrix}-\sin \varphi \\\cos \varphi \\0\end{pmatrix}}$$
However, this doesn't make much more sense, as - if I understood it correctly, $\varphi$ and $\theta$ would be $45^\circ$ (and $r=1$) which results in weird basis vectors and doesn't seem to help me.

REPLY [2 votes]: It is sadly common when dealing with different coordinate systems, such as in this case, not to distinguish the function under consideration from its pullback under a coordinate transformation.
Pullback.
Let me introduce some terminology. Suppose we have sets $A$, $B$, and $C$ along with maps
$$A \xrightarrow{\Phi} B \xrightarrow{u} C.$$
We may then form the composition $u \circ \Phi$ of $u$ with $\Phi$, which is the mapping of $A$ into $C$,
$$A \xrightarrow{u \circ \Phi} C,$$
defined by letting
$$(u \circ \Phi)(a) = u(\Phi(a)) \quad\text{for all}\quad a \in A.$$
We call $u \circ \Phi$ the pullback of $u$ under $\Phi$, sometimes denoted $\Phi^\ast u$. Below we will also denote this map simply by $\tilde u$, thus
$$\tilde u = \Phi^\ast u = u \circ \Phi$$
will all denote the same map in the below considerations. (The trouble arises when people do not distinguish between $u$ and $\tilde u$.)
Spherical Coordinate Example.
Consider now the subset $A$ of $\mathbf{R}^3$ given by
$$A = [0, \infty) \times [0, \pi] \times [0, 2\pi] = \bigl\{ (r, \theta, \phi) : r \ge 0, 0 \le \theta \le \pi, 0 \le \phi \le 2\pi \bigr\}$$
along with the mapping $\Phi$ of $A$ into $\mathbf{R}^3$ given by
$$\Phi(r, \theta, \phi) = \begin{bmatrix} r\sin\theta\cos\phi \\ r\sin\theta\sin\phi \\ r\cos\theta \end{bmatrix}.$$
If for instance $u$ is a mapping of $\mathbf{R}^3$ into $\mathbf{R}$, we call the pullback $\tilde u = u \circ \Phi$ of $u$ under $\Phi$ the representation of $u$ in spherical coordinates.
As an example, suppose $u$ is defined by letting
$$u(x, y, z) = (x^2 + y^2 + z^2)^{\frac 12} \quad\text{for all}\quad (x, y, z) \in \mathbf{R}^3,$$
and let us compute the representation of $u$ in spherical coordinates. For a point $(r, \theta, \phi) \in A$, one has
$$\begin{align*}
\tilde u(r, \theta, \phi)
&= u(\Phi(r, \theta, \phi))
= u\bigl( r\sin\theta\cos\phi, r\sin\theta\sin\phi, r\cos\theta \bigr) \\
&= \bigl( (r\sin\theta\cos\phi)^2 + (r\sin\theta\sin\phi)^2 + (r\cos\theta)^2 \bigr)^{\frac 12} \\
&= r.
\end{align*}$$
Thus, $u$ is the mapping on $\mathbf{R}^3$ sending the point $(x,y,z)$ to $(x^2 + y^2 + z^2)^{1/2}$, whereas $\tilde u$ is the mapping on $A$ sending the point $(r, \theta, \phi)$ to $r$, i.e., projection on the first factor.
Observe that $u$ looks a lot simpler in spherical coordinates! It is therefore of interest to see if we can compute such quantities as the gradient $\nabla u$ of $u$ in terms of the gradient $\nabla\tilde u$ of $\tilde u$ and the mapping $\Phi$.
Vector Fields in Spherical Coordinates.
The representation of a vector field in spherical coordinates is worked out in the same way. Consider your example $F$ mapping a point $(x,y,z)$ in $\mathbf{R}^3$ to itself:
$$F(x,y,z) = \begin{bmatrix} x \\ y \\ z \end{bmatrix}.$$
Its pullback under $\Phi$ at a point $(r, \theta, \phi) \in A$ is given by
$$\begin{align*}
\tilde F(r, \theta, \phi)
&= F(\Phi(r, \theta, \phi)) \\
&= F\bigl( r\sin\theta\cos\phi, r\sin\theta\sin\phi, r\cos\theta \bigr) \\
&= \begin{bmatrix} r\sin\theta\cos\phi \\ r\sin\theta\sin\phi \\ r\cos\theta \end{bmatrix}.
\end{align*}$$
Note carefully that although $F$ is the identity mapping on $\mathbf{R}^3$, the representation $\tilde F$ of $F$ in spherical coordinates is not the identity mapping on $A$! However, an interesting question is whether one can recover $\operatorname{div} F$ from $\tilde F$ and $\Phi$, which is what the formula you provided is meant to do.
As for the notation «$F_r$, $F_\theta$, $F_\phi$», one means the projection of $\tilde F$ along the directions of the unit vectors $e_r$, $e_\theta$, and $e_\phi$, respectively. (It would be better to denote these by $\tilde F_r$, $\tilde F_\theta$, $\tilde F_\phi$, but we will not do so below.) Thus at a point $(r, \theta, \phi)$, one means for instance
$$F_r(r, \theta, \phi) = \tilde F(r, \theta, \phi) \cdot e_r(r, \theta, \phi),$$
where we have explicitly evaluated $e_r$ at $(r, \theta, \phi)$.
It is an exercise to show that
$$F_r(r, \theta, \phi) = r, \quad F_\theta(r, \theta, \phi) = 0, \quad F_\phi(r, \theta, \phi) = 0.$$
Using this, the formula you provide for the divergence of $F$ in spherical coordinates becomes
$$\begin{align*}
(\operatorname{div} F)(r\sin\theta\cos\phi, r\sin\theta\sin\phi, r\cos\theta)
&= (\operatorname{div} F)(\Phi(r, \theta, \phi)) \\
&= \frac{1}{r^2}\frac{\partial}{\partial r} \bigl( r^2 F_r(r, \theta, \phi) \bigr) \\
&= \frac{1}{r^2} \frac{\partial}{\partial r}\bigl( r^3 \bigr) \\
&= 3,
\end{align*}$$
which is correct.