#Taylor Series Expansion with Python from Data Science Fabric 

recovered from **[Data Science Fabric](https://dsfabric.org/taylor-series-expansion-with-python)**

Note: for ease and organization, titles were placed on the notebook for quick reading

##Libraries


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


```
from sympy.functions import ln
```


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


```
# 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()
```

##First's Cases of Use


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

print('cos(x) ≅', taylor(cos(x), 0, 4))

print('e(x) ≅', taylor(exp(x), 0, 4))
```

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



```
print("Ejercicio")
print('ln(x+1) ≅', taylor(ln(x+1), 0, 4))
```

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



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

print('cos(1) =', taylor(cos(x), 0, 4).subs(x,1))

print('e(1) =', taylor(exp(x), 0, 4).subs(x,1))
```

    sin(1) = 5/6
    cos(1) = 1/2
    e(1) = 8/3



```
print("Ejercicio")

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

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


##Tests of Taylor's Series


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

    Taylor 0 exp(x) ≅ 0
    Taylor 1 exp(x) ≅ 1
    Taylor 2 exp(x) ≅ x + 1
    Taylor 3 exp(x) ≅ x**2/2 + x + 1
    Taylor 4 exp(x) ≅ x**3/6 + x**2/2 + x + 1
    Taylor 5 exp(x) ≅ x**4/24 + x**3/6 + x**2/2 + x + 1
    Taylor 6 exp(x) ≅ x**5/120 + x**4/24 + x**3/6 + x**2/2 + x + 1
    Taylor 7 exp(x) ≅ x**6/720 + x**5/120 + x**4/24 + x**3/6 + x**2/2 + x + 1
    Taylor 8 exp(x) ≅ x**7/5040 + x**6/720 + x**5/120 + x**4/24 + x**3/6 + x**2/2 + x + 1



```
print("Ejercicio")

for i in range(1,10):
  print('Taylor', i,'ln(x+1) ≅', taylor(ln(x+1), 0, i))
```

    Ejercicio
    Taylor 1 ln(x+1) ≅ 0
    Taylor 2 ln(x+1) ≅ x
    Taylor 3 ln(x+1) ≅ -x**2/2 + x
    Taylor 4 ln(x+1) ≅ x**3/3 - x**2/2 + x
    Taylor 5 ln(x+1) ≅ -x**4/4 + x**3/3 - x**2/2 + x
    Taylor 6 ln(x+1) ≅ x**5/5 - x**4/4 + x**3/3 - x**2/2 + x
    Taylor 7 ln(x+1) ≅ -x**6/6 + x**5/5 - x**4/4 + x**3/3 - x**2/2 + x
    Taylor 8 ln(x+1) ≅ x**7/7 - x**6/6 + x**5/5 - x**4/4 + x**3/3 - x**2/2 + x
    Taylor 9 ln(x+1) ≅ -x**8/8 + x**7/7 - x**6/6 + x**5/5 - x**4/4 + x**3/3 - x**2/2 + x



```
print("Ejercicio")

for i in range(1,10):
  print('Taylor', i,'sin(x) ≅', taylor(sin(x), 0, i))
```

    Ejercicio
    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
    Taylor 9 sin(x) ≅ -x**7/5040 + x**5/120 - x**3/6 + x



```
print('Taylor 0 sin(x) ≅', taylor(sin(x), 0, 0).subs(x,2),' = ',taylor(sin(x), 0, 0).subs(x,2).evalf())
print('Taylor 1 cos(x) ≅', taylor(cos(x), 0, 1).subs(x,2),' = ',taylor(cos(x), 0, 1).subs(x,2).evalf())
print('Taylor 2 exp(x) ≅', taylor(exp(x), 0, 2).subs(x,2),' = ',taylor(exp(x), 0, 2).subs(x,2).evalf())
print('Taylor 3 exp(x) ≅', taylor(exp(x), 0, 3).subs(x,2),' = ',taylor(exp(x), 0, 3).subs(x,2).evalf())
print('Taylor 4 exp(x) ≅', taylor(exp(x), 0, 4).subs(x,2),' = ',taylor(exp(x), 0, 4).subs(x,2).evalf())
print('Taylor 5 exp(x) ≅', taylor(exp(x), 0, 5).subs(x,2),' = ',taylor(exp(x), 0, 5).subs(x,2).evalf())
print('Taylor 6 exp(x) ≅', taylor(exp(x), 0, 6).subs(x,2),' = ',taylor(exp(x), 0, 6).subs(x,2).evalf())
print('Taylor 7 exp(x) ≅', taylor(exp(x), 0, 8).subs(x,2),' = ',taylor(exp(x), 0, 7).subs(x,2).evalf())
```

    Taylor 0 sin(x) ≅ 0  =  0
    Taylor 1 cos(x) ≅ 1  =  1.00000000000000
    Taylor 2 exp(x) ≅ 3  =  3.00000000000000
    Taylor 3 exp(x) ≅ 5  =  5.00000000000000
    Taylor 4 exp(x) ≅ 19/3  =  6.33333333333333
    Taylor 5 exp(x) ≅ 7  =  7.00000000000000
    Taylor 6 exp(x) ≅ 109/15  =  7.26666666666667
    Taylor 7 exp(x) ≅ 155/21  =  7.35555555555556



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

    Ejercicio
    Taylor 0 sin(x) ≅ 0  =  0
    Taylor 1 sin(x) ≅ 0  =  0
    Taylor 2 sin(x) ≅ 2  =  2.00000000000000
    Taylor 3 sin(x) ≅ 2  =  2.00000000000000
    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


##Comparison between methods


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

    sympy exp(x)subs(x,2) = exp(2)
    sympy exp(x).subs(x,2).evalf() = 7.38905609893065
    math.exp(2) = 7.38905609893065



```
print("Ejercicio")
import math
print('sympy ln(x+1)subs(x,2) =', ln(x+1).subs(x,2))
print('sympy ln(x+1).subs(x,2).evalf() =', ln(x+1).subs(x,2).evalf())
print('math.ln(2+1) =', math.log1p(2))
```

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



```
print("Ejercicio")
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))
```

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


##Plots of `exp()`


```
import math
import numpy as np
import matplotlib.pyplot as plt

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()
```


```
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_exp2 = [taylor(exp(x), 0, 2).subs(x,v) for v in values]
legends = ['exp() ','Taylor 2 (linear)']

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

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()
```


```
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_exp3 = [taylor(exp(x), 0, 3).subs(x,v) for v in values]
legends = ['exp() ','Taylor 3 (quadratic)']

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

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()
```


```
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_exp4 = [taylor(exp(x), 0, 4).subs(x,v) for v in values]
legends = ['exp() ','Taylor 4 (cubic)']

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

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()
```


```
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]
t_exp2 = [taylor(exp(x), 0, 2).subs(x,v) for v in values]
t_exp3 = [taylor(exp(x), 0, 3).subs(x,v) for v in values]
t_exp4 = [taylor(exp(x), 0, 4).subs(x,v) for v in values]
legends = ['exp() ','Taylor 1 (constant)','Taylor 3 (linear)','Taylor 3 (quadratic)','Taylor 4 (cubic)']

