---
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 define an example function whose Taylor series we'd like to compute.


```python
var( 'x' )
formula = exp( 2*x + 1 )
formula
```




$\displaystyle e^{2 x + 1}$



Let's ask for a degree-5 Taylor series centered at $x=2$.  From the code below,
you can tell that the third parameter is the center point and the fourth
parameter is the degree.


```python
series( formula, x, 2, 5 )
```




$\displaystyle e^{5} + 2 \left(x - 2\right) e^{5} + 2 \left(x - 2\right)^{2} e^{5} + \frac{4 \left(x - 2\right)^{3} e^{5}}{3} + \frac{2 \left(x - 2\right)^{4} e^{5}}{3} + O\left(\left(x - 2\right)^{5}; x\rightarrow 2\right)$



The final term (starting with O---oh, not zero) means that there are more terms
in the infinite Taylor series not shown in this finite approximation.
If you want to show just the approximation, you can tell it to remove the O term.


```python
series( formula, x, 2, 5 ).removeO()
```




$\displaystyle \frac{2 \left(x - 2\right)^{4} e^{5}}{3} + \frac{4 \left(x - 2\right)^{3} e^{5}}{3} + 2 \left(x - 2\right)^{2} e^{5} + 2 \left(x - 2\right) e^{5} + e^{5}$



You can also compute individual coefficients in a Taylor series
by remembering the formula for the $n^\text{th}$ term in the series and applying it,
as follows.  The formula for a series centered on $x=a$ is $\frac{f^{(n)}(a)}{n!}$.

From the answer above, we can see that the coefficient on the $n=3$ term is $\frac43e^5$.


```python
n = 3
a = 2
diff( formula, x, n ).subs( x, a ) / factorial( n )
```




$\displaystyle \frac{4 e^{5}}{3}$



118273e60145cf84412ed75063b60a78dce45b48
ipynb
Jupyter Notebook
database/tasks/How to compute the Taylor series for a function/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to compute the Taylor series for a function/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to compute the Taylor series for a function/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