19. The product and quotient rules

📖 Reading · 14 min
💡 Every code box below is live — edit it and hit Run.

Differentiation is linear, so it passes cleanly through sums. It does not pass through products, and the last lesson's one-line refutation — \frac{d}{dx}(x \cdot x) = 2x \neq 1\cdot1 — settles that.

Here is what actually happens, and why the answer has the shape it does.

The product rule

\frac{d}{dx}\left[f(x)g(x)\right] = f'(x)g(x) + f(x)g'(x)

"First times the derivative of the second, plus the second times the derivative of the first" — or in Leibniz form, (uv)' = u'v + uv'.

Proof. The difference quotient is

\frac{f(x+h)g(x+h) - f(x)g(x)}{h}

There's nothing to cancel, so manufacture something: add and subtract f(x+h)g(x), a term worth zero in total.

= \frac{f(x+h)g(x+h) - f(x+h)g(x) + f(x+h)g(x) - f(x)g(x)}{h}

Now group the first two and last two:

= f(x+h)\cdot\frac{g(x+h)-g(x)}{h} + g(x)\cdot\frac{f(x+h)-f(x)}{h}

Take h \to 0. The two fractions become g'(x) and f'(x). And f(x+h) \to f(x) because f is continuous — which we know from §2.1, since f is differentiable. So

= f(x)g'(x) + g(x)f'(x) \qquad\blacksquare

That trick — add and subtract a hybrid term — is worth remembering. It reappears in the proof of the multivariable chain rule in §10.4.

Why two terms

The algebra is convincing but the reason is geometric, and it's the reason the rule generalises.

Picture a rectangle with sides u and v; its area is uv. Grow both sides slightly, by du and dv. The new area exceeds the old by three pieces:

  • a strip along the top: u\,dv
  • a strip along the side: v\,du
  • a tiny corner square: du\,dv

\Delta(uv) = u\,dv + v\,du + du\,dv

Divide by dx and let everything shrink. The two strips survive as u\frac{dv}{dx} + v\frac{du}{dx}. The corner is second order — a product of two small things — so it vanishes relative to the others.

Two terms because there are two sides to grow. With three factors there are three strips:

(uvw)' = u'vw + uv'w + uvw'

and in general each factor takes a turn being differentiated. That's exactly the pattern the chain rule generalises in §14.3, where backpropagation is this rule applied a few million times.

The quotient rule

\frac{d}{dx}\left[\frac{f(x)}{g(x)}\right] = \frac{f'(x)g(x) - f(x)g'(x)}{[g(x)]^2}

"Bottom times the derivative of the top, minus top times the derivative of the bottom, all over bottom squared." Some people chant "low d-high minus high d-low, over low squared"; use whatever sticks.

The order matters. Unlike the product rule, this one has a minus sign, so swapping the terms gives the wrong answer with the wrong sign. That is the single most common error with this rule.

