---
title: The chain rule in several variables
heading: When everything changes at once
eyebrow: A9 · Lesson 5 · The chain rule
xp: 40
---

::: hook
A heater's supply voltage is creeping up as a regulator warms, and at the same time its element's resistance is climbing as it heats. Is its power rising or falling, and how fast? The power $P = V^2/R$ depends on two quantities that both depend on time. The chain rule for several variables answers it directly: add up how much each input's change contributes, each weighted by how sensitive the output is to that input.
:::

::: play
- A heater's voltage rises at $V'$ volts per second while its resistance rises at $R'$ ohms per second, starting from 12 V and 10 Ω.
- The curve is the power over time. Rising voltage pushes the power up; rising resistance pulls it down.
- The readouts split the rate of change at $t = 0$ into the two contributions. Find settings where they cancel.
:::
:::

```sim plot
view: graph
x: t = 0..20 s
y: P = 0..40 W
slider: Vr = 0..1 step 0.05 = 0.5 V/s
slider: Rr = 0..1 step 0.05 = 0.2 Ω/s
plot: (12 + Vr*t)^2/(10 + Rr*t) | label: P = V²/R
readout: from the voltage, W/s = 2*12/10*Vr | digits: 3
readout: from the resistance, W/s = -(144/100)*Rr | digits: 3
readout: total rate, W/s = 2*12/10*Vr - (144/100)*Rr | digits: 3
```

::: predict
## $V$ = 12 V is rising at 0.5 V/s and $R$ = 10 Ω is rising at 0.2 Ω/s. Is the power rising or falling?

- [x] Rising, at about 0.9 W/s
- [ ] Falling, at about 0.3 W/s
- [ ] Steady
- [ ] Rising, at about 1.2 W/s

::: reveal
Rising, at 0.912 W/s. The voltage's contribution is $\frac{\partial P}{\partial V}V' = \frac{2V}{R}(0.5) = 1.2$ W/s; the resistance's is $\frac{\partial P}{\partial R}R' = -\frac{V^2}{R^2}(0.2) = -0.288$ W/s. Add them.
:::
:::

::: nameit
## One path in, several variables

If $z = f(x, y)$ and both $x$ and $y$ depend on $t$, then

$$\frac{dz}{dt} = \frac{\partial f}{\partial x}\frac{dx}{dt} + \frac{\partial f}{\partial y}\frac{dy}{dt}$$

Each term is "how sensitive $z$ is to this input" times "how fast this input is changing". With more inputs, add more terms. A **tree diagram**, with $z$ at the top, the inputs below and $t$ at the bottom, shows which products to add: one per path.

## Several variables in, several out

If $x = x(s, t)$ and $y = y(s, t)$, then

$$\frac{\partial z}{\partial s} = \frac{\partial f}{\partial x}\frac{\partial x}{\partial s} + \frac{\partial f}{\partial y}\frac{\partial y}{\partial s}$$

and likewise for $t$. In matrix form it's the product of the two Jacobian matrices (lesson 19).

## Implicit differentiation

If $F(x, y) = 0$ defines $y$ as a function of $x$, differentiating gives $F_x + F_y\frac{dy}{dx} = 0$, so

$$\frac{dy}{dx} = -\frac{F_x}{F_y}$$

For a circle $x^2 + y^2 = r^2$, $\frac{dy}{dx} = -\frac{x}{y}$, the same answer as A5's implicit differentiation, in one line.
:::

::: example
## $z = x^2y$ with $x = \cos t$, $y = \sin t$. Find $\frac{dz}{dt}$ at $t = \pi/4$.

::: solution
1. **Apply the chain rule.**
   $$\frac{dz}{dt} = 2xy(-\sin t) + x^2\cos t$$
2. **Evaluate at $t = \pi/4$.** There $x = y = 0.7071$:
   $$\begin{aligned} \frac{dz}{dt} &= 2(0.5)(-0.7071) \\ &\quad + 0.5(0.7071) \\ &= -0.7071 + 0.3536 \\ &= -0.354 \end{aligned}$$

**Answer:** $\frac{dz}{dt} = -0.354$ at $t = \pi/4$.
:::
:::

::: example
## A current $I = V/R$ has $V$ = 5 V rising at 0.1 V/s and $R$ = 100 Ω rising at 2 Ω/s. How fast is $I$ changing?

