# derivatives

> the slope at a point: how fast something changes right now.

source: https://learning.anthnydas.com/math/derivatives/

## tl;dr

the slope between two points of a curve is a secant. slide the second point onto the first and the secant becomes the tangent. its slope is the derivative, how fast f is changing right there.

- **the secant:** (f(x + h) − f(x)) / h (rise over run between two points. as h → 0 it becomes f'(x))
- **power rule:** (xⁿ)' = n xⁿ⁻¹ (x² has slope 2x, and x³ has 3x²)
- **sums and constants:** (f + g)' = f' + g' (a constant has slope 0, and c·f has slope c·f')
- **product rule:** (fg)' = f'g + fg' (not f'·g')
- **chain rule:** f(g(x))' = f'(g(x)) · g'(x) (the inner slope multiplies. sin 2x has slope 2 cos 2x)
- **sin, eˣ, ln x:** cos x, eˣ, 1/x (eˣ is its own slope)
- **f' = 0:** a high point, a low point, or neither (x³ is flat at 0 and keeps climbing)
- **reach for it:** velocity from position, and acceleration from velocity, finding where something is biggest or smallest, replacing a curve by a line near a point, gradient descent, physics, and the fade curves in noise

## the idea

a straight line has one slope: rise over run, the same between any two of its points. a curve doesn't. on $y = x^2$ the slope from $(1, 1)$ to $(2, 4)$ is $3$, and from $(1, 1)$ to $(1.5, 2.25)$ it's $2.5$. a line through two points of a curve is a secant, and its slope is the average rate of change over that stretch.

the derivative asks a sharper question: how fast is $f$ changing at one point? slide the second point toward the first. the secant swings, its slope settles down, and in the limit the line only touches the curve: the tangent. its slope is the derivative of $f$ at that point, written $f'(x)$ or $\frac{dy}{dx}$.

> **interactive figure:** an animation on the graph of y = x² from x = −1.5 to 3.5 and y = −0.5 to 4.5: a line through (1, 1) and a second point (1 + h, (1 + h)²), with the run h and the rise drawn under it. h shrinks from 1 to 0.5, 0.1 and 0.01, the line swings as the second point slides in, and a table under the plot collects the slopes: 3, 2.5, 2.1, 2.01, closing in on 2. then the two points meet, the secant becomes the tangent, and its slope is 2: the derivative of x² at 1.

the curve $y = x^2$ and the point $(1, 1)$ on it. a curve has no single slope, but a line through two of its points does. take a second point one step to the right, $(2, 4)$. the line through both is a secant, and its slope is rise over run: $(4 - 1) / (2 - 1) = 3$.

slide the second point halfway in, to $h = 0.5$. now it's $(1.5, 2.25)$, and the secant's slope is $1.25 / 0.5 = 2.5$. the line swings as the point moves.

closer. at $h = 0.1$ the second point is $(1.1, 1.21)$, and the slope is $0.21 / 0.1 = 2.1$.

at $h = 0.01$ the two points are too close to tell apart on screen, but the quotient still has an answer: $0.0201 / 0.01 = 2.01$. at $h = 0.001$ it's $2.001$. the slopes are closing in on 2.

no $h > 0$ reaches 2, but 2 is where the slopes are headed: the limit. there the secant is the tangent, and its slope is the derivative of $x^2$ at 1: $f'(1) = 2$. for any $h$ the secant's slope is $((1 + h)^2 - 1) / h = 2 + h$, and $2 + h \to 2$ as $h \to 0$.

a derivative is a limit, the subject of the limits page. $h$ never gets to $0$, since the quotient would read $0/0$, but the slopes head somewhere, and that somewhere is $f'(x)$.

## explore

the derivative is a function of its own: a slope at every $x$. pick a curve and drag the point. the tangent follows, and the graph of $f'$ below records its slope. where $f'$ is above the axis the curve climbs, where it's below the curve falls, and where $f'$ crosses zero the tangent is flat.

