# Spherical Gradient along $z$-axis

Let us consider a function $f(r,\theta,\phi)$ that can only be efficiently computed in spherical coordinates. We need its Cartesian gradient. We can implement its analytical spherical derivatives, compute the spherical gradient, $\left(\frac{\partial f}{\partial r},\frac{1}{r}\frac{\partial f}{\partial \theta},\frac{1}{r\sin\theta}\frac{\partial f}{\partial \phi}\right)$, and convert the gradient to Cartesian component.

What would you do with the values along the $z$-axis, where $\theta$ is undefined and usually set to 0, and in the origin, where $r$ is 0 as well? More specifically, what would you do with the terms in the denominators $r$ and $r\sin\theta$?

• If the surface given by $f$ is smooth enough (I guess you are with an expansion of $f$ in terms of spherical harmonics) , you will not have any problem with your gradient (as long as you take care within these areas $\theta=0$ and $r=0$). Usually, the smoothness of the surface implies that the terms $\partial u/\partial\theta /(r\sin{\theta})=\mathcal{O}(1)$ or $\partial u/\partial \phi /r=\mathcal{O}(1)$ at least, when $\theta\to 0$ and $r\to 0$ – HBR Jul 21 '17 at 18:13
• Is your function numerically unstable at the origin? If it is, then there's no general technique for fixing that for an arbitrary $f$ (apart from using arbitrary precision arithmetic), so one would have to know some details about the function itself. – Kirill Jul 21 '17 at 21:12
• @Kirill Let us say that $f$ returns high-order spherical harmonics computed by Legendre polynomial recursion (so you need spherical coordinates for that). You get that the $\phi$-component of the spherical gradient blows up along the $z$-axis. – Pippo Jul 22 '17 at 9:53
• Supposing that your function may be expandible in terms of spherical harmonics, $i.e.$ $f=\sum_{l,m}{f_{lm} Y_l^{m}}$. If the spherical harmonics depend only on the Legendre polynomials, the function $f$ can be considered axisymetric and therefore $\partial f/\partial\phi=0$. If not, the spherical harmonics that depend on $\phi$ are always proportional to some power of $\sin{\theta}$ and therefore the third component of yout gradient, computed in spherical coodinates will be always finite and well defined when $\theta\to 0$, see for example mathworld.wolfram.com/SphericalHarmonic.html – HBR Jul 22 '17 at 11:18
• You are right, @HBR, I made a mistake in my calculations. – Pippo Jul 22 '17 at 12:54