在以下代码上:
import numpy as np
from scipy.interpolate import lagrange
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a+b
return a
x = np.array(range(64))
y = np.array([fib(n) for n in xrange(64)])
P = lagrange(x, y)
print P(10)
我使用scipy在64个点上插值函数(fib
),从而创建一个多项式P
。因此,我期望,P(n) == fib(n)
对于任何0 <= n < 64
。相反,例如,对于P(10)
,我得到的是-248014823183360.0
,而不是55
(fib(10)
)。我想这是某个地方的精度错误,但我不确定在哪里。如何构造满足此期望的多项式函数P
?你知道吗
你可以试试这个numpy.polyfit公司在多项式秩较高时哪个更稳定
610609.9896美元
610,-645662.4698毫米
除此之外,我要说的是,多项式拟合在大量的点上很少是一个好的解决方案。如果你对斐波那契级数感兴趣(或者只是一个例子?),您可以使用基于黄金数幂的分析公式,您可以导出浮动索引(请查看Fibonacci Wikipedia页面)。你知道吗
相关问题 更多 >
编程相关推荐