TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › Expanding Functions into Series

Expanding Functions into Series

Many important functions can be written as power series: exe^x, sin⁡x\sin x, cos⁡x\cos x, ln⁡(1+x)\ln(1 + x) and 11−x\frac{1}{1 - x} all have simple, memorable expansions. This lesson lists them, shows how to build new expansions from old ones by substitution, and checks them numerically. The general recipe behind them (Taylor's formula) uses derivatives, so here we take the series as given and focus on using them.

The standard expansions

Each of these is a power series centred at 0 (a Maclaurin series):

Function Series Converges for
11−x\frac{1}{1 - x} 1+x+x2+x3+⋯1 + x + x^2 + x^3 + \cdots −1<x<1-1 < x < 1
exe^x 1+x+x22!+x33!+x44!+⋯1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \cdots all xx
sin⁡x\sin x x−x33!+x55!−x77!+⋯x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots all xx
cos⁡x\cos x 1−x22!+x44!−x66!+⋯1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + \cdots all xx
ln⁡(1+x)\ln(1 + x) x−x22+x33−x44+⋯x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4} + \cdots −1<x≤1-1 < x \le 1
(1+x)k(1 + x)^k 1+kx+k(k−1)x22!+⋯1 + kx + \frac{k(k-1)x^2}{2!} + \cdots −1<x<1-1 < x < 1

Patterns worth noticing: sin⁡x\sin x has only odd powers (it is an odd function, sin⁡(−x)=−sin⁡x\sin(-x) = -\sin x), cos⁡x\cos x has only even powers (cos⁡(−x)=cos⁡x\cos(-x) = \cos x), and both alternate in sign. The last row is the general binomial series; for a whole number kk it stops and becomes the ordinary binomial expansion.

Checking an expansion

Truncating a series gives a polynomial approximation. Let's compare sin⁡x\sin x with polynomials of increasing degree:

python · try it yourself
import math

def sin_poly(x, degree):
    total, sign = 0.0, 1
    for n in range(1, degree + 1, 2):
        total += sign * x**n / math.factorial(n)
        sign = -sign
    return total

print(" x     sin x     deg 1     deg 3     deg 5     deg 7")
for x in [0.1, 0.5, 1.0, 2.0, 3.0]:
    row = [sin_poly(x, d) for d in (1, 3, 5, 7)]
    print(f"{x:3.1f} {math.sin(x):9.5f} " + " ".join(f"{v:9.5f}" for v in row))
 x     sin x     deg 1     deg 3     deg 5     deg 7
0.1   0.09983   0.10000   0.09983   0.09983   0.09983
0.5   0.47943   0.50000   0.47917   0.47943   0.47943
1.0   0.84147   1.00000   0.83333   0.84167   0.84147
2.0   0.90930   2.00000   0.66667   0.93333   0.90794
3.0   0.14112   3.00000  -1.50000   0.52500   0.09107

Close to 0 even the degree-1 polynomial xx is good: that is the small-angle approximation sin⁡x≈x\sin x \approx x used in physics and game code. Further away you need more terms. A picture shows how each extra term hugs the curve over a wider range:

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

