Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 68 ยท Derivatives

Taylor and Maclaurin Series

Approximate functions with polynomials: Taylor and Maclaurin series, the remainder, series for e^x, sin, cos and ln(1+x), sympy series() and plots.

A computer can only add, subtract, multiply and divide. Yet your calculator returns sin(0.3) and e**2 in a blink. Behind the scenes, functions like these are replaced by polynomials that are close enough to the real thing. The tool that builds those polynomials is the Taylor series, and it is made entirely out of derivatives.

The idea extends linearization. A tangent line matches a function's value and slope at one point. A Taylor polynomial also matches the second derivative, the third, and so on, so it hugs the curve for longer.

Building the polynomial

Pick a centre a. We want a polynomial P(x) whose value and first n derivatives at a equal those of f. Matching them one by one gives the Taylor polynomial of degree n:

P_n(x) = f(a) + f'(a)(x - a) + f''(a)/2! (x - a)**2 + ... + f^(n)(a)/n! (x - a)**n

Each term is a derivative at the centre, divided by a factorial, times a power of (x - a). The factorial appears because differentiating (x - a)**k exactly k times produces k!, and we divide it away so the derivative matches.

When the centre is a = 0, the series is called a Maclaurin series. It is the most common case.

flowchart LR
    A["f(a): value"] --> B["f'(a): slope"]
    B --> C["f''(a): curvature"]
    C --> D["higher derivatives"]
    D --> E["Taylor polynomial P_n"]

Each extra derivative adds one more term and one more degree of agreement with the function.

The series you will meet again and again

For e**x, every derivative is e**x, and at 0 it equals 1. So:

e**x = 1 + x + x**2/2! + x**3/3! + x**4/4! + ...

For sin(x), the derivatives cycle through cos, -sin, -cos, sin, and at 0 they are 1, 0, -1, 0 in turn, so only odd powers survive:

sin(x) = x - x**3/3! + x**5/5! - x**7/7! + ...

For cos(x) only even powers survive:

cos(x) = 1 - x**2/2! + x**4/4! - x**6/6! + ...

For ln(1 + x) the derivatives give

ln(1 + x) = x - x**2/2 + x**3/3 - x**4/4 + ...

and the geometric-type series 1/(1 - x) = 1 + x + x**2 + x**3 + ... comes for free.

Python Partial sums of the exponential series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each extra term cuts the error by a large factor. Ten terms already give about seven correct decimal places at x = 1.

Asking sympy for the series

You rarely expand series by hand. In sympy, series(f, x, x0, n) returns the expansion around x0 up to (but not including) order n, followed by an O(...) term that stands for "everything we left out".

Python series() in sympy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Calling .removeO() gives an ordinary polynomial that you can differentiate, integrate or evaluate.

Seeing the approximation improve

Plotting shows the story better than any table. Near the centre every Taylor polynomial is excellent. Farther away each one eventually peels off the curve, but higher degrees stay faithful for longer.

Python Taylor polynomials of sin(x)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How big is the error? The remainder

A truncated series is only useful if you know how wrong it is. The difference R_n(x) = f(x) - P_n(x) is the remainder. Taylor's theorem (in Lagrange form) says that for some number c between a and x:

R_n(x) = f^(n+1)(c) / (n+1)! * (x - a)**(n+1)

You do not know c, but you can often bound the derivative. For sin(x) every derivative is at most 1 in absolute value, so after the term of degree n the error is at most |x|**(n+1) / (n+1)!. For x = 0.5 and n = 5, that is 0.5**6 / 720, about 2.2e-5.

Two lessons follow. The error shrinks very fast when |x - a| is small, and the bound grows when you move far from the centre. Choose the centre near where you will evaluate.

Where a series converges

Not every Taylor series reproduces its function everywhere. The series for e**x, sin(x) and cos(x) converge for all real x. But ln(1 + x) converges only for -1 < x <= 1, and 1/(1 - x) only for |x| < 1. The size of this interval is the radius of convergence, studied in Sequences and Infinite Series.

Python A series that stops working

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For x = 0.5 the values settle onto the true number. For x = 1.5, outside the interval, the partial sums swing wildly and grow instead of settling. A polynomial can never be trusted outside the series' interval of convergence.

Why engineers care

Taylor series explain why sin(x) ~ x for small angles (a pendulum), why sqrt(1 + x) ~ 1 + x/2 for small x, and why e**x - 1 ~ x. They also reveal error terms: a finite-difference formula for a derivative is just a Taylor expansion rearranged. You will see that in Numerical Integration and Floating-Point Error and Conditioning.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the coefficient of x3 in the Maclaurin series of ex?

    The coefficient is f'''(0)/3!. Since f''' is e**x and equals 1 at 0, the coefficient is 1/6.
  2. 2. Which terms appear in the Maclaurin series of sin(x)?

    sin is an odd function, so only odd powers survive, and the signs alternate: x - x3/3! + x5/5! - ...
  3. 3. What is the difference between a Taylor series and a Maclaurin series?

    A Maclaurin series is the special case a = 0 of a Taylor series.
  4. 4. Why does the Lagrange remainder help in practice?

    You cannot find the unknown point c, but bounding the (n+1)-th derivative gives a guaranteed upper bound on the error.
  5. 5. In sympy, what does the O(x**6) in a series() result mean?

    O(x**6) is a placeholder for all omitted terms of degree 6 and above. Use removeO() to drop it.

Summary

  • A Taylor polynomial matches a function's value and its first n derivatives at a centre a; a Maclaurin series has centre 0.
  • Key series: e**x, sin(x), cos(x), ln(1 + x) and 1/(1 - x).
  • sympy.series(f, x, x0, n) expands a function; .removeO() turns the result into a polynomial.
  • The remainder R_n measures the truncation error and can be bounded using the next derivative.
  • A series converges only inside its radius of convergence; outside it, adding terms makes things worse.

Next: The Integral as Accumulated Change.