Plot of Fourier Series from Part 17

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')
pi = np.pi
n = 40
mystring = "y1 = pi/4"
for k in range(n):
    if k != 0:
        mystring += f"+np.cos({k}*x)*(1/{k}**2)*(1-(-1)**{k})/pi+np.sin({k}*x)/({k})"

print(f"Last index is k = {k}")

x = np.arange(-1*pi, pi, 0.01)
x2 = np.arange(-1*pi, 0, 0.01)
x3 = np.arange(0*pi, pi, 0.01)
exec(mystring)
y2 = np.cos(2*x2)*0
y3 = np.cos(0)*(pi-x3)
 
plt.plot(x, y1, color='blue')
plt.plot(x2, y2, color='black')
plt.plot(x3, y3, color='black')
plt.show()

plot