Two sanity checks you can run in your head:

  • If f is constant, the rule should give \frac{-fg'}{g^2} — and it does, since f' = 0. Check against \frac{d}{dx}\frac1x = -\frac{1}{x^2}: here f=1, g=x, giving \frac{0 - 1}{x^2} ✓.
  • The denominator is always g^2, hence always positive. So the sign of the derivative is decided entirely by the numerator.

Deriving one from the other

You don't strictly need both. Writing \frac fg = f \cdot g^{-1} and using the product rule plus the chain rule (next lesson):

\left(fg^{-1}\right)' = f'g^{-1} + f\cdot(-g^{-2}g') = \frac{f'}{g} - \frac{fg'}{g^2} = \frac{f'g - fg'}{g^2}

Same rule. Many people find the product-plus-chain route less error-prone than memorising the quotient rule, and it has the advantage of failing loudly rather than silently when you misremember.

The power rule for negative exponents

The quotient rule completes part of §2.2's unfinished business. For a positive integer n:

\frac{d}{dx}x^{-n} = \frac{d}{dx}\frac{1}{x^n} = \frac{0\cdot x^n - 1\cdot nx^{n-1}}{x^{2n}} = \frac{-nx^{n-1}}{x^{2n}} = -nx^{-n-1}

which is exactly nx^{n-1} with n replaced by -n. The power rule now covers all integers. Rational exponents arrive in §2.9, irrational ones in §2.6.

When not to use these rules

Both rules are more work than the alternative when the algebra simplifies first.

\frac{d}{dx}\left[x^2(x^3+1)\right]

Product rule: 2x(x^3+1) + x^2(3x^2) = 2x^4 + 2x + 3x^4 = 5x^4 + 2x.

Expand first: x^5 + x^2, derivative 5x^4 + 2x. Same answer, half the work.

\frac{d}{dx}\frac{x^3 + x}{x}

Quotient rule works and takes four lines. Dividing first gives x^2 + 1, whose derivative is 2x. Instantly.

Look for simplification before reaching for a rule. This was true in §2.2 and it stays true for the rest of the course.

Doing it in Python

Both rules against numerical truth:

def numerical(f, x, h=1e-6):
    return (f(x + h) - f(x - h)) / (2 * h)

f = lambda x: x**2 + 1
g = lambda x: x**3 - x
fp = lambda x: 2 * x
gp = lambda x: 3 * x**2 - 1

x = 1.7
product_rule = fp(x) * g(x) + f(x) * gp(x)
quotient_rule = (fp(x) * g(x) - f(x) * gp(x)) / g(x)**2

print(f"d/dx [f*g]  rule {product_rule:>12.8f}   numeric {numerical(lambda t: f(t)*g(t), x):>12.8f}")
print(f"d/dx [f/g]  rule {quotient_rule:>12.8f}   numeric {numerical(lambda t: f(t)/g(t), x):>12.8f}")
print(f"\nthe naive f'*g' would give {fp(x) * gp(x):.8f} -- nowhere near")

The rectangle picture, with the corner term shrinking out of relevance:

u, v = 3.0, 5.0

print(f"{'du=dv':>10} {'u*dv':>12} {'v*du':>12} {'du*dv':>12} {'corner share':>14}")
for k in range(1, 7):
    d = 10.0 ** -k
    strips = u * d + v * d
    corner = d * d
    print(f"{d:>10.0e} {u*d:>12.6f} {v*d:>12.6f} {corner:>12.2e} "
          f"{corner/(strips + corner):>13.4%}")

print("\nthe corner's share of the growth vanishes -- which is why it drops out")

Three factors, three terms:

import sympy as sp

x = sp.Symbol('x')
u, v, w = sp.sin(x), x**2, sp.exp(x)

direct = sp.diff(u * v * w, x)
by_rule = sp.diff(u, x)*v*w + u*sp.diff(v, x)*w + u*v*sp.diff(w, x)

print(f"sympy      : {sp.simplify(direct)}")
print(f"three-term : {sp.simplify(by_rule)}")
print(f"identical  : {sp.simplify(direct - by_rule) == 0}")

And the quotient rule with its terms swapped — the classic error, priced:

def numerical(f, x, h=1e-6):
    return (f(x + h) - f(x - h)) / (2 * h)

f, fp = lambda x: x**2, lambda x: 2 * x
g, gp = lambda x: x + 1, lambda x: 1

x = 2.0
right = (fp(x) * g(x) - f(x) * gp(x)) / g(x)**2
wrong = (f(x) * gp(x) - fp(x) * g(x)) / g(x)**2

print(f"correct order : {right:.8f}")
print(f"swapped order : {wrong:.8f}")
print(f"numerical     : {numerical(lambda t: f(t)/g(t), x):.8f}")
print("\nswapping flips the sign. it is always exactly wrong, never approximately.")

Worked example

Differentiate y = \dfrac{x^2\sin x}{x+1}.

Two rules, nested. Handle the numerator as a product first, then feed it to the quotient rule.

Numerator. With u = x^2 and v = \sin x (using $\frac{d}{dx}\sin x = \cos x$, proved in §2.5):

\frac{d}{dx}\left[x^2\sin x\right] = 2x\sin x + x^2\cos x

Whole thing. Quotient rule with top = x^2\sin x, bottom = x+1:

y' = \frac{(2x\sin x + x^2\cos x)(x+1) - (x^2\sin x)(1)}{(x+1)^2}

Expand the numerator:

= \frac{2x^2\sin x + 2x\sin x + x^3\cos x + x^2\cos x - x^2\sin x}{(x+1)^2}

= \frac{x^2\sin x + 2x\sin x + x^3\cos x + x^2\cos x}{(x+1)^2}

Factoring an x out tidies it:

y' = \frac{x\left[(x+2)\sin x + x(x+1)\cos x\right]}{(x+1)^2}

Check it at a point. At x = 0: the formula gives 0. And $y = \frac{x^2\sin x}{x+1}$ is roughly x^3 near 0 (since \sin x \approx x), which has zero slope at the origin ✓.

The lesson in the layering: differentiate from the outside in, and name the pieces. Writing "top = x^2\sin x" before applying the quotient rule is what keeps the bookkeeping straight when rules nest.

Your turn

1. \dfrac{d}{dx}\left[(3x^2+1)(x^3-2x)\right] — two ways.

2. \dfrac{d}{dx}\dfrac{2x+1}{x^2+3}

3. \dfrac{d}{dx}\dfrac{x^2 - 4}{x - 2} — think before computing.

4. If f(2) = 3, f'(2) = -1, g(2) = 5, g'(2) = 4, find (fg)'(2) and (f/g)'(2).

Solutions

1. Product rule.

6x(x^3-2x) + (3x^2+1)(3x^2-2) = 6x^4 - 12x^2 + 9x^4 - 6x^2 + 3x^2 - 2

= \boxed{15x^4 - 15x^2 - 2}

Expand first. (3x^2+1)(x^3-2x) = 3x^5 - 6x^3 + x^3 - 2x = 3x^5 - 5x^3 - 2x, so the derivative is 15x^4 - 15x^2 - 2 ✓.

Same answer; expanding was faster here, and for polynomial products it usually is.

2. Quotient rule with top = 2x+1, bottom = x^2+3:

\frac{2(x^2+3) - (2x+1)(2x)}{(x^2+3)^2} = \frac{2x^2 + 6 - 4x^2 - 2x}{(x^2+3)^2} = \boxed{\frac{-2x^2 - 2x + 6}{(x^2+3)^2}}

3. Don't. Factor:

\frac{x^2-4}{x-2} = \frac{(x-2)(x+2)}{x-2} = x+2 \qquad (x \neq 2)

so the derivative is \boxed{1}.

The quotient rule gives the same thing after considerably more algebra, and it also obscures the important fact that this function is a line with a hole in it (§1.0). Simplifying first is not laziness — it reveals what the function actually is.

4. Straight substitution into the rules.

(fg)'(2) = f'(2)g(2) + f(2)g'(2) = (-1)(5) + (3)(4) = -5 + 12 = \boxed{7}

\left(\frac fg\right)'(2) = \frac{f'(2)g(2) - f(2)g'(2)}{[g(2)]^2} = \frac{(-1)(5) - (3)(4)}{25} = \frac{-17}{25} = \boxed{-0.68}

Note you never needed formulas for f and g — the rules only ever consume values and derivatives at the point. That's exactly how automatic differentiation works in §14.4.

Check yourself in code

Verify both rules against numerical differentiation.

With f(x) = x^2+1 and g(x) = x^3-x, evaluate at x = 1.7: the product rule, the quotient rule, the naive (wrong) f'g', and central-difference numerics for fg and f/g with h = 10^{-6}. Print each to 8 decimals.

Print exactly this:

product rule   40.76050000
product numeric 40.76050000
naive f'*g'    26.07800000
quotient rule  -1.83196978
quotient numeric -1.83196978
def numerical(fn, x, h=1e-6):
    return (fn(x + h) - fn(x - h)) / (2 * h)

f = lambda x: x**2 + 1
g = lambda x: x**3 - x
fp = lambda x: 2 * x
gp = lambda x: 3 * x**2 - 1
x = 1.7

# product rule, its numerical check, the naive version, then the quotient pair
print(f"product rule   ...")
def numerical(fn, x, h=1e-6):
    return (fn(x + h) - fn(x - h)) / (2 * h)

f = lambda x: x**2 + 1
g = lambda x: x**3 - x
fp = lambda x: 2 * x
gp = lambda x: 3 * x**2 - 1
x = 1.7

print(f"product rule   {fp(x)*g(x) + f(x)*gp(x):.8f}")
print(f"product numeric {numerical(lambda t: f(t)*g(t), x):.8f}")
print(f"naive f'*g'    {fp(x)*gp(x):.8f}")
print(f"quotient rule  {(fp(x)*g(x) - f(x)*gp(x)) / g(x)**2:.8f}")
print(f"quotient numeric {numerical(lambda t: f(t)/g(t), x):.8f}")

(uv)' = u'v + uv' — two terms because a rectangle has two sides to grow, and the corner is second order and drops out. (u/v)' = \frac{u'v - uv'}{v^2}, where the minus sign's order is the thing to get right, and which you can always re-derive as u \cdot v^{-1} if you don't trust your memory. The quotient rule extends the power rule to negative integers. And before either rule, check whether expanding or dividing makes the problem disappear.

Next: the rule that makes everything else composable — and the one you'll use more than all the others combined.