# Trapezoidal Method Python Program

Trapezoidal Method is a rule that calculates the area under the curve by dividing the area into trapezoids.

``````#Trapezoidal Method
#Define Function To Integrate
def f(x):
return 1/(1+x**2)

#Implementing Trapezoidal Method
def trapezoidal(x0,xn,n):
#calculate step size
h=(xn-x0)/n

#Finding sum
integration=f(x0)+f(xn)

for i in range(1,n):
k=x0+i*h
integration=integration+2*f(k)
integration=integration*h/2

return integration

#Input section
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 interval: "))

#call trapezoidal function
result=trapezoidal(lower_limit, upper_limit, sub_interval)
print("Integration result by trapezoidal method is: %0.6f"%(result))``````
Python

Output

``````Enter lower limit of integration: 3
Enter upper limit of integration: 6
Enter number of sub interval: 4
Integration result by trapezoidal method is: 0.158980``````