gpt4 book ai didi

python - 在 python 中找到切比雪夫多项式的根

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:34 25 4
gpt4 key购买 nike

我想使用 Python 求任意阶切比雪夫多项式的根。我看过similar threads对于勒让德多项式。但是,我使用定义的方法构建了多项式 here作为

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])

我尝试使用 np.roots(f4),但收到以下错误:TypeError: float() argument must be a string or a number, not 'function' 。此外,it seems that即使我可以,它也不适用于高阶多项式。

最佳答案

您可以通过使用标题为“基本评估”here 下的方法找到切比雪夫多项式的系数来实现此目的, 然后使用 np.roots在反向列表上生成多项式的根。

使用 np.roots(f4) 无效,因为 roots 函数只接受多项式系数列表,而不是 lambda 函数。

代码:

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]))

输出:

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

希望对您有所帮助。

关于python - 在 python 中找到切比雪夫多项式的根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com