Why This Matters
Most real-world functions are not simple polynomials -- they are compositions of simpler functions. The temperature at your location depends on the weather model, which depends on atmospheric pressure, which depends on altitude. Each layer is a function of the previous one. The chain rule tells you how to differentiate through these layers.
If you have a nested function like sin(x^2), the chain rule says: differentiate the outer function (sin becomes cos) evaluated at the inner function (x^2), then multiply by the derivative of the inner function (2x). The result is cos(x^2) * 2x. This "multiply the derivatives along the chain" pattern is why it is called the chain rule.
The chain rule is also the mathematical foundation of backpropagation in neural networks. When a neural network computes its output through dozens of layers, the gradient of the loss with respect to each weight is computed by chaining derivatives backward through the network. Without the chain rule, modern deep learning would not exist.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
The chain rule multiplies derivatives along the composition chain: outer derivative times inner derivative.
Code Example
// Numerical derivative helper
function deriv(f, x, h = 0.00001) {
return (f(x + h) - f(x - h)) / (2 * h);
}
// Chain rule: d/dx[f(g(x))] = f prime(g(x)) * g prime(x)
function chainRuleAt(f, g, x) {
const gx = g(x); // Evaluate inner function
const fPrimeAtG = deriv(f, gx); // Outer derivative at inner value
const gPrime = deriv(g, x); // Inner derivative
return fPrimeAtG * gPrime;
}
// Example 1: d/dx[sin(x^2)]
// f(u) = sin(u), g(x) = x^2
const result1 = chainRuleAt(Math.sin, x => x*x, 2);
console.log("d/dx[sin(x^2)] at x=2:", result1.toFixed(4));
// Exact: cos(4) * 4 = -2.6146
// Example 2: d/dx[(3x+1)^4]
// f(u) = u^4, g(x) = 3x+1
const result2 = chainRuleAt(u => Math.pow(u, 4), x => 3*x+1, 1);
console.log("d/dx[(3x+1)^4] at x=1:", result2.toFixed(4));
// Exact: 4*(3*1+1)^3 * 3 = 4*64*3 = 768
// Verify by direct numerical differentiation
const directCheck1 = deriv(x => Math.sin(x*x), 2);
console.log("Direct check sin(x^2):", directCheck1.toFixed(4));
const directCheck2 = deriv(x => Math.pow(3*x+1, 4), 1);
console.log("Direct check (3x+1)^4:", directCheck2.toFixed(4));
// Triple chain: d/dx[sin(cos(x^2))]
function tripleChain(x) {
const h1 = deriv(u => u*u, x); // 2x
const h2 = deriv(Math.cos, x*x); // -sin(x^2)
const h3 = deriv(Math.sin, Math.cos(x*x)); // cos(cos(x^2))
return h3 * h2 * h1;
}
console.log("\nTriple chain sin(cos(x^2)) at x=1:", tripleChain(1).toFixed(4));
const directTriple = deriv(x => Math.sin(Math.cos(x*x)), 1);
console.log("Direct check:", directTriple.toFixed(4));Interactive Experiment
Try these exercises:
- Compute d/dx[e^(x^2)] at x = 1 using the chain rule function. Verify with direct numerical differentiation.
- Try a three-layer composition: d/dx[ln(sin(x^2))]. Break it into three derivatives and multiply.
- What happens to the chain rule when the inner function is linear, like g(x) = 3x + 1? The chain rule just multiplies by the slope of the inner function.
- Explore d/dx[sqrt(1 + x^2)] using the chain rule. This function appears often in arc length calculations.
- Try computing the derivative of a ReLU-like function: max(0, x^2 - 1). Where does the derivative not exist?
Quick Quiz
Coding Challenge
Write a function called `verifyChainRule` that takes an outer function f, an inner function g, and a value x. It should compute the chain rule derivative (f prime at g(x) times g prime at x) using numerical derivatives with h = 0.00001. Also compute the direct numerical derivative of the composite f(g(x)). Return true if the two results agree within a tolerance of 0.01, false otherwise.
Real-World Usage
The chain rule is the workhorse of applied calculus:
- Deep learning (backpropagation): The chain rule is applied layer by layer to compute how each weight in a neural network affects the final loss. This is the mathematical core of training AI models.
- Sensitivity analysis: In engineering, the chain rule quantifies how uncertainty in one variable propagates through a chain of dependent calculations.
- Robotics: Forward and inverse kinematics use the chain rule to relate joint angles to end-effector position through a chain of transformations.
- Computational finance: The chain rule propagates risk through derivative pricing models with nested dependencies.
- Climate modeling: Temperature depends on CO2 concentration, which depends on emissions, which depends on policy. The chain rule computes the sensitivity of temperature to policy changes.