import numpy as np import matplotlib.pyplot as plt def coefs(z): n = len(z) h = 1/(n+1) inds = np.arange(1,n+1) mul_table = np.outer(inds,inds) T = np.sin(np.pi*h * mul_table) return 2*h*T@z f = lambda x : np.where(x >= 1/2, 1-x, x) x = np.linspace(0,1,1000) plt.plot(x, f(x), label='f(x)') n_list = [1,5,10,100] for n in n_list: h = 1/(n+1) sample_x = np.linspace(h,1-h,n) c = coefs(f(sample_x)) g = c @ np.sin(np.pi * np.outer(sample_x/h, x)) plt.plot(x, g, label = f'g(x) for n = {n}') plt.xlabel('x') plt.legend() plt.show()