Chapter 4: Visualizing the Chain Rule and Product Rule

Derivatives of Composite Functions

So far: derivatives of single functions
Now: derivatives of combinations of functions

Two Essential Rules

  1. Product Rule: d(f·g)/dx

  2. Chain Rule: d(f(g(x)))/dx

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style('whitegrid')
plt.rcParams['figure.figsize'] = (14, 10)

1. The Product Rule

If h(x) = f(x) · g(x), what is h’(x)?

Wrong answer: f’(x) · g’(x) ❌

Correct answer: $\( \frac{d}{dx}[f(x) \cdot g(x)] = f'(x) \cdot g(x) + f(x) \cdot g'(x) \)$

Geometric Intuition

Think of f(x) and g(x) as sides of a rectangle.
Area = f(x) · g(x)

When both change slightly:

  • One strip: f·dg

  • Other strip: df·g

  • Corner: df·dg (negligible)

Total change ≈ f·dg + df·g

def visualize_product_rule():
    """Show geometric meaning of product rule."""
    
    fig, ax = plt.subplots(1, 1, figsize=(10, 10))
    
    f, g = 4, 3
    df, dg = 0.6, 0.5
    
    # Original rectangle
    rect = plt.Rectangle((0, 0), f, g, fill=True,
                        facecolor='lightblue', edgecolor='blue',
                        linewidth=2, alpha=0.7, label='Original: f·g')
    ax.add_patch(rect)
    ax.text(f/2, g/2, 'f·g', ha='center', va='center',
           fontsize=18, fontweight='bold')
    
    # Right strip (f · dg)
    strip1 = plt.Rectangle((0, g), f, dg, fill=True,
                          facecolor='lightgreen', edgecolor='green',
                          linewidth=2, alpha=0.7)
    ax.add_patch(strip1)
    ax.text(f/2, g + dg/2, 'f·dg', ha='center', va='center',
           fontsize=14, fontweight='bold')
    
    # Top strip (df · g)
    strip2 = plt.Rectangle((f, 0), df, g, fill=True,
                          facecolor='lightcoral', edgecolor='red',
                          linewidth=2, alpha=0.7)
    ax.add_patch(strip2)
    ax.text(f + df/2, g/2, 'df·g', ha='center', va='center',
           fontsize=14, fontweight='bold', rotation=90)
    
    # Corner (df · dg) - negligible
    corner = plt.Rectangle((f, g), df, dg, fill=True,
                          facecolor='yellow', edgecolor='orange',
                          linewidth=2, alpha=0.7)
    ax.add_patch(corner)
    ax.text(f + df/2, g + dg/2, 'df·dg\n(tiny!)', ha='center', va='center',
           fontsize=10)
    
    # Dimensions
    ax.plot([0, f], [-0.3, -0.3], 'k-', linewidth=2)
    ax.text(f/2, -0.6, f'f = {f}', ha='center', fontsize=14, fontweight='bold')
    
    ax.plot([f, f+df], [-0.3, -0.3], 'k-', linewidth=2)
    ax.text(f + df/2, -0.6, f'df = {df}', ha='center', fontsize=12)
    
    ax.plot([-0.3, -0.3], [0, g], 'k-', linewidth=2)
    ax.text(-0.6, g/2, f'g = {g}', va='center', fontsize=14, fontweight='bold')
    
    ax.plot([-0.3, -0.3], [g, g+dg], 'k-', linewidth=2)
    ax.text(-0.6, g + dg/2, f'dg = {dg}', va='center', fontsize=12)
    
    ax.set_xlim(-1, f + df + 0.5)
    ax.set_ylim(-1, g + dg + 0.5)
    ax.set_aspect('equal')
    ax.grid(True, alpha=0.3)
    ax.set_title('Product Rule Geometric Proof', fontsize=16, fontweight='bold')
    
    plt.tight_layout()
    plt.show()
    
    print("Product Rule: d(f·g)/dx = f·dg/dx + df/dx·g\n")
    print(f"Change in area:")
    print(f"  Green strip: f·dg = {f}·{dg} = {f*dg:.2f}")
    print(f"  Red strip: df·g = {df}·{g} = {df*g:.2f}")
    print(f"  Total (ignoring corner): {f*dg + df*g:.2f}")

visualize_product_rule()

2. The Chain Rule

For composite functions h(x) = f(g(x)):

\[ \frac{dh}{dx} = \frac{df}{dg} \cdot \frac{dg}{dx} \]

Example: h(x) = (2x + 1)³

Let g(x) = 2x + 1, f(g) = g³

Then:

  • dg/dx = 2

  • df/dg = 3g² = 3(2x+1)²

  • dh/dx = 3(2x+1)² · 2 = 6(2x+1)²

def demonstrate_chain_rule():
    """Show chain rule with examples."""
    
    print("Chain Rule: d/dx[f(g(x))] = f'(g(x)) · g'(x)\n")
    print("=" * 60)
    
    print("\nExample 1: h(x) = (2x + 1)³")
    print("  Inner function: g(x) = 2x + 1")
    print("  Outer function: f(g) = g³")
    print("  g'(x) = 2")
    print("  f'(g) = 3g² = 3(2x+1)²")
    print("  h'(x) = 3(2x+1)² · 2 = 6(2x+1)²")
    
    print("\nExample 2: h(x) = sin(x²)")
    print("  Inner: g(x) = x²")
    print("  Outer: f(g) = sin(g)")
    print("  g'(x) = 2x")
    print("  f'(g) = cos(g) = cos(x²)")
    print("  h'(x) = cos(x²) · 2x = 2x·cos(x²)")
    
    print("\n" + "=" * 60)

demonstrate_chain_rule()

Summary

Product Rule

\[ \boxed{\frac{d}{dx}[f \cdot g] = f' \cdot g + f \cdot g'} \]

Think: “derivative of first times second, plus first times derivative of second”

Chain Rule

\[ \boxed{\frac{d}{dx}[f(g(x))] = f'(g(x)) \cdot g'(x)} \]

Think: “derivative of outer evaluated at inner, times derivative of inner”

Next: Exponential functions!