Taylor Series Expansion with Python from Data Science Fabric

https://dsfabric.org/taylor-series-expansion-with-python

# LAB 9 Taylor Series

**Universidad Naional de Colombia - Sede Bogotá**

**Metodos Numericos**

**Docente:** _German Hernandez_

**Estudiante:**
  * Luis Miguel Báez Aponte - lmbaeza@unal.edu.co


```python

from sympy import series, Symbol
from sympy.functions import sin, cos, exp, log
from sympy.plotting import plot
import numpy as np
import matplotlib.pyplot as plt
```


```python
# Define symbol
x = Symbol('x')
```


```python

# Function for Taylor Series Expansion

def taylor(function, x0, n):
    """
    Parameter "function" is our function which we want to approximate
    "x0" is the point where to approximate
    "n" is the order of approximation
    """
    return function.series(x,x0,n).removeO()
```


```python
print('sin(x) ≅', taylor(sin(x), 0, 4))

print('ln(x+1) ≅', taylor(log(x+1), 0, 4))

```

    sin(x) ≅ -x**3/6 + x
    ln(x+1) ≅ x**3/3 - x**2/2 + x



```python
print('sin(1) =', taylor(sin(x), 0, 4).subs(x,1))

print('ln(1+1) =', taylor(log(x+1), 0, 4).subs(x,1))

```

    sin(1) = 5/6
    ln(1+1) = 5/6



```python
print('Taylor 0 sin(x) ≅', taylor(sin(x), 0, 0))
print('Taylor 1 sin(x) ≅', taylor(sin(x), 0, 1))
print('Taylor 2 sin(x) ≅', taylor(sin(x), 0, 2))
print('Taylor 3 sin(x) ≅', taylor(sin(x), 0, 3))
print('Taylor 4 sin(x) ≅', taylor(sin(x), 0, 4))
print('Taylor 5 sin(x) ≅', taylor(sin(x), 0, 5))
print('Taylor 6 sin(x) ≅', taylor(sin(x), 0, 6))
print('Taylor 7 sin(x) ≅', taylor(sin(x), 0, 7))
print('Taylor 8 sin(x) ≅', taylor(sin(x), 0, 8))
```

    Taylor 0 sin(x) ≅ 0
    Taylor 1 sin(x) ≅ 0
    Taylor 2 sin(x) ≅ x
    Taylor 3 sin(x) ≅ x
    Taylor 4 sin(x) ≅ -x**3/6 + x
    Taylor 5 sin(x) ≅ -x**3/6 + x
    Taylor 6 sin(x) ≅ x**5/120 - x**3/6 + x
    Taylor 7 sin(x) ≅ x**5/120 - x**3/6 + x
    Taylor 8 sin(x) ≅ -x**7/5040 + x**5/120 - x**3/6 + x



```python
print('Taylor 0 ln(x+1) ≅', taylor(log(x+1), 0, 0).subs(x,2),' = ',taylor(log(x+1), 0, 0).subs(x,2).evalf())
print('Taylor 1 ln(x+1) ≅', taylor(log(x+1), 0, 1).subs(x,2),' = ',taylor(log(x+1), 0, 1).subs(x,2).evalf())
print('Taylor 2 ln(x+1) ≅', taylor(log(x+1), 0, 2).subs(x,2),' = ',taylor(log(x+1), 0, 2).subs(x,2).evalf())
print('Taylor 3 ln(x+1) ≅', taylor(log(x+1), 0, 3).subs(x,2),' = ',taylor(log(x+1), 0, 3).subs(x,2).evalf())
print('Taylor 4 ln(x+1) ≅', taylor(log(x+1), 0, 4).subs(x,2),' = ',taylor(log(x+1), 0, 4).subs(x,2).evalf())
print('Taylor 5 ln(x+1) ≅', taylor(log(x+1), 0, 5).subs(x,2),' = ',taylor(log(x+1), 0, 5).subs(x,2).evalf())
print('Taylor 6 ln(x+1) ≅', taylor(log(x+1), 0, 6).subs(x,2),' = ',taylor(log(x+1), 0, 6).subs(x,2).evalf())
print('Taylor 7 ln(x+1) ≅', taylor(log(x+1), 0, 8).subs(x,2),' = ',taylor(log(x+1), 0, 7).subs(x,2).evalf())
```

    Taylor 0 ln(x+1) ≅ 0  =  0
    Taylor 1 ln(x+1) ≅ 0  =  0
    Taylor 2 ln(x+1) ≅ 2  =  2.00000000000000
    Taylor 3 ln(x+1) ≅ 0  =  0
    Taylor 4 ln(x+1) ≅ 8/3  =  2.66666666666667
    Taylor 5 ln(x+1) ≅ -4/3  =  -1.33333333333333
    Taylor 6 ln(x+1) ≅ 76/15  =  5.06666666666667
    Taylor 7 ln(x+1) ≅ 444/35  =  -5.60000000000000



```python
import math
print('sympy sin(x)subs(x,2) =', sin(x).subs(x,2))
print('sympy sin(x).subs(x,2).evalf() =', sin(x).subs(x,2).evalf())
print('math.sin(2) =', math.sin(2))
```

    sympy sin(x)subs(x,2) = sin(2)
    sympy sin(x).subs(x,2).evalf() = 0.909297426825682
    math.sin(2) = 0.9092974268256817



