Ashish Unit 2
Ashish Unit 2
import numpy as np
import pandas as pd
from numpy.polynomial.legendre import leggauss
from scipy.integrate import quad
from tabulate import tabulate
w1 = [1, 1]
w2 = [5/9, 8/9, 5/9]
p1 = [np.sqrt(1/3), -np.sqrt(1/3)]
p2 = [-np.sqrt(3/5), 0, np.sqrt(3/5)]
x4, w4 = leggauss(4)
x8, w8 = leggauss(8)
def f(x):
a=0
b=2
t = ((b + a) * x) / 2 + (b - a) / 2
dt = (b + a) / 2
return (t**5) * dt
Output:
Input:
import numpy as np
import pandas as pd
from numpy.polynomial.legendre import leggauss
from scipy.integrate import quad
from tabulate import tabulate
w1 = [1, 1]
w2 = [5/9, 8/9, 5/9]
p1 = [np.sqrt(1/3), -np.sqrt(1/3)]
p2 = [-np.sqrt(3/5), 0, np.sqrt(3/5)]
x4, w4 = leggauss(4)
x8, w8 = leggauss(8)
def f(x):
a=0
b=3
t = ((b + a) * x) / 2 + (b - a) / 2
dt = (b + a) / 2
return (t**2+3*t-5) * dt
Output:
Input:
import numpy as np
import pandas as pd
from numpy.polynomial.legendre import leggauss
from scipy.integrate import quad
from tabulate import tabulate
w1 = [1, 1]
w2 = [5/9, 8/9, 5/9]
p1 = [np.sqrt(1/3), -np.sqrt(1/3)]
p2 = [-np.sqrt(3/5), 0, np.sqrt(3/5)]
x4, w4 = leggauss(4)
x8, w8 = leggauss(8)
def f(x):
a=0
b=3
t = ((b + a) * x) / 2 + (b - a) / 2
dt = (b + a) / 2
return (t**5) * dt
Output:
Q 2.4 Write a Python program to solve the following improper integral over the entire real
axis using gauss Legendre Quadrature
∞ 2 1
∫−∞ 𝑒 −𝑥 1+𝑥 2
𝑑𝑥
Tabulate your integral value with the number of quadrature points . Compare the results
obtained using in build function.
Input:
import numpy as np
import pandas as pd
from numpy.polynomial.legendre import leggauss
from scipy.integrate import quad
from tabulate import tabulate
w=[1,1]
w2 = [5/9, 8/9, 5/9]
p=[np.sqrt(1/3),-np.sqrt(1/3)]
p2 = [-np.sqrt(3/5), 0, np.sqrt(3/5)]
x4,w4=leggauss(4)
x8,w8=leggauss(8)
def f(x):
return (np.pi/2)*np.exp((-1)*(np.tan(x*(np.pi/2)))**2)
Output:
Q 2.5 Write a Python program to solve following improper integral over real axis using
gauss Laguerre quadrature.
∞
1
∫ 𝑒 −𝑥+2 𝑑𝑥
0 1 + 𝑥2
Tabulate your integral value with number of quadrature points. Compare the results
obtained using inbuilt functions. Note: apply the method to the function of exponential
weights.
Input:
import numpy as np
from numpy.polynomial.laguerre import laggauss
from scipy.integrate import quad
from tabulate import tabulate
print("Ashish Kumar Meena | 13935")
def integrand(x):
return np.exp(2) / (1 + x**2)
# Calculate the integral using Gauss-Laguerre quadrature for different numbers of points
results = []
for n in n_points:
approx_integral = gauss_laguerre_quadrature(integrand, n)
results.append((n, approx_integral))
Output:
Q 2.6 Write a Python program to solve following improper integral over real axis using
gauss Laguerre quadrature.
∞
∫ 𝑥 5 𝑒 −𝑥+2.2 𝑑𝑥
0
Tabulate your integral value with number of quadrature points. Compare the results
obtained using inbuilt functions. Note: apply the method to the function of exponential
weights.
Input:
import numpy as np
from numpy.polynomial.laguerre import laggauss
from scipy.integrate import quad
from tabulate import tabulate
def integrand(x):
return x**5 * np.exp( 2.2)
# Calculate the integral using Gauss-Laguerre quadrature for different numbers of points
results = []
for n in n_points:
approx_integral = gauss_laguerre_quadrature(integrand, n)
results.append((n, approx_integral))
# Column headers
headers = ['n', 'Gauss Laguerre Method', 'Actual Integral Value', '% Error']
print(tabulate(data, headers, tablefmt="grid"))
Output:
Q 2.7 Write a Python program to solve following improper integral over real axis using
Gauss Hermite quadrature.
∞
1 2
∫ 2
𝑒 −𝑥 𝑑𝑥
−∞ 1 + 𝑥
Tabulate your integral value with number of quadrature points. Compare the results
obtained using inbuilt functions. Note: apply the method to the function of Gaussian
Weight.
Input:
import numpy as np
from numpy.polynomial.hermite import hermgauss
from scipy.integrate import quad
from tabulate import tabulate
def integrand(x):
return 1/(1+x**2)
# Calculate the integral using Gauss-hermite quadrature for different numbers of points
results = []
for n in n_points:
approx_integral = gauss_hermite_quadrature(integrand, n)
results.append((n, approx_integral))
# Column headers
headers = ['n', 'Gauss hermite Method', 'Actual Integral Value', '% Error']
print(tabulate(data, headers, tablefmt="grid"))
Output:
Q 2.8 Write a Python program to solve following improper integral over real axis using
Gauss Hermite quadrature.
∞
2 −1
∫ 𝑥4 𝑒−𝑥 𝑑𝑥
−∞
Tabulate your integral value with number of quadrature points. Compare the results
obtained using inbuilt functions. Note: apply the method to the function of Gaussian
Weight.
Input:
import numpy as np
from numpy.polynomial.hermite import hermgauss
from scipy.integrate import quad
from tabulate import tabulate
def integrand(x):
return (x**4)*(np.exp(-1))
# Calculate the integral using Gauss-hermite quadrature for different numbers of points
results = []
for n in n_points:
approx_integral = gauss_hermite_quadrature(integrand, n)
results.append((n, approx_integral))
# Column headers
headers = ['n', 'Gauss hermite Method', 'Actual Integral Value', '% Error']
print(tabulate(data, headers, tablefmt="grid"))
Output: