TExcelTAD
Home › Calculus Essentials › Partial Derivatives

Partial Derivatives

A partial derivative measures how a function of several variables changes when you nudge one input and hold all the others fixed. It is the ordinary derivative you already know, applied one variable at a time. It is also the basic quantity of machine learning: the partial derivative of the loss with respect to each weight says how to adjust that weight.

Definition

For f(x,y)f(x, y), the partial derivative with respect to xx at (a,b)(a, b) is

∂f∂x(a,b)=lim⁡h→0f(a+h,b)−f(a,b)h\frac{\partial f}{\partial x}(a, b) = \lim_{h \to 0} \frac{f(a + h, b) - f(a, b)}{h}

and ∂f/∂y\partial f/\partial y nudges yy instead. Geometrically, slice the surface z=f(x,y)z = f(x, y) with the plane y=by = b; ∂f/∂x\partial f/\partial x is the slope of the resulting curve. Common notations:

Notation Meaning
∂f∂x\dfrac{\partial f}{\partial x}, fxf_x, ∂xf\partial_x f partial derivative with respect to xx
∂2f∂x2\dfrac{\partial^2 f}{\partial x^2}, fxxf_{xx} second partial, xx twice
∂2f∂y ∂x\dfrac{\partial^2 f}{\partial y \, \partial x}, fxyf_{xy} first xx, then yy (mixed partial)

Computing partials

Treat every other variable as a constant and use the usual rules (power, product, chain). For f(x,y)=x3y2+4xy−sin⁡(y)f(x, y) = x^3y^2 + 4xy - \sin(y):

fx=3x2y2+4y(y is a constant: sin⁡y disappears)fy=2x3y+4x−cos⁡y(x is a constant)\begin{aligned} f_x &= 3x^2y^2 + 4y && (y \text{ is a constant: } \sin y \text{ disappears}) \\ f_y &= 2x^3y + 4x - \cos y && (x \text{ is a constant}) \end{aligned}

SymPy's diff(f, x) does exactly this, and a finite difference checks it numerically:

python · try it yourself
import sympy as sp

x, y = sp.symbols('x y')
f = x**3 * y**2 + 4*x*y - sp.sin(y)

fx, fy = sp.diff(f, x), sp.diff(f, y)
print("f_x =", fx)
print("f_y =", fy)

a, b = 1.5, 0.8
exact = float(fx.subs({x: a, y: b}))
h = 1e-6
F = sp.lambdify((x, y), f)
numeric = (F(a + h, b) - F(a - h, b)) / (2 * h)
print(f"f_x(1.5, 0.8): exact {exact:.6f}, central difference {numeric:.6f}")
f_x = 3*x**2*y**2 + 4*y
f_y = 2*x**3*y + 4*x - cos(y)
f_x(1.5, 0.8): exact 7.520000, central difference 7.520000

SymPy prints x**2 for x2x^2; that is Python syntax, not a different answer.

Second partials and Clairaut's theorem

Differentiate again to get four second partials of f(x,y)f(x, y): fxxf_{xx}, fyyf_{yy} and the two mixed ones fxyf_{xy} and fyxf_{yx}. Clairaut's theorem says that if the second partials are continuous (true for every formula built from polynomials, exp, sin and so on, away from trouble spots), the order does not matter: fxy=fyxf_{xy} = f_{yx}.

python · try it yourself
import sympy as sp

x, y = sp.symbols('x y')
f = sp.exp(x * y) + x**2 * sp.cos(y)

print("f_xx =", sp.diff(f, x, 2))
print("f_yy =", sp.diff(f, y, 2))
fxy = sp.diff(f, x, y)          # x first, then y
fyx = sp.diff(f, y, x)          # y first, then x
print("f_xy =", fxy)
print("equal?", sp.simplify(fxy - fyx) == 0)
f_xx = y**2*exp(x*y) + 2*cos(y)
f_yy = x**2*(exp(x*y) - cos(y))
f_xy = x*y*exp(x*y) - 2*x*sin(y) + exp(x*y)
equal? True

The second partials collected into a matrix form the Hessian, which describes curvature and appears in Jacobian and Hessian.

Linear approximation and the tangent plane

Near a point (a,b)(a, b), a smooth surface looks like its tangent plane:

f(a+Δx,b+Δy)≈f(a,b)+fx(a,b)⋅Δx+fy(a,b)⋅Δyf(a + \Delta x, b + \Delta y) \approx f(a, b) + f_x(a, b) \cdot \Delta x + f_y(a, b) \cdot \Delta y

This is the two-variable version of the tangent line, and it is how errors propagate: if a rectangle's sides xx and yy are measured with small errors Δx\Delta x and Δy\Delta y, the area A=xyA = xy has error ΔA≈y⋅Δx+x⋅Δy\Delta A \approx y \cdot \Delta x + x \cdot \Delta y.

In software: gradients of a loss

For the line model y^=w⋅x+b\hat{y} = w \cdot x + b with mean squared error L(w,b)=1n∑(w⋅xi+b−yi)2L(w, b) = \frac{1}{n}\sum (w \cdot x_i + b - y_i)^2, the partial derivatives are

∂L∂w=2n∑(w⋅xi+b−yi)⋅xi∂L∂b=2n∑(w⋅xi+b−yi)\begin{aligned} \frac{\partial L}{\partial w} &= \frac{2}{n} \sum (w \cdot x_i + b - y_i) \cdot x_i \\ \frac{\partial L}{\partial b} &= \frac{2}{n} \sum (w \cdot x_i + b - y_i) \end{aligned}

