---
title: Taylor series
heading: Building any smooth function out of powers of x
eyebrow: A6 · Lesson 36 · Taylor series
xp: 40
---

::: hook
A calculator has no sine table inside. To find $\sin 0.3$ it adds up a handful of terms: $0.3 - 0.3^3/6 + 0.3^5/120 - \cdots$, and stops when the next term is too small to matter. Smooth functions can all be built this way, out of plain powers of $x$, and engineers use the first one or two terms of these sums constantly as approximations.

::: play
- The yellow curve is $\sin x$. The blue curve is its Taylor polynomial: the sum of its series up to the power $N$.
- Start at $N = 1$: a straight line, $x$. Raise $N$ one step at a time and watch how far from the middle the polynomial hugs the sine.
- At $N = 15$, over what range of $x$ is the fit good? What happens beyond it?
:::
:::

```sim plot
view: graph
x: x = -10..10
y: y = -2..2
slider: N = 1..15 step 2 = 3
plot: sin(x) | label: sin x
plot: x - step(N - 3)*x^3/6 + step(N - 5)*x^5/120 - step(N - 7)*x^7/5040 + step(N - 9)*x^9/362880 - step(N - 11)*x^11/39916800 + step(N - 13)*x^13/6227020800 - step(N - 15)*x^15/1307674368000 | color: ch2 | label: polynomial
readout: highest power = N
readout: error at x of 1 = abs(sin(1) - (1 - step(N - 3)/6 + step(N - 5)/120 - step(N - 7)/5040 + step(N - 9)/362880 - step(N - 11)/39916800 + step(N - 13)/6227020800 - step(N - 15)/1307674368000)) | format: sci
```

::: predict
## Near $x = 0$, the first term alone gives $\sin x \approx x$. Using the first two terms, $x - x^3/6$, how close is the estimate at $x = 0.5$?

- [ ] Off by about 10%
- [ ] Off by about 1%
- [x] Off by less than 0.1%
- [ ] Exact

::: reveal
Off by about 0.05%: $0.5 - 0.125/6 = 0.479167$, while $\sin 0.5 = 0.479426$. The error is roughly the next term, $x^5/120 = 0.00026$. Each term adds another power of a small number, so near the center a few terms are extremely accurate. Far from it, you need many more.
:::
:::

::: nameit
## Matching a function's derivatives at a point

The **Taylor series** of $f$ about a point $a$ is the power series whose value and every derivative match $f$'s at $a$:

$$f(x) = f(a) + f'(a)(x - a) + \frac{f''(a)}{2!}(x - a)^2 + \frac{f'''(a)}{3!}(x - a)^3 + \cdots$$

The $k$th term's coefficient is $f^{(k)}(a)/k!$: differentiating $(x - a)^k$ $k$ times leaves $k!$, which the factorial cancels. About $a = 0$ (a **Maclaurin series**) the ones to know are:

| Function | Series | Converges for |
|---|---|---|
| $e^x$ | $1 + x + \dfrac{x^2}{2!} + \dfrac{x^3}{3!} + \cdots$ | all $x$ |
| $\sin x$ | $x - \dfrac{x^3}{3!} + \dfrac{x^5}{5!} - \cdots$ | all $x$ |
| $\cos x$ | $1 - \dfrac{x^2}{2!} + \dfrac{x^4}{4!} - \cdots$ | all $x$ |
| $\dfrac{1}{1 - x}$ | $1 + x + x^2 + x^3 + \cdots$ | $\lvert x\rvert < 1$ |
| $\ln(1 + x)$ | $x - \dfrac{x^2}{2} + \dfrac{x^3}{3} - \cdots$ | $-1 < x \le 1$ |
| $(1 + x)^n$ | $1 + nx + \dfrac{n(n - 1)}{2}x^2 + \cdots$ | $\lvert x\rvert < 1$ |

**Using them as approximations.** For small $x$, keep the first one or two terms:

$$\sin x \approx x, \qquad \cos x \approx 1 - \frac{x^2}{2}, \qquad e^{x} \approx 1 + x, \qquad \sqrt{1 + x} \approx 1 + \frac{x}{2}, \qquad \frac{1}{1 + x} \approx 1 - x$$

**How good is it?** For series like these, whose terms shrink and alternate in sign, the error is smaller than the first term left out. That's the number to quote when you say an approximation is good to 0.1%.