> **interactive figure:** two plots stacked, with a button for each of four curves: x², x³ − 3x, sin x and eˣ. on top, the curve in cyan from x = −3 to 3, with a point to drag along it and the tangent line through the point. below, the graph of the derivative in amber over the same x, with the same x marked and its height drawn as a bar, so the slope on top is the height below. a readout gives x, f(x), f'(x), the tangent's equation, and whether the curve is climbing, falling or flat there. it opens on x² at x = 1: f(1) = 1, slope 2, tangent y = 1 + 2(x − 1), climbing. on x³ − 3x the slope is 0 at x = −1, a high point at (−1, 2), and at x = 1, a low point at (1, −2). on eˣ the two plots show the same curve. the arrow keys move the point by 0.1.

- $x^2$: the slope is $2x$, a line. negative on the left, $0$ at the bottom, positive on the right
- $x^3 - 3x$: the slope is $3x^2 - 3$, which is $0$ at $x = -1$ and $x = 1$. the curve turns at both, a high point at $(-1, 2)$ and a low one at $(1, -2)$, and falls between them
- $\sin x$: the slope is $\cos x$. it's $1$ at $0$, where the wave climbs fastest, and $0$ at $\pm\pi/2$, the crest and the trough
- $e^x$: the slope is $e^x$. the two plots show the same curve, and the point below sits at the height of the point above

## try it

> **interactive figure:** a practice exercise with four kinds of question: pick the derivative of a function like x³ or sin(2x), give the slope at a point, say where a function's slope is 0, and say whether a curve is climbing, falling or flat at a point. it opens on the derivative of x³, which is 3x², then the slope of x² at x = 3, which is 6. a slope question draws its curve with the tangent after the answer.

## the rules

### the definition

$$
f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}
$$

the slope of the secant from $x$ to $x + h$, as $h$ shrinks to nothing. $f'(x)$, $\frac{dy}{dx}$ and $\frac{d}{dx} f(x)$ all name it. the $d$'s stand for small changes: $dy/dx$ is the rise over the run of a step so short that the curve is straight along it.

### constants and multiples

$$
(c)' = 0 \qquad (c \cdot f)' = c \cdot f'
$$

a constant is a flat line: $f(x + h) - f(x)$ is $0$ for every $h$, so the quotient is $0$. a multiple scales every rise by $c$ and leaves the run alone, so the slope scales by $c$.

### the power rule

$$
(x^n)' = n\,x^{n-1}
$$

expand $(x + h)^n$. the first term is $x^n$, the next is $n x^{n-1} h$, and every term after that carries $h^2$ or more. subtract $x^n$, divide by $h$, and all that survives as $h \to 0$ is $n x^{n-1}$. the key idea below does it in full. the rule holds for every real $n$, not only whole ones: $\sqrt{x} = x^{1/2}$ has slope $\frac{1}{2\sqrt{x}}$, and $1/x = x^{-1}$ has slope $-1/x^2$.

### sums

$$
(f + g)' = f' + g'
$$

the rise of a sum is the sum of the rises, so the quotient splits in two. with the two rules above, a polynomial differentiates term by term: $3x^4 - 5x^2 + 7$ has slope $12x^3 - 10x$.

### the product rule

$$
(fg)' = f'g + fg'
$$

think of $f \cdot g$ as the area of a rectangle with sides $f$ and $g$. when $x$ moves by $h$, one side grows by about $f'h$ and the other by $g'h$. the area gains a strip $f'h \cdot g$ along one edge and a strip $f \cdot g'h$ along the other, plus a corner $f'h \cdot g'h$ that carries $h^2$ and vanishes in the quotient. each factor changes while the other holds still, and the two changes add. a check: $x \cdot x^2$ has slope $1 \cdot x^2 + x \cdot 2x = 3x^2$, which is $(x^3)'$.

### the quotient rule

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

$f / g$ is $f \cdot g^{-1}$. the power rule with the chain rule below gives $(g^{-1})' = -g' / g^2$, and the product rule then gives $f'/g - fg'/g^2$. put both over $g^2$. the order in the numerator matters: $f'g$ first, then minus $fg'$.

### the chain rule

$$
\big(f(g(x))\big)' = f'(g(x)) \cdot g'(x)
$$

move $x$ by $h$. the inside moves by about $g'(x)\,h$, and $f$ sees that as its own step, so the outside moves by about $f'(g(x))$ times it. the rates multiply. it's [composition](/math/functions/#composition) with slopes: $g$ runs first, then $f$, and the slope is read inside out. $\sin(2x)$ is $\sin$ of $2x$, so its slope is $\cos(2x) \cdot 2 = 2\cos(2x)$. $(x^2 + 1)^3$ has slope $3(x^2 + 1)^2 \cdot 2x = 6x(x^2 + 1)^2$.

