# A Chain rule proof using Mathematica

If $z=f(x,y)$, where $x=r \cos\theta$ and $y=r\sin\theta$, how can I use Mathematica to prove that: $$\frac{\partial^2z}{\partial x^2}+\frac{\partial^2z}{\partial y^2}=\frac{\partial^2z}{\partial r^2}+\frac{1}{r^2}\frac{\partial^2z}{\partial \theta^2}+\frac1r\frac{\partial z}{\partial r}$$

Understanding the chain rule in Mathematica:

Jen's and Ian's effort on my behalf is helping me understand Mathematica notation involving the chain rule. If $z=f(x,y)$, where $x=r \cos t$ and $y=r\sin t$, here is how we start to find $\partial z/\partial r$ by hand in class. We draw this image as a reminder of the chain rule.

Then, because $x=r\cos t$ and $y=r\sin t$, we get: \begin{align*} \frac{\partial z}{\partial r} &=\frac{\partial z}{\partial x}\frac{\partial x}{\partial r}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial r}\\ \frac{\partial z}{\partial r} &=\cos t\ \frac{\partial z}{\partial x}+\sin t\ \frac{\partial z}{\partial y} \end{align*}

Now we enter just part of Ian's code, concentrating only on $\partial z/\partial r$ in order to understand Mathematica notation.

Clear[z, f, x, y, r, t];
z[r, t] = f[x[r, t], y[r, t]];
x[r_, t_] = r Cos[t];
y[r_, t_] = r Sin[t];
D[z[r, t], r]


The output is:

It's in a different order from my answer, but it is the same.

I am interpreting Cos[t] $f^{(1,0)}$[r Cos[t], r Sin[t]] + Sin[t] $f^{(0,1)}$ [r Cos[t], r Sin[t]] as meaning

$$\cos t\ \frac{\partial z}{\partial x}+\sin t\ \frac{\partial z}{\partial y},$$

or equivalently:

$$\cos t\ \frac{\partial}{\partial x}\ f(r \cos t, r\sin t)+\sin t\ \frac{\partial}{\partial y}\ f(r \cos t, r\sin t)$$

I also computed $\frac{\partial^2z}{\partial r^2}$ by hand and got the same answer using Ian's Mathematica notation.

Was Able to Understand Another Answer:

The question was find $\partial^2z/\partial t^2$, where $z=f(x,y)$ and $x=g(s,t)$ and $y=h(s,t)$. I entered:

Clear[z, f, x, y, g, s, t];
D[f[g[s, t], h[s, t]], {t, 2}] // Simplify


And I interpreted that as meaning:

$$\left(\frac{\partial y}{\partial t}\right)^2\frac{\partial^2f}{\partial y^2} +\frac{\partial f}{\partial y}\frac{\partial^2y}{\partial t^2} +\frac{\partial^2 x}{\partial t^2}\frac{\partial f}{\partial x} +2\frac{\partial x}{\partial t}\frac{\partial y}{\partial t}\frac{\partial^2f}{\partial x\partial y} +\left(\frac{\partial x}{\partial t}\right)^2\frac{\partial^2f}{\partial x^2}$$

Which agrees with the answer in the textbook.

Thanks to Jens and Ian, I have learned quite a bit.

Here is a way to do this using the new commands FromPolarCoordinates and ToPolarCoordinates in version 10.1. I'll use ψ for the function name, instead of f or z. Also, I like to use ϕ for the polar angle:

Clear[x, y, r, ϕ, ψ];

polarCoords =


{x -> r Cos[ϕ], y -> r Sin[ϕ]}

ψChain[x_, y_] = Apply[ψ, ToPolarCoordinates[{x, y}]]


$$\psi \left(\sqrt{x^2+y^2},\tan ^{-1}(x,y)\right)$$

FullSimplify[
(D[ψChain[x, y], x, x] + D[ψChain[x, y], y, y]) /.
polarCoords, -Pi < ϕ < Pi && r > 0]


$$\frac{\psi ^{(0,2)}(r,\phi )+r \psi ^{(1,0)}(r,\phi )}{r^2}+\psi ^{(2,0)}(r,\phi )$$

Here, I defined the coordinate transformation as a Rule named polarCoords that can simply be appended to a Cartesian expression with /. in order to go to polar coordinates. Then I do the Laplacian in Cartesian coordinates but on a function ψChain[x, y] defined in terms of the polar-coordinate function ψ. After converting back to polar coordinates, the resulting derivatives of ψ give the polar form of the Laplacian.

Edit

