slow_learner
slow_learner

Reputation: 495

Finding the Roots of Chebyshev's polynomials in python

I want to find the roots of the Chebysev polynomial of any order using Python. I have seen similar threads for Legendre polynomials. However, I have constructed my polynomials using the method defined here as

import numpy as np 
import sympy as sp 

f0 = lambda x: chebyt(0,x)
f1 = lambda x: chebyt(1,x)
f2 = lambda x: chebyt(2,x)
f3 = lambda x: chebyt(3,x)
f4 = lambda x: chebyt(4,x)
plot([f0,f1,f2,f3,f4],[-1,1])

I have tried to use np.roots(f4), but I receive the following error: TypeError: float() argument must be a string or a number, not 'function'. Additionally, it seems that even if I could, it wouldn't work for high order polynomials.

Upvotes: 2

Views: 1230

Answers (1)

CDJB
CDJB

Reputation: 14536

You can do this by finding the coefficients of the Chebyshev polynomials using the method under the heading "Basic Evaluation" here, and then using np.roots on the reversed list to generate the roots of the polynomial.

Using np.roots(f4) wasn't working because the roots function only accepts a list of polynomial coefficients, rather than a lambda function.

Code:

from mpmath import chebyt, chop, taylor
import numpy as np

for n in range(5):
    print(np.roots(chop(taylor(lambda x: chebyt(n, x), 0, n))[::-1]))

Output:

[]
[0.]
[ 0.70710678 -0.70710678]
[ 0.8660254 -0.8660254  0.       ]
[-0.92387953  0.92387953 -0.38268343  0.38268343]

Hope that helps.

Upvotes: 2

Related Questions