Sure! Let's break down the chain rule in plain English, and then look at it in Python with a simple example.
🌱 What is the Chain Rule? (Plain English)
The chain rule is a rule in calculus for finding the derivative (rate of change) of a function inside another function.
Imagine you have a machine inside another machine:
The inner machine takes an input x and turns it into u = g(x)
The outer machine takes u and turns it into f(u)
Together, they make a new machine:
👉 y = f(g(x))
Now, to find the rate at which y changes with respect to x, we can't just look at the outer machine alone or the inner one alone — we need both!
🔗 Chain Rule Formula
When you have y = f(g(x)), then the derivative is:
📘 dy/dx = f'(g(x)) * g'(x)
Think of it like:
How fast the outer function changes × how fast the inner function changes
🧪 Example
Let’s say:
python
g(x) = 2x # inner function
f(u) = u ** 2 # outer function; u = g(x)
So the full function is:
python
y = f(g(x)) = (2x)^2 = 4x^2
Now apply the chain rule to find the derivative:
g(x) = 2x → g'(x) = 2
f(u) = u² → f'(u) = 2u → f'(g(x)) = 2*(2x) = 4x
Then:
dy/dx = f'(g(x)) * g'(x) = 4x * 2 = 8x
🐍 Simple Python Implementation
Let’s write this example in Python, demonstrating the derivative using the chain rule:
python
def g(x):
return 2 * x # inner function
def g_prime(x):
return 2 # derivative of g
def f(u):
return u ** 2 # outer function
def f_prime(u):
return 2 * u # derivative of f
def chain_rule_derivative(x):
u = g(x)
return f_prime(u) * g_prime(x)
# Test at x = 3
x_val = 3
print("Derivative at x = 3:", chain_rule_derivative(x_val)) # Should output 24
Expected Output:
code
Derivative at x = 3: 24
Because:
g(3) = 6
f'(g(3)) = f'(6) = 2 × 6 = 12
g'(3) = 2
12 × 2 = 24
✅ So, the chain rule correctly gives dy/dx = 8x, evaluated at x = 3 → 8 × 3 = 24
✅ Summary
The chain rule helps differentiate nested functions.
You multiply the derivative of the outer function evaluated at the inner function by the derivative of the inner function.
Python can model this process simply using function composition.
Let me know if you’d like a visual explanation or examples with more complex functions!