37. Taylor and Maclaurin series

📖 Reading · 10 min
💡 Every code box below is live — edit it and hit Run.

§8.0 studied power series in the abstract — given coefficients c_n, find where the series converges. This lesson runs the question the other direction: given a function, find the coefficients that make its power series actually equal that function. The answer connects straight back to §2.10's higher-order derivatives, and it's arguably the single most consequential construction in this course.

Building the coefficients from derivatives

Suppose f(x)=\sum_{n=0}^\infty c_n(x-a)^n near x=a — assume, for now, that such a representation exists, and work out what the c_n must be. Set x=a: every term with n\ge1 vanishes (since (x-a)^n=0), leaving f(a)=c_0.

Differentiate the series term by term (this is legitimate inside the radius of convergence — a fact whose careful justification is §8.4's topic) and set x=a again:

f'(x)=c_1+2c_2(x-a)+3c_3(x-a)^2+\cdots\ \Longrightarrow\ f'(a)=c_1

Differentiate once more:

f''(x)=2c_2+6c_3(x-a)+\cdots\ \Longrightarrow\ f''(a)=2c_2\ \Longrightarrow\ c_2=\frac{f''(a)}2

The pattern is now visible: differentiating n times and evaluating at a isolates c_n multiplied by n! (every lower power vanishes at x=a; every higher power still has a factor of (x-a) left over). This gives the Taylor coefficients:

c_n=\frac{f^{(n)}(a)}{n!}

f(x)=\sum_{n=0}^\infty\frac{f^{(n)}(a)}{n!}(x-a)^n

A Maclaurin series is simply a Taylor series centered at a=0 — a special case worth naming because so many of the standard examples are built there.

Reading the low-order terms

The first two terms of any Taylor series, f(a)+f'(a)(x-a), are exactly §3.1's linear approximation — the tangent line. Adding the next term, \frac{f''(a)}2(x-a)^2, produces the tangent parabola, matching not just the function's value and slope at a but its concavity too. Each additional term matches one more derivative — a Taylor series is linear approximation, continued indefinitely, and §8.2 will make precise just how much better each additional term makes the match.

The standard Maclaurin series

Three series worth recognizing on sight, all convergent for every real x (R=\infty — see §8.0):

e^x=\sum_{n=0}^\infty\frac{x^n}{n!}=1+x+\frac{x^2}2+\frac{x^3}6+\cdots

\sin x=\sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!}=x-\frac{x^3}6+\frac{x^5}{120}-\cdots

\cos x=\sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!}=1-\frac{x^2}2+\frac{x^4}{24}-\cdots

Why \sin x has only odd powers and \cos x only even ones: their derivatives cycle through \sin,\cos,-\sin,-\cos with period 4, and at x=0, \sin(0)=0 kills every even-order coefficient of \sin x's series while \cos(0)=1 and its cycle kills every odd-order coefficient of \cos x's — a direct fingerprint of \sin being an odd function and \cos being even (§0.2), showing up automatically in the coefficient formula rather than needing to be imposed separately.

Doing it in Python

Building the Maclaurin series for e^x term by term, straight from the derivative formula (every derivative of e^x is e^x itself, so f^{(n)}(0)=1 for every n):

import sympy as sp

x = sp.Symbol('x')
f = sp.exp(x)

terms = []
fn = f
for n in range(6):
    coeff = fn.subs(x, 0) / sp.factorial(n)
    terms.append(coeff * x**n)
    fn = sp.diff(fn, x)

print(f"Maclaurin series for e^x: {sum(terms)}")
print(f"SymPy's built-in series:  {sp.series(f, x, 0, 6).removeO()}")