Each says how the loss reacts to a small change of one parameter. Every training step of linear regression, and of a neural network, computes these numbers (by backpropagation or automatic differentiation) and moves each parameter a little against its own partial derivative. A finite-difference check like the one below is the standard way to test hand-written gradient code.

python · try it yourself
import numpy as np

x = np.array([0.0, 1.0, 2.0, 3.0])
y = np.array([1.0, 3.2, 4.8, 7.1])

def L(w, b):
    return np.mean((w * x + b - y) ** 2)

w, b = 1.0, 0.5
r = w * x + b - y
dw = 2 * np.mean(r * x)
db = 2 * np.mean(r)

h = 1e-6
print(f"dL/dw: formula {dw:.6f}, numeric {(L(w + h, b) - L(w - h, b)) / (2 * h):.6f}")
print(f"dL/db: formula {db:.6f}, numeric {(L(w, b + h) - L(w, b - h)) / (2 * h):.6f}")
dL/dw: formula -8.550000, numeric -8.550000
dL/db: formula -4.050000, numeric -4.050000

Both partials are negative, so increasing ww or bb would lower the loss: the line is currently too low and too flat.

Exercises

Exercise 1: First partials

Find fxf_x and fyf_y for f(x,y)=5x2y−3y3+7xf(x, y) = 5x^2y - 3y^3 + 7x.

Answer

fx=10xy+7f_x = 10xy + 7, fy=5x2−9y2f_y = 5x^2 - 9y^2.

Solution

Holding yy fixed, 5x2y5x^2y differentiates to 10xy10xy, −3y3-3y^3 is a constant, and 7x7x gives 7. Holding xx fixed, 5x2y5x^2y gives 5x25x^2, −3y3-3y^3 gives −9y2-9y^2, and 7x7x is a constant.

Exercise 2: Chain rule inside a partial

Find ∂g/∂y\partial g/\partial y for g(x,y)=ln⁡(x2+y2)g(x, y) = \ln(x^2 + y^2) and evaluate it at (1,2)(1, 2).

Answer

∂g/∂y=2y/(x2+y2)\partial g/\partial y = 2y / (x^2 + y^2), which is 4/5=0.84/5 = 0.8 at (1,2)(1, 2).

Solution
∂∂yln⁡(u)=1u⋅∂u∂y,u=x2+y2,∂u∂y=2y∂g∂y=2yx2+y2;at (1,2): 45\begin{aligned} &\frac{\partial}{\partial y} \ln(u) = \frac{1}{u} \cdot \frac{\partial u}{\partial y}, \qquad u = x^2 + y^2, \qquad \frac{\partial u}{\partial y} = 2y \\ &\frac{\partial g}{\partial y} = \frac{2y}{x^2 + y^2}; \qquad \text{at } (1, 2){:}\ \frac{4}{5} \end{aligned}

Exercise 3: Mixed partial

For f(x,y)=x⋅e3yf(x, y) = x \cdot e^{3y}, compute fxyf_{xy} and fyxf_{yx}.

Answer

Both equal 3e3y3e^{3y}.

Solution
fx=e3yfxy=3e3yfy=3xe3yfyx=3e3y\begin{aligned} f_x &= e^{3y} & f_{xy} &= 3e^{3y} \\ f_y &= 3x e^{3y} & f_{yx} &= 3e^{3y} \end{aligned}

Exercise 4: Error estimate

A cylinder has radius r=5 cmr = 5\ \text{cm} and height h=12 cmh = 12\ \text{cm}. Use the linear approximation to estimate the change in its volume V=πr2hV = \pi r^2 h if rr grows by 0.1 cm and hh shrinks by 0.2 cm, then compare with the exact change in code.

Answer

ΔV≈7π≈21.99 cm3\Delta V \approx 7\pi \approx 21.99\ \text{cm}^3; the exact change is about 21.73 cm321.73\ \text{cm}^3.

Solution
Vr=2πrh=120π,Vh=πr2=25πΔV≈120π⋅(0.1)+25π⋅(−0.2)=12π−5π=7π≈21.99\begin{aligned} V_r &= 2\pi rh = 120\pi, \qquad V_h = \pi r^2 = 25\pi \\ \Delta V &\approx 120\pi \cdot (0.1) + 25\pi \cdot (-0.2) = 12\pi - 5\pi = 7\pi \approx 21.99 \end{aligned}
python · try it yourself
import math
V = lambda r, h: math.pi * r**2 * h
print(round(7 * math.pi, 2), round(V(5.1, 11.8) - V(5, 12), 2))
21.99 21.73

Test yourself

When computing ∂f/∂x\partial f/\partial x, the other variables are treated as…

  1. zero
  2. constants
  3. functions of xx
  4. infinitely small

What is ∂/∂y\partial/\partial y of x2y3x^2y^3?

  1. 2xy32xy^3
  2. 3x2y23x^2y^2
  3. 6xy26xy^2
  4. x2x^2

Clairaut's theorem says that (for smooth functions)…

  1. fxx=fyyf_{xx} = f_{yy}
  2. fx=fyf_x = f_y
  3. fxy=fyxf_{xy} = f_{yx}
  4. all second partials are zero