How can I evaluate a Chebishev polynomial in python?

In summary, the conversation discusses the need for a function in Python that returns the evaluation of a Chebishev polynomial of order k evaluated in x. The provided code uses the chebval function from numpy to calculate the polynomial, but it does not provide the desired result. The conversation then discusses using the recurrence relation for Chebyshev polynomials of the first kind to calculate the polynomial, and a potential solution using mt.cos and mt.acos. However, there is a numerical error with this approach and the conversation ends with a suggestion to use the correct formula from Wikipedia to fix the error.
  • #1
confused_engineer
39
2
TL;DR Summary
I can't find a python function which provides me with the evaluation of a Chebishev polynomial of a concrete order at a concrete point
Hello everyone. I need to construct in python a function which returns the evaluation of a Chebishev polynomial of order k evaluated in x. I have tested the function chebval form these documents, but it doesn't provide what I look for, since I have tested the third one, 4t^3-3t and
Python:
import numpy as np
import numpy.polynomial.chebyshev as cheb

gfg = cheb.chebval((3), (3))
does not return 4*(3)^3-3*3, but instead it returns 3. I have a code which does this but for Legendre polynomials, but I cannot reproduce it whith these because the recurrence relationship uses the last two terms, not the first two as Legendre's. The code is as follow.
Code:
import numpy as np
import numpy.polynomial.chebyshev as cheb

gfg = cheb.chebval((3), (3))
 print(gfg)
x=5
K=2

pn2 = 2*(x)**K+3print(pn2)

So, if someone could tell me how to do this but with Chebishev's polynomials I would be most grateful.

Thanks for reading.
 
Last edited:
Technology news on Phys.org
  • #2
The recurrence relation for Chebyshev polynomials of the first kind is ##T_{n}(x) = 2xT_{n-1}(x) - T_{n-2}(x)##. So to calculate ##T_{n}(x)##, you just need a loop. Start with ##T_0=1##, and ##T_1=x## and iterate until you get ##T_n##.
 
Last edited:
  • #3
tnich said:
The recurrence relation for Chebyshev polynomials of the first kind is ##T_{n}(x) = 2xT_{n-1}(x) - T_{n-2}(x)##. So to calculate ##T_{n}(x)##, you just need a loop. Start with ##T_0=1##, and ##T_1=x## and iterate until you get ##T_n##.
Thanks for the answer, but I am afraid that is exactly the problem. I need a function that returns the evaluation of a polynomial of order n evaluated at x. For that, I need to calculate the polynomial of order n based on the polynomials of order (n-1) and (n-2). Currently, I am using mt.cos(n*mt.acos(x)) to evaluate the polynomial, but a numerical error arrises.

Is there a way to calculate the Chevishev polynomial of order n based on the first two?
 
  • #4
confused_engineer said:
Currently, I am using mt.cos(n*mt.acos(x)) to evaluate the polynomial, but a numerical error arrises.
Your example was ##n=3##, ##x=3##. At least according to Wikipedia, ##T_n(x)=\cosh(n\cosh^{-1}(x))## for ##x>1##, rather than ##\cos## and ##\cos^{-1}## as it is for ##-1\leq x\leq 1##. Does that fix your numerical error?
 

Related to How can I evaluate a Chebishev polynomial in python?

1. How can I import the necessary libraries to evaluate a Chebyshev polynomial in python?

To evaluate a Chebyshev polynomial in python, you will need to import the "numpy" and "scipy" libraries. These libraries contain functions and methods for calculating and manipulating polynomials and their coefficients.

2. What is the syntax for evaluating a Chebyshev polynomial in python?

The syntax for evaluating a Chebyshev polynomial in python is "scipy.special.eval_chebyt(n, x)", where "n" is the polynomial degree and "x" is the value at which the polynomial is to be evaluated. This function returns the value of the polynomial at the given value of "x".

3. Can I evaluate a Chebyshev polynomial at multiple points using python?

Yes, you can evaluate a Chebyshev polynomial at multiple points using python by passing an array or list of values for "x" to the "scipy.special.eval_chebyt(n, x)" function. The function will return an array or list of the polynomial values at each given value of "x".

4. How can I find the roots of a Chebyshev polynomial using python?

To find the roots of a Chebyshev polynomial using python, you can use the "scipy.special.chebyt_roots(n)" function, where "n" is the degree of the polynomial. This function will return an array of the polynomial's roots.

5. Is there a specific method for evaluating higher-order Chebyshev polynomials in python?

Yes, there is a specific method for evaluating higher-order Chebyshev polynomials in python called the Clenshaw algorithm. This algorithm can efficiently evaluate polynomials of any degree and is implemented in the "scipy.special.eval_chebyt2(n, x)" function.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
910
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
361
  • Programming and Computer Science
Replies
2
Views
925
Back
Top