0% found this document useful (0 votes)
18 views2 pages

LAB 8 (19-04-25) - Jupyter Notebook

The document outlines a lab exercise focused on computing the area under the curve using various numerical integration methods: Trapezoidal, Simpson's 1/3rd, and Simpson's 3/8th rules. It includes Python code examples for evaluating integrals such as ∫0 1/(1+x^2) dx and ∫0.6 e^(-x^2) dx, along with user inputs for limits and sub-intervals. The results of the integrations are printed for each method applied.

Uploaded by

mayurgawade1616
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)
18 views2 pages

LAB 8 (19-04-25) - Jupyter Notebook

The document outlines a lab exercise focused on computing the area under the curve using various numerical integration methods: Trapezoidal, Simpson's 1/3rd, and Simpson's 3/8th rules. It includes Python code examples for evaluating integrals such as ∫0 1/(1+x^2) dx and ∫0.6 e^(-x^2) dx, along with user inputs for limits and sub-intervals. The results of the integrations are printed for each method applied.

Uploaded by

mayurgawade1616
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/ 2

5/5/25, 9:37 AM LAB 8 (19-04-25) - Jupyter Notebook

LAB 8: Computation of area under the curve using Trapezoidal,Simpson's 1/3𝑟𝑑


and Simpson's 3/8𝑡ℎRule.

LAB 8.1:Evaluate ∫0 1+𝑥


5 1 𝑑𝑥 using Trapezoidal Rule.
2
In [4]: 1 def my_func(x):
2 return 1/(1+x**2)
3 def trapezoidal(x0,xn,n):
4 h=(xn-x0)/n
5 integration=my_func(x0)+ my_func(xn)
6 for i in range (1,n):
7 k=x0+i*h
8 integration= integration+2*my_func(k)
9 integration=integration*h/2
10 return integration
11 lower_limit =float(input("Enter lower limit of integration:"))
12 upper_limit = float(input("Enter upper limit of integration:"))
13 sub_interval = int(input("Enter number of sub intervals:"))
14 result = trapezoidal(lower_limit,upper_limit,sub_interval)
15 print("Integration result by trapezoidal method is: ",result)

Enter lower limit of integration:0


Enter upper limit of integration:5
Enter number of sub intervals:10
Integration result by trapezoidal method is: 1.3731040812301099

LAB 8.2:Evaluate ∫05 1+𝑥1 2 𝑑𝑥 using Simpson's 1/3𝑟𝑑 Rule.


In [5]: 1 def my_func(x):
2 return 1/(1+x**2)
3 def simpson13(x0,xn,n):
4 h=(xn-x0)/n
5 integration=my_func(x0)+ my_func(xn)
6 k=x0+h
7 for i in range (1,n):
8 if i%2 == 0:
9 integration= integration+2*my_func(k)
10 else:
11 integration= integration+4*my_func(k)
12 k+=h
13 integration=integration*h*(1/3)
14 return integration
15 lower_limit =float(input("Enter lower limit of integration:"))
16 upper_limit = float(input("Enter upper limit of integration:"))
17 sub_interval = int(input("Enter number of sub intervals:"))
18 result =simpson13(lower_limit,upper_limit,sub_interval)
19 print("Integration result by simpson's 1/3rd rule is: ",result)

Enter lower limit of integration:0


Enter upper limit of integration:5
Enter number of sub intervals:10
Integration result by simpson's 1/3rd rule is: 1.3714540087593017

LAB 8.3:Evaluate ∫06 1+𝑥1 2 𝑑𝑥 using Simpson's 3/8𝑡ℎ Rule,taking 6 sub-


intervals.
In [7]: 1 def my_func(x):
2 return 1/(1+x**2)
3 def simpson38(x0,xn,n):
4 h=(xn-x0)/n
5 integration=my_func(x0)+my_func(xn)
6 k=x0+h
7 for i in range (1,n):
8 if i%3 == 0:
9 integration= integration+2*my_func(k)
10 else:
11 integration= integration+3*my_func(k)
12 k+=h
13 integration=integration*h*(3/8)
14 return integration
15 lower_limit =float(input("Enter lower limit of integration:"))
16 upper_limit = float(input("Enter upper limit of integration:"))
17 sub_interval = int(input("Enter number of sub intervals:"))
18 result =simpson38(lower_limit,upper_limit,sub_interval)
19 print("Integration result by simpson's 3/8th rule is: ",result)

