CMP 6
CMP 6
import numpy as np
# Trapezoidal Rule
def trapezoidal_2d(Z, h, k):
m, n = Z.shape
integral = 0
for i in range(m):
for j in range(n):
Page 1 of 4
COEP TECHNOLOGICAL UNIVERSITY, PUNE
weight = 1
if i == 0 or i == m - 1:
weight *= 0.5
if j == 0 or j == n - 1:
weight *= 0.5
integral += weight * Z[i, j]
return integral * h * k
# Simpson's Rule
def simpsons_2d(Z, h, k):
m, n = Z.shape
if m % 2 == 0 or n % 2 == 0:
raise ValueError("Simpson's rule requires an odd number of points in
both directions.")
integral = 0
for i in range(m):
for j in range(n):
if i == 0 or i == m - 1 or j == 0 or j == n - 1:
weight = 1
elif (i % 2 == 1 and j % 2 == 1):
weight = 4
elif (i % 2 == 1 or j % 2 == 1):
weight = 2
else:
weight = 1
integral += weight * Z[i, j]
return (h * k / 9) * integral
import numpy as np
Page 3 of 4
COEP TECHNOLOGICAL UNIVERSITY, PUNE
# Limits of integration
a, b = 0, 1
# Print results
print("2-Point Gauss-Legendre Quadrature Result:", integral_2_point)
print("3-Point Gauss-Legendre Quadrature Result:", integral_3_point)
The results for the given integral using Gauss-Legendre Quadrature are:
2-Point Rule: 0.78690.78690.7869
3-Point Rule: 0.78530.78530.7853
Page 4 of 4