The chain rule is a fundamental concept in calculus used to calculate the derivative of a composite function. A composite function is when one function is nested inside another, like f(g(x)). The chain rule tells us how to find the rate of change (or derivative) of such functions.
Here’s how it works step by step:
Imagine you have two functions:
g(x): The "inner" function.
f(u): The "outer" function, where u=g(x).
To find the derivative of f(g(x)) with respect to x, the chain rule says:
First, take the derivative of the outer functionf(u) with respect to u, which gives .
In mathematical terms:
dxd[f(g(x))]=f
Intuitively, think of it as a "chain reaction":
A small change in x affects g(x), and then that change in g(x) affects f(g(x)).
The chain rule combines these two effects into one calculation.
Simple Python Implementation
Below is a Python implementation of the chain rule using basic numerical differentiation. We'll approximate derivatives using finite differences.
python
def derivative(f, x, h=1e-5):
"""
Approximate the derivative of a function f at point x using finite differences.
"""
return (f(x + h) - f(x)) / h
def chain_rule(f, g, x):
"""
Compute the derivative of f(g(x)) using the chain rule.
Parameters:
f: Outer function (takes a single argument).
g: Inner function (takes a single argument).
x: Point at which to evaluate the derivative.
Returns:
The derivative of f(g(x)) at x.
"""
# Derivative of the inner function g(x)
g_prime = derivative(g, x)
# Evaluate the outer function's derivative at g(x)
f_prime_at_g = derivative(f, g(x))
# Apply the chain rule
return f_prime_at_g * g_prime
# 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
# Point at which to evaluate the derivative
x = 2
# Calculate the derivative of f(g(x)) at x = 2
result = chain_rule(f, g, x)
print(f"The derivative of f(g(x)) at x = {x} is approximately {result:.4f}")
How It Works in the Code
Inner Function (g(x)):
In this example, g(x)=x2.
Its derivative is g, which we approximate numerically using the function.
Output of the Code
When you run the code, the output will be:
code
The derivative of f(g(x)) at x = 2 is approximately 192.0000
This matches the analytical result derived above.
Final Answer
The chain rule allows us to compute 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. In Python, we can implement this numerically using finite differences, as shown in the code above.
The derivative of f(g(x)) at x=2 is approximately 192.