x = np.linspace(-2 * math.pi, 2 * math.pi, 400)
plt.figure(figsize=(6, 4))
plt.plot(x, np.sin(x), "k", lw=2.5, label="sin x")
for degree in (1, 3, 5, 9):
    y = sum((-1)**k * x**(2 * k + 1) / math.factorial(2 * k + 1) for k in range((degree + 1) // 2))
    plt.plot(x, y, label=f"degree {degree}")
plt.ylim(-2, 2)
plt.legend()
plt.title("Polynomial approximations of sin x")
print("plotted 4 approximations")
plotted 4 approximations

New series from old

You rarely need to start from scratch. Substitute, multiply or add known series:

e−x2:put −x2 into ex→1−x2+x42!−x63!+⋯11+x2:put −x2 into 11−x→1−x2+x4−x6+⋯(∣x∣<1)x⋅ex:multiply ex by x→x+x2+x32!+x43!+⋯cosh⁡x:ex+e−x2→1+x22!+x44!+⋯(cos⁡x without the minus signs)\begin{aligned} &e^{-x^2}{:} && \text{put } -x^2 \text{ into } e^x && \to 1 - x^2 + \frac{x^4}{2!} - \frac{x^6}{3!} + \cdots \\ &\frac{1}{1 + x^2}{:} && \text{put } -x^2 \text{ into } \frac{1}{1 - x} && \to 1 - x^2 + x^4 - x^6 + \cdots && (\lvert x \rvert < 1) \\ &x \cdot e^x{:} && \text{multiply } e^x \text{ by } x && \to x + x^2 + \frac{x^3}{2!} + \frac{x^4}{3!} + \cdots \\ &\cosh x{:} && \frac{e^x + e^{-x}}{2} && \to 1 + \frac{x^2}{2!} + \frac{x^4}{4!} + \cdots && (\cos x \text{ without the minus signs}) \end{aligned}

SymPy's series function produces these expansions for you, which is a good way to check your work:

python · try it yourself
import sympy as sp

x = sp.symbols("x")
for f in [sp.exp(-x**2), 1 / (1 + x**2), sp.cos(x), sp.log(1 + x)]:
    print(f, "=", sp.series(f, x, 0, 7))
exp(-x**2) = 1 - x**2 + x**4/2 - x**6/6 + O(x**7)
1/(x**2 + 1) = 1 - x**2 + x**4 - x**6 + O(x**7)
cos(x) = 1 - x**2/2 + x**4/24 - x**6/720 + O(x**7)
log(x + 1) = x - x**2/2 + x**3/3 - x**4/4 + x**5/5 - x**6/6 + O(x**7)

O(x**7) means "plus terms of degree 7 and higher". SymPy writes log for the natural logarithm ln⁡\ln.

In software

  • Approximations in hot loops. Graphics and audio code sometimes replaces sin⁡x\sin x by a short polynomial when speed matters more than the last digit; cos⁡x≈1−x2/2\cos x \approx 1 - x^2/2 is the standard small-angle shortcut.
  • Numerical stability. For tiny xx, computing ex−1e^x - 1 directly loses digits (1.0000000001−11.0000000001 - 1). The series x+x2/2+⋯x + x^2/2 + \cdots does not, which is why libraries offer math.expm1 and math.log1p.
  • Machine learning. Activation functions and the softmax are built on exe^x, and the analysis of gradient descent rests on the quadratic (degree-2) approximation of a function.

How the coefficients are found (from derivatives) and how big the error of a truncated series can be are the topics of Taylor Series and Approximation Error, and in depth Taylor and Maclaurin Series.

Exercises

Exercise 1: Substitute

Write the first four nonzero terms of e3xe^{3x}.

Answer

1+3x+9x22+9x321 + 3x + \frac{9x^2}{2} + \frac{9x^3}{2}

Solution
eu=1+u+u22+u36+⋯ , with u=3x:1+3x+9x22+27x36=1+3x+9x22+9x32\begin{aligned} &e^u = 1 + u + \frac{u^2}{2} + \frac{u^3}{6} + \cdots, \text{ with } u = 3x{:} \\ &1 + 3x + \frac{9x^2}{2} + \frac{27x^3}{6} = 1 + 3x + \frac{9x^2}{2} + \frac{9x^3}{2} \end{aligned}

Exercise 2: Estimate a value

Use the first three nonzero terms of cos⁡x\cos x to estimate cos⁡0.2\cos 0.2. How far is it from the true value?

Answer

0.9800667 (about 9⋅10−89 \cdot 10^{-8} too big).

Solution
cos⁡0.2≈1−0.042+0.001624=1−0.02+0.0000667=0.9800667The next term, −0.26/720≈−8.9⋅10−8, is about the size of the error.\begin{aligned} &\cos 0.2 \approx 1 - \frac{0.04}{2} + \frac{0.0016}{24} = 1 - 0.02 + 0.0000667 = 0.9800667 \\ &\text{The next term, } -0.2^6/720 \approx -8.9 \cdot 10^{-8}, \text{ is about the size of the error.} \end{aligned}
python · try it yourself
import math

approx = 1 - 0.2**2 / 2 + 0.2**4 / 24
print(round(approx, 9), round(math.cos(0.2), 9), f"{approx - math.cos(0.2):.2e}")
0.980066667 0.980066578 8.88e-08

Exercise 3: ln⁡2\ln 2 the slow way

Use ln⁡(1+x)\ln(1 + x) at x=1x = 1 to estimate ln⁡2\ln 2 with 1000 terms. Then use ln⁡2=−ln⁡(1−1/2)=12+12⋅22+13⋅23+⋯\ln 2 = -\ln(1 - 1/2) = \frac{1}{2} + \frac{1}{2 \cdot 2^2} + \frac{1}{3 \cdot 2^3} + \cdots with 20 terms. Which is better?

Answer

The second: 20 terms give about 7 correct digits, while 1000 terms of the first give only 3.

Solution

At x=1x = 1 the series is at the edge of its interval and converges very slowly (error about 1/(2⋅1000)1/(2 \cdot 1000)). The second series has ratio 12\frac{1}{2} and converges geometrically.

python · try it yourself
import math

slow = sum((-1)**(n + 1) / n for n in range(1, 1001))
fast = sum(1 / (n * 2**n) for n in range(1, 21))
print(f"slow {slow:.10f}  error {abs(slow - math.log(2)):.1e}")
print(f"fast {fast:.10f}  error {abs(fast - math.log(2)):.1e}")
slow 0.6926474306  error 5.0e-04
fast 0.6931471371  error 4.4e-08

Exercise 4: Odd or even

Without SymPy, write the series of x⋅sin⁡xx \cdot \sin x up to x6x^6 and say whether it is odd or even.

Answer

x2−x46+x6120x^2 - \frac{x^4}{6} + \frac{x^6}{120}; it is even.

Solution
x⋅(x−x36+x5120−⋯ )=x2−x46+x6120−⋯Only even powers appear, matching (−x)⋅sin⁡(−x)=x⋅sin⁡x.\begin{aligned} &x \cdot \left(x - \frac{x^3}{6} + \frac{x^5}{120} - \cdots\right) = x^2 - \frac{x^4}{6} + \frac{x^6}{120} - \cdots \\ &\text{Only even powers appear, matching } (-x) \cdot \sin(-x) = x \cdot \sin x. \end{aligned}

Test yourself

Which is the series for cos⁡x\cos x?

  1. x−x33!+x55!−⋯x - \frac{x^3}{3!} + \frac{x^5}{5!} - \cdots
  2. 1+x+x22!+⋯1 + x + \frac{x^2}{2!} + \cdots
  3. 1−x22!+x44!−⋯1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \cdots
  4. x−x22+x33−⋯x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots

What is the series of 11+x\frac{1}{1 + x} for ∣x∣<1\lvert x \rvert < 1?

  1. 1+x+x2+⋯1 + x + x^2 + \cdots
  2. 1−x+x2−x3+⋯1 - x + x^2 - x^3 + \cdots
  3. x−x22+x33−⋯x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots
  4. 1−x22+⋯1 - \frac{x^2}{2} + \cdots

Why is sin⁡x≈x\sin x \approx x a good approximation for small angles (in radians)?

  1. Because the next term, −x3/6-x^3/6, is tiny when xx is small
  2. Because sin⁡\sin is linear
  3. It only works in degrees
  4. Because sin⁡0=1\sin 0 = 1