Since the functions I'm using above can also be used in three dimensions to convert to spherical coordinates, it may be good to add that case for completeness. I'll just do it all in one expression:

Clear[x, y, z, r, ϕ, θ, ψ];

Expand@FullSimplify[(D[#, x, x] + D[#, y, y] + D[#, z, z]) &[
Apply[ψ, ToPolarCoordinates[{x, y, z}]]] /.
FromPolarCoordinates[{r, θ, ϕ}]], -Pi < ϕ < Pi &&
r > 0 && 0 < θ < Pi]


$$\frac{\psi ^{(0,2,0)}(r,\theta ,\phi )}{r^2}+\frac{\cot (\theta ) \psi ^{(0,1,0)}(r,\theta ,\phi )}{r^2}+\frac{\csc ^2(\theta ) \psi ^{(0,0,2)}(r,\theta ,\phi )}{r^2}+\frac{2 \psi ^{(1,0,0)}(r,\theta ,\phi )}{r}+\psi ^{(2,0,0)}(r,\theta ,\phi )$$

This agrees with the result of the built-in function

Expand@Laplacian[ψ[r, θ, ϕ], {r, θ, ϕ}, "Spherical"]


This Mathematica proof focuses on doing only the major logical steps by hand - the ones that are independent of the particulars of the coordinate choice. It leaves the details of how to apply the chain rule to Mathematica.

• Awesome! I just spend two hours by hand calculating $\partial^2z/\partial x^2$ and checked it with FullSimplify[ D[zChain[x, y], x, x] /. polarCoords, -Pi < \[Theta] < Pi && r > 0] // Expand and got the same answer. My hand calculation started with $\partial z/\partial x=(\partial z/\partial r)(\partial r/\partial x)+(\partial z/\partial \theta)(\partial \theta/\partial x)$. Normally, the class first step is $\partial z/\partial r=(\partial z/\partial x)(\partial x/\partial r)+(\partial z/\partial y)(\partial y/\partial r)$, which is a little easier. – David Jul 20 '15 at 5:32
• @David The tricky thing in getting these theorems across is not to lose sight of the big picture with all the hairy chain-rule computations... – Jens Jul 20 '15 at 17:52
• I'm looking at ToPolarCoordinates in the Documentation, then opening the directions, and seeing a bizarre image indicating $\theta$ and $\phi$ that I have never seen before. I did just a part of your code, namely ToPolarCoordinates[{x, y, z}], and got an answer that agrees with the image in the documentation. But those angles are not azimuth and co-latitude, which are used in spherical coordinates. Still, you get the correct answer. I also found that if I run your code with ToSphericalCoordinates and FromSphericalCoordinates, I get the same answer, but spherical angles I understand. Amazing. – David Jul 21 '15 at 4:17
• @David Ah yes, that's a potential point of confusion: the hyperspherical coordinates as defined in the docs have a cyclically rotated set of reference axes, with x replacing the spherical z, y replacing the spherical x axis. Other than that, they are (in 3D) identical to spherical coordinates. Since the Laplacian is invariant under the relabeling of the Cartesian coordinates, this difference between hypershperical and spherical coordinates has no effect on the result. This again shows that in my "proof", you can use any To... and From... (maybe your own) and get the correct result. – Jens Jul 21 '15 at 4:45

The nub of the problem is that we all get a little sloppy in writing down this kind of equation. Fortunately, it's possible to convey what we mean to Mathematica in our sloppy notation. Here's what's sloppy: $z(x,y)$ involves $x$ and $y$ as dummy variables and is no different from $z(r,\theta)$. You can't tell me the value of $z(0, 1)$ unless I tell you the coordinate system. So, let's use $(x, y)$ and $(r,\theta)$ as stand-ins for different coordinate system:

z[x, y] := f[x, y]
z[r, t] := f[x[r, t], y[r, t]]


Note the lack of underscores.

Now the test is straightforward:

x[r_, t_] := r Cos[t]
y[r_, t_] := r Sin[t]

LHS = D[z[x, y], {x, 2}] + D[z[x, y], {y, 2}];
RHS = D[z[r, t], {r, 2}] + 1/r^2 D[z[r, t], {t, 2}] + 1/r D[z[r, t], r];

Simplify[LHS==RHS /. {x -> x[r, t], y -> y[r, t]}]
(* True *)


Jens's answer actually gives you the RHS, which is very nice. I thought an answer using basic Mathematica functionality would be helpful too.

• Thanks for the help. I spent quite a bit of time playing with your answer and I've added my view of what is going on to my original post. – David Jul 20 '15 at 16:42