0% found this document useful (0 votes)
8 views17 pages

Ashish Unit 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views17 pages

Ashish Unit 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Input:

import numpy as np
import pandas as pd
from numpy.polynomial.legendre import leggauss
from scipy.integrate import quad
from tabulate import tabulate

print("Ashish Kumar Meena | 13935")

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

def inte(w, q, n):


total = 0
for i in range(n):
total += w[i] * f(q[i])
return total
result, _ = quad(lambda x: f(x), -1, 1)
re1 = inte(w1, p1, 2)
re2 = inte(w2, p2, 3)
re3 = inte(w4, x4, 4)
re4 = inte(w8, x8, 8)

print( 'Two Point Form:',re1)


print('Three Point Form:',re2)
print('Four Point Form:',re3)
print('Eight Point Form:',re4)

err = ((result - re1) / result) * 100


err1 = ((result - re2) / result) * 100
err2 = ((result - re3) / result) * 100
err3 = ((result - re4) / result) * 100

from tabulate import tabulate


data = [
['2', re1,result,err],
['3', re2,result,err1],
['4', re3,result,err2],
['8', re4,result,err3]
]
headers = ['n','Gauss Legendre Method', 'Actual Integral Value', '%error']
print(tabulate(data, headers, tablefmt="grid"))

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

print("Ashish Kumar Meena | 13935")

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

def inte(w, q, n):


total = 0
for i in range(n):
total += w[i] * f(q[i])
return total

result, _ = quad(lambda x: f(x), -1, 1)


re1 = inte(w1, p1, 2)
re2 = inte(w2, p2, 3)
re3 = inte(w4, x4, 4)
re4 = inte(w8, x8, 8)

print( 'Two Point Form:',re1)


print('Three Point Form:',re2)
print('Four Point Form:',re3)
print('Eight Point Form:',re4)

err = ((result - re1) / result) * 100


err1 = ((result - re2) / result) * 100
err2 = ((result - re3) / result) * 100
err3 = ((result - re4) / result) * 100

from tabulate import tabulate


data = [
['2', re1,result,err],
['3', re2,result,err1],
['4', re3,result,err2],
['8', re4,result,err3]
]
headers = ['n','Gauss Legendre Method', 'Actual Integral Value', '%error']
print(tabulate(data, headers, tablefmt="grid"))

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

print("Ashish Kumar Meena | 13935")

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

def inte(w, q, n):


total = 0
for i in range(n):
total += w[i] * f(q[i])
return total

result, _ = quad(lambda x: f(x), -1, 1)


re1 = inte(w1, p1, 2)
re2 = inte(w2, p2, 3)
re3 = inte(w4, x4, 4)
re4 = inte(w8, x8, 8)

print( 'Two Point Form:',re1)


print('Three Point Form:',re2)
print('Four Point Form:',re3)
print('Eight Point Form:',re4)

err = ((result - re1) / result) * 100


err1 = ((result - re2) / result) * 100
err2 = ((result - re3) / result) * 100
err3 = ((result - re4) / result) * 100

from tabulate import tabulate


data = [
['2', re1,result,err],
['3', re2,result,err1],
['4', re3,result,err2],
['8', re4,result,err3]
]
headers = ['n','Gauss Legendre Method', 'Actual Integral Value', '%error']
print(tabulate(data, headers, tablefmt="grid"))

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

print("Ashish Kumar Meena | 13935")

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)

def inte(w, q, n):


total = 0
for i in range(n):
total += w[i] * f(q[i])
return total

result, _ = quad(lambda x: f(x), -1, 1)


re1 = inte(w, p, 2)
re2 = inte(w2, p2, 3)
re3 = inte(w4, x4, 4)
re4 = inte(w8, x8, 8)

print( 'Two Point Form:',re1)


print('Three Point Form:',re2)
print('Four Point Form:',re3)
print('Eight Point Form:',re4)
err = ((result - re1) / result) * 100
err1 = ((result - re2) / result) * 100
err2 = ((result - re3) / result) * 100
err3 = ((result - re4) / result) * 100

from tabulate import tabulate


data = [
['2', re1,result,err],
['3', re2,result,err1],
['4', re3,result,err2],
['8', re4,result,err3]
]
# Column headers
headers = ['n','Gauss Hermite Method', 'Actual Integral Value', '%error']
print(tabulate(data, headers, tablefmt="grid"))

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)

def gauss_laguerre_quadrature(func, n):


x, w = laggauss(n)
integral_approx = np.sum(w * func(x))
return integral_approx

# Number of quadrature points to use


n_points = [2, 3, 4, 6]

# 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))

# Calculate the integral using scipy's quad function


result, error = quad(lambda x: integrand(x) * np.exp(-x), 0, np.inf)

# Tabulating results and errors


data = []
for n, approx_integral in results:
error_percentage = ((result - approx_integral) / result) * 100
data.append([n, approx_integral, result, error_percentage])
# Column headers
headers = ['n', 'Gauss Laguerre Method', 'Actual Integral Value', '% Error']
print(tabulate(data, headers, tablefmt="grid"))

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:

print("Ashish Kumar Meena | 13935")

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)

def gauss_laguerre_quadrature(func, n):


x, w = laggauss(n)
integral_approx = np.sum(w * func(x))
return integral_approx

# Number of quadrature points to use


n_points = [2, 3, 4, 6]

# 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))

# Calculate the integral using scipy's quad function


result, error = quad(lambda x: integrand(x)*np.exp(-x), 0, np.inf)

# Tabulating results and errors


data = []
for n, approx_integral in results:
error_percentage = ((result - approx_integral) / result) * 100
data.append([n, approx_integral, result, error_percentage])

# 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:

print("Ashish Kumar Meena | 13935")

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)

def gauss_hermite_quadrature(func, n):


x, w = hermgauss(n)
integral_approx = np.sum(w * func(x))
return integral_approx

# Number of quadrature points to use


n_points = [2, 3, 4, 6]

# 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))

# Calculate the integral using scipy's quad function


result, error = quad(lambda x: integrand(x)*np.exp(-x**2), -np.inf, np.inf)

# Tabulating results and errors


data = []
for n, approx_integral in results:
error_percentage = ((result - approx_integral) / result) * 100
data.append([n, approx_integral, result, error_percentage])

# 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:

print("Ashish Kumar Meena | 13935")

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))

def gauss_hermite_quadrature(func, n):


x, w = hermgauss(n)
integral_approx = np.sum(w * func(x))
return integral_approx

# Number of quadrature points to use


n_points = [2, 3, 4, 6]

# 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))

# Calculate the integral using scipy's quad function


result, error = quad(lambda x: integrand(x)*np.exp(-x**2), -np.inf, np.inf)

# Tabulating results and errors


data = []
for n, approx_integral in results:
error_percentage = ((result - approx_integral) / result) * 100
data.append([n, approx_integral, result, error_percentage])

# Column headers
headers = ['n', 'Gauss hermite Method', 'Actual Integral Value', '% Error']
print(tabulate(data, headers, tablefmt="grid"))

Output:

You might also like