---
title: The error of a Taylor polynomial
heading: How many terms are enough?
eyebrow: A6 · Lesson 37 · Taylor's remainder
xp: 40
---

::: hook
A math library computing $\sin x$ for single-precision floats needs about seven correct digits. It can't check its answer against the true value, because it doesn't have the true value; that's what it's computing. It needs a guarantee in advance: this many terms always gets within $10^{-7}$ on this range. Taylor's theorem gives that guarantee. It says exactly what the error of a Taylor polynomial is, in a form you can bound without knowing the answer.
:::

::: play
- The blue curve is $\sin x$; the other is its Taylor polynomial of degree $N$ about 0.
- Raise $N$. The polynomial hugs $\sin x$ over a wider and wider range.
- At $x_0$, the readouts show the actual error and the bound from Taylor's theorem. Is the actual error ever bigger than the bound?
:::
:::

```sim plot
view: graph
x: x = -6..6
y: y = -2..2
slider: N = 1..9 step 2 = 3
slider: x0 = 0.2..6 step 0.1 = 1.5
plot: sin(x) | label: sin x | samples: 800
plot: x - step(N - 2)*x^3/6 + step(N - 4)*x^5/120 - step(N - 6)*x^7/5040 + step(N - 8)*x^9/362880 | label: degree N | color: ch2 | samples: 800
marker: x = x0 | label: x₀
readout: actual error at x0 = abs(sin(x0) - (x0 - step(N - 2)*x0^3/6 + step(N - 4)*x0^5/120 - step(N - 6)*x0^7/5040 + step(N - 8)*x0^9/362880)) | digits: 3
readout: Taylor bound = abs(x0)^(N + 2)/(N + 2)! | digits: 3
```

::: predict
## Every derivative of $\sin x$ is $\pm\sin x$ or $\pm\cos x$, so none is ever bigger than 1 in size. What does that suggest about the error of its Taylor polynomials?

- [ ] It's always less than 1
- [x] It's at most $\frac{|x|^{n+1}}{(n + 1)!}$, which shrinks fast as $n$ grows
- [ ] It grows with each extra term
- [ ] It can't be bounded without the true value

::: reveal
The error after the degree-$n$ polynomial is at most $\frac{|x|^{n+1}}{(n + 1)!}$. The factorial in the denominator eventually crushes any power of $x$, so for any fixed $x$ the error goes to zero; for large $x$ it just takes more terms. That's why the sine series converges everywhere.
:::
:::

::: nameit
## Taylor's theorem

If $f$ has $n + 1$ derivatives near $a$, then $f(x) = T_n(x) + R_n(x)$, where $T_n$ is the Taylor polynomial of degree $n$ and the remainder is

$$R_n(x) = \frac{f^{(n+1)}(\xi)}{(n + 1)!}(x - a)^{n+1}$$

for some $\xi$ between $a$ and $x$. The remainder looks like the next term of the series, with the derivative evaluated at an unknown point $\xi$ instead of at $a$.

## The bound you can use

You don't know $\xi$, but if $|f^{(n+1)}| \le M$ everywhere between $a$ and $x$, then

$$|R_n(x)| \le \frac{M}{(n + 1)!}|x - a|^{n+1}$$

For $\sin$ and $\cos$, $M = 1$. For $e^x$ on $[0, b]$, $M = e^b$ (use 3 for $e^1$ if you like round numbers).

## Even and odd functions get a bonus

The degree-3 polynomial of $\sin x$ is also its degree-4 polynomial, since the $x^4$ term is zero. So you may use the remainder $R_4$, giving the bound $\frac{|x|^5}{5!}$ instead of $\frac{|x|^4}{4!}$. That's why the sim uses $N + 2$.

## Keep $x$ small

The bound grows like $|x - a|^{n+1}$, so libraries first **reduce the range**: using $\sin(x + 2\pi) = \sin x$ and other identities, they bring $x$ into $[-\frac{\pi}{4}, \frac{\pi}{4}]$ or so, where a short polynomial is enough.
:::

::: example
## Bound the error of $e^{0.5} \approx 1 + 0.5 + \frac{0.5^2}{2}$.

::: solution
1. **Write the remainder.** This is $T_2$, so the remainder is $\frac{e^\xi}{3!}(0.5)^3$ with $0 < \xi < 0.5$.
2. **Bound it.** Since $e^\xi < e^{0.5} < 2$,
   $$\begin{aligned} |R_2| &< \frac{2}{6}(0.125) \\ &= 0.042 \end{aligned}$$
3. **Compare.** The estimate is 1.625 and the true value is 1.6487, so the actual error is 0.024, inside the bound.

