17  Multivariable Chain Rule

We previously established that small changes in \(x\) and \(y\) produce a corresponding change in \(z\) according to the total differential, as in Equation 16.2. If we imagine those small changes occurring over a small period of time \(dt\), the total differential is closely related to the multivariable chain rule.

17.1 Variables Changing in Time

Multi-variable chain rule (Stewart et al. 2020, 14.5, pp. 985)

If \(z = f(x,y)\) is differentiable and \(x = g(t)\) and \(y = h(t)\) are both differentiable functions of \(t\), then \(z = f(g(t), h(t))\) is also a differentiable function of \(t\) and \[ \frac{\mathrm{d} z}{\mathrm{d} {t}} = \frac{\partial z}{\partial {x}} \frac{\mathrm{d} x}{\mathrm{d} {t}} + \frac{\partial z}{\partial {y}} \frac{\mathrm{d} y}{\mathrm{d} {t}}. \]

This is the most common version of the chain rule we will use: it can describe the trajectory of a particle moving across a surface, or otherwise related quantities changing over time.

Example 17.1 Consider a particle moving along the surface \(S\) defined by \[ z = \frac{x+y^2}{e^{x^2+y^2}} \] along the path \((x,y) = (\cos t, \sin t)\). In order to determine how the altitude of the particle is changing in time, we can compute \(\frac{\mathrm{d} z}{\mathrm{d} {t}}\). We begin by computing the partial derivatives of \(z\): \[ \begin{aligned} \frac{\partial z}{\partial {x}} &= \frac{\partial}{\partial {x}} \left( (x+y^2)e^{-x^2-y^2} \right) \\ &= \left( e^{-x^2-y^2} + (x+y^2) (-2x) e^{-x^2-y^2} \right) \\ &= \left( 1 - 2x^2 - 2xy^2 \right) e^{-x^2-y^2} \end{aligned} \] and \[ \begin{aligned} \frac{\partial z}{\partial {y}} &= \frac{\partial}{\partial {y}} \left( (x+y^2)e^{-x^2-y^2} \right) \\ &= \left( 2y e^{-x^2-y^2} + (x+y^2) (-2y) e^{-x^2-y^2} \right) \\ &= 2y \left( 1 - x - y^2 \right) e^{-x^2-y^2}. \end{aligned} \] Next, since \(x'(t) = -\sin t\) and \(y'(t) = \cos t\), we apply the chain rule: \[ \frac{\mathrm{d} z}{\mathrm{d} {t}} = \left[ \left( 1 - 2x^2 - 2xy^2 \right) e^{-x^2-y^2} \right] (-\sin t) + \left[ 2y \left( 1 - x - y^2 \right) e^{-x^2-y^2} \right] (\cos t). \] The power of the chain rule is especially evident when we evaluate derivatives at specific points. For example, at \(t = \tfrac{\pi}{2}\), we have \((x,y) = (0,1)\), and we can evaluate the rate of change in altitude directly: \[ \begin{aligned} \left. \frac{\mathrm{d} z}{\mathrm{d} {t}} \right|_{t = \tfrac{\pi}{2}} &= \left[ \left( 1 - 0 - 0 \right) e^{-1} \right] (-1) + \left[ 2(1) \left( 1 - 0 - 1 \right) e^{-1} \right] (0) \\ &= \left(\frac{1}{e}\right) (-1) + (0)(0) \\ &= -\frac{1}{e}. \end{aligned} \]

The trajectory traced out by \((x(t), y(t))\) on the surface \(S\) with the \(t=\frac{\pi}{2}\) point marked.

Example 17.2 (Stewart et al. (2020), chap. 14.5, pp. 986) The pressure \(P\), volume \(V\), and temperature \(T\) of a mole of an ideal gas are related by the ideal gas law, \(P = 8.31 \frac{T}{V}\). If the volume and temperature are changing over time, the pressure changes accordingly: \[ \begin{aligned} \frac{\mathrm{d} P}{\mathrm{d} {t}} &= \frac{\partial P}{\partial {T}} \frac{\mathrm{d} T}{\mathrm{d} {t}} + \frac{\partial P}{\partial {V}} \frac{\mathrm{d} V}{\mathrm{d} {t}} \\ &= 8.31 \left( \frac{1}{V} \frac{\mathrm{d} T}{\mathrm{d} {t}} - \frac{T}{V^2} \frac{\mathrm{d} V}{\mathrm{d} {t}} \right). \end{aligned} \]

