Expanding Functions into Series
Many important functions can be written as power series: , , , and 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 |
|---|---|---|
| all | ||
| all | ||
| all | ||
Patterns worth noticing: has only odd powers (it is an odd function, ), has only even powers (), and both alternate in sign. The last row is the general binomial series; for a whole number it stops and becomes the ordinary binomial expansion.
Checking an expansion
Truncating a series gives a polynomial approximation. Let's compare with polynomials of increasing degree:
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 is good: that is the small-angle approximation 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:
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:
SymPy's series function produces these expansions for you, which is a good way to check your work:
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 .
In software
- Approximations in hot loops. Graphics and audio code sometimes replaces by a short polynomial when speed matters more than the last digit; is the standard small-angle shortcut.
- Numerical stability. For tiny , computing directly loses digits (). The series does not, which is why libraries offer
math.expm1andmath.log1p. - Machine learning. Activation functions and the softmax are built on , 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 .
Answer
Solution
Exercise 2: Estimate a value
Use the first three nonzero terms of to estimate . How far is it from the true value?
Answer
0.9800667 (about too big).
Solution
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: the slow way
Use at to estimate with 1000 terms. Then use 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 the series is at the edge of its interval and converges very slowly (error about ). The second series has ratio and converges geometrically.
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 up to and say whether it is odd or even.
Answer
; it is even.
Solution
Test yourself
Which is the series for ?
What is the series of for ?
Why is a good approximation for small angles (in radians)?
- Because the next term, , is tiny when is small
- Because is linear
- It only works in degrees
- Because