0% found this document useful (0 votes)
10 views

Lab 8

Lab 8 programs

Uploaded by

akshayandani05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab 8

Lab 8 programs

Uploaded by

akshayandani05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

𝟏 𝟑

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)

def trapezoidal(x0 , xn , n):

h=(xn-x0)/n

integration=my_func(x0)+my_func(xn)

for i in range(1, n):

k=x0+i*h

integration=integration+2*my_func(k)

integration=integration*h / 2

return integration

lower_limit=float(input("Enter lower limit of integration : "))

upper_limit=float(input("Enter upper limit of integration : "))

sub_interval=int(input("Enter number of sub intervals : "))

result=trapezoidal(lower_limit , upper_limit , sub_interval)

print("Integration result by Trapezoidal method is: " , result)

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)

def simpson13(x0 , xn , n):

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

lower_limit=float(input (" Enter lower limit of integration : "))

upper_limit=float(input(" Enter upper limit of integration : "))

sub_interval=int(input(" Enter number of sub intervals : "))

result=simpson13(lower_limit , upper_limit , sub_interval)

print("Integration result by Simpson 's 1/3 method is: %0.6f" % ( result ))

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
𝟏+𝒙𝟐

def simpsons_3_8_rule(f , a , b , n):

h=(b - a)/n

s=f(a)+f(b)

for i in range(1 , n , 3):

s+=3*f(a + i * h)

for i in range(2 , n-1 , 3):

s+=3*f(a + i * h)

for i in range(3 , n-2 , 3):

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)

print('%3.5f '% result)

output:
1.31381

You might also like