[Numpy-discussion] fitting legendre polynomial

Charles R Harris charlesr.harris at gmail.com
Tue May 17 10:37:31 EDT 2011


On Tue, May 17, 2011 at 8:03 AM, Wolfgang Kerzendorf <
wkerzendorf at googlemail.com> wrote:

> Hello,
>
> The science package I'm using fits legendre polynomials to data. I heard
> it is more stable than the normal polynomials for a fit. Is there a
> polyfit for legendre polynomials? How do I do that with the new legendre
> polynomials module?
>
>
In [1]: from numpy.polynomial import Legendre as L

In [2]: x = np.linspace(0,10)

In [3]: y = x*(x - 5)*(x - 8)

In [4]: p = L.fit(x, y, 3)

In [5]: plot(*p.linspace())
Out[5]: [<matplotlib.lines.Line2D object at 0x2ea5590>]

In [6]: p.coef
Out[6]:
array([  1.66666667e+01,  -1.36479923e-14,   3.33333333e+01,
         5.00000000e+01])

In [7]: p.domain
Out[7]: array([  0.,  10.])

Note that the data is mapped to the domain [-1, 1] and then fitted with the
Legendre polynomials. The 'p' polynomial keeps track of that. The Chebyshev
polynomials probably work as well as the Legendre polynomials for most
things. Plot produced is attached.

Chuck.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110517/a69a7f29/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: legendre-fit.png
Type: image/png
Size: 22650 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110517/a69a7f29/attachment.png>


More information about the NumPy-Discussion mailing list