fig, ax = plt.subplots()
ax.plot(values,p_exp)
ax.plot(values,t_exp1)
ax.plot(values,t_exp2)
ax.plot(values,t_exp3)
ax.plot(values,t_exp4)

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()
```

##Plots of $\ln(x+1)$


```
import math
import numpy as np
import matplotlib.pyplot as plt

values = np.arange(0,5,0.1)
p_ln = [math.log1p(value) for value in values]
t_ln1 = [taylor(ln(x+1), 0, 1).subs(x,v) for v in values]
legends = ['ln(x+1) ','Taylor 1 (constant)']

fig, ax = plt.subplots()
ax.plot(values,p_ln, color ='red')
ax.plot(values,t_ln1)

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()
print("Note that the blue line is in y=0")
```


```
import math
import numpy as np
import matplotlib.pyplot as plt

values = np.arange(0,5,0.1)
p_ln = [math.log1p(value) for value in values]
t_ln2 = [taylor(ln(x+1), 0, 2).subs(x,v) for v in values]
legends = ['ln(x+1) ','Taylor 2 (Lineal)']


fig, ax = plt.subplots()
ax.plot(values,p_ln, color ='red')
ax.plot(values,t_ln2)

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()
```


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

values = np.arange(0,5,0.1)
p_ln = [math.log1p(value) for value in values]
t_ln3 = [taylor(ln(x+1), 0, 3).subs(x,v) for v in values]
legends = ['ln(x+1) ','Taylor 3 (Quadratic)']

fig, ax = plt.subplots()
ax.plot(values,p_ln, color ='red')
ax.plot(values,t_ln3)

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()
```


