The chain rule is a fundamental concept in calculus that helps us find the derivative of a composite function — that is, a function made up of one function applied to the result of another. In simpler terms, if you have a function inside another function, the chain rule tells us how to compute its rate of change.
Example:
Imagine you have a function f(g(x)). Here, g(x) is the "inner" function, and f is the "outer" function. To find the derivative of f(g(x)) with respect to x, the chain rule says:
First, find the derivative of the outer functionf with respect to its input (which is g(x)).
Then, multiply it by the derivative of the inner functiong(x) with respect to x.
In plain English:
The rate of change of the whole thing depends on how fast the outer function changes and how fast the inner function changes.
Mathematically, the chain rule is expressed as:
dxdf(g(x))=f
Simple Python Implementation
Let’s implement the chain rule in Python using basic numerical differentiation. We’ll approximate derivatives using small changes (Δx).
python
def derivative(f, x, delta_x=1e-5):
"""Approximates the derivative of function f at point x."""
return (f(x + delta_x) - f(x)) / delta_x
def chain_rule(f, g, x, delta_x=1e-5):
"""
Approximates the derivative of f(g(x)) using the chain rule.
Parameters:
f: Outer function (takes one argument).
g: Inner function (takes one argument).
x: Point at which to evaluate the derivative.
delta_x: Small value for numerical differentiation.
Returns:
Derivative of f(g(x)) at x using the chain rule.
"""
# Derivative of the outer function f, evaluated at g(x)
df_dg = derivative(f, g(x), delta_x)
# Derivative of the inner function g, evaluated at x
dg_dx = derivative(g, x, delta_x)
# Apply the chain rule: df/dx = df/dg * dg/dx
return df_dg * dg_dx
# Example usage
if __name__ == "__main__":
# Define the inner and outer functions
g = lambda x: x**2 # Inner function: g(x) = x^2
f = lambda u: u**3 # Outer function: f(u) = u^3
# Evaluate the derivative of f(g(x)) at x = 2
x = 2
result = chain_rule(f, g, x)
print(f"The derivative of f(g(x)) at x = {x} is approximately {result:.6f}")
How It Works in the Code:
derivative Function: This approximates the derivative of any function f(x) at a point x using the formula:
f′(x)
When you run the code, it will output something like:
code
The derivative of f(g(x)) at x = 2 is approximately 192.000000
Final Answer:
The chain rule computes the derivative of a composite function by multiplying the derivative of the outer function (evaluated at the inner function) by the derivative of the inner function. The provided Python implementation demonstrates this numerically.