LeoMath

Foundations of calculus

Taylor expansion

Approximating functions by polynomials, with error control.

about 9 min

Start from a problem

Problem

A calculator can only add, subtract, multiply and divide. How does it compute sin⁡(0.3)\sin(0.3) or e0.3e^{0.3}?

We need a way to approximate any smooth function by expressions using only those four operations (polynomials), and we must be able to say how large the error is; otherwise the approximation is worthless.

Observe

How does a calculator compute sin⁡(0.3)\sin(0.3)? It can only add, subtract, multiply and divide. But near 00: sin⁡x≈x,sin⁡x≈x−x36,sin⁡x≈x−x36+x5120.\sin x\approx x,\qquad\sin x\approx x-\frac{x^3}6,\qquad\sin x\approx x-\frac{x^3}6+\frac{x^5}{120}. At x=0.3x=0.3 these give 0.30.3, 0.29550.2955, 0.2955200.295520, while the true value is 0.295520…0.295520\ldots. The longer the polynomial, the better the approximation.

Conjecture

If a polynomial pp is to "look like" ff near x0x_0, the natural demand is that its value, slope, curvature, … at x0x_0 all agree with those of ff: p(k)(x0)=f(k)(x0)p^{(k)}(x_0)=f^{(k)}(x_0) for k=0,1,…,nk=0,1,\dots,n. Such a polynomial is unique, and the error should shrink as nn grows.

Interactive experimentTaylor approximation
Function
P3(x)=∑k=03f(k)(a)k!(x−a)kP_{3}(x)=\sum_{k=0}^{3}\frac{f^{(k)}(a)}{k!}(x-a)^{k}max error on [a−2, a+2] 0.2426

Definition

Definition 5.1Taylor polynomial

Let ff have nn derivatives at x0x_0. The polynomial Pn(x)=∑k=0nf(k)(x0)k!(x−x0)kP_n(x)=\sum_{k=0}^n\frac{f^{(k)}(x_0)}{k!}(x-x_0)^k is the nn-th Taylor polynomial of ff at x0x_0, and Rn(x)=f(x)−Pn(x)R_n(x)=f(x)-P_n(x) is the remainder.

Why f(k)(x0)k!\dfrac{f^{(k)}(x_0)}{k!}? Differentiate PnP_n kk times and set x=x0x=x_0: of the terms (x−x0)j(x-x_0)^j, only j=kj=k survives, contributing k!k!. So Pn(k)(x0)=f(k)(x0)P_n^{(k)}(x_0)=f^{(k)}(x_0), exactly the demand in the conjecture.

Theorem and proof

Polynomials are easy to evaluate, but an approximation without an error bound is meaningless. The theorem gives the error exactly.

Theorem 5.1Taylor's theorem (Lagrange remainder)

Let ff have n+1n+1 derivatives on an interval containing x0x_0 and xx. Then there is ξ\xi between x0x_0 and xx with Rn(x)=f(n+1)(ξ)(n+1)!(x−x0)n+1.R_n(x)=\frac{f^{(n+1)}(\xi)}{(n+1)!}(x-x_0)^{n+1}.

Proof

Fix x≠x0x\ne x_0 and choose MM with f(x)−Pn(x)=M(x−x0)n+1f(x)-P_n(x)=M(x-x_0)^{n+1}. Define g(t)=f(t)−Pn(t)−M(t−x0)n+1.g(t)=f(t)-P_n(t)-M(t-x_0)^{n+1}. Then g(x0)=g′(x0)=⋯=g(n)(x0)=0g(x_0)=g'(x_0)=\cdots=g^{(n)}(x_0)=0 (since PnP_n matches ff to order nn at x0x_0 and the first nn derivatives of (t−x0)n+1(t-x_0)^{n+1} vanish there) and g(x)=0g(x)=0.

By Rolle's theorem gg has a zero ξ1\xi_1 between x0x_0 and xx; then g′g' vanishes at x0x_0 and ξ1\xi_1, so has a zero ξ2\xi_2; after n+1n+1 steps, g(n+1)(ξn+1)=0g^{(n+1)}(\xi_{n+1})=0. Since Pn(n+1)≡0P_n^{(n+1)}\equiv0 and ((t−x0)n+1)(n+1)=(n+1)!\bigl((t-x_0)^{n+1}\bigr)^{(n+1)}=(n+1)!, g(n+1)(ξ)=f(n+1)(ξ)−M(n+1)!=0 ⇒ M=f(n+1)(ξ)(n+1)!.g^{(n+1)}(\xi)=f^{(n+1)}(\xi)-M(n+1)!=0\ \Rightarrow\ M=\frac{f^{(n+1)}(\xi)}{(n+1)!}.

