Python 当使用numpy.polymone.Legendre时,如何获得将输入转换为勒让德多项式参数的函数? 我在做什么
受此启发,我将一系列 时间值:Python 当使用numpy.polymone.Legendre时,如何获得将输入转换为勒让德多项式参数的函数? 我在做什么,python,numpy,math,curve-fitting,Python,Numpy,Math,Curve Fitting,受此启发,我将一系列 时间值: curve1 = \ np.asarray([942.153,353.081,53.088,125.110,140.851,188.170,70.536,-122.473,-369.061,-407.945,88.734,484.334,267.762,65.831,74.010,-55.781,-260.024,-466.830,-524.511,-76.833,-36.779,-117.366,218.578,175.662,185.653,299.285,2
curve1 = \
np.asarray([942.153,353.081,53.088,125.110,140.851,188.170,70.536,-122.473,-369.061,-407.945,88.734,484.334,267.762,65.831,74.010,-55.781,-260.024,-466.830,-524.511,-76.833,-36.779,-117.366,218.578,175.662,185.653,299.285,215.276,546.048,1210.132,3087.326,7052.849,13867.824,27156.939,51379.664,91908.266,148874.563,215825.031,290073.219,369567.781,437031.688])
使用numpy函数:
tvals = \
np.asarray([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40])
这件衣服看起来很合身:
degree=10
legendrefit_curve1 = np.polynomial.legendre.Legendre.fit(tvals, curve1, deg=degree)
问题是什么
打印(legendrefit_curve1)
返回:
# generate points of fitted curve
n=100
fitted_vals_curve1 = legendrefit_curve1.linspace(n=n)
# plot data and fitted curve
plt.scatter(tvals, curve1)
plt.plot(fitted_vals_curve1[0],fitted_vals_curve1[1],c='r')
但是,我使用的是Jupyter笔记本,因此如果我只写legendrefit_curve1
,而不写,我会得到一个输出:
(对Jupyter输出的影响与此相关。)
显然,print(legendrefit_curve1)
只给出了每个Legendre多项式的系数(与legendrefit_curve1.coef相同)
如何获得将x转换为每个勒让德多项式的参数的值?
ie如何从表达式中获取值:-1.05128205128205113+0.05128205128205128x
:-1.05128205128205128012820513
和0.05128205128205128
(不需要手动复制)
什么不起作用
依靠我跑步:
这有一个很长的文本输出,但我没有在其中找到-1.05
(ctrl-f
),因此这表明-1.051282051282820513
值没有返回,所以这个方法不起作用。通过查看这些数字,我意识到我可以从数学中构造它们
1/(len(curve1)-1)*2
,即1/39*2
返回:0.0512820518205128
1+1/(len(curve1)-1)*2
ie1+1/39*2
返回值:`1.05
这是我们要找的号码
我仍然不知道在Jupyter笔记本电脑中执行legendrefit_curve1
时它是如何显示的,但这不是重点
我不知道上面的公式为什么有效,这可能是个问题
# generate points of fitted curve
n=100
fitted_vals_curve1 = legendrefit_curve1.linspace(n=n)
# plot data and fitted curve
plt.scatter(tvals, curve1)
plt.plot(fitted_vals_curve1[0],fitted_vals_curve1[1],c='r')
leg([ 36823.85778316 96929.13731379 123557.55165344 112110.13559758
75345.0434688 32377.19460001 -182.38440131 -15562.47475287
-16142.22533582 -8379.06875482 -744.73929814])
for attr in dir(legendrefit_curve1):
print('###'+attr+'###')
print(getattr(legendrefit_curve1, attr))