The Taylor series of \ln x centered at a=1 (it can't be expanded at a=0 at all, since \ln0 is undefined) — a genuine Taylor series, not a Maclaurin one:

import sympy as sp

x = sp.Symbol('x')
f = sp.ln(x)

coeffs = []
fn = f
for n in range(5):
    coeffs.append(fn.subs(x, 1) / sp.factorial(n))
    fn = sp.diff(fn, x)

print(f"coefficients c_0 through c_4: {coeffs}")
print("series: (x-1) - (x-1)^2/2 + (x-1)^3/3 - (x-1)^4/4 + ...")

Confirming that truncating a Taylor series at more and more terms tracks the true function increasingly well near the center — the linear approximation idea, extended:

import sympy as sp

x = sp.Symbol('x')
f = sp.sin(x)
test_point = 0.5

for order in (1, 3, 5, 7):
    approx = sp.series(f, x, 0, order + 1).removeO()
    val = float(approx.subs(x, test_point))
    exact = float(f.subs(x, test_point))
    print(f"order {order}: approx = {val:.8f}, exact = {exact:.8f}, error = {abs(val-exact):.2e}")

Worked example

Find the first four nonzero terms of the Taylor series for f(x)=\ln x, centered at a=1.

f(x)=\ln x,\qquad f(1)=0 f'(x)=\frac1x,\qquad f'(1)=1 f''(x)=-\frac1{x^2},\qquad f''(1)=-1 f'''(x)=\frac2{x^3},\qquad f'''(1)=2 f^{(4)}(x)=-\frac6{x^4},\qquad f^{(4)}(1)=-6

c_0=0,\quad c_1=\frac1{1!}=1,\quad c_2=\frac{-1}{2!}=-\frac12,\quad c_3=\frac2{3!}=\frac13,\quad c_4=\frac{-6}{4!}=-\frac14

\ln x=(x-1)-\frac{(x-1)^2}2+\frac{(x-1)^3}3-\frac{(x-1)^4}4+\cdots

Sanity check. At x=1: every term with (x-1)^n vanishes, giving \ln1=0 — correct. The derivative of the series' linear term is 1, matching f'(1)=\frac11=1 exactly, confirming the tangent-line term is right before even checking the higher-order pieces. ✓ Notice the pattern in the coefficients, 1,-\frac12,\frac13,-\frac14,\ldots: this is literally the alternating harmonic series' term structure from §7.8, here appearing as a coefficient sequence rather than a numerical series in its own right.

Your turn

1. Find the Maclaurin series for f(x)=\dfrac1{1-x} up through the x^3 term, by computing derivatives directly — then compare to the geometric series formula from §7.3.

2. Find the first three nonzero terms of the Maclaurin series for f(x)=\cos x by computing f(0),f'(0),f''(0),f'''(0),f^{(4)}(0) directly.

3. True or false: the Taylor series of a function is guaranteed to converge to that same function everywhere the series itself converges.

Solutions

1. f(x)=(1-x)^{-1}, f'(x)=(1-x)^{-2}, f''(x)=2(1-x)^{-3}, f'''(x)=6(1-x)^{-4}. At x=0: f(0)=1, f'(0)=1, f''(0)=2, f'''(0)=6.

c_0=1,\quad c_1=1,\quad c_2=\frac22=1,\quad c_3=\frac66=1

\frac1{1-x}=1+x+x^2+x^3+\cdots

This is exactly §7.3's geometric series formula \frac a{1-r} with a=1, r=x — the Taylor-series machinery, applied to \frac1{1-x}, rebuilds a result already known from a completely different argument, a strong consistency check on the whole framework.

2. f(x)=\cos x: f(0)=1. f'(x)=-\sin x, f'(0)=0. f''(x)=-\cos x, f''(0)=-1. f'''(x)=\sin x, f'''(0)=0. f^{(4)}(x)=\cos x, f^{(4)}(0)=1.

c_0=1,\quad c_2=\frac{-1}{2!}=-\frac12,\quad c_4=\frac1{4!}=\frac1{24}

(odd coefficients all zero, as the concept section predicted)

\cos x=1-\frac{x^2}2+\frac{x^4}{24}-\cdots

matching the standard series listed above.

3. False. This surprising subtlety is exactly what §8.2's Taylor remainder is built to settle. A Taylor series can converge (as a power series, in the sense of §8.0) to a different value than f(x) itself, or even to a value that has nothing to do with f at certain points — the classic example is f(x)=e^{-1/x^2} (with f(0)=0), whose Maclaurin series is identically 0 everywhere, converging trivially, while the actual function is nonzero for every x\ne0. Matching every derivative at a point is not automatically the same as matching the function everywhere the resulting series happens to converge.

Check yourself in code

Compute the first 5 Maclaurin coefficients (c_0 through c_4) of f(x)=\ln(1+x) using derivatives, via SymPy.

Print exactly this:

c_0 = 0
c_1 = 1
c_2 = -1/2
c_3 = 1/3
c_4 = -1/4
import sympy as sp

x = sp.Symbol('x')
f = sp.ln(1 + x)

fn = f
for n in range(5):
    coeff = fn.subs(x, 0) / sp.factorial(n)
    print(f"c_{n} = ...")
    fn = sp.diff(fn, x)
import sympy as sp

x = sp.Symbol('x')
f = sp.ln(1 + x)

fn = f
for n in range(5):
    coeff = fn.subs(x, 0) / sp.factorial(n)
    print(f"c_{n} = {coeff}")
    fn = sp.diff(fn, x)

A Taylor series's coefficients, c_n=\frac{f^{(n)}(a)}{n!}, come directly from matching every derivative of the series to the function's own derivatives at the center — the tangent line and tangent parabola from Module 3, extended to infinitely many matched derivatives at once. Three series worth memorizing outright — e^x, \sin x, and \cos x — recur throughout the rest of this module, and the parity of \sin and \cos shows up automatically as which powers survive in their coefficient formulas. The one loose thread this lesson leaves dangling — whether a Taylor series actually equals the function it was built from — is §8.2's entire subject.

Next: how far off a truncated Taylor series can be from the true function, made precise with an explicit bound on the error.