# Using chain rule to calculate a second-order partial derivative in spherical polar co-ordinates

I have a function $$f$$ and I am supposed to calculate its second-order partial derivative first in cartesian co-ordinates, which is fine. However, I am stuck while solving the problem in spherical coordinates.

Without worrying about the function $$f$$, how do I use chain-rule in Mathematica to express $$\frac{\partial^{2}}{\partial x^{2}}$$ operation in spherical polar coordinates?

I'm uncertain about what you are asking exactly, but you imply that you have a function f[x,y,z] that you have taken the second derivative wrt x such as d2fdx2 and want to convert that to Spherical coordinates.

For that case it is a simple matter to convert your result by

cartValues = {x, y, z} ->  CoordinateTransformData["Spherical" -> "Cartesian",
"Mapping", {r, θ, ϕ}] // Thread
(*{x -> r Sin[θ] Cos[ϕ], y -> r Sin[θ] Sin[ϕ], z -> r Cos[θ]}*)


Then use

d2fdx2/.cartValues


which will give your result in terms of r, θ, ϕ. Such a conversion does not involve the chain rule. If, on the other hand you want to take the second derivative wrt x of f[r, θ, ϕ] it is a bit more involved, and does involve the chain rule.

First get some conversions.

sphValues = {r, θ, ϕ} ->
CoordinateTransformData["Cartesian" -> "Spherical","Mapping", {x, y, z}] // Thread
(*{r -> Sqrt[x^2 + y^2 + z^2], θ -> ArcTan[z, Sqrt[x^2 + y^2]], ϕ -> ArcTan[x, y]}*)


and for later simplifications the reverse values:

reverseSphValues = {Sqrt[x^2 + y^2 + z^2] -> r, ArcTan[z, Sqrt[x^2 + y^2]] -> θ, ArcTan[x, y] -> ϕ}


Take the second derivative of f[r, θ, ϕ] wrt x

d2xdx2 = D[f[r, θ, ϕ] /. sphValues, x, x] // Simplify


The results are in terms of x,y,z and are lengthy, so I won't print them out. Some simplification.

d2xdx2 = d2xdx2 /. reverseSphValues


And further simplification.

d2xdx2 = d2xdx2 /. cartValues // Simplify[#, {r > 0, 0 <= θ <= π/2}] &


The result involves a long expression of trig functions and has may ways to simplify, so you might want to do your own manipulations.