How can I evaluate a Chebishev polynomial in python?

Click For Summary
SUMMARY

The discussion focuses on evaluating Chebyshev polynomials in Python, specifically addressing issues with the numpy.polynomial.chebyshev.chebval function. Users reported that the function does not return the expected results for Chebyshev polynomials, particularly for the third polynomial, T_3(x) = 4x^3 - 3x. The correct approach involves using the recurrence relation T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x) to compute the polynomial iteratively. Additionally, numerical errors arise when using mt.cos(n*mt.acos(x)) for evaluation, especially for values of x outside the range of -1 to 1.

PREREQUISITES
  • Understanding of Chebyshev polynomials and their properties
  • Familiarity with Python programming and the NumPy library
  • Knowledge of recurrence relations in polynomial evaluation
  • Experience with numerical methods and potential sources of error
NEXT STEPS
  • Implement the Chebyshev polynomial evaluation using the recurrence relation in Python
  • Explore the numpy.polynomial.chebyshev documentation for advanced usage
  • Investigate numerical stability techniques for evaluating polynomials
  • Learn about the differences in evaluating polynomials for values outside the standard range
USEFUL FOR

Mathematicians, data scientists, and software developers working with numerical methods and polynomial evaluations in Python, particularly those interested in Chebyshev polynomials.

confused_engineer
Messages
34
Reaction score
1
TL;DR
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
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:
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?
 
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?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
55
Views
7K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K