2

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:

enter image description here

And I use these points to compute the polynomial, using the formula:enter image description here

Where:

  1. -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:enter image description here

Now I have some questions:

  1. What have I written above, is correct?
  2. How can I find the values of Ti(x)?
  3. How can I represent the Chebyshev polynomials in python with a plot?

Thanks in advance!

askedJul 10, 2021 at 8:48
John_maddon's user avatar

1 Answer1

4
  1. I believe what you write is correct.
  2. 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).
  3. 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()

enter image description here

  1. You really should try outnp.polynomial.chebyshev.chebfit if you want to understand chebyshev polynomial interpolation.
answeredJul 10, 2021 at 9:28
Lukas S's user avatar
Sign up to request clarification or add additional context in comments.

5 Comments

Oh man, you can be My hero, can I ask you other questions? I am working with a thesis and I am a little bit in trouble: 1) why did I you set 10^4 points? 2) What is the meaning of '*fcheb.linspace()'? Thank you so much
I don't have a good reason to pick 10^4 points. It's just a number big enough so it looks good and small enough so it doesn't take forever. What I didn't know before reviewing the docs offcheb.linspace() for you is that that one picks just 100 points. So you see my choice was kinda arbitrary.
And 2) fcheb.linspace gives you two arrays of 100, evenly spaced x values in the interval (-1,1) and the corresponding y values. The * just gives both to pyplot as if you had plugged in the first array as the first argument and the 2nd array as the 2nd argument. Notice that thats technically different from passing a tuple of tow arrays as one argument what would have happened had I omitted the *.
Just another question, if I can: I have seen in your fourth point the following command: np.polynomial.chebyshev.chebfit, and searching some docs in numpy, I found the following link:numpy.org/doc/stable/reference/generated/… ; now, I have not understand very much what are the element "x and y" to put inside the function.. Sorry to bother you!
Hahaha never mind: I am not sure what you want to use this for but usually one wants to either interpolate data or a complicated function that you want to make simpler. If you have x/y data you just give it tochebfit 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/….

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.