::: solution
1. **Differentiate through both variables.**
   $$\frac{dI}{dt} = \frac{1}{R}V' - \frac{V}{R^2}R'$$
2. **Put in the numbers.**
   $$\begin{aligned} \frac{dI}{dt} &= \frac{0.1}{100} - \frac{5}{10\,000}(2) \\ &= 0.001 - 0.001 \\ &= 0 \end{aligned}$$
3. **Read the result.** The two effects cancel exactly: the current holds steady while both change.

**Answer:** $I$ isn't changing at all: $dI/dt = 0$.
:::
:::

::: example
## A resistor's power is $P = I^2R$. With $I$ = 2 A falling at 0.1 A/s and $R$ = 10 Ω rising at 0.5 Ω/s, how fast is $P$ changing?

::: solution
1. **Chain rule through both variables.**
   $$\begin{aligned} \frac{dP}{dt} &= \frac{\partial P}{\partial I}\frac{dI}{dt} + \frac{\partial P}{\partial R}\frac{dR}{dt} \\ &= 2IR\frac{dI}{dt} + I^2\frac{dR}{dt} \end{aligned}$$
2. **Put in the numbers.**
   $$\begin{aligned} \frac{dP}{dt} &= 2 \times 2 \times 10 \times (-0.1) \\ &\quad + 4 \times 0.5 \\ &= -4 + 2 \\ &= -2\ \text{W/s} \end{aligned}$$
3. **Read the result.** The falling current wins: power drops at 2 W each second even though the resistance is rising.

**Answer:** $P$ is falling at 2 W/s.
:::
:::

::: check
given: x = 2 | 3; y = 5 | -1; xr = 1 | -2; yr = 3 | 4
answer: y*xr + x*yr

## $z = xy$. At a moment when $x$ = {x}, $y$ = {y}, $\dot x$ = {xr} and $\dot y$ = {yr}, what is $\dot z$?

::: right
Right. $\frac{\partial z}{\partial x}\dot x + \frac{\partial z}{\partial y}\dot y = y\dot x + x\dot y$ = {answer}.
:::

::: misses
- [xr*yr] Multiply each rate by the sensitivity to that variable, not by the other rate.
- [x*xr + y*yr] $\frac{\partial(xy)}{\partial x} = y$: the sensitivities swap.
- [any] Not yet. $y\dot x + x\dot y$.
:::
:::

::: check
given: V = 12 | 24; R = 10 | 50; Vr = 0.5 | 1; Rr = 0.2 | 2
answer: 2*V/R*Vr - V^2/R^2*Rr
unit: W/s
tolerance: 0.5%

## A heater has $V$ = {V} V rising at {Vr} V/s and $R$ = {R} Ω rising at {Rr} Ω/s. How fast is $P = V^2/R$ changing?

::: right
Right. $\frac{2V}{R}V' - \frac{V^2}{R^2}R'$ = {answer}.
:::

::: misses
- [2*V/R*Vr] Include the resistance's contribution, which pulls the power down.
- [2*V/R*Vr + V^2/R^2*Rr] Rising resistance lowers the power: $\frac{\partial P}{\partial R} = -\frac{V^2}{R^2}$.
- [any] Not yet. Add the two contributions.
:::
:::

::: check
given: x = 3 | 5; y = 4 | 12
answer: -x/y
tolerance: 0.5%

## On the circle $x^2 + y^2 = r^2$, what is $\frac{dy}{dx}$ at $(\given{x}, \given{y})$?

::: right
Right. $-\frac{F_x}{F_y} = -\frac{2x}{2y}$ = {answer}.
:::

::: misses
- [x/y] Check the sign: $-F_x/F_y$.
- [-y/x] Upside down.
- [any] Not yet. $F = x^2 + y^2 - r^2$, then $-F_x/F_y$.
:::
:::

::: check
given: t = 1 | 2 | 0.5
answer: 8*t + 18*t^2
tolerance: 0.5%

## $f(x, y) = x^2 + 3xy$ with $x = 2t$, $y = t^2$. What is $\frac{df}{dt}$ at $t$ = {t}?

::: right
Right. $(2x + 3y)(2) + 3x(2t) = 8t + 18t^2$ = {answer}.
:::