Enter lower limit of integration:0


Enter upper limit of integration:6
Enter number of sub intervals:6
Integration result by simpson's 3/8th rule is: 1.3570808364926013

localhost:8888/notebooks/Desktop/prajwal-220/LAB 8 (19-04-25).ipynb 1/3


5/5/25, 9:37 AM LAB 8 (19-04-25) - Jupyter Notebook

Exercise:

1:Evaluate ∫01 1+𝑥𝑥2 3 𝑑𝑥 using Simpson's 1/3𝑟𝑑 Rule.


In [1]: 1 def my_func(x):
2 return x**2/(1+x**3)
3 def simpson13(x0,xn,n):
4 h=(xn-x0)/n
5 integration=my_func(x0)+ my_func(xn)
6 k=x0+h
7 for i in range (1,n):
8 if i%2 == 0:
9 integration= integration+2*my_func(k)
10 else:
11 integration= integration+4*my_func(k)
12 k+=h
13 integration=integration*h*(1/3)
14 return integration
15 lower_limit =float(input("Enter lower limit of integration:"))
16 upper_limit = float(input("Enter upper limit of integration:"))
17 sub_interval = int(input("Enter number of sub intervals:"))
18 result =simpson13(lower_limit,upper_limit,sub_interval)
19 print("Integration result by simpson's 1/3rd rule is: ",result)

Enter lower limit of integration:0


Enter upper limit of integration:1
Enter number of sub intervals:6
Integration result by simpson's 1/3rd rule is: 0.2310565977232644

2.Evaluate ∫00.6 𝑒−𝑥2 using Simpson's 3/8𝑡ℎ Rule,taking seven ordinates.


In [2]: 1 import math
2 def my_func(x):
3 return (math.exp(-x**2))
4 def simpson38(x0,xn,n):
5 h=(xn-x0)/n
6 integration=my_func(x0)+ my_func(xn)
7 k=x0+h
8 for i in range (1,n):
9 if i%3 == 0:
10 integration= integration+2*my_func(k)
11 else:
12 integration= integration+3*my_func(k)
13 k+=h
14 integration=integration*h*(3/8)
15 return integration
16 lower_limit =float(input("Enter lower limit of integration:"))
17 upper_limit = float(input("Enter upper limit of integration:"))
18 sub_interval = int(input("Enter number of sub intervals:"))
19 result =simpson38(lower_limit,upper_limit,sub_interval)
20 print("Integration result by simpson's 3/8rd rule is: ",result)

Enter lower limit of integration:0


Enter upper limit of integration:0.6
Enter number of sub intervals:6
Integration result by simpson's 3/8rd rule is: 0.5351583836786553

3. evaluate ∫0𝜋 𝑠𝑖𝑛2 𝑥𝑑𝑥 using Simpson's 3/8rd Rule.


In [6]: 1 import math
2 def my_func(x):
3 return (math.sin(x))**2
4 def trapezoidal(x0,xn,n):
5 h=(xn-x0)/n
6 integration=my_func(x0)+my_func(xn)
7 for i in range (1,n):
8 k=x0+i*h
9 integration= integration+2*my_func(k)
10 integration=integration*h/2
11 return integration
12 lower_limit =float(input("Enter lower limit of integration:"))
13 upper_limit = float(input("Enter upper limit of integration:"))
14 sub_interval = int(input("Enter number of sub intervals:"))
15 result =trapezoidal(lower_limit,upper_limit,sub_interval)
16 print("Integration result by trapezoidal method is: ",result)

Enter lower limit of integration:0


Enter upper limit of integration:3.142
Enter number of sub intervals:6
Integration result by trapezoidal method is: 1.570815293866167

localhost:8888/notebooks/Desktop/prajwal-220/LAB 8 (19-04-25).ipynb 2/3

You might also like