On Tue, May 17, 2011 at 8:03 AM, Wolfgang Kerzendorf < [email protected]> 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.
<<attachment: legendre-fit.png>>
_______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