**Answer:** the error is less than 0.042; the actual error is 0.024.
:::
:::

::: example
## What degree does a sine polynomial need for an error below $10^{-7}$ on $[-\frac{\pi}{2}, \frac{\pi}{2}]$?

::: solution
1. **Write the bound.** For an odd degree $N$, the bound is
   $$\frac{(\pi/2)^{N+2}}{(N + 2)!}$$
2. **Try degree 9.**
   $$\frac{(\pi/2)^{11}}{11!} = 3.6 \times 10^{-6}$$
   which is not enough.
3. **Try degree 11.**
   $$\frac{(\pi/2)^{13}}{13!} = 5.7 \times 10^{-8}$$
   which is below $10^{-7}$.
4. **What libraries do.** Real libraries do better by reducing to $[-\frac{\pi}{4}, \frac{\pi}{4}]$ and fine-tuning the coefficients, but this is the principle.

**Answer:** degree 11, which is six nonzero terms.
:::
:::

::: example
## Estimate $\ln 1.2$ with the degree-3 Taylor polynomial about 1, and bound the error.

::: solution
1. **Add three terms.** Use $\ln(1 + h) \approx h - \frac{h^2}{2} + \frac{h^3}{3}$ with $h = 0.2$:
   $$\begin{aligned} \ln 1.2 &\approx 0.2 - 0.02 + 0.002\,667 \\ &= 0.182\,67 \end{aligned}$$
2. **Bound the remainder.** It is $\frac{f^{(4)}(c)}{4!}h^4$ with $f^{(4)} = -6/c^4$, at most 6 for $c \ge 1$:
   $$\frac{6}{24} \times 0.0016 = 0.0004$$
3. **Compare.** The true value is 0.182 32, so the actual error, 0.000 35, is just inside the bound.

**Answer:** $\ln 1.2 \approx 0.182\,67$, with an error of at most 0.0004.
:::
:::

::: check
given: n = 3 | 5; x = 0.5 | 1
answer: 3*x^(n + 1)/(n + 1)!
tolerance: 0.5%

## You approximate $e^{\given{x}}$ with its Taylor polynomial of degree {n} about 0. Using $M = 3$ as a bound on $e^\xi$, what is the error bound?

::: right
Right. $\frac{3}{(n + 1)!}x^{n+1}$ = {answer}.
:::

::: misses
- [3*x^n/n!] The remainder uses the next power and the next factorial: $n + 1$.
- [x^(n + 1)/(n + 1)!] Include $M = 3$.
- [any] Not yet. $\frac{M}{(n + 1)!}|x|^{n+1}$.
:::
:::

::: check
given: N = 3 | 5; x = 0.5 | 1 | 2
answer: x^(N + 2)/(N + 2)!
tolerance: 0.5%

## You approximate $\sin\given{x}$ with its Taylor polynomial of degree {N}. Using the bonus for odd functions, what is the error bound?

::: right
Right. The next nonzero term is degree $N + 2$, so the bound is $\frac{|x|^{N+2}}{(N + 2)!}$ = {answer}.
:::

::: misses
- [x^(N + 1)/(N + 1)!] That's a valid bound but a weaker one. The degree-$(N + 1)$ term is zero, so you can use $N + 2$.
- [any] Not yet. $M = 1$ for sine, and the next nonzero power is $N + 2$.
:::
:::

::: check
given: eps = 0.1 | 0.01 | 0.001 | 0.0001 | 0.00001 | 0.000001
answer: 4 + step(3/120 - eps) + step(3/720 - eps) + step(3/5040 - eps) + step(3/40320 - eps) + step(3/362880 - eps)

## You compute $e = e^1$ from its Taylor polynomial about 0, using the bound $\frac{3}{(n + 1)!}$. What is the smallest degree $n$ that guarantees an error below {eps}?

::: right
Right. You need $(n + 1)! > 3/\varepsilon$, which first happens at degree {answer}.
:::

::: misses
- [any] Not yet. Try $n = 5, 6, 7, \ldots$ until $\frac{3}{(n + 1)!}$ drops below {eps}.
:::
:::

::: check
given: x = 0.3 | 0.5 | 0.8
answer: abs(cos(x) - 1 + x^2/2)
tolerance: 0.1%

## What is the actual error of $\cos\given{x} \approx 1 - \frac{x^2}{2}$? (Use a calculator for $\cos$.)

::: right
Right. {answer}, a little under the bound $\frac{x^4}{24}$.
:::

::: misses
- [x^4/24] That's the bound. The actual error is slightly smaller.
- [any] Not yet. Compute $\cos x$ and subtract $1 - x^2/2$.
:::
:::

