---
title: The derivative in several variables
heading: The best linear approximation, the Jacobian, the chain rule and error propagation
eyebrow: A14 · Lesson 43 · The derivative in several variables
xp: 40
---

::: hook
For a function of one variable, the derivative is a number. For a map from $\mathbb R^n$ to $\mathbb R^m$, such as node voltages as functions of component values, the derivative is a matrix: the best linear approximation to the map near a point. Defining it properly takes care, because a function can have partial derivatives in every coordinate direction and still not be differentiable. With the right definition come the chain rule, the mean value inequality, and the formulas engineers use every day to propagate tolerances and measurement errors through a calculation.
:::

::: play
- The surface is $f(x, y) = \frac{xy}{x^2 + y^2}$ (with f(0, 0) = 0), shown by shading and contours.
- Along both axes f is 0, so both partial derivatives at the origin are 0. But along the line y = x it's ½, and along y = −x it's −½: f isn't even continuous at the origin.
- So partial derivatives alone don't make a function differentiable. Move the slider to see f on a line through the origin at angle θ: it's constant, $\frac12\sin 2\theta$, right up to the origin.
:::

```sim plot
view: flow
x: x = -1..1
y: y = -1..1
slider: t = 0..3.1416 step 0.01 = 0.785
shade: x*y/(x^2 + y^2 + 1e-12) | contours: 12
readout: value along the line at this angle = sin(2*t)/2 | digits: 3
```

::: predict
## $f(x, y) = x^2y$ near (1, 2). Estimate f(1.01, 2.02) from the derivative.

- [ ] 2.02
- [x] 2.06
- [ ] 2.00
- [ ] 2.10

::: reveal
2.06. The gradient is $(2xy, x^2) = (4, 1)$, so $f \approx 2 + 4(0.01) + 1(0.02) = 2.06$. The exact value is $1.0201 \times 2.02 = 2.0606$; the difference, 0.0006, is second order in the step.
:::
:::

::: nameit
## The derivative

$f: \mathbb R^n \to \mathbb R^m$ is **differentiable** at x if there's a linear map Df(x) (an m × n matrix) with

$$f(x + h) = f(x) + Df(x)h + o(\lVert h\rVert),$$

meaning the error divided by $\lVert h\rVert$ goes to 0. Df(x) is the **Jacobian**, with entries $\frac{\partial f_i}{\partial x_j}$.

## Partials aren't enough

Partial derivatives existing doesn't imply differentiability, or even continuity, as $\frac{xy}{x^2 + y^2}$ shows. But if all partial derivatives exist and are continuous near x, then f is differentiable there.

## Chain rule and mean value inequality

$D(g \circ f)(x) = Dg(f(x))\,Df(x)$: matrices multiply. For vector-valued f there's no mean value equality, but there is an inequality: on a convex set, $\lVert f(x) - f(y)\rVert \le \sup\lVert Df\rVert\,\lVert x - y\rVert$, with the operator norm of lesson 38.

## Error propagation

A small error δx in the inputs gives $\delta f \approx Df\,\delta x$. For a scalar f with independent input errors:

- worst case: $\lvert\delta f\rvert \le \sum_i\left\lvert\frac{\partial f}{\partial x_i}\right\rvert\lvert\delta x_i\rvert$;
- random, with standard deviations $\sigma_i$: $\sigma_f \approx \sqrt{\sum_i\left(\frac{\partial f}{\partial x_i}\sigma_i\right)^2}$.

## Second order

$f(x + h) = f(x) + \nabla f\cdot h + \frac12h^THh + o(\lVert h\rVert^2)$, with the Hessian $H_{ij} = \frac{\partial^2f}{\partial x_i\partial x_j}$, symmetric when the second partials are continuous. That's the basis of Newton's method for optimization and of testing critical points.
:::

::: example
## $f(x, y) = (x^2y,\; x + \sin y)$. Find Df at (1, 0) and estimate f(1.01, 0.02).

::: solution
1. **The derivative.**
   $$Df = \begin{pmatrix}2xy & x^2\\ 1 & \cos y\end{pmatrix}$$
2. **At (1, 0).**
   $$Df(1, 0) = \begin{pmatrix}0 & 1\\ 1 & 1\end{pmatrix}$$
3. **Linear estimate.** With f(1, 0) = (0, 1) and h = (0.01, 0.02):
   $$\begin{aligned} f &\approx (0, 1) + (0.02, 0.03) \\ &= (0.02, 1.03) \end{aligned}$$
4. **Compare with the exact value.** Exactly, $f(1.01, 0.02) = (0.020402, 1.029999)$. The errors, $4 \times 10^{-4}$ and $10^{-6}$, are of order $\lVert h\rVert^2$, as the definition requires.
5. **The second component.** It's almost linear here, since $\sin y \approx y$.