```python
import math
print('sympy ln(x+1)subs(x,2) =', log(x+1).subs(x,2))
print('sympy ln(x+1).subs(x,2).evalf() =', log(x+1).subs(x,2).evalf())
print('math.ln(2+1) =', math.log(2+1))
```

    sympy ln(x+1)subs(x,2) = log(3)
    sympy ln(x+1).subs(x,2).evalf() = 1.09861228866811
    math.ln(2+1) = 1.0986122886681098



```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_exp = np.exp(values)
t_exp1 = [taylor(exp(x), 0, 1).subs(x,v) for v in values]
legends = ['exp() ','Taylor 1 (constant)']

fig, ax = plt.subplots()
ax.plot(values,p_exp, color ='red')
ax.plot(values,t_exp1)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```


```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_sin = np.sin(values)
t_sin2 = [taylor(sin(x), 0, 2).subs(x,v) for v in values]
legends = ['sin() ','Taylor 2 (linear)']

fig, ax = plt.subplots()
ax.plot(values,p_sin, color ='red')
ax.plot(values,t_sin2)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```


```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_log = np.log(values)
t_log2 = [taylor(log(x+1), 0, 2).subs(x,v) for v in values]
legends = ['ln(x+1) ','Taylor 2 (linear)']

fig, ax = plt.subplots()
ax.plot(values,p_log, color ='red')
ax.plot(values,t_log2)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```


```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_sin = np.sin(values)
t_sin3 = [taylor(sin(x), 0, 3).subs(x,v) for v in values]
legends = ['sin(x) ','Taylor 3 (quadratic)']

fig, ax = plt.subplots()
ax.plot(values,p_sin, color ='red')
ax.plot(values,t_sin3)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```


```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_log = np.log(values)
t_log3 = [taylor(log(x+1), 0, 3).subs(x,v) for v in values]
legends = ['log(x+1) ','Taylor 3 (quadratic)']

fig, ax = plt.subplots()
ax.plot(values,p_log, color ='red')
ax.plot(values,t_log3)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```


```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_sin = np.sin(values)
t_sin4 = [taylor(sin(x), 0, 4).subs(x,v) for v in values]
legends = ['sin(x) ','Taylor 4 (cubic)']

fig, ax = plt.subplots()
ax.plot(values,p_sin, color ='red')
ax.plot(values,t_sin4)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```


```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_log = np.log(values)
t_log4 = [taylor(log(x+1), 0, 4).subs(x,v) for v in values]
legends = ['ln(x+1) ','Taylor 4 (cubic)']

fig, ax = plt.subplots()
ax.plot(values,p_log, color ='red')
ax.plot(values,t_log4)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```


```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_sin = np.sin(values)
t_sin1 = [taylor(sin(x), 0, 1).subs(x,v) for v in values]
t_sin2 = [taylor(sin(x), 0, 2).subs(x,v) for v in values]
t_sin3 = [taylor(sin(x), 0, 3).subs(x,v) for v in values]
t_sin4 = [taylor(sin(x), 0, 4).subs(x,v) for v in values]
legends = ['sin() ','Taylor 1 (constant)','Taylor 3 (linear)','Taylor 3 (quadratic)','Taylor 4 (cubic)']

fig, ax = plt.subplots()
ax.plot(values,p_sin)
ax.plot(values,t_sin1)
ax.plot(values,t_sin2)
ax.plot(values,t_sin3)
ax.plot(values,t_sin4)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```


```python
import math
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

values = np.arange(-5,5,0.1)
p_log = np.log(values)
t_log1 = [taylor(log(x+1), 0, 1).subs(x,v) for v in values]
t_log2 = [taylor(log(x+1), 0, 2).subs(x,v) for v in values]
t_log3 = [taylor(log(x+1), 0, 3).subs(x,v) for v in values]
t_log4 = [taylor(log(x+1), 0, 4).subs(x,v) for v in values]
legends = ['ln(x+1) ','Taylor 1 (constant)','Taylor 3 (linear)','Taylor 3 (quadratic)','Taylor 4 (cubic)']

fig, ax = plt.subplots()
ax.plot(values,p_log)
ax.plot(values,t_log1)
ax.plot(values,t_log2)
ax.plot(values,t_log3)
ax.plot(values,t_log4)

ax.set_ylim([-5,5])
ax.axhline(y=0.0, xmin=-5.0, xmax=5.0, color='black')
ax.axvline(x=0.0, ymin=-10.0, ymax=10.0, color='black')
ax.legend(legends)

plt.show()
```

0709fcf18ca1bbd32690ad4e574ff05a9993cf02
ipynb
Jupyter Notebook
Lab09/lab9_lmbaeza_taylor_sympy_in_python.ipynb
lmbaeza/numerical-methods-2021
9e3d1ec7039067cf2a33a10328b307e7a27479c7
MIT
Lab09/lab9_lmbaeza_taylor_sympy_in_python.ipynb
lmbaeza/numerical-methods-2021
9e3d1ec7039067cf2a33a10328b307e7a27479c7
MIT
Lab09/lab9_lmbaeza_taylor_sympy_in_python.ipynb
lmbaeza/numerical-methods-2021
9e3d1ec7039067cf2a33a10328b307e7a27479c7
MIT
Qwen/Qwen-72B
1. YES
2. YES