Use python:
Find the Taylor polynomials of cos(x) about 0, i.e., p_n(x) where n=1, 3, 5(no programming here). For the domain -pi to pi, graph these 3 polynomials along with cos(x) all versus x. Use numpy in your program. Generate the vector x by
import numpy as N
x = np.arange( -3.14 , 3.14 , 0.01)
Save your graph to a pdf file. Use different symbols for each plot likes “+” and “o”s. Provide a legend and title. Use an x-label and y-label for axes.
Adjust the size of the plot. Give some axis parameters.
What do you observe about the graph?
Expert Answer
Python code:
#cos x=sigma(n=0,n=inf) (-1)**n (x**(2n))/(2n)!
#cos x = 1-x**2/2!+x**4/4!-x**6/6!+x**8/8!
import matplotlib.pyplot as plt
import numpy as np
import math
x=np.arange(-3.14,3.14,0.01)
y=[]
for i in x:
y.append(1-i**2/math.factorial(2))
plt.plot(x,y,’–‘)
plt.axis([-4,4,-5,5])
plt.xlabel(‘x’)
plt.ylabel(‘cos(x)’)
plt.title(‘cos x taylor series’)
y=[]
for i in x:
y.append(1-i**2/math.factorial(2)+i**4/math.factorial(4)-i**6/math.factorial(6))
plt.plot(x,y,’o’)
y=[]
for i in x:
y.append(1-i**2/math.factorial(2)+i**4/math.factorial(4)-i**6/math.factorial(6)+i**8/math.factorial(8)-i**10/math.factorial(10))
plt.plot(x,y,’+’)
plt.show()
Plot graph
As we observe the graph as n increases the cos x values are falling back in the range [-1,1]
You can save graph as different formats for the plot window