How can I evaluate a Chebishev polynomial in python?

Click For Summary

Discussion Overview

The discussion revolves around evaluating Chebyshev polynomials in Python, specifically focusing on constructing a function that accurately computes these polynomials of order k at a given value x. Participants explore various methods, including recurrence relations and existing functions, while addressing numerical errors encountered in calculations.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant seeks a Python function to evaluate Chebyshev polynomials of order k at x, expressing dissatisfaction with the existing chebval function from NumPy.
  • Another participant provides the recurrence relation for Chebyshev polynomials of the first kind, suggesting a loop-based approach starting from T_0=1 and T_1=x to compute T_n.
  • A later reply reiterates the recurrence relation but emphasizes the need for a function that computes the polynomial based on previous orders, expressing concern about numerical errors when using the cosine method for evaluation.
  • One participant mentions that for x>1, the evaluation should use the hyperbolic cosine function, referencing Wikipedia, and questions whether this adjustment resolves the numerical error.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method to evaluate Chebyshev polynomials, with multiple approaches and concerns about numerical accuracy being discussed.

Contextual Notes

Participants note potential numerical errors when using the cosine method for values of x outside the range [-1, 1], suggesting that the evaluation method may depend on the input range.

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
4K
  • · Replies 2 ·
Replies
2
Views
1K