I am trying to interpolate a generic polynomial using python, just to understand the theory better. I have seen something regarding the library in numpy, with respect to polynomials using cèbyshèv, and I have developed the following code:
# f(x) = -3 + 2x^2 - x^3 + x^4f = lambda x: -3 + 2*x**2 - x**3 + x**4pcoeffs = [-3, 0, 2, -1, 1]ccoeffs = np.polynomial.chebyshev.poly2cheb(pcoeffs)fpoly = np.polynomial.Polynomial(pcoeffs)fcheb = np.polynomial.Chebyshev(ccoeffs)I know that the Chebyshev polynomials is a polynomial based on chebyshev points, computed as follow:
And I use these points to compute the polynomial, using the formula:
Where:
- -ak is the function evaluated at each at each Chebyshev points before defined, and If I am correct, I am able to find this function with "np.polynomial.Chebyshev(ccoeffs)"
Now, running the previous code, I get the following output:
Now I have some questions:
- What have I written above, is correct?
- How can I find the values of Ti(x)?
- How can I represent the Chebyshev polynomials in python with a plot?
Thanks in advance!
1 Answer1
- I believe what you write is correct.
- If I understand you correctly you want to evaluate your polynomial displayed in a basis of T_i. If so you can just call the polynomial at some value e.g.
fcheb(0) --> -3. If you actually want to know what e.g.T_10(np.pi)is donp.polynomial.Chebyshev.basis(10)(np.pi). - Here is a plot of your polynomials side by side. Notice that I slightly adjusted your 2nd polynomial to see both and not have them be exactly the same.
import matplotlib.pyplot as pltx = np.linspace(-1, 1, 10**4)plt.plot(*fcheb.linspace(), label='fcheb')plt.plot(x, f(x)+0.1, label='fpoly+0.1')plt.legend()- You really should try out
np.polynomial.chebyshev.chebfitif you want to understand chebyshev polynomial interpolation.
5 Comments
fcheb.linspace() for you is that that one picks just 100 points. So you see my choice was kinda arbitrary.chebfit and if you have a function you can just obtain data by usingx=np.linspace(a,b,n) andy=f(x). Thenchebfit gives you coefficients of a chebyshev polynomial being close to your data/function. Similar to the following though there I used splines instead:stackoverflow.com/questions/68319759/….Explore related questions
See similar questions with these tags.



