How do I get the function which transforms an input to be the argument of a Legendre polynomial when using numpy.polynomial.legendre?

给你一囗甜甜゛ 提交于 2020-08-08 04:46:30

问题


# import packages we need later
import matplotlib.pyplot as plt
import numpy as np

What I am doing

Inspired by this question & answer, I am fitting a series of Legendre polynomials to a time series:

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

The time values:

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

Using a numpy's function:

degree=10
legendrefit_curve1 = np.polynomial.legendre.Legendre.fit(tvals, curve1, deg=degree)

The fit seems pretty good:

# 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') 


What's the question

print(legendrefit_curve1) returns:

leg([ 36823.85778316  96929.13731379 123557.55165344 112110.13559758
  75345.0434688   32377.19460001   -182.38440131 -15562.47475287
 -16142.22533582  -8379.06875482   -744.73929814])

However, I am using a Jupyter notebook, so if I just write legendrefit_curve1, without print(), I get an output:

(What difference print() makes to Jupyter's output is related to this question.)

Clearly, print(legendrefit_curve1) only gave the coefficients of each Legendre polynomial (same with legendrefit_curve1.coef).

How do I get the values which transform x to be the argument of each Legendre polynomial?

ie how to obtain the values from the expression: -1.0512820512820513+0.05128205128205128x: -1.0512820512820513 and 0.05128205128205128 (without just copying them manually)?


What didn't work

Relying on this thread I run:

for attr in dir(legendrefit_curve1):
    print('###'+attr+'###')
    print(getattr(legendrefit_curve1, attr))

This had a long text output, but I did not find -1.05 in it (ctrl-f), so that suggest that the -1.0512820512820513 value did not get returned, so this method doesn't work.


回答1:


By looking at those numbers I realized I can construct them from math.

1/(len(curve1)-1)*2, ie 1/39*2 returns: 0.05128205128205128

1+1/(len(curve1)-1)*2 ie 1+1/39*2 returns: `1.05

Which are the numbers we were looking for.


I still don't know how it is displayed when executing legendrefit_curve1 in a Jupyter Notebook cell, but that is less of the point.


I don't know why the formula above works, it'll probably be a question on math.stackexchange.com.



来源:https://stackoverflow.com/questions/63120942/how-do-i-get-the-function-which-transforms-an-input-to-be-the-argument-of-a-lege

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!