0

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()
Progman
  • 16,827
  • 6
  • 33
  • 48
  • I'm not proficient with the math, but are you sure that you need to reverse the order of chebyshev coefficients with `chebyshev_coefficients[::-1]`? – Alexey S. Larionov Dec 19 '22 at 12:05
  • I have tried both ways. I was uncertain of the order since some algorithms set the Chebyshev extreme points x = -np.cos(np.arange(N) * np.pi / N). – Kristoffer Lindvall Dec 19 '22 at 12:20

0 Answers0