Graphical Representation Of Legendre Function Using Python

 Graphical Representation of Legendre Polynomial using Python:

Algorithm:
  • Importing necessary libraries such as math to get factorial value , numpy for array creation and matplotlib.pyplot for plotting the graph .
  • Define Legendre function with parameter n and x .
  • Creating an array such as numpy linspace from -1 to +1 .
  • Ploting the graph .

Source Code :



import numpy as np 
from math import *
import matplotlib.pyplot as plt
newparams = {'axes.labelsize': 10, 'axes.linewidth': 1.5, 'savefig.dpi':  
      1000,  
     'lines.linewidth': 2, 'figure.figsize': (12, 10),    
     'legend.frameon': True,  
      'legend.handlelength': 0.7}  
plt.rcParams.update(newparams)  
 
 
def Pn(n,x):
    a=0
    m=n//2 
    for i in range(m+1):
        t = (-1)**i*factorial(2*n-2*i)/(2**n*factorial(i)*factorial(n-i)*factorial(n-2*i))
        b = t*x**(n-2*i)
        a += b
    return a    
 
x = np.linspace(-1,1,100)
col = ['r','b','g','y','c']
for i in range(5):  
   plt.plot(x,Pn(i,x),c=col[i] ,label='$P_%d(x)$'%i ,markevery =None)  
plt.legend(loc = 'best' ,prop={'size':12})
plt.xlabel('x')  
plt.ylabel(r' $ P_n(x)$')
plt.ylim(-0.5,1.1)
plt.xlim(0,1)
plt.title("Graphical Representation Of Legendre Function")
plt.show()
plot Legendre's Function with python
                                                       PDF of Source Code
Share:

Post a Comment

For query , suggestion and others , please comment below. Don't share external link or spam on comment box . 💌

Previous Post Next Post