Experiment 6 User Defined Function Legendre Function ( Series Expansion )

User Defined Function Legendre Function ( Series Expansion )


CONCEPT 
 
(a)(Legendre Differential Equation)
                                     The Legendre differential equation is 
                                 (1 − x^2 )y"− 2xy' + n(n + 1)y = 0 
    where n is a +ve integer. 

(b)(Legendre Function) 
                                  The series solution Pn(x) to this equation 
                         Pn(x) = X(m,i=0)(((−1)^i*(2n − 2i)!)/( 2^n *i!* (n − i)!*(n − 2i)!))*x^(n−2i) 
         is known as the Legendre function. Here m = n/2 if n is even and m = (n − 1)/2 if n is odd. 



METHOD / CODE 

(a)(Factorial Function)  
                    The Gamma function Γ(n) or the factorial n! = Γ(n + 1) (when n is a positive integer) follow the recursive relation  
                                            Γ(n + 1) = nΓ(n) or (n + 1)! = (n + 1)n! 
         Write a code to generate the Gamma function Γ(n) for an arbitrary n. 

(b)(Legendre Function) 
               Write a code to generate the Legendre function as sum of terms of the series expansion.
 
(c)(Derivative of Legendre Function) 
Write a code to generate the derivative of Legendre function as sum of terms of the series expansion.



APPLICATION 

(a)(Legendre Polynomial) Generate the values of P0(x), P1(x) and P2(x) Legendre polynomials in the range x = [−1, 1] using explicit series representation taking 100 data points of x within the given range. Store them in tabulated form as x, P0(x), P1(x) and P2(x) in the data file leg01.dat. Retrieve these values and plot these functions. Display the same on the console. 

(b)(Derivative of the Legendre Polynomial) Generate the values of derivative P 0 0 (x), P 0 1 (x) and P 0 2 (x) of the Legendre polynomial P0(x), P1(x) and P2(x) in the range [−1, 1] using explicit series representation taking 100 data points of x within the given range. Append the same in the data file leg01.dat. Retrieve these values and plot P 0 0 (x), P 0 2 (x) along with P1(x). Display the same on the console.


SCILAB CODE FOR THE GIVEN PROBLEM :-


#CODE FOR LEGENDRE POLYNOMIAL, DERIVATIVE, DATA FILE AND POLAR PLOT

// *******Legendre Function ******
clf
clear
function f=fact(a)
f=1
for i=1:a
f=f*i
end
endfunction
for n=0:1:2
for x=-1:0.02:1
if (modulo(n,2)==0)
m=n/2
else
m=(n-1)/2
end
le=0
for i=0:m
le=le+(((-1)^(i))fact((2*n)-(2*i))(x^(n-(2*i))))/((2^n)*fact(i)*fact(n-i)*fact(n-(2*i)))
end
if n==0
s(f:100)=le
f=f+1
end
if n==1
z(g:100)=le
g=g+1
end
if n==2
k(b:100)=le
b=b+1
end
if n==0
plot(x,le,".b")
else
if n==1
plot(x,le,".r")
else
plot(x,le,".y")
end
end
end
end
disp(" p0(x) p1(x) p2(x)")
disp([s z k])
tab=[s z k]
legend("legendre",)
legends(['p0';'p1';'p2'],[[2;9],[5;9] [7;9]], opt=4, font_size=3,)
xgrid(6,1,7)
xtitle("legendre","x values","legendre values")

//********* Data Files ****
fd=mopen('leg01.dat','w');
mfprintf(fd, '%12.2f\t%12.8f\t%12.8f\n',tab)
mclose(fd)
retrieve=fscanfMat('leg01.dat')
disp("retrieve ")
disp(retrieve)

//**** Derivative of GiveN Legendre Function **
scf
for n=1:2
for x=-1:0.02:1
if (modulo(n,2)==0)
m=n/2
else
m=(n-1)/2
end
dL=0
for i=0
dL=dL+(((-1)^(i))(n-2*i)*fact((2*n)-(2*i))(x^(n-(2*i)-1)))/((2^n)*fact(i)*fact(n-i)*fact(n-(2*i)))
end
if n==1
c(g)=dL
g=g+1
end
if n==2
v(b)=dL
b=b+1
end
if n==1
plot(x,dL,".r")
else
plot(x,dL,".y")
end
end
end
legend("DERIVATIVE OF LEGENDRE")
xgrid(6,1,7)
n=0
for x=-1:0.02:1
dL0=0
i=0
dL0=dL0+(((-1)^(i))(n-2*i)*fact((2*n)-(2*i))(x^(n-(2*i)-1)))/((2^n)*fact(i)*fact(n-i)*fact(n-(2*i)))
m(f)=dL0
f=f+1
plot(x,dL0,".b")
end
display=[c,v,m]
disp("derivatives")
disp(display)

//*********************Polar Plot ******************************

function g= gfull(n)
    g=1
for i=1:n-1
    g=g*i
        end
endfunction
function pol=lfp(n)
    p=1
    if (modulo(n,2)==0) then
        upper=n/2;
    else
        upper=(n-1)/2;
    end
    for theta=-%pi:(%pi/100):%pi
        x=cos(theta)
        sumle=0;sumlp=0;
        for i=0:upper
            num=gfull((2*n)-(2*i)+1)((-1)^i)(x^(n-2*i))
            den=(2^n)*gfull(n-i+1)*gfull(n-(2*i)+1)*gfull(i+1)
            st=num/den;
            sumle=sumle+st
        end
        pol(p)=sumle//value
            p=p+1
        end
    endfunction
    scf
    pol1=lfp(0)
    pol2=lfp(1)
    pol3=lfp(2)
    //ydatapolar2=legendrefunctionpolar(01)
    thetap=linspace(-%pi,%pi,200)
    polarplot(thetap,pol1)
    scf
    polarplot(thetap,pol2)
     //polarplot(xpolar,ydatapolar2)
    scf
     polarplot(thetap,pol3)

 





              
Make Adjustment according to Your Given Problem .
NOTE :- If you have any Problem or Query Regarding this Blog Please Comment Down Or Reach us Out at scilabprogram@gmail.com
//university of Delhi //Department of Physics 

Comments

Popular posts from this blog

Experiment 4:- Simultaneous Equations Matrix Method ( Gauss-Jordan Gauss Siedel Method )

Experiment 1 Data Fitting, Method of Least Squares ( Linear Regression )

Experiment 7 Differential Equation First Order ( Euler Method )