Example 5.1Bounding an error with the remainder

Approximate cos⁡x\cos x by 1−x221-\frac{x^2}2 for ∣x∣≤0.5|x|\le0.5. This polynomial is in fact P3P_3 (the x3x^3 coefficient is zero), so ∣R3(x)∣=∣cos⁡(4)(ξ)4!x4∣≤0.5424≈0.0026.|R_3(x)|=\left|\frac{\cos^{(4)}(\xi)}{4!}x^4\right|\le\frac{0.5^4}{24}\approx0.0026.

Definition 5.2Taylor series

If ff is infinitely differentiable at x0x_0, ∑k=0∞f(k)(x0)k!(x−x0)k\displaystyle\sum_{k=0}^\infty\frac{f^{(k)}(x_0)}{k!}(x-x_0)^k is its Taylor series. The series converges to f(x)f(x) if and only if Rn(x)→0R_n(x)\to0.

Proposition 5.2The series of e^x, sin, cos converge to them everywhere

ex=∑k=0∞xkk!,sin⁡x=∑k=0∞(−1)kx2k+1(2k+1)!,cos⁡x=∑k=0∞(−1)kx2k(2k)!.e^x=\sum_{k=0}^\infty\frac{x^k}{k!},\qquad\sin x=\sum_{k=0}^\infty\frac{(-1)^kx^{2k+1}}{(2k+1)!},\qquad\cos x=\sum_{k=0}^\infty\frac{(-1)^kx^{2k}}{(2k)!}.

Proof

All derivatives of these functions are uniformly bounded on any bounded interval (the derivative of exe^x is itself; those of sin⁡,cos⁡\sin,\cos have absolute value at most 1). Fix xx and let C=max⁡∣ξ∣≤∣x∣∣f(n+1)(ξ)∣C=\max_{|\xi|\le|x|}|f^{(n+1)}(\xi)| (e∣x∣e^{|x|} for exe^x, 11 for sin⁡,cos⁡\sin,\cos). Then ∣Rn(x)∣≤C∣x∣n+1(n+1)!→0,|R_n(x)|\le C\frac{|x|^{n+1}}{(n+1)!}\to0, because ∣x∣nn!→0\dfrac{|x|^n}{n!}\to0 (from some point on each term is multiplied by a factor below 12\tfrac12).

Remark

Not every Taylor series converges to its function. f(x)=e−1/x2f(x)=e^{-1/x^2} with f(0)=0f(0)=0 has all derivatives equal to 00 at 00; its Taylor series is identically 00, yet ff is not. The remainder Rn(x)=f(x)R_n(x)=f(x) never tends to 00. The clause "if and only if Rn→0R_n\to0" in the definition is not decoration.

Common mistake

A Taylor series converging is not the same as converging to ff. For f(x)=e−1/x2f(x)=e^{-1/x^2} (with f(0)=0f(0)=0) the series converges everywhere (to 00) yet equals ff only at x=0x=0. The only way to know "series == function" is to prove the remainder Rn→0R_n\to0.

Applications

ApplicationWhere numerical error comes from

In Numerical solutions, Euler's method has local error O(h2)O(h^2) and RK4 has O(h5)O(h^5). All such statements come from expanding the true solution y(t+h)y(t+h) in a Taylor series and comparing it term by term with the numerical scheme.

ApplicationEuler's formula

Splitting the series of eiθe^{i\theta} into real and imaginary parts gives eiθ=cos⁡θ+isin⁡θe^{i\theta}=\cos\theta+i\sin\theta. This is why complex characteristic roots produce trigonometric solutions in Second-order linear equations.

Exercises

01
In the Taylor series of sin⁡x\sin x at 00, what is the coefficient of x3x^3 (four decimals)?
02
Approximating cos⁡x\cos x by 1−x221-\tfrac{x^2}{2} for ∣x∣≤0.5|x|\le0.5, the Lagrange remainder bounds the error by approximately