Link Search Menu Expand Document (external link)

How to compute the error bounds on a Taylor approximation (in Python, using SymPy)

See all solutions.

Task

A Taylor series approximation of degree $n$ to the function $f(x)$, centered at the point $x=a$, has an error bounded by the following formula, where $c$ ranges over all points between $x=a$ and the point $x=x_0$ at which we will be applying the approximation.

\[\frac{|x_0-a|^{n+1}}{(n+1)!}\max|f^{(n+1)}(c)|\]

How can we compute this error bound using mathematical software?

Related tasks:

Solution

This answer assumes you have imported SymPy as follows.

1
2
from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output

Let’s create a simple example. We’ll be approximating $f(x)=\sin x$ centered at $a=0$ with a Taylor series of degree $n=5$. We will be applying our approximation at $x_0=1$. What is the error bound?

1
2
3
4
5
var( 'x' )
formula = sin(x)
a = 0
x_0 = 1
n = 5

We will not ask SymPy to compute the formula exactly, but will instead have it sample a large number of $c$ values from the interval in question, and compute the maximum over those samples. (The exact solution can be too hard for SymPy to compute.)

1
2
3
4
5
6
7
8
# Get 1000 evenly-spaced c values:
cs = [ Min(x_0,a) + abs(x_0-a)*i/1000 for i in range(1001) ]
# Create the formula |f^(n+1)(x)|:
formula2 = abs( diff( formula, x, n+1 ) )
# Find the max of it on all the 1000 values:
m = Max( *[ formula2.subs(x,c) for c in cs ] )
# Compute the error bound:
N( abs(x_0-a)**(n+1) / factorial(n+1) * m )

$\displaystyle 0.00116870970112208$

The error is at most $0.00116871\ldots$.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)