write a code for a mac laurin for cosinusfor 5 iterations with math and not with numpy and plot in python

main.py
import math
import matplotlib.pyplot as plt

def cos_taylor(x, n):
    result = 0
    for i in range(n):
        coef = (-1) ** i
        num = x ** (2*i)
        denom = math.factorial(2*i)
        result += coef * num / denom
    return result

x_values = [i for i in range(-10, 10)]
cos_values = [cos_taylor(math.radians(i), 5) for i in x_values]

plt.plot(x_values, cos_values)
plt.xlabel('x')
plt.ylabel('cos(x) estimate')
plt.title('Approximation of cos(x) using MacLaurin series')
plt.grid()
plt.show()
510 chars
22 lines

related categories

gistlibby LogSnag