::: misses
- [8*t + 6*t^2] Include $\frac{\partial f}{\partial x}$'s $3y$ term: $(2x + 3y) \cdot 2$.
- [any] Not yet. $f_x\dot x + f_y\dot y$.
:::
:::

::: check
given: s = 2 | 3; t = 1 | 4
answer: 2*s

## $z = xy$ with $x = s + t$, $y = s - t$. What is $\frac{\partial z}{\partial s}$?

::: right
Right. $y \cdot 1 + x \cdot 1 = (s - t) + (s + t) = 2s$ = {answer}.
:::

::: misses
- [-2*t] That's $\frac{\partial z}{\partial t}$.
- [any] Not yet. $z_x x_s + z_y y_s$.
:::
:::

::: check
given: V = 5 | 10; R = 100 | 1000; Vr = 0.1 | 0.5; Rr = 1 | 10
answer: (Vr/R - V/R^2*Rr)*1000
unit: mA/s
tolerance: 0.5%

## $I = V/R$ with $V$ = {V} V rising at {Vr} V/s and $R$ = {R} Ω rising at {Rr} Ω/s. How fast is $I$ changing?

::: right
Right. $\frac{V'}{R} - \frac{V}{R^2}R'$ = {answer}.
:::

::: misses
- [Vr/R*1000] Rising resistance also changes the current.
- [any] Not yet. Two contributions, one per input.
:::
:::

::: check
kind: parts
given: I = 1 | 3; R = 10 | 47; dI = -0.1 | 0.2; dR = 0.5 | 2

## $P = I^2R$ with $I$ = {I} A changing at {dI} A/s and $R$ = {R} Ω changing at {dR} Ω/s.

::: part
answer: 2*I*R*dI + I^2*dR
unit: W/s
tolerance: 0.5%

### How fast is $P$ changing?

::: misses
- [2*I*R*dI] Include the resistance's change too.
- [any] $\frac{dP}{dt} = 2IR\frac{dI}{dt} + I^2\frac{dR}{dt}$.
:::
:::
:::

::: check
kind: derivation
vars: t = 0..3

## $z = x^2y$ with $x = \cos t$ and $y = \sin t$.

::: step
answer: -2*cos(t)*sin(t)*sin(t)

### What is $\frac{\partial z}{\partial x}\frac{dx}{dt}$?
:::

::: step
answer: -2*cos(t)*sin(t)^2 + cos(t)^3

### What is $\frac{dz}{dt}$?
:::

::: right
Right. The chain rule adds the two paths: $2xy(-\sin t) + x^2\cos t$.
:::
:::

::: explain
## Why does the multivariable chain rule add the contributions from each input?

::: model
Over a short time $dt$, each input changes by a small amount, and to first order the output responds to each change as if the other input were held fixed: $dz \approx f_x\,dx + f_y\,dy$. That's the linear approximation of $z$ near the current point, a tangent plane. Dividing by $dt$ gives $\frac{dz}{dt} = f_x\frac{dx}{dt} + f_y\frac{dy}{dt}$. The small changes simply add because, close enough, a smooth function is linear.
:::
:::

::: practice

::: check
given: x = 1 | 4; y = 2 | 3; xr = 2 | -1; yr = 1 | 5
answer: 2*x*xr + 2*y*yr

## $z = x^2 + y^2$. With $x$ = {x}, $y$ = {y}, $\dot x$ = {xr}, $\dot y$ = {yr}, what is $\dot z$?

::: misses
- [any] $2x\dot x + 2y\dot y$.
:::
:::

::: check
given: a = 2 | 3; b = 1 | 4
answer: -a/b
tolerance: 0.5%

## On the line $\given{a}x + \given{b}y = 7$, what is $\frac{dy}{dx}$?

::: misses
- [-b/a] Upside down.
- [any] $-F_x/F_y$.
:::
:::

::: check
given: s = 1 | 3; t = 2 | 5
answer: -2*t

## $z = xy$ with $x = s + t$, $y = s - t$. What is $\frac{\partial z}{\partial t}$?

::: misses
- [2*s] That's $\partial z/\partial s$.
- [any] $y \cdot 1 + x \cdot (-1)$.
:::
:::
:::