### sin and cos

$$
\begin{gathered}
(\sin x)' = \cos x \\[6pt]
(\cos x)' = -\sin x
\end{gathered}
$$

the [angle sum](/math/trig/#the-angle-sum) opens up $\sin(x + h)$ as $\sin x \cos h + \cos x \sin h$. subtract $\sin x$ and divide by $h$:

$$
\sin x \cdot \frac{\cos h - 1}{h} + \cos x \cdot \frac{\sin h}{h}
$$

as $h \to 0$, $\sin h / h \to 1$ and $(\cos h - 1) / h \to 0$. at $h = 0.01$ they read $0.99998$ and $-0.0050$. so the slope is $\cos x$, which is where the [trig page](/math/trig/#the-circle-unrolled) left off: $1$ at $0$, where the wave starts out steepest. cos is sin a quarter turn ahead, so its slope is cos a quarter turn ahead: $-\sin x$. this is in radians. in degrees, the slope of sin at 0 would be $\pi / 180 \approx 0.01745$.

### exponentials and logs

$$
\begin{gathered}
(e^x)' = e^x \qquad (b^x)' = b^x \ln b \\[6pt]
(\ln x)' = \frac{1}{x}
\end{gathered}
$$

for any base, $b^{x + h} - b^x = b^x (b^h - 1)$, so the quotient is $b^x$ times $(b^h - 1) / h$, a number that depends on the base and not on $x$. at $h = 0.001$ that number is $0.6934$ for base 2 and $1.0992$ for base 3, closing in on $\ln 2 \approx 0.6931$ and $\ln 3 \approx 1.0986$. exactly one base makes it $1$, and that base is $e$: at $h = 0.001$ its factor reads $1.0005$. so $e^x$ is its own slope, and every other $b^x$ is its own slope times $\ln b$. that is why $e$ is the natural base the [exponents page](/math/exponents-logs/#e) promised: it grows at a rate equal to its size.

$\ln x$ undoes $e^x$: $y = \ln x$ means $e^y = x$. differentiate both sides, the left one with the chain rule: $e^y \cdot y' = 1$, so $y' = 1 / e^y = 1 / x$.

### the second derivative

$$
f'' = (f')'
$$

the slope of the slope. where $f'' > 0$ the slope is increasing and the curve bends up, like a bowl. where $f'' < 0$ it bends down. at a point where $f' = 0$, that settles which kind of flat it is: bending up means a low point, bending down a high one, and $f'' = 0$ means look closer. $x^3 - 3x$ has $f' = 3x^2 - 3$, zero at $\pm 1$, and $f'' = 6x$: $-6$ at $-1$, a high point at $(-1, 2)$, and $6$ at $1$, a low point at $(1, -2)$. $x^3$ has $f' = 3x^2$ and $f'' = 6x$, both $0$ at $0$, and it is neither: the curve flattens for an instant and keeps climbing.

the [noise](/graphics/webgl-glsl/#noise) in a shader blends with the fade curve $6t^5 - 15t^4 + 10t^3$ because both its slope, $30t^2(t - 1)^2$, and its second derivative, $60t(2t - 1)(t - 1)$, are $0$ at $t = 0$ and at $t = 1$. the cubic it replaced, $3t^2 - 2t^3$, has a flat slope at both ends too, but its second derivative is $6 - 12t$, which is $6$ at $0$ and $-6$ at $1$, and the bend jumped where cells met.[^perlin]

### motion

$$
\begin{gathered}
v(t) = s'(t) \\[6pt]
a(t) = v'(t) = s''(t)
\end{gathered}
$$

the slope of position is velocity: a rise in meters over a run in seconds. the slope of velocity is acceleration. a ball thrown straight up at 20 m/s, with gravity rounded to 10 m/s², is at height $s(t) = 20t - 5t^2$ after $t$ seconds. then $v(t) = 20 - 10t$ and $a(t) = -10$. at $t = 1$ it's 15 m up and climbing at 10 m/s. at $t = 2$ it's at the top, 20 m, with $v = 0$. at $t = 3$ it's back at 15 m, falling at 10 m/s. the top is where the velocity crosses zero, a high point of $s$. the integrals page runs this backwards, from velocity to distance.

### linear approximation

$$
f(a + h) \approx f(a) + f'(a)\,h
$$

the definition, turned around: for a small $h$ the quotient is close to $f'(a)$, so $f(a + h)$ is close to $f(a)$ plus $f'(a)$ times $h$. that's the value of the tangent line at $a + h$, the line the explore figure's readout writes out. $\sqrt{17}$: take $f(x) = \sqrt{x}$ at $a = 16$, where $f = 4$ and $f' = 1 / (2\sqrt{16}) = 1/8$. then $\sqrt{17} \approx 4 + 1/8 = 4.125$, against $4.1231$. $\sin 0.1 \approx 0 + 1 \cdot 0.1 = 0.1$, against $0.0998$. $1.02^5 \approx 1 + 5 \cdot 0.02 = 1.1$, against $1.1041$.

this is why a small enough piece of a smooth curve looks straight. the gap between the curve and its tangent is about $\tfrac{1}{2} f''(a)\, h^2$, so it shrinks like $h^2$ while the piece shrinks like $h$. on $x^2$ at $1$, a step of $0.1$ rises $0.21$ against the tangent's $0.2$: a gap of $0.01$, about a twentieth of the rise. a step of $0.01$ rises $0.0201$ against $0.02$: a gap of $0.0001$, about a two-hundredth. every zoom by 10 makes the curve look 10 times straighter. the series page keeps going, adding the $h^2$ term and the ones after it.

## worked examples

### a polynomial, term by term

$f(x) = 3x^4 - 5x^2 + 7$. the power rule on each term, with the constants in front riding along and the 7 dropping out: $f'(x) = 12x^3 - 10x$. at $x = 1$ the slope is $12 - 10 = 2$, and $f(1) = 5$, so the tangent there is $y = 5 + 2(x - 1)$.

### a product with a chain inside

$g(x) = x^2 \sin(3x)$. the product rule first, with $f = x^2$ and the second factor $\sin(3x)$: $2x \sin(3x) + x^2 \cdot (\sin 3x)'$. the chain rule for that last slope: $\cos(3x) \cdot 3$. so $g'(x) = 2x \sin(3x) + 3x^2 \cos(3x)$.

### a quotient

$q(x) = \dfrac{x}{x^2 + 1}$, so $f = x$, $g = x^2 + 1$, $f' = 1$ and $g' = 2x$:

$$
\begin{aligned}
q'(x) &= \frac{1 \cdot (x^2 + 1) - x \cdot 2x}{(x^2 + 1)^2} \\[6pt]
&= \frac{1 - x^2}{(x^2 + 1)^2}
\end{aligned}
$$

the slope is $0$ at $x = \pm 1$: a high point at $(1, 1/2)$ and a low one at $(-1, -1/2)$.

### where a curve turns

$f(x) = x^3 - 3x$. $f' = 3x^2 - 3 = 3(x - 1)(x + 1)$, zero at $x = -1$ and $x = 1$. $f'' = 6x$: $-6$ at $-1$, so the curve bends down there, a high point at $(-1, 2)$. $6$ at $1$, bending up, a low point at $(1, -2)$. between them $f' < 0$ and the curve falls. drag the explore figure's point across it.

### a tangent line as a calculator

$\sqrt{9.3}$: take $f(x) = \sqrt{x}$ at $a = 9$, where $f(9) = 3$ and $f'(9) = 1 / (2 \cdot 3) = 1/6$. then $\sqrt{9.3} \approx 3 + 0.3 / 6 = 3.05$. the true value is $3.0496$.

## the key idea up close

### the power rule from the limit

$x^2$ first. expand, subtract, divide:

$$
\begin{gathered}
(x + h)^2 = x^2 + 2xh + h^2 \\[4pt]
(x + h)^2 - x^2 = 2xh + h^2 \\[6pt]
\frac{2xh + h^2}{h} = 2x + h
\end{gathered}
$$

the $x^2$ cancels, the $h$ divides out, and what's left is $2x + h$, which heads to $2x$ as $h \to 0$. at $x = 1$ that's the movie's $2 + h$.

then $x^n$. the binomial theorem expands $(x + h)^n$ as $x^n + n x^{n-1} h + \binom{n}{2} x^{n-2} h^2 + \cdots + h^n$, and every term after the second carries $h^2$ or more. call that tail $h^2 r$, where $r$ is what's left when $h^2$ is pulled out of it, a polynomial in $h$:

$$
\begin{gathered}
(x + h)^n - x^n = n x^{n-1} h + h^2 r \\[6pt]
\frac{(x + h)^n - x^n}{h} = n x^{n-1} + h r
\end{gathered}
$$

$r$ stays finite as $h \to 0$, so $hr$ goes to $0$, and $(x^n)' = n x^{n-1}$. the point of the proof is bigger than the rule. the change $f(x + h) - f(x)$ is a pile of terms in $h$, and the derivative is the coefficient of the one that's linear in $h$. everything with $h^2$ or more is lost in the quotient and the limit. that is also what the linear approximation says: $f(x + h) = f(x) + f'(x)\,h + (\text{terms with } h^2)$.

## in code

the forward difference is the secant's slope from $x$ to $x + h$. the central difference is the secant from $x - h$ to $x + h$: the $h$ terms in its error cancel, so it's off by about $h^2$ where the forward one is off by about $h$. the run takes the slope of $x^3$ at $1$, where the answer is $3$, with $h$ from $10^{-1}$ down to $10^{-16}$.

`python/math/derivative.py`

```python
"""numerical derivatives: the forward difference (f(x + h) - f(x)) / h and the central difference (f(x + h) - f(x - h)) / (2h), with a run that shows their error as h shrinks. it falls with h, then climbs again once rounding takes over, because f(x + h) - f(x) loses digits when the two values are close. run the file to print the table for x³ at 1, where the slope is 3."""

from __future__ import annotations

from collections.abc import Callable
from typing import NamedTuple

Fn = Callable[[float], float]


def forward(f: Fn, x: float, h: float) -> float:
    """Return the slope of the secant from x to x + h: (f(x + h) - f(x)) / h.

    Off by about h · f''(x) / 2, so the error shrinks like h.
    """
    return (f(x + h) - f(x)) / h


def central(f: Fn, x: float, h: float) -> float:
    """Return the slope of the secant from x - h to x + h: (f(x + h) - f(x - h)) / (2h).

    The h terms cancel, leaving about h² · f'''(x) / 6, so the error shrinks like h².
    """
    return (f(x + h) - f(x - h)) / (2 * h)


def cube(x: float) -> float:
    """Return x³ by two multiplies, the same two in any language, so the run matches the page's chart bit for bit."""
    return x * x * x


class Row(NamedTuple):
    """One step size, and how far each estimate lands from the exact slope."""

    h: float
    forward_error: float
    central_error: float


def error_table(f: Fn, x: float, exact: float, smallest: int = 16) -> list[Row]:
    """Return a row for h = 1e-1, 1e-2, ... down to 10**-smallest, each error measured against `exact`.

    Each h is parsed from its decimal spelling, so it's the same double everywhere: 10.0 ** -k would go through pow.
    """
    rows = []
    for k in range(1, smallest + 1):
        h = float(f"1e-{k}")
        rows.append(Row(h, abs(forward(f, x, h) - exact), abs(central(f, x, h) - exact)))
    return rows


def best(rows: list[Row], column: str) -> Row:
    """Return the row where `column` ("forward_error" or "central_error") is smallest: where that method bottoms out."""
    return min(rows, key=lambda r: getattr(r, column))


def main() -> None:
    """Print the table for x³ at 1, where the slope is 3, and where each method bottoms out."""
    rows = error_table(cube, 1.0, 3.0)
    print(f"{'h':>6}  {'forward error':>14}  {'central error':>14}")
    for r in rows:
        print(f"{r.h:>6.0e}  {r.forward_error:>14.2e}  {r.central_error:>14.2e}")
    for column in ("forward_error", "central_error"):
        r = best(rows, column)
        print(f"{column.split('_')[0]} bottoms out at h = {r.h:.0e}, off by {getattr(r, column):.1e}")


if __name__ == "__main__":
    main()
```

the forward difference is off by $0.31$ at $h = 0.1$, $0.030$ at $0.01$ and $0.0030$ at $0.001$: a tenth of the error per decade, like $3h$. it bottoms out at $h = 10^{-8}$, off by $4.0 \times 10^{-9}$, and then the error climbs back: $2.5 \times 10^{-7}$ at $10^{-9}$, $2.7 \times 10^{-4}$ at $10^{-12}$, $0.33$ at $10^{-15}$, and at $10^{-16}$ the quotient reads $0$, off by the whole $3$. the central difference is off by $0.010$, then $1.0 \times 10^{-4}$, then $1.0 \times 10^{-6}$: a hundredth per decade, like $h^2$. it bottoms out at $10^{-6}$, off by $2.5 \times 10^{-11}$, and climbs the same way.

the climb is rounding. a double carries 53 bits, about 16 decimal digits,[^py-float] and $f(x + h) - f(x)$ throws most of them away: when two numbers share their first $k$ digits, their difference keeps only the rest. at $h = 10^{-8}$ the two values of $x^3$ agree to 8 digits, the difference has 8 left, and dividing by $10^{-8}$ turns the digits that were lost into an error around $10^{-8}$. at $10^{-16}$ nothing is left. the gap between $1.0$ and the next double up is `sys.float_info.epsilon`, $2.2 \times 10^{-16}$,[^py-sys] so $1 + 10^{-16}$ is $1$, $f(1 + h) - f(1)$ is $0$, and so is the quotient. the best $h$ sits where the two errors meet: about $\sqrt{\varepsilon} \approx 1.5 \times 10^{-8}$ for the forward difference and $\sqrt[3]{\varepsilon} \approx 6.1 \times 10^{-6}$ for the central one.

> **interactive figure:** a chart of the error of two difference quotients for the slope of x³ at 1, against h from 10⁻¹ down to 10⁻¹⁶, both axes logarithmic. the forward difference, in cyan, is off by 0.31 at h = 0.1 and falls like h to 4.0 × 10⁻⁹ at h = 10⁻⁸, then climbs back as rounding takes over, to 3 at 10⁻¹⁶, where 1 + h is 1 and the quotient reads 0. the central difference, in amber, is off by 0.010 at 0.1 and falls like h² to 2.5 × 10⁻¹¹ at h = 10⁻⁶, then climbs the same way.

## pitfalls

- **the chain rule's inner slope, forgotten.** the slope of $\sin(2x)$ is $2\cos(2x)$, not $\cos(2x)$: at $0$ the wave is twice as steep as $\sin x$, so its slope is $2$. anything inside the function contributes its own slope as a factor
- **$(fg)'$ isn't $f'g'$.** $x \cdot x^2$ is $x^3$, with slope $3x^2$. the product rule agrees: $1 \cdot x^2 + x \cdot 2x$. the wrong rule gives $1 \cdot 2x$, which at $x = 1$ says $2$ for a slope that is $3$
- **the power rule needs the $x$ in the base.** $2^x$ has slope $2^x \ln 2$, about $0.693 \cdot 2^x$, which is $1.386$ at $x = 1$. $x \cdot 2^{x-1}$ says $1$ there. and a constant like $\pi^2$ has slope $0$, not $2\pi$
- **no derivative at a corner or a vertical tangent.** $\lvert x \rvert$ at $0$: the secant slopes are $1$ from the right and $-1$ from the left, so they settle on nothing. $\sqrt[3]{x}$ at $0$: its slope $1 / (3\sqrt[3]{x^2})$ grows without bound, $33.3$ at $x = 0.001$ and $3{,}333$ at $10^{-6}$. the tangent is vertical, and a vertical line has no slope
- **$f' = 0$ isn't a high or a low point.** $x^3$ at $0$ has slope $0$ and climbs on both sides. check the sign of $f'$ on each side, or the sign of $f''$
- **$h$ too small in code.** the error of a difference quotient falls with $h$ only until rounding takes over, at about $10^{-8}$ for the forward difference and $10^{-6}$ for the central one. past that it climbs, and at $10^{-16}$ the forward difference reads $0$, because $1 + 10^{-16}$ is $1$

## practice

1. differentiate $f(x) = 3x^4 - 5x^2 + 7$, and give the slope at $x = 1$.

   **answer:** term by term with the power rule: $f'(x) = 12x^3 - 10x$. the $7$ is a constant and drops out. at $x = 1$: $12 - 10 = 2$.

2. find the slope of $y = \sqrt{x}$ at $x = 9$, write the tangent line there, and use it to estimate $\sqrt{9.3}$.

   **answer:** $\sqrt{x} = x^{1/2}$, so $y' = \tfrac{1}{2} x^{-1/2} = \dfrac{1}{2\sqrt{x}}$, and at $9$ that's $1/6$. the tangent is $y = 3 + \tfrac{1}{6}(x - 9)$. at $x = 9.3$ it gives $3 + 0.3/6 = 3.05$, against the true $3.0496$.

3. differentiate $(x^2 + 1)^5$, $x e^x$ and $\cos(5x)$.

   **answer:** chain rule: $5(x^2 + 1)^4 \cdot 2x = 10x(x^2 + 1)^4$. product rule: $1 \cdot e^x + x \cdot e^x = e^x + x e^x$. chain rule again: $-\sin(5x) \cdot 5 = -5\sin(5x)$.

4. a particle's position is $s(t) = t^3 - 6t^2 + 9t$ meters after $t$ seconds. find its velocity, the times it's at rest, and its acceleration at those times.

   **answer:** $v(t) = s'(t) = 3t^2 - 12t + 9 = 3(t - 1)(t - 3)$, which is $0$ at $t = 1$ and $t = 3$: the particle stops at $s(1) = 4$ m and again at $s(3) = 0$ m. $a(t) = v'(t) = 6t - 12$, so $a(1) = -6$ and $a(3) = 6$ m/s². at $t = 1$ it's being pushed back toward the start, and between the two stops it moves backwards: $v(2) = -3$.

5. $f(x) = x^2 - 4x + 1$. where is its lowest point, and how do you know it's the lowest?

   **answer:** $f'(x) = 2x - 4$, which is $0$ at $x = 2$, so that's the only flat point. $f''(x) = 2 > 0$: the curve bends up there, a low point. $f(2) = 4 - 8 + 1 = -3$, so the lowest point is $(2, -3)$. a parabola that opens up has one low point and no high point.

6. estimate $e^{0.05}$ with a tangent line. then compute the forward and central differences of $x^3$ at $1$ with $h = 0.1$, and say why one is closer.

   **answer:** $f(x) = e^x$ at $a = 0$: $f(0) = 1$ and $f'(0) = 1$, so $e^{0.05} \approx 1 + 0.05 = 1.05$, against $1.0513$.

   forward: $(1.1^3 - 1) / 0.1 = 3.31$. central: $(1.1^3 - 0.9^3) / 0.2 = 3.01$. the exact slope is $3$. the forward quotient is $3 + 3h + h^2$, off by about $3h$, and the central one is $3 + h^2$: the $h$ terms from the two sides cancel, so only $h^2 = 0.01$ is left.

[^perlin]: ken perlin, improving noise, acm transactions on graphics 21(3), 2002, [doi 10.1145/566654.566636](https://doi.org/10.1145/566654.566636), open copy [on his site](https://mrl.cs.nyu.edu/~perlin/paper445.pdf): the cubic interpolant 3t² − 2t³ has the second derivative 6 − 12t, which isn't zero at t = 0 or t = 1 and puts second order discontinuities across the faces of adjoining cells, so it's replaced by 6t⁵ − 15t⁴ + 10t³, whose first and second derivatives are zero at both ends.

[^py-float]: python's tutorial, [floating-point arithmetic: issues and limitations](https://docs.python.org/3/tutorial/floatingpoint.html): almost all platforms map python floats to ieee 754 binary64 doubles, which carry 53 bits of precision, and each operation's rounding error is about 1 part in 2⁵³ at most.

[^py-sys]: python's [sys.float_info](https://docs.python.org/3/library/sys.html#sys.float_info) docs: `epsilon` is the difference between 1.0 and the least float above it, c's `DBL_EPSILON`, and `dig`, the most decimal digits a float represents faithfully, is 15.

## related

- https://learning.anthnydas.com/math/limits.md
- https://learning.anthnydas.com/math/integrals.md
- https://learning.anthnydas.com/math/series.md
- https://learning.anthnydas.com/math/functions.md
- https://learning.anthnydas.com/math/exponents-logs.md
- https://learning.anthnydas.com/math/trig.md
- https://learning.anthnydas.com/graphics/webgl-glsl.md
