Legendre Polynomial
¶The ODE is given by, \begin{equation} \frac{d}{dx} \left[ \left( 1- x^2 \right) \frac{d P_l}{dx} \right]+ l \left(l+1\right) P_l(x) =0 \end{equation}
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import legendre
x=np.linspace(-1,1,100)
L=10
for ii in range (0,L) :
p=legendre(ii)
plt.plot(x, p(x), label='l='+str(ii))
plt.legend()
plt.grid(alpha=0.3)
plt.xlabel='$x$'
plt.ylabel='$P_l$'
plt.title('Plot of Legendre polynomials')
Text(0.5, 1.0, 'Plot of Legendre polynomials')
import numpy as np
import matplotlib.pyplot as plt
import imageio
import os
from scipy.special import legendre
from IPython.display import Image
x=np.linspace(-np.pi, np.pi, 1000)
infinity=20
filenames=[]
images = []
for n in range(infinity) :
p=legendre(n)
plt.plot(x, p(x), 'C0', label='$P_{'+str(n)+'}(x)$')
plt.axhline(y=0,color='r',label='Zero')
plt.grid(alpha=0.3)
#plt.xlabel('x')
#plt.ylabel('$P_l$')
plt.xlim([ -1.1, 1.1])
plt.ylim([-1.1, 1.1])
plt.legend(loc='upper center')
plt.tight_layout()
plt.title('Plot of Legendre Polynomial l='+str(n))
fname='legendre_'+str(n).rjust(3,'0')+'.png'
plt.savefig(fname)
filenames.append(fname)
plt.close()
for filename in filenames:
images.append(imageio.v3.imread(filename))
imageio.mimsave('Step_Legendre.gif', images, duration=1)
for filename in filenames:
os.remove(filename)
Image(url='Step_Legendre.gif')
Any $L^2$ function in the interval can be expressed interms of Legendre Polynomial as, \begin{equation} f(x) = \sum_{l=0}^{\infty} \, A_l P_l(x)\,, \end{equation} where, \begin{equation} A_l = \frac{2l+1}{2} \, \int_{-1}^{+1} \, f(x) P_l(x) \, dx\,. \end{equation}
Express the function \begin{equation} f(x)=\left\{ \begin{array}{cc} +1 & \left(x<0\right)\\ -1 & \left(x\geq0\right) \end{array}\right. \end{equation} interms Fourier-Legendre Series
From Jackson we have $$ A_l =\left(=\frac12\right)^{(l-1)/2} \frac{(2l+1)(l-2)!!}{2\left(\frac {l+1}{2}\right)!} $$ The explicit expression is given by, $$ f(x) = \frac 32 P_1(x) - \frac 78 P_3(x) + \frac{11}{16} P_5(x) + \cdots $$
import numpy as np
import matplotlib.pyplot as plt
import imageio
import os
from scipy.special import legendre
import scipy.special as spl
from IPython.display import Image
def step_3term_leg(x) :
p1=legendre(1)
p3=legendre(3)
p5=legendre(5)
fx=3/2*p1(x)-7/8*p3(x)+11/16*p5(x)
return fx
def jackson_al(l) :
n2f=spl.factorial2((l-2), exact=True)
nf=spl.factorial((l+1)/2, exact=True)
Al=((-1/2)**((l-1)/2))*0.5*(2*l+1)*n2f/nf
return Al
def step(x) :
fx=np.zeros(x.shape)
(idx)=np.where(x < 0)
fx[idx]=-1
(idx)=np.where(x > 0)
fx[idx]=1
(idx)=np.where(x < -1.0)
fx[idx]=None
(idx)=np.where(x > 1.0)
fx[idx]=None
return fx
infty=60
x=np.linspace(-1.0,1.0, 1000)
plt.plot(x, step(x), 'C0', label='Step')
sts=np.zeros(x.shape);
plt.plot(x, step_3term_leg(x), 'C1', label='3 Term Legendre')
plt.grid(alpha=0.3)
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.xlim([-1.1,1.1])
filenames=[]
images = []
fname='step_'+str(0).rjust(3,'0')+'.png'
plt.savefig(fname)
filenames.append(fname)
plt.close()
for ll in range(1,infty,2) :
Al=jackson_al(ll)
pl=legendre(ll)
sts+=Al*pl(x)
plt.plot(x, step(x), 'C0', label='Step')
plt.plot(x, step_3term_leg(x), 'C1', label='3 Term Legendre')
plt.plot(x, sts, 'C2', label=str(ll)+' terms')
plt.grid(alpha=0.3)
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.xlim([-1.1,1.1])
fname='step_'+str(ll).rjust(3,'0')+'.png'
plt.savefig(fname)
filenames.append(fname)
plt.close()
for filename in filenames:
images.append(imageio.v3.imread(filename))
imageio.mimsave('Step_Legendre_large.gif', images, duration=1)
for filename in filenames:
os.remove(filename)
Image(url='Step_Legendre_large.gif')
Express $\delta(x-x_0)$ in the interval $[-1,\,1]$ \begin{equation} \delta(x-x) \ = \ \sum_{l=0}^{-\infty} \, A_l P_l(x) \end{equation} where \begin{eqnarray} A_l & = & \frac{2 l+1}{2} \int_{-1}^{1} \delta(x-x_0) \, P_l(x) & = & \frac{2 l+1}{2} P_l(x_0) \end{eqnarray} \begin{equation} \delta(x-x) \ = \ \sum_{l=0}^{-\infty} \, \frac{2 l+1}{2} P_l(x_0) P_l(x) \end{equation}
import numpy as np
import matplotlib.pyplot as plt
import imageio
import os
from scipy.special import legendre
import scipy.special as spl
from IPython.display import Image
infty=30
x=np.linspace(-1.0,1.0, 1000)
x0=0.3
sts=np.zeros(x.shape);
filenames=[]
images = []
for ll in range(infty) :
Al=(2*ll+1)/2
pl=legendre(ll)
sts+=Al*pl(x)*pl(x0)
plt.plot(x, sts, 'C2', label=str(ll)+' terms')
plt.axhline(y=0, color='c', label='0')
plt.axvline(x=x0, color='r', label='x0='+str(x0))
plt.grid(alpha=0.3)
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.xlim([-1.1,1.1])
plt.ylim([-3.1,11])
fname='dirac_legendre'+str(ll).rjust(3,'0')+'.png'
plt.savefig(fname)
filenames.append(fname)
plt.close()
for filename in filenames:
images.append(imageio.v3.imread(filename))
imageio.mimsave('Dirac_Legendre.gif', images, duration=1)
for filename in filenames:
os.remove(filename)
Image(url='Dirac_Legendre.gif')