Math for AILoading progress…

Products and quotients

Calculate slopes, use the chain rule and distinguish derivatives from partial derivatives.

Both factors move

Suppose a rectangle has width u(x) and height v(x), and both depend on x. Its area is p = u·v. When x grows by h the width grows by about u′·h and the height by about v′·h. Does the area grow by (u′h)·(v′h)? That is the tempting guess "derivative of a product is the product of derivatives". The picture shows why it is wrong.

The new area splits into the old rectangle, a strip on the right (u′h by v), a strip on top (u by v′h) and a tiny corner (u′h by v′h). Divide the change by h. The strips give v·u′ and u·v′. The corner gives u′v′·h, which melts to 0. So (u·v)′ = u′v + u·v′.

2 × 2.5 = 5u = 2v = 2.5
u = x + 1, v = x/2 + 22, 2.5
blue strip u′·h·v1.2500
orange strip u·v′·h0.5000
corner u′h · v′h0.1250
change in area1.8750
change / h3.7500
product rule u v′ + v u′3.5000
wrong rule u′ · v′0.5000

Divide the change by h and let h shrink: the two strips give u·v′ + v·u′, and the corner (which carries h² in it) disappears.

Here u = x + 1 and v = x/2 + 2. Shrink h and watch the purple corner vanish while change / h approaches the product-rule value.

Predict first. For p(x) = x·x, the product rule gives x + x = 2x. At x = 3, which number is p′(3)?

f = lambda x: x * x
h = 1e-6
print((f(3 + h) - f(3 - h)) / (2 * h))
print(1 * 1)
# output:
6.000000000838668
1

Quotients: the denominator moves too

For u / v both parts change, and a growing denominator shrinks the whole fraction. The rule is (u / v)′ = (u′v − u·v′) / v². Memorise the order: derivative of the top times the bottom, minus the top times derivative of the bottom, over the bottom squared. Test it with real numbers: q(x) = x / (x² + 1), so u = x and v = x² + 1.

q(x) = x / (x² + 1) at x = 2value
q(x)0.40000
central difference, h = 1e-5-0.12000
quotient rule (u′v − uv′) / v²-0.12000
wrong rule u′ / v′ = 1 / 2x0.25000

The first two agree at every x. The wrong rule does not, except by accident.

Pick x and compare the three bottom numbers: the rule agrees with the numerical slope at every x; the wrong rule u′ / v′ does not.

At x = 1: (1·2 − 1·2) / 2² = 0, the top of the bump. At x = 2: (1·5 − 2·4) / 5² = -0.12, a gently falling curve. The wrong rule would claim 1 / (2·2) = 0.25 at x = 2, a slope with the wrong sign.

Common mistakes

  • Multiplying the derivatives: (u·v)′ ≠ u′·v′. At the single point x = −2 in the picture above both happen to equal 0.5, which is an accident of that particular pair of functions.
  • Swapping the subtraction in the quotient rule: u′v − uv′ is not the same as uv′ − u′v. The sign flips.
  • Dividing where v = 0: the quotient is undefined there, and so is its derivative.
Chapter context

A derivative measures a local rate of change. It describes what a function does under a very small input change, rather than its average behavior over an arbitrary interval. This local sensitivity is what training algorithms use.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

f′(x)=lim⁡h→0f(x+h)−f(x)h.f'(x)=\lim_{h\to0}\frac{f(x+h)-f(x)}h. ddxf(g(x))=f′(g(x))g′(x),∂∂x(x2+3xy)=2x+3y.\frac{d}{dx}f(g(x))=f'(g(x))g'(x),\quad \frac{\partial}{\partial x}(x^2+3xy)=2x+3y.

For numerical checking, use (f(x+h)−f(x−h))/(2h)(f(x+h)-f(x-h))/(2h) and report a tolerance.

Worked example

Let f(x)=(3x+1)^2. At x=2, the inner value is 7, its derivative is 3, and the outer derivative is 14. Multiplying gives 42. A central difference with h=0.00001 gives approximately the same answer, within numerical error.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
f = lambda x: (3*x + 1)**2
x, h = 2, 1e-5
analytic = 6*(3*x + 1)
numeric = (f(x+h)-f(x-h))/(2*h)
assert analytic == 42
assert isclose(numeric, analytic, rel_tol=1e-7)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

A function value is not its derivative. The chain rule multiplies sensitivities, not intermediate values. At a corner such as abs(x) at zero, a two-sided derivative does not exist.