I'm trying to interpolate a function at arbitrary points and I have the function values at Chebyshev extreme points. I use the real values from Fast Fourier Transform to compute the Chebyshev coefficients. Then I scale them with 2/N and then I use the polynomial library to evaluate the series of chebyshev polynomials at a set of points. This produces the wrong function approximation. Where am I going wrong?
import numpy as np
import matplotlib.pyplot as plt
# Define the number of
# Chebyshev extreme points
N = 10
# Define the function to be
# approximated
def f(x):
return x**2
# Evaluate the function at the
# Chebyshev extreme points
x = np.cos(np.arange(N) * np.pi / N)
y = f(x)
# Compute the discrete Fourier
# transform (DFT) of the function
# values using the FFT algorithm
DFT = np.fft.fft(y).real
# Compute the correct scaling
# factor
scaling_factor = 2/N
# Scale the DFT coefficients by
# the correct scaling factor
chebyshev_coefficients = scaling_factor * DFT
# Use Chebval to
# evaluate the approximated
# polynomial at a set of points
x_eval = np.linspace(-1, 1, 100)
y_approx = np.polynomial.chebyshev.chebval(x_eval, chebyshev_coefficients[::-1])
# Plot the original function
# and the approximated function
plt.plot(x, y, 'o',
label='Original function')
plt.plot(x_eval, y_approx, '-',
label='Approximated function')
plt.legend()
plt.show()