The series for $1/(1 - x)$ is the geometric series from lesson 24; it stops converging at $|x| = 1$, where the function itself blows up. A series can only reach as far from its center as the nearest place where the function misbehaves.
:::

::: example
## Estimate $e^{0.2}$ with the Taylor polynomial of degree 3 about 0.

::: solution
1. **Add the four terms.**
   $$\begin{aligned} e^{0.2} &\approx 1 + 0.2 + \frac{0.04}{2} + \frac{0.008}{6} \\ &= 1 + 0.2 + 0.02 \\ &\quad + 0.001333 \\ &= 1.221333 \end{aligned}$$
2. **Compare.** The true value is 1.221403, so four terms give four correct digits. Near the center, each extra term multiplies the accuracy by roughly $x/n$.

**Answer:** $e^{0.2} \approx 1.221333$, against the true value 1.221403.
:::
:::

::: example
## Use the Taylor polynomial of $\sqrt{x}$ about $x = 4$ to estimate $\sqrt{4.1}$.

::: solution
1. **Derivatives at $x = 4$.**
   $$\begin{aligned} f(4) &= 2 \\ f'(x) &= \frac{1}{2\sqrt x} \\ f'(4) &= \frac{1}{4} \\ f''(x) &= -\frac{1}{4}x^{-3/2} \\ f''(4) &= -\frac{1}{32} \end{aligned}$$
2. **Build the polynomial.**
   $$\sqrt{x} \approx 2 + \frac{x - 4}{4} - \frac{(x - 4)^2}{64}$$
3. **Evaluate at 4.1.**
   $$\begin{aligned} \sqrt{4.1} &\approx 2 + 0.025 - 0.000156 \\ &= 2.024844 \end{aligned}$$
4. **Compare.** The true value is 2.024846. Centering the series at a point where you know the function well is what makes it useful.

**Answer:** $\sqrt{4.1} \approx 2.024844$, against the true value 2.024846.
:::
:::

::: example
## Estimate $\cos 62°$ from a degree-2 Taylor polynomial about 60°.

::: solution
1. **Expand about $a = \pi/3$.**
   $$\begin{aligned} \cos(a + h) &\approx \cos a - \sin a\,h \\ &\quad - \frac{\cos a}{2}h^2 \\ &= 0.5 - 0.866h - 0.25h^2 \end{aligned}$$
2. **Put in $h$.** With $h = 2° = 0.034\,91$ rad:
   $$\begin{aligned} \cos 62° &\approx 0.5 - 0.030\,23 \\ &\quad - 0.000\,30 \\ &= 0.469\,47 \end{aligned}$$
3. **Compare.** The true value is 0.469 47, to five places: expanding about a nearby point with known values works far better than expanding about 0.

**Answer:** $\cos 62° \approx 0.469\,47$, right to five places.
:::
:::

::: check
given: x = 0.1 | 0.2 | 0.3
answer: 1 + x + x^2/2
tolerance: 0.0001%

## Estimate $e^{\given{x}}$ with the first three terms of its series.

::: right
Right. $1 + \given{x} + \given{x}^2/2$ = {answer}, already very close to $e^{\given{x}}$.
:::

::: misses
- [exp(x)] That's the exact value. The three-term estimate stops at $x^2/2$.
- [1 + x + x^2] The third term is $x^2/2!$, with a factorial.
- [1 + x] That's two terms. Add $x^2/2$.
- [any] Not yet. Add $1 + x + x^2/2!$.
:::
:::

::: check
given: k = 2..5 int; n = 2..5 int
answer: k^n/n!
tolerance: 0.5%

## What is the coefficient of $x^{\given{n}}$ in the series for $e^{\given{k}x}$?

::: right
Right. Replace $x$ by $\given{k}x$ in the series for $e^x$: the term is $(\given{k}x)^{\given{n}}/\given{n}!$, so the coefficient is $\given{k}^{\given{n}}/\given{n}!$ = {answer}.
:::

::: misses
- [1/n!] The $x$ in $e^x$ became $\given{k}x$, so the power also raises {k}.
- [k^n] Divide by $\given{n}!$.
- [k/n!] The whole $\given{k}x$ is raised to the power {n}.
- [any] Not yet. Substitute $\given{k}x$ into $\sum x^n/n!$.
:::
:::

::: check
kind: expression
answer: 1 - x^2/2 + x^4/24
vars: x = -1..1

## Write the Taylor polynomial of $\cos x$ about 0 up to the $x^4$ term.

