How can I evaluate a Chebishev polynomial in python?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
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:
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?