```
import math
import numpy as np
import matplotlib.pyplot as plt

values = np.arange(0,5,0.1)
p_ln = [math.log1p(value) for value in values]
t_ln4 = [taylor(ln(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_ln, color ='red')
ax.plot(values,t_ln4)

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()
```


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

values = np.arange(0,5,0.1)
p_ln = [math.log1p(value) for value in values]
t_ln1 = [taylor(ln(x+1), 0, 1).subs(x,v) for v in values]
t_ln2 = [taylor(ln(x+1), 0, 2).subs(x,v) for v in values]
t_ln3 = [taylor(ln(x+1), 0, 3).subs(x,v) for v in values]
t_ln4 = [taylor(ln(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_ln)
ax.plot(values,t_ln1)
ax.plot(values,t_ln2)
ax.plot(values,t_ln3)
ax.plot(values,t_ln4)

ax.set_ylim([-2,3])
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()
```

##Plots of $\sin(x)$


```
import math
import numpy as np
import matplotlib.pyplot as plt

values = np.arange(-2*math.pi,2*math.pi,0.1)
p_sin = [math.sin(value) for value in values]
t_sin1 = [taylor(sin(x), 0, 1).subs(x,v) for v in values]
legends = ['sin() ','Taylor 1 (constant)']

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

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()
```


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

values = np.arange(-2*math.pi,2*math.pi,0.1)
p_sin = [math.sin(value) for value in 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()
```


```
import math
import numpy as np
import matplotlib.pyplot as plt

values = np.arange(-2*math.pi,2*math.pi,0.1)
p_sin = [math.sin(value) for value in values]
t_sin3 = [taylor(sin(x), 0, 3).subs(x,v) for v in values]
legends = ['sin()','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()
```


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

values = np.arange(-2*math.pi,2*math.pi,0.1)
p_sin = [math.sin(value) for value in values]
t_sin4 = [taylor(sin(x), 0, 4).subs(x,v) for v in values]
legends = ['sin() ','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()
```


```
import math
import numpy as np
import matplotlib.pyplot as plt

values = np.arange(-2*math.pi,2*math.pi,0.1)
p_sin = [math.sin(value) for value in 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()
```

3ac0d047d8983a5050bb0ac63d9009d635808392
ipynb
Jupyter Notebook
Lab9/TaylorSymPy.ipynb
crvargasm/MetNumUN2021I
e8b56337482c51eb0ca397ebd173360979e9013c
MIT
2021-07-27T05:34:50.000Z
2021-07-27T05:34:50.000Z
Lab9/TaylorSymPy.ipynb
crvargasm/MetNumUN2021I
e8b56337482c51eb0ca397ebd173360979e9013c
MIT
Lab9/TaylorSymPy.ipynb
crvargasm/MetNumUN2021I
e8b56337482c51eb0ca397ebd173360979e9013c
MIT
Qwen/Qwen-72B
1. YES
2. YES