0% found this document useful (0 votes)
12 views4 pages

CMP 6

The document presents a laboratory assignment for evaluating double integrals using numerical methods, specifically the trapezoidal and Simpson's rules, as well as Gauss-Legendre quadrature. It includes Python code for calculating the integrals of the function e^(x+y) and 1/(1+x²) over specified limits, yielding results for both methods. The final results are provided for each method, highlighting the numerical approximations obtained.

Uploaded by

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

CMP 6

The document presents a laboratory assignment for evaluating double integrals using numerical methods, specifically the trapezoidal and Simpson's rules, as well as Gauss-Legendre quadrature. It includes Python code for calculating the integrals of the function e^(x+y) and 1/(1+x²) over specified limits, yielding results for both methods. The final results are provided for each method, highlighting the numerical approximations obtained.

Uploaded by

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

COEP TECHNOLOGICAL UNIVERSITY, PUNE

Name: Dhruv Pravin Kalbhor


TY BTech Mechanical, Batch: H
MIS: 642310022
Subject: CMP (Laboratory)

1. Evaluate ∫∫ e^(x+y) dx dy, with limits from 0 to 1 for both integrals,


using (i) trapezoidal rule (ii) Simpson's rule and taking h = k = 0.5.

import numpy as np

# Define the function


def f(x, y):
return np.exp(x + y)

# Define integration parameters


a, b = 0, 1 # Limits for x
c, d = 0, 1 # Limits for y
h = k = 0.5 # Step sizes

# Discretize the domain


x_vals = np.arange(a, b + h, h)
y_vals = np.arange(c, d + k, k)
X, Y = np.meshgrid(x_vals, y_vals)
Z = f(X, Y)

# 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

# Compute the integral values


trapezoidal_result = trapezoidal_2d(Z, h, k)
simpsons_result = simpsons_2d(Z, h, k)

print("Trapezoidal Rule Result:", trapezoidal_result)


Page 2 of 4
COEP TECHNOLOGICAL UNIVERSITY, PUNE

print("Simpson's Rule Result:", simpsons_result)

The results for the given double integral are:


 Trapezoidal Rule: 3.07633.07633.0763
 Simpson's Rule: 1.02671.02671.0267

2. Evaluate the integral I = ∫ (from 0 to 1) dx / (1 + x²) using (i) 2-point and


(ii) 3-point Gauss-Legendre's Quadrature formula.

import numpy as np

# Define the function


def f(x):
return 1 / (1 + x**2)

# Transform from [a, b] to [-1,1]


def gauss_legendre_quadrature(n, a, b, f):
# Get nodes and weights for n-point Gauss-Legendre quadrature
nodes, weights = np.polynomial.legendre.leggauss(n)

# Transform nodes from [-1,1] to [a,b]


transformed_nodes = 0.5 * (b - a) * nodes + 0.5 * (b + a)

# Compute integral approximation

Page 3 of 4
COEP TECHNOLOGICAL UNIVERSITY, PUNE

integral = 0.5 * (b - a) * np.sum(weights * f(transformed_nodes))


return integral

# Limits of integration
a, b = 0, 1

# Compute integral using 2-point and 3-point Gauss-Legendre quadrature


integral_2_point = gauss_legendre_quadrature(2, a, b, f)
integral_3_point = gauss_legendre_quadrature(3, a, b, f)

# 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

You might also like