TExcelTAD
Home › Calculus Essentials › Taylor and Maclaurin Series

Taylor and Maclaurin Series

A Taylor series rebuilds a function from its derivatives at a single point. Cut it off after a few terms and you get a polynomial that imitates the function near that point. This is how calculators and math libraries compute sin, exp and log, and why physicists may replace sin⁡θ\sin \theta by θ\theta for small angles.

The formula

If ff has derivatives of every order at aa, its Taylor series at aa is

f(x)=f(a)+f′(a)(x−a)+f′′(a)2!(x−a)2+f′′′(a)3!(x−a)3+…=∑n=0∞f(n)(a)n!(x−a)n\begin{aligned} f(x) &= f(a) + f'(a)(x - a) + \frac{f''(a)}{2!} (x - a)^2 + \frac{f'''(a)}{3!} (x - a)^3 + \ldots \\ &= \sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!} (x - a)^n \end{aligned}

With a=0a = 0 it is called a Maclaurin series. The first two terms are the tangent line from The Derivative; each further term matches one more derivative, so the polynomial hugs the curve more and more closely.

The coefficient rule is easy to see: differentiate c0+c1x+c2x2+c3x3+…c_0 + c_1x + c_2x^2 + c_3x^3 + \ldots nn times and set x=0x = 0; only n!⋅cnn! \cdot c_n survives, so cn=f(n)(0)/n!c_n = f^{(n)}(0)/n!.

The series to know

Function Maclaurin series Valid for
exe^x 1+x+x2/2!+x3/3!+…1 + x + x^2/2! + x^3/3! + \ldots all xx
sin⁡x\sin x x−x3/3!+x5/5!−…x - x^3/3! + x^5/5! - \ldots all xx
cos⁡x\cos x 1−x2/2!+x4/4!−…1 - x^2/2! + x^4/4! - \ldots all xx
ln⁡(1+x)\ln(1 + x) x−x2/2+x3/3−…x - x^2/2 + x^3/3 - \ldots −1<x≤1-1 < x \le 1
1/(1−x)1/(1 - x) 1+x+x2+x3+…1 + x + x^2 + x^3 + \ldots ∣x∣<1\lvert x \rvert < 1
(1+x)k(1 + x)^k 1+kx+k(k−1)/2!⋅x2+…1 + kx + k(k - 1)/2! \cdot x^2 + \ldots ∣x∣<1\lvert x \rvert < 1

Derivation of the sine row: the derivatives of sin⁡\sin at 0 cycle 0,1,0,−1,0,1,…0, 1, 0, -1, 0, 1, \ldots, so only odd powers appear, with alternating signs.

python · try it yourself
import sympy as sp

x = sp.symbols("x")
for f in [sp.exp(x), sp.sin(x), sp.cos(x), sp.log(1 + x)]:
    print(f"{str(f):10}", sp.series(f, x, 0, 6))

h = sp.symbols("h")                       # x = 1 + h, a step away from 1
print("sqrt(1 + h):", sp.series(sp.sqrt(1 + h), h, 0, 3))
exp(x)     1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + O(x**6)
sin(x)     x - x**3/6 + x**5/120 + O(x**6)
cos(x)     1 - x**2/2 + x**4/24 + O(x**6)
log(x + 1) x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
sqrt(1 + h): 1 + h/2 - h**2/8 + O(h**3)

O(x**6) stands for "terms of order x6x^6 and higher, left out". The last line is the Taylor series of x\sqrt{x} centred at a=1a = 1, written with h=x−1h = x - 1: x≈1+(x−1)/2−(x−1)2/8\sqrt{x} \approx 1 + (x - 1)/2 - (x - 1)^2/8.

Watching the polynomials hug the curve

python · try it yourself
import math
import numpy as np
import matplotlib.pyplot as plt

xs = np.linspace(-2 * np.pi, 2 * np.pi, 400)