**Answer:** $Df(1, 0) = \begin{pmatrix}0 & 1\\ 1 & 1\end{pmatrix}$, and $f(1.01, 0.02) \approx (0.02, 1.03)$.
:::
:::

::: example
## Show that $f(x, y) = \frac{xy}{x^2 + y^2}$ (f(0, 0) = 0) has both partial derivatives at the origin but isn't differentiable there.

::: solution
1. **The partials.** On the axes f is identically 0, so $f_x(0, 0) = f_y(0, 0) = 0$.
2. **What differentiability would need.** Its derivative would be the zero matrix, and $f(h) = o(\lVert h\rVert)$.
3. **Look along the diagonal.** Along h = (t, t), for every t ≠ 0,
   $$\begin{aligned} f &= \frac{t^2}{2t^2} \\ &= \frac12 \end{aligned}$$
   which doesn't even go to 0.
4. **Conclude.** So f isn't continuous at the origin, let alone differentiable.
5. **Why partials aren't enough.** The partials only look along two directions; differentiability demands a linear approximation in every direction at once, with a uniform error.

**Answer:** both partials are 0 at the origin, but f is ½ all along the diagonal, so f isn't even continuous there, let alone differentiable.
:::
:::

::: example
## P = V²/R with V = 5.00 ± 0.05 V and R = 100 ± 1 Ω. Propagate the errors.

::: solution
1. **Nominal power.** P = 0.25 W.
2. **Sensitivity to V.**
   $$\begin{aligned} \frac{\partial P}{\partial V} &= \frac{2V}{R} \\ &= 0.1\ \text{W/V} \end{aligned}$$
3. **Sensitivity to R.**
   $$\begin{aligned} \frac{\partial P}{\partial R} &= -\frac{V^2}{R^2} \\ &= -0.0025\ \text{W/}\Omega \end{aligned}$$
4. **Contributions.**
   $$0.1 \times 0.05 = 0.005\ \text{W}$$
   $$0.0025 \times 1 = 0.0025\ \text{W}$$
5. **Worst case.**
   $$\lvert\delta P\rvert \le 0.0075\ \text{W}$$
   or 3%.
6. **Independent errors.** If the errors are independent with those standard deviations:
   $$\begin{aligned} \sigma_P &= \sqrt{0.005^2 + 0.0025^2}\ \text{W} \\ &= 0.0056\ \text{W} \end{aligned}$$
   or 2.2%. The voltage dominates because P depends on its square.

**Answer:** P = 0.25 W, within 0.0075 W (3%) in the worst case, or with a standard deviation of 0.0056 W (2.2%) if the errors are independent.
:::
:::

::: check
kind: parts
given: x = 1 | 2; y = 0.5 | 2

## $f(x, y) = (x^2y,\; xy^3)$ at ({x}, {y}).

::: part
answer: 2*x*y
tolerance: 0.5%

### What is the Jacobian's entry $\frac{\partial f_1}{\partial x}$?

::: misses
- [any] Differentiate $x^2y$ in x.
:::
:::

::: part
answer: 3*x*y^2
tolerance: 0.5%

### And $\frac{\partial f_2}{\partial y}$?

::: misses
- [any] Differentiate $xy^3$ in y.
:::
:::
:::

::: check
given: a = 1 | 3; h = 0.01 | 0.05; k = 0.02 | -0.03
answer: a + h + a*k
tolerance: 0.1%

## $f(x, y) = xe^y$. What does the linear approximation at ({a}, 0) give for f({a} + {h}, {k})?

::: right
Right. $\nabla f(a, 0) = (1, a)$, so $f \approx a + h + ak$.
:::

::: misses
- [any] $f(a, 0) + f_x h + f_y k$.
:::
:::

::: check
given: t = 0.3 | 1 | 2
answer: -2*cos(t)*sin(t) + 3*cos(t)
tolerance: 0.5%

## $g(x, y) = x^2 + 3y$ along the circle $(\cos t, \sin t)$. What is $\frac{d}{dt}g$ at t = {t}?

::: right
Right. $\nabla g \cdot (-\sin t, \cos t) = -2\cos t\sin t + 3\cos t$.
:::

::: misses
- [2*cos(t) + 3] Multiply by the velocity $(-\sin t, \cos t)$.
- [any] Chain rule: gradient times velocity.
:::
:::

::: check
kind: parts
given: V = 5 | 12; dV = 0.05 | 0.1; R = 100 | 470; dR = 1 | 5

## P = V²/R with V = {V} ± {dV} V and R = {R} ± {dR} Ω.

::: part
answer: 2*V/R*dV + V^2/R^2*dR
unit: W
tolerance: 0.5%

### What is the worst-case error in P?

::: misses
- [sqrt((2*V/R*dV)^2 + (V^2/R^2*dR)^2)] That's the RMS combination; the worst case adds the magnitudes.
- [any] $\left\lvert\frac{\partial P}{\partial V}\right\rvert\delta V + \left\lvert\frac{\partial P}{\partial R}\right\rvert\delta R$.
:::
:::