::: right
Right. Cosine's derivatives at 0 cycle 1, 0, −1, 0, 1, so the even terms survive with alternating signs.
:::

::: misses
- [1 - x^2 + x^4] Divide each term by its factorial: $2! = 2$, $4! = 24$.
- [1 + x^2/2 + x^4/24] The signs alternate: cosine curves downward from its peak at 0.
- [x - x^3/6] That's sine's series. Cosine has the even powers.
- [any] Not yet. Use $f^{(k)}(0)/k!$ with cosine's derivatives at 0: 1, 0, −1, 0, 1.
:::
:::

::: check
kind: choice

## For small $x$, which is the best simple approximation to $\sqrt{1 + x}$?

- [ ] $1 + x$
- [x] $1 + x/2$
- [ ] $1 + x^2/2$
- [ ] $\sqrt{x}$

::: right
Right. The binomial series with $n = \tfrac12$ starts $1 + \tfrac12x$. At $x = 0.1$ it gives 1.05, against the exact 1.0488.
:::

::: misses
- [1] That's $1 + x$ itself. Taking the square root halves the small change: $1 + x/2$.
- [3] The first correction is proportional to $x$, not $x^2$.
- [4] For small x, $\sqrt{1 + x}$ is close to 1; $\sqrt x$ is close to 0.
:::
:::

::: check
kind: parts
given: d = 1 | 2 | 5

## Estimate $\cos(60° + \given{d}°)$ from the degree-2 Taylor polynomial about 60°.

::: part
answer: 0.5 - sqrt(3)/2*(d*pi/180) - 0.25*(d*pi/180)^2
tolerance: 0.001%

### What does the polynomial give?

::: misses
- [0.5 - sqrt(3)/2*d - 0.25*d^2] Put the step in radians.
- [any] $\cos a - \sin a\,h - \frac{\cos a}{2}h^2$ with $h$ in radians.
:::
:::

::: part
answer: cos((60 + d)*pi/180)
tolerance: 0.001%

### What is the true value?

::: misses
- [any] Compute it.
:::
:::
:::

::: check
kind: derivation
vars: x = -1..1

## Build the Taylor series of $e^x$ about 0.

::: step
answer: 1

### Every derivative of $e^x$ is $e^x$. What is $f^{(n)}(0)$?
:::

::: step
answer: 1 + x + x^2/2 + x^3/6

### Write the terms up to $x^3$, using $\frac{f^{(n)}(0)}{n!}x^n$.
:::

::: right
Right. $e^x = \sum\frac{x^n}{n!}$, from one fact: its derivative is itself.
:::
:::

::: explain
## Why does a Taylor polynomial get more accurate near its center as you add terms?

::: model
Each term matches one more derivative of the function at the center: the value, then the slope, then the bend, and so on. A polynomial that agrees with the function in more and more of these ways follows it more and more closely near that point. The next term left out involves a higher power of the distance from the center, so near the center, where that distance is small, the leftover error shrinks very fast as terms are added.
:::
:::

::: practice

::: check
given: x = 0.3 | 0.4 | 0.5
answer: x - x^3/6
tolerance: 0.0001%

## Estimate $\sin \given{x}$ with two terms of its series.

::: misses
- [sin(x)] That's the exact value; use two terms.
- [1*x] That's one term. Subtract $x^3/6$.
- [any] $\sin x \approx x - x^3/6$.
:::
:::

::: check
given: x = 0.01 | 0.02 | 0.05
answer: 1 + x/2
tolerance: 0.0001%

## Estimate $\sqrt{1 + \given{x}}$ to first order.

::: misses
- [1 + x] The square root halves the first-order term.
- [any] $\sqrt{1 + x} \approx 1 + x/2$.
:::
:::

::: check
given: x = 0.05 | 0.1 | 0.2
answer: x - x^2/2
tolerance: 0.0001%

## Estimate $\ln(1 + \given{x})$ with two terms.

::: misses
- [1*x] That's one term. Subtract $x^2/2$.
- [any] $\ln(1 + x) \approx x - x^2/2$.
:::
:::

::: check
given: n = 3..9 int
answer: 1/(n + 1)
tolerance: 0.5%

## In the series for $\ln(1 + x)$, what is the size of the coefficient of $x^{\given{n}+1}$?

::: misses
- [1/n] The coefficient of $x^k$ is $1/k$; here $k = \given{n} + 1$.
- [any] $\ln(1 + x) = \sum (-1)^{k+1}x^k/k$.
:::
:::
:::
