---
author: Nathan Carter (ncarter@bentley.edu)
---

This answer assumes you have imported SymPy as follows.


```python
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?


```python
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.)


```python
# 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$.

4993b8178d24d78fc53cec86f83d4a6933804dad
ipynb
Jupyter Notebook
database/tasks/How to compute the error bounds on a Taylor approximation/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to compute the error bounds on a Taylor approximation/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to compute the error bounds on a Taylor approximation/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
2021-07-18T19:01:29.000Z
2022-03-29T06:47:11.000Z
Qwen/Qwen-72B
1. YES
2. YES