::: part
answer: sqrt((2*V/R*dV)^2 + (V^2/R^2*dR)^2)
unit: W
tolerance: 0.5%

### And the RMS error, if the input errors are independent standard deviations?

::: misses
- [any] Add the contributions in squares.
:::
:::
:::

::: check
given: x = 1 | 2; y = 1 | -1
answer: 2*sqrt(x^2 + y^2)
tolerance: 0.5%

## $f(x, y) = (x^2 - y^2,\; 2xy)$, which is $z^2$ in complex form. What is the operator 2-norm of Df at ({x}, {y})?

::: right
Right. Df is $2\lvert z\rvert$ times a rotation, so it stretches every direction by $2\sqrt{x^2 + y^2}$, the size of the complex derivative $2z$.
:::

::: misses
- [any] $Df = \begin{pmatrix}2x & -2y\\ 2y & 2x\end{pmatrix}$, a scaled rotation.
:::
:::

::: check
kind: choice

## A function has partial derivatives everywhere. What can you conclude?

- [ ] It's differentiable
- [ ] It's continuous
- [x] Nothing more, unless the partials are also continuous
- [ ] Its Jacobian is symmetric

::: right
Right. $\frac{xy}{x^2 + y^2}$ has partials everywhere yet is discontinuous at 0; continuous partials guarantee differentiability.
:::

::: misses
- [1] Differentiability needs a good linear approximation in all directions at once.
- [2] Not even that.
- [4] Symmetry is for Hessians of $C^2$ functions.
:::
:::

::: check
kind: derivation
vars: x = -2..2, y = -2..2

## $f(x, y) = x^2y + y^3$.

::: step
answer: 2*x*y

### What is $f_x$?
:::

::: step
answer: x^2 + 3*y^2

### What is $f_y$?
:::

::: step
answer: 2*x

### What is the mixed partial $f_{xy}$?
:::

::: right
Right. $f_{yx}$ is also 2x, as it must be for a smooth function.
:::
:::

::: check
kind: python

## Write `jacobian(f, x, e)` approximating Df(x) by central differences with step e in each coordinate. Set `J` for $f(x, y) = (x^2y,\; x + \sin y)$ at (1, 0) with $e = 10^{-6}$.

```python starter
import numpy as np

def jacobian(f, x, e):
    return np.zeros((2, 2))

f = lambda v: np.array([v[0]**2 * v[1], v[0] + np.sin(v[1])])
J = jacobian(f, np.array([1.0, 0.0]), 1e-6)
print(J)
```

```python solution
import numpy as np

def jacobian(f, x, e):
    n = len(x)
    cols = [(f(x + e * np.eye(n)[j]) - f(x - e * np.eye(n)[j])) / (2 * e) for j in range(n)]
    return np.column_stack(cols)

f = lambda v: np.array([v[0]**2 * v[1], v[0] + np.sin(v[1])])
J = jacobian(f, np.array([1.0, 0.0]), 1e-6)
print(J)
```

```python tests
import numpy as np
assert np.allclose(J, [[0, 1], [1, 1]], atol=1e-8), f"Df(1, 0) is [[0, 1], [1, 1]]; got {J}."
J2 = jacobian(f, np.array([2.0, 1.0]), 1e-6)
assert np.allclose(J2, [[4, 4], [1, np.cos(1)]], atol=1e-6), "At (2, 1) it is [[2xy, x^2], [1, cos y]] = [[4, 4], [1, cos 1]]."
```

::: right
Right. The columns are directional derivatives along each coordinate; for a differentiable f they assemble into the Jacobian.
:::
:::

::: explain
## Why isn't having all partial derivatives the same as being differentiable?

::: model
A partial derivative measures how f changes along one coordinate direction only, with the other coordinates frozen. Differentiability asks for a single linear map that predicts the change along every direction, with an error small compared with the step, uniformly over directions. A function can behave perfectly along the axes and badly along diagonals, as $\frac{xy}{x^2 + y^2}$ does, so the axis information can't determine the behavior elsewhere. Continuity of the partials near the point rules this out, because then the change along any path can be built from small axis-aligned steps whose rates are all close to the partials at the point.
:::
:::

::: practice

::: check
given: a = 2 | 0.5; h = 0.02 | -0.01; k = 0.01 | 0.04
answer: a + h + a*k
tolerance: 0.1%

## Linear estimate of $xe^y$ at ({a} + {h}, {k})?

::: misses
- [any] $a + h + ak$.
:::
:::

::: check
given: t = 0.3 | 0.5 | 0.8 | 1 | 1.5 | 2
answer: -2*cos(t)*sin(t) + 3*cos(t)
tolerance: 0.5%

## $\frac{d}{dt}(x^2 + 3y)$ on $(\cos t, \sin t)$ at t = {t}?

::: misses
- [any] Gradient times velocity.
:::
:::
:::
