-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGauss_laguerre_Quadrature_N.py
65 lines (52 loc) · 1.32 KB
/
Gauss_laguerre_Quadrature_N.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 27 23:19:30 2021
@author: DELL
"""
import scipy.integrate as integral
import numpy as np
import numpy.polynomial.laguerre as lag
import math
import matplotlib.pyplot as plt
#Recurrence for laguerre
def L(n,x):
if n==0:
return 1
elif n==1:
return 1-x
else:
return ((2*n-1-x)*L(n-1,x)-(n-1)*L(n-2,x))/(n)
#question: f(x)=(2+x^2)^(-1) ; hence
# g(x)=exp(x)*(2+x^2)^(-1)
def f(x):
f=1/(2+x**2)
return f
def func(x):
g=np.exp(x)*f(x)
return g
# Recurrence for L'(n,x):x*L'(n,x)=(x-n-1)*L(n,x)+(n+1)*L(n+1,x)
dl_dx=lambda n,x:((x-n-1)*L(n,x)+(n+1)*L(n+1,x))/(x)
n=int(input("enter n"))
"""finding roots of nth laguerre polynomial and storing it in t"""
Q=[]
for i in range (n):
Q.append(0)
Q.append(1)
t=lag.lagroots(Q)
print("x found by quadrature rule",t)
#finding the solution
sol=0
for i in range(n):
#weight
w=-1/(n*dl_dx(n,t[i])*L(n-1,t[i]))
#print(w)
sol1=w*func(t[i])
sol=sol+sol1
main_sol=sol
print("the value of integral of the function with n=",n,"is\n",main_sol)
man_sol=integral.quad(f, 0, math.inf)
man_sol=round(man_sol[0],10)
print("manual sol",man_sol)
y = lambda x:1/(2+x**2)
x = np.linspace(0,1000,1)
plt.plot(x,f(x))