def taylor_sin(x, degree):
    return sum((-1) ** k * x ** (2 * k + 1) / math.factorial(2 * k + 1)
               for k in range(degree // 2 + 1))

plt.figure(figsize=(6, 4))
plt.plot(xs, np.sin(xs), color="black", linewidth=2, label="sin x")
for d in [1, 3, 5, 7]:
    plt.plot(xs, taylor_sin(xs, d), label=f"degree {d}")
plt.ylim(-3, 3)
plt.legend(loc="lower left")
print("at x = 2: ", [round(taylor_sin(2, d), 4) for d in (1, 3, 5, 7)], "sin 2 =", round(math.sin(2), 4))
at x = 2:  [2.0, 0.6667, 0.9333, 0.9079] sin 2 = 0.9093

Each extra term widens the region where the polynomial is a good copy of sin⁡x\sin x, but far from 0 every polynomial eventually shoots off.

In software: how math.exp and math.sin are computed

Library functions do not sum a long Taylor series for large xx; that would be slow and inaccurate. Instead they:

  1. Reduce the range: write x=kln⁡2+rx = k \ln 2 + r with ∣r∣≤ln⁡2/2≈0.35\lvert r \rvert \le \ln 2 / 2 \approx 0.35, so ex=2k⋅ere^x = 2^k \cdot e^r. Multiplying by 2k2^k is just adjusting the floating-point exponent. For sin⁡\sin, subtract multiples of π/2\pi/2 and use symmetries.
  2. Approximate on the small range with a short polynomial. Real libraries use minimax polynomials, close cousins of the Taylor polynomial whose coefficients are tuned to spread the error evenly, but a Taylor polynomial shows the idea.
python · try it yourself
import math

LN2 = math.log(2)

def my_exp(x, terms=12):
    k = round(x / LN2)                  # step 1: x = k·ln2 + r
    r = x - k * LN2
    p = 0.0
    for n in reversed(range(terms)):    # step 2: Taylor polynomial (Horner form)
        p = p * r + 1 / math.factorial(n)
    return math.ldexp(p, k)             # p · 2^k

for x in [0.3, 1.0, 10.0, -20.0, 300.0]:
    mine, ref = my_exp(x), math.exp(x)
    print(f"x={x:6}: my_exp = {mine:.15e}  relative error {abs(mine - ref) / ref:.1e}")
x=   0.3: my_exp = 1.349858807576002e+00  relative error 9.9e-16
x=   1.0: my_exp = 2.718281828459042e+00  relative error 9.8e-16
x=  10.0: my_exp = 2.202646579480671e+04  relative error 3.3e-16
x= -20.0: my_exp = 2.061153622438560e-09  relative error 8.0e-16
x= 300.0: my_exp = 1.942426395241234e+130  relative error 1.1e-14

Twelve terms on a range of ±0.35\pm 0.35 already give errors at the level of floating-point rounding (about 10−1510^{-15}). At x=300x = 300 the error grows to about 10−1410^{-14} because kln⁡2k \ln 2 with k=433k = 433 is itself rounded before the subtraction; real libraries store ln⁡2\ln 2 as a high and a low part (Cody–Waite reduction) to keep rr exact. GPU shader functions and fixed-point game math use the same two steps with fewer terms, trading accuracy for speed.

In physics: the small-angle approximation

Keeping only the first term, sin⁡θ≈θ\sin \theta \approx \theta (θ\theta in radians) and cos⁡θ≈1−θ2/2\cos \theta \approx 1 - \theta^2/2. The error of sin⁡θ≈θ\sin \theta \approx \theta is about θ3/6\theta^3/6, so for small angles it is tiny. This turns the pendulum equation into a solvable one, giving the period T≈2πL/gT \approx 2\pi\sqrt{L/g}, and lets game code use cheap approximations for small rotations per frame.

python · try it yourself
import math

print("angle   sin θ      θ (rad)   relative error")
for deg in [1, 5, 10, 20, 45]:
    th = math.radians(deg)
    print(f"{deg:4d}°   {math.sin(th):.6f}   {th:.6f}   {(th - math.sin(th)) / math.sin(th):.3%}")
angle   sin θ      θ (rad)   relative error
   1°   0.017452   0.017453   0.005%
   5°   0.087156   0.087266   0.127%
  10°   0.173648   0.174533   0.510%
  20°   0.342020   0.349066   2.060%
  45°   0.707107   0.785398   11.072%

Up to about 10° the error stays below 1%, which is why the approximation is standard for pendulums and small steering angles.

Exercises

Exercise 1: A Maclaurin series by substitution

Write the first four non-zero terms of the Maclaurin series of e2xe^{2x}.

Answer

1+2x+2x2+(4/3)x31 + 2x + 2x^2 + (4/3)x^3

Solution

Put 2x2x into the series of eue^u:

1+(2x)+(2x)22!+(2x)33!=1+2x+4x22+8x36=1+2x+2x2+43x31 + (2x) + \frac{(2x)^2}{2!} + \frac{(2x)^3}{3!} = 1 + 2x + \frac{4x^2}{2} + \frac{8x^3}{6} = 1 + 2x + 2x^2 + \frac{4}{3}x^3

Exercise 2: Taylor series away from zero

Find the Taylor polynomial of degree 3 for ln⁡x\ln x at a=1a = 1.

Answer

(x−1)−(x−1)2/2+(x−1)3/3(x - 1) - (x - 1)^2/2 + (x - 1)^3/3

Solution
f(1)=0,f′=1/x→1,f′′=−1/x2→−1,f′′′=2/x3→2P3=0+1⋅(x−1)+−12!(x−1)2+23!(x−1)3\begin{aligned} &f(1) = 0, \quad f' = 1/x \to 1, \quad f'' = -1/x^2 \to -1, \quad f''' = 2/x^3 \to 2 \\ &P_3 = 0 + 1 \cdot (x - 1) + \frac{-1}{2!} (x - 1)^2 + \frac{2}{3!} (x - 1)^3 \end{aligned}

Exercise 3: A limit with series

Use the sine series to find lim⁡(sin⁡x−x)/x3\lim (\sin x - x)/x^3 as x→0x \to 0.

Answer

−1/6-1/6

Solution
sin⁡x−x=−x3/6+x5/120−…(sin⁡x−x)/x3=−1/6+x2/120−…  →  −1/6\begin{aligned} \sin x - x &= -x^3/6 + x^5/120 - \ldots \\ (\sin x - x)/x^3 &= -1/6 + x^2/120 - \ldots \;\to\; -1/6 \end{aligned}

Exercise 4: Your own sine

Write my_sin(x) that first reduces x into [−π,π][-\pi, \pi] (using math.remainder(x, 2π)) and then sums the Taylor series up to x15x^{15}. Compare it with math.sin at x=1,10,100x = 1, 10, 100.

Answer

x=1x = 1 and x=100x = 100 agree to about 10−1510^{-15}. x=10x = 10 reduces to r≈−2.57r \approx -2.57, near the edge of [−π,π][-\pi, \pi], and the error is about 2.5×10−82.5 \times 10^{-8}: the first omitted term ∣r∣17/17!\lvert r \rvert^{17}/17! is that size. More terms, or reducing further with symmetries, would fix it. Without range reduction, x=100x = 100 would be hopeless.

Solution
python · try it yourself
import math

def my_sin(x):
    r = math.remainder(x, 2 * math.pi)       # now -π ≤ r ≤ π
    return sum((-1) ** k * r ** (2 * k + 1) / math.factorial(2 * k + 1) for k in range(8))

for x in [1, 10, 100]:
    print(x, f"{my_sin(x):.12f}", f"{math.sin(x):.12f}", f"{abs(my_sin(x) - math.sin(x)):.1e}")
1 0.841470984808 0.841470984808 2.8e-15
10 -0.544021085826 -0.544021110889 2.5e-08
100 -0.506365641110 -0.506365641110 3.3e-15

Test yourself

What is the coefficient of x4x^4 in the Maclaurin series of cos⁡x\cos x?

  1. 1/41/4
  2. 1/241/24
  3. −1/24-1/24
  4. −1/2-1/2

Why do math libraries apply range reduction before using a polynomial?

  1. Polynomials only work for integers
  2. A short polynomial is accurate only near its centre, so the input is first moved into a small range
  3. To avoid computing derivatives
  4. Range reduction is exact for every function

The small-angle approximation sin⁡θ≈θ\sin \theta \approx \theta has an error of roughly what size?

  1. θ/2\theta/2
  2. θ2\theta^2
  3. θ3/6\theta^3/6
  4. It is exact