import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
plt.rcParams['axes.facecolor'] = '#ffff97ff'
fig = plt.figure()
fig.patch.set_facecolor('#ffff97ff')
n = 50
mystring = "y1 = (np.pi-2)/(2*np.pi)"
for k in range(n+1):
if k != 0:
mystring += f"+np.sin({k}*x)*(np.cos(np.pi*{k})-np.cos(2*{k}))/(np.pi*{k})"
mystring += f"+np.cos({k}*x)*(np.sin(np.pi*{k})-np.sin(2*{k}))/(np.pi*{k})"
print(f"Last index is n = {k}")
print(mystring)
x = np.arange(-1*np.pi, np.pi, 0.01)
x2 = np.arange(-1*np.pi, -2, 0.01)
x3 = np.arange(-2, np.pi, 0.01)
exec(mystring)
y2 = 1+np.cos(2*x2)*0
y3 = np.cos(2*x3)*0
plt.plot(x, y1, color='blue')
plt.plot(x2, y2, color='black')
plt.plot(x3, y3, color='black')
plt.text(0.35, 0.9, f"Fourier series until n = {k}",
bbox={'facecolor': 'red', 'alpha': 0.2, 'pad': 10})
plt.show()