Lab 8
Lab 8
LAB 8: Computation of area under the curve using trapezoidal, Simpson’s( )rd and Simpsons ( )th
𝟑 𝟖
rule
Objectives:
Use python
1. to find the area under the curve represented by a given function using the Trapezoidal rule.
1
2. to find the area under the curve represented by a given function using Simpson’s ( 3)rd rule
3
3. to find the area under the curve represented by a given function using Simpson’s ( 8)th rule
4. to find the area below the curve when discrete points on the curve are given.
Trapezoidal Rule
𝟓 𝟏
1. Evaluate ∫𝟎 𝟏+𝒙𝟐
def my_func( x ):
return 1/(1 + x ** 2)
h=(xn-x0)/n
integration=my_func(x0)+my_func(xn)
k=x0+i*h
integration=integration+2*my_func(k)
integration=integration*h / 2
return integration
output:
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
Simpson’s 1/3rd rule
𝟓 𝟏
2.Evaluate ∫𝟎 𝟏+𝒙𝟐
def my_func(x):
return 1/(1 + x ** 2)
h=(xn - x0)/n
integration=(my_func( x0 )+my_func(xn))
k=x0
for i in range(1,n):
if i%2 == 0:
integration=integration+4*my_func(k)
else:
integration=integration+2*my_func(k)
k+= h
integration=integration*h*(1/3)
return integration
output:
Enter lower limit of integration : 0
Enter upper limit of integration : 5
Enter number of sub intervals : 100
Integration result by Simpson 's 1/3 method is: 1.404120
Simpson’s 3/8th rule
𝟔 𝟏
2.Evaluate ∫𝟎 using Simpson’s 3/8 th rule, taking 6 sub interval
𝟏+𝒙𝟐
h=(b - a)/n
s=f(a)+f(b)
s+=3*f(a + i * h)
s+=3*f(a + i * h)
s+=2* f(a + i * h)
return s*3*h/8
def f(x):
return 1/(1+x**2)
a=0
b=6
n=6
result=simpsons_3_8_rule(f , a , b , n)
output:
1.31381