# [SciPy-user] Legendre Polynomials

rgold@lsw.uni-heidelbe... rgold@lsw.uni-heidelbe...
Thu Aug 23 04:58:22 CDT 2007

```Hi everybody,
I am using: Python 2.4 and Scipy 5.0.1-3ubuntu2

I have run into problems trying to expand a function into
Legendre-Polynomials:
The calculated coefficients start to explode from l=33 on...
The problem is that (for some reason) the evaluation of the Legendre
Polynomials  near x <= -1 starts to become wrong from l=33 on!
See for example:

>>> scipy.special.legendre(21)(-1)
-0.99999988137941176
>>> scipy.special.legendre(31)(-1)
-1.0034823356832285
>>> scipy.special.legendre(33)(-1)
-1.2927111532386935
>>> scipy.special.legendre(35)(-1)
-3.8408406352908409
>>> scipy.special.legendre(45)(-1)
129887.55400302292

Moreover the xrange of that error grows with l:

>>> scipy.special.legendre(35)(-0.8)
0.15667302303740524
>>> scipy.special.legendre(45)(-0.8)
185.85665104241005
>>> scipy.special.legendre(45)(-0.5)
0.1229019272524782

The problem occures also for the even Polynomials but interestingly again
for the negative arguments only as you can see here:

>>> scipy.special.legendre(36)(-1)
11.445346667471807
>>> x=arange(-1,1,0.01)
>>> plot(x,scipy.special.legendre(36)(x))
[<matplotlib.lines.Line2D instance at 0xb6053b2c>]
>>> show()

A first quick (but not satisfying) idea was to use the values calculated
for x>=0 and copy them (of course correcting for the minus sign if l is
odd).
Problems remaining:
-> WHY is the evaluation wrong for x close to -1?
-> Can I trust the routine at all?

As a test I tried to expand cos(x) over Legendre-Polynomials because I
know the result: Coefficient 1 should be 1 and the others as small as
possible!

It works quite fine as long as l<33!