If the temperature is 300 K and volume is 100 L, increasing at respective rates of 0.1 K/sec and 0.2 L/sec, then \[ \begin{aligned} \frac{\mathrm{d} P}{\mathrm{d} {t}} &= 8.31 \left( \frac{1}{100} (0.1) - \frac{300}{{100}^2} (0.2) \right) \\ &= 8.31 \left( 0.001 - 0.006 \right) \\ &= 8.31(-0.005) = -0.04155. \end{aligned} \] The pressure is decreasing by approximately 0.042 kPa/sec.

17.2 General Chain Rule

Variables can depend on more than just a single parameter \(t\). If \(x\) and \(y\) are themselves functions of multiple variables, the chain rule expands to match.

General chain rule (Stewart et al. 2020, 14.5, pp. 988)

If \(u = f(x_1, \dots, x_n)\) is differentiable and each \(x_i\) is a differentiable function of some variables \(t_1, \dots, t_m\), then \(u\) is also a differentiable function of these variables and \[ \frac{\partial u}{\partial {t_i}} = \frac{\partial u}{\partial {x_1}} \frac{\partial x_1}{\partial {t_i}} + \cdots + \frac{\partial u}{\partial {x_n}} \frac{\partial x_n}{\partial {t_i}}. \]

To keep track of which derivatives to multiply and add, we use dependency trees. The process is straightforward: to find the derivative of the top variable with respect to a bottom variable, we sum the products of the derivatives along all paths leading to the desired variable.

Example 17.3 For our first rule, where \(z = f(x,y)\) and \(x, y\) depend only on \(t\), the tree is as follows:

graph TD
    Z((z)) -->|∂z/∂x| X((x))
    Z -->|∂z/∂y| Y((y))
    X -->|dx/dt| T1((t))
    Y -->|dy/dt| T2((t))

Dependency tree for \(z = f(x(t), y(t))\).

Example 17.4 For a system where \(z\) depends on \(x\) and \(y\), which in turn depend on two new variables \(r\) and \(s\), the tree branches out further:

graph TD
    Z((z)) -->|∂z/∂x| X((x))
    Z -->|∂z/∂y| Y((y))
    X -->|∂x/∂r| R1((r))
    X -->|∂x/∂s| S1((s))
    Y -->|∂y/∂r| R2((r))
    Y -->|∂y/∂s| S2((s))

Dependency tree for \(z = f(x(r, s), y(r, s))\).

Tracing the paths from \(z\) down to \(r\) yields the formula \[ \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}}, \] and a similar equation for \(\frac{\partial z}{\partial {s}}\).

Example 17.5 Suppose \(z = x^2 + e^{x+y}\), where \(x = r \cos \theta\) and \(y = r \sin \theta\); we want to compute \(\frac{\partial z}{\partial {r}}\) and \(\frac{\partial z}{\partial {\theta}}\). First, we find the partial derivatives of the outer function \(z\): \[ \frac{\partial z}{\partial {x}} = 2x + e^{x+y} \quad \text{and} \quad \frac{\partial z}{\partial {y}} = e^{x+y}. \] Next, we find the partial derivatives of the inner functions \(x\) and \(y\): \[ \frac{\partial x}{\partial {r}} = \cos \theta, \quad \frac{\partial y}{\partial {r}} = \sin \theta, \quad \frac{\partial x}{\partial {\theta}} = -r \sin \theta, \quad \text{and} \quad \frac{\partial y}{\partial {\theta}} = r \cos \theta. \] Following our dependency tree, we assemble the pieces: \[ \begin{aligned} \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}} \\ &= (2x + e^{x+y})(\cos \theta) + (e^{x+y})(\sin \theta). \end{aligned} \] and \[ \begin{aligned} \frac{\partial z}{\partial {\theta}} &= \frac{\partial z}{\partial {x}} \frac{\partial x}{\partial {\theta}} + \frac{\partial z}{\partial {y}} \frac{\partial y}{\partial {\theta}} \\ &= (2x + e^{x+y})(-r \sin \theta) + (e^{x+y})(r \cos \theta). \end{aligned} \] To complete the process, we could substitute \(x = r \cos \theta\) and \(y = r \sin \theta\) into the final expressions to write the derivatives entirely in terms of \(r\) and \(\theta\).

17.3 More Implicit Differentiation

As discussed in Section 15.2: When faced with an implicit relationship between variables, we can apply a partial differential operator (like \(\frac{\partial}{\partial {x}}\)) to both sides of the equation. In doing so, we treat the independent variables normally and apply the chain rule whenever we encounter the dependent variable. This can be helpful for us here!