::: check
given: x = 0.1 | 0.2 | 0.04
answer: x^2/8
tolerance: 0.5%

## For $x > 0$, $\sqrt{1 + x} \approx 1 + \frac{x}{2}$. The second derivative is $-\frac{1}{4}(1 + \xi)^{-3/2}$, which is at most $\frac{1}{4}$ in size. What is the error bound at $x$ = {x}?

::: right
Right. $\frac{1/4}{2!}x^2 = \frac{x^2}{8}$ = {answer}.
:::

::: misses
- [x^2/4] Divide by $2!$ as well.
- [x^2/2] Include $M = \frac{1}{4}$.
- [any] Not yet. $\frac{M}{2!}x^2$ with $M = \frac{1}{4}$.
:::
:::

::: check
kind: choice

## Why does the Taylor series for $e^x$ converge for every $x$, even $x = 100$?

- [x] The factorial in $\frac{x^{n+1}}{(n + 1)!}$ eventually outgrows any power of $x$
- [ ] Because $e^x$ is always positive
- [ ] Because the coefficients are all positive
- [ ] It doesn't; it only converges for $|x| < 1$

::: right
Right. At $x = 100$ the terms grow until $n \approx 100$, then the factorial wins and they shrink to zero. It takes many terms, but it converges.
:::

::: misses
- [4] That's the geometric series. Factorial coefficients give an infinite radius.
- [any] Look at what happens to $\frac{x^n}{n!}$ for large $n$.
:::
:::

::: check
kind: parts
given: h = 0.1 | 0.2 | 0.3

## Estimate $\ln(1 + \given{h})$ with the degree-3 Taylor polynomial about 1.

::: part
answer: h - h^2/2 + h^3/3
tolerance: 0.001%

### What does it give?

::: misses
- [any] $h - \frac{h^2}{2} + \frac{h^3}{3}$.
:::
:::

::: part
answer: 6/24*h^4
tolerance: 0.5%

### What does the Lagrange remainder bound the error by? ($|f^{(4)}| = 6/c^4 \le 6$.)

::: misses
- [h^4/24] Include the factor 6 from $f^{(4)}$.
- [any] $\frac{\max|f^{(4)}|}{4!}h^4$.
:::
:::
:::

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

## Bound the Taylor error of the degree-3 polynomial for $e^x$ on $[0, 1]$.

::: step
answer: exp(1)

### The remainder after degree 3 is $\frac{f^{(4)}(c)}{4!}x^4$. What is the largest $f^{(4)}(c)$ can be for $c$ in $[0, 1]$?
:::

::: step
answer: exp(1)*x^4/24

### Write the bound.
:::

::: right
Right. The error is at most $\frac{e\,x^4}{24}$; after degree $n$ it is $\frac{e\,x^{n+1}}{(n + 1)!}$, which shrinks fast as $n$ grows.
:::
:::

::: explain
## A Taylor polynomial is very accurate near its center and poor far away. Use the remainder formula to explain why.

::: model
The remainder is $\frac{f^{(n+1)}(\xi)}{(n + 1)!}(x - a)^{n+1}$. Near the center, $|x - a|$ is small, and raising a small number to the power $n + 1$ makes it tiny, so the error is very small and shrinks fast as you add terms. Far from the center, $|x - a|^{n+1}$ is large, so many terms are needed before the factorial in the denominator brings the error down. That's why libraries shrink the argument before using a series.
:::
:::

::: practice

::: check
given: N = 2 | 4; x = 0.5 | 1
answer: x^(N + 2)/(N + 2)!
tolerance: 0.5%

## You approximate $\cos\given{x}$ with its Taylor polynomial of degree {N}. Using the bonus for even functions, what is the error bound?

::: misses
- [any] The next nonzero power of the cosine series is $N + 2$.
:::
:::

::: check
given: x = 0.2 | 0.4 | 0.5 | 0.6 | 0.8 | 1
answer: 3*x^4/24
tolerance: 0.5%

## You approximate $e^{\given{x}}$ with $1 + x + \frac{x^2}{2} + \frac{x^3}{6}$. Using $M = 3$, what is the error bound?

::: misses
- [any] $\frac{3}{4!}x^4$.
:::
:::

::: check
given: x = 0.5 | 1 | 1.5
answer: x^5/120
tolerance: 0.5%

## What is the bound on the error of $\sin\given{x} \approx x - \frac{x^3}{6}$?

::: misses
- [x^4/24] Use the bonus: the $x^4$ term of sine is zero.
- [any] $\frac{|x|^5}{5!}$.
:::
:::
:::
