Taylor Series
Taylor Series Expansion
This notebook presents the Taylor series expansion of the sine function, close to zero for an increasing number of terms in the approximation.
Note that in this example, only odd powers of contribute to the expansion.
First import the tools needed
import numpy as np
from math import factorial
import matplotlib.pyplot as plt
Define the values and set up a figure for plotting
x = np.linspace(-np.pi, np.pi, 200)
plt.figure(figsize = (10,8))
First begin with the first order approximation
n = 0
approximate values
y = np.zeros(len(x))
Because only every other term is not equal to zero, we only have to compute terms
n = n / 2
Taylor series expansion up to
for i in np.arange(0, int(n)+1):
y = y + ((-1)**i * x**(2*i+1)) / factorial(2*i+1)
Plotting
plt.plot(x, y, label = 'First Order')
Third order (same structure as before)
n = 2
y = np.zeros(len(x))
n = n / 2
for i in np.arange(0, int(n)+1):
y = y + ((-1)**i * (x)**(2*i+1)) / factorial(2*i+1)
plt.plot(x, y, label = 'Third Order')
Seventh order
n = 6
y = np.zeros(len(x))
n = n / 2
for i in np.arange(0,int(n)+1):
y = y + ((-1)**i * (x)**(2*i+1)) / factorial(2*i+1)
plt.plot(x, y, label = 'Seventh Order')
Eleventh order
n = 10
y = np.zeros(len(x))
n = n / 2
for i in np.arange(0,int(n)+1):
y = y + ((-1)**i * (x)**(2*i+1)) / factorial(2*i+1)
plt.plot(x, y, label = 'Eleventh Order', marker='.')
Plotting of full solution, i.e.
plt.plot(x, np.sin(x), 'k', label = 'sin(x)')
plt.grid()
plt.title('Taylor Series Approximations of Various Orders for $\sin(x)$')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