Example 17.6 (Implicit Tangent Planes) We want to find the tangent plane to the hyperboloid defined by \(2x^2 + 3y^2 = 1 + z^2\) at the point \((-1, 0, 1)\). We can solve the problem by solving for \(z\), differentiating, and using Equation 16.1, but this involves computing derivatives involving square roots. Alternatively, we can differentiate implicitly. First, consider the partial derivative of the defining equation in \(x\): \[ \begin{aligned} \frac{\partial}{\partial {x}} \left( 2x^2 + 3y^2 \right) & = 4x, \text{ and } \\ \frac{\partial}{\partial {x}} \left( 1 + z^2 \right) & = 2z \frac{\partial z}{\partial {x}}. \\ \end{aligned} \] Hence we have \(\frac{\partial z}{\partial {x}} = 2 \frac{x}{z}\). Similarly, differentiating with respect to \(y\) gives \[ 6y = 2z \frac{\partial z}{\partial {y}}, \] and so \(\frac{\partial z}{\partial {y}} = 3 \frac{y}{z}\). In particular, if we evaluate at the point \((x, y, z) = (-1, 0, 1)\), we have \[ \left[ \frac{\partial z}{\partial {x}} \right]_{(-1, 0, 1)} = -2 \quad \text{and} \quad \left[ \frac{\partial z}{\partial {y}} \right]_{(-1, 0, 1)} = 0 \] Hence, the tangent plane is given by \[ \begin{aligned} z & = -2(x+1) + 0(y-0) + 1 \\ & = -2x - 1 \end{aligned} \]

var('x y z')
pt = point((-1,0,1), color='black', size=50)
surface = implicit_plot3d(
    2*x^2 + 3*y^2 == 1 + z^2,
    (x, -2, 2), (y, -2, 2), (z, -2, 2),
    plot_points=120, opacity=0.7
)
plane = implicit_plot3d(
    z == -2*x-1,
    (x, -2, 2), (y, -2, 2), (z, -2, 2),
    color='green'
)
show(pt + surface + plane)

Example 17.7 (The Van der Waals Equation) The Ideal Gas Law is a simplification! Chemists and physicists often use the Van der Waals equation for a more realistic description, which accounts for intermolecular forces and the volume of the gas particles themselves: \[ \left( P + \frac{a}{V^2} \right)(V - b) = RT, \] where \(P\) is pressure, \(V\) is volume, \(T\) is temperature, \(R\) is the ideal gas constant, and \(a\) and \(b\) are constants specific to the gas. Suppose we want to find \(\frac{\partial V}{\partial {T}}\), the rate at which the volume expands as temperature increases, assuming the pressure \(P\) is held constant.

Trying to isolate \(V\) first is practically impossible—expanding the equation reveals a cubic equation in terms of \(V\)! Instead, we will treat \(V\) as a dependent function of the independent variables \(P\) and \(T\), and apply the partial differential operator \(\frac{\partial}{\partial {T}}\) to both sides.

On the right side, \(R\) is a constant, so: \[ \frac{\partial}{\partial {T}}(RT) = R. \] On the left side, we use the product rule. Because \(P\) and \(T\) are our independent variables, the partial derivative treats \(P\) as a constant, but since \(V\) depends on \(T\) the chain rule applies: \[ \begin{aligned} \frac{\partial}{\partial {T}} \left[ \left( P + aV^{-2} \right)(V - b) \right] &= \left[ \frac{\partial}{\partial {T}} \left( P + aV^{-2} \right) \right](V - b) + \left( P + aV^{-2} \right) \left[ \frac{\partial}{\partial {T}} (V - b) \right] \\ &= \left( -2aV^{-3} \frac{\partial V}{\partial {T}} \right)(V - b) + \left( P + aV^{-2} \right) \left( \frac{\partial V}{\partial {T}} \right). \end{aligned} \]

Equating the left and right sides gives us: \[ -2aV^{-3}(V - b) \frac{\partial V}{\partial {T}} + \left( P + \frac{a}{V^2} \right) \frac{\partial V}{\partial {T}} = R. \]

We conclude by factoring out \(\frac{\partial V}{\partial {T}}\) and applying a bit of algebra: \[ \begin{aligned} \frac{\partial V}{\partial {T}} \left[ -2aV^{-3}(V - b) + P + \frac{a}{V^2} \right] &= R \\ \frac{\partial V}{\partial {T}} &= \frac{R}{P + \frac{a}{V^2} - \frac{2a(V - b)}{V^3}}. \end{aligned} \]