Derivatives
How a function's output responds to a change in its input: instantaneous rate of change, differentiation rules, and partial derivatives.
Vectors and matrices describe objects and transformations. But many problems come down to a more basic question: if an input changes by a tiny amount, how does the output respond? The tool for measuring that response is the derivative.
The Derivative
The derivative of a function at a point is the instantaneous rate of change. Increase by a small amount ; the derivative captures how much changes in response, as shrinks to zero:
Geometrically, is the slope of the tangent line to the curve at . A large positive value means the function is rising steeply. Zero means a flat region, typically a minimum or maximum. Negative means the function is falling.
Take , whose derivative is :
- At : slope , the function is rising.
- At : slope , the bottom of the parabola.
- At : slope , the function is falling as increases.
Two notations appear interchangeably. Lagrange notation writes . Leibniz notation writes , which reads as "the rate of change of with respect to ." Both mean the same thing; you will encounter both in papers and code.
Differentiation Rules
Computing derivatives from the limit definition every time would be impractical. A handful of rules covers the vast majority of cases:
- Power:
- Constant:
- Constant multiple:
- Sum:
- Product:
- Chain:
The chain rule handles composed functions: apply , then apply to the result. Its derivative is the product of the two individual derivatives, evaluated at the right point. It is fundamental enough to have its own article.
A few examples using these rules:
The last one uses the chain rule: , .
Functions of Multiple Variables
Some functions take more than one input. Consider . How does respond when changes? When changes?
Each question has its own answer: a partial derivative. To compute , treat as a fixed constant and differentiate with respect to alone:
To compute , treat as a fixed constant:
The symbol signals that other variables are held fixed. Each partial derivative is a standard one-variable derivative in disguise — the slope along one axis of a multi-dimensional surface, with every other axis frozen.
At the point :
Nudging by a small amount has a larger effect than nudging by the same amount, at that specific point.
In Code
The limit definition translates directly into a numerical approximation. Using two nearby points (centered difference) instead of a one-sided limit gives better accuracy for the same :
def deriv(f, x, eps=1e-5):
return (f(x + eps) - f(x - eps)) / (2 * eps)
f = lambda x: x ** 2
print(deriv(f, 1.0)) # 2.0 — f'(x) = 2x, so f'(1) = 2
print(deriv(f, 3.0)) # 6.0
# Partial derivatives of g(x, y) = x² + 3xy, evaluated at (2, 1)
g = lambda x, y: x**2 + 3*x*y
print(deriv(lambda x: g(x, 1.0), 2.0)) # 7.0 — ∂g/∂x = 2x + 3y
print(deriv(lambda y: g(2.0, y), 1.0)) # 6.0 — ∂g/∂y = 3x
For a function of inputs, there are partial derivatives: one for each axis of the space. Listing them all in order turns them into a vector, and that vector has a precise geometric meaning of its own. That is the subject of the next article.