An Detailed Paper On Integration
An Detailed Paper On Integration
Trapezoidal method
In this method, the whole range of integration is divided into an n
number of equally spaced subintervals of width h.
The value of the integral in the first subinterval is the area of the
trapezium with base h and bounded by two parallel lines y and y : 0 1
,
h(y0 +y1 )
where h
xn −x0
=
n
xn h
∫ ydx = [y0 + 2(y1 + y2 +. . . . . . . +yn−1 ) + yn ]
x0 2
For each case verify your answer using the corresponding function from
the SciPy library.
Also, plot the integrand functions as f(x) vs x in the range of the given
integration limit using matplotlib in separate figures.
In [1]: import numpy as np
from scipy.integrate import trapz
import matplotlib.pyplot as plt
def f(x):
return (1+x**2+x**4)**(1/4)
def f1(x):
return np.sqrt(np.tan(x))
n=int(input("No of intervals"))
print (n)
a,b=0, 2.5
a1, b1=0, np.pi/4
h=(b-a)/n
h1=(b1-a1)/n
I_1=0.5*h*(f(a)+f(b)+2*sum([f(a+i*h) for i in range(1,n)]))
I_2=0.5*h1*(f1(a1)+f1(b1)+2*sum([f1(a1+i*h1) for i in range(1,n)]))
print('Value of first integral=', I_1)
print('Value of second integral=', I_2)
# Compare the result with the result obtained using the Scipy function
x=np.linspace(a,b, num=n)
x1=np.linspace(a1,b1, num=n)
I1_tra=trapz(f(x),x)
I2_tra=trapz(f1(x1),x1)
print('Value of first integral using trapz function from Scipy library =', I1_tra)
print('Value of second integral using trapz function from Scipy library =', I2_tra)
20
Value of first integral= 3.9961570250846172
Value of second integral= 0.4860062400095109
Value of first integral using trapz function from Scipy library = 3.9962907405062658
Value of second integral using trapz function from Scipy library = 0.4858907386385015
Simson's 1/3 method
In this method, the whole range of integration is divided into an n-even
number of equally spaced subintervals of width h.
f (x) = ax
2
+ bx + c .
h
h h
2
∫ f (x) dx = (2ah + 6c) = [f (x0 ) + 4f (x1 ) + f (x2 ]
3 3
−h
n 0, x1 , x2 )
+ f (xn )]
For each case verify your answer using the corresponding function from
the SciPy library.
Also, plot the integrand functions as f(x) vs x in the range of the given
integration limit using matplotlib in separate figures.
In [2]: n=20
print ([i for i in range(1, n,2)]) # odd values of n
print ([i for i in range(2, n-1, 2)]) # even values of n
def f(x):
return x**2*np.sin(2*x)
def f1(x):
return np.exp(-x**2/2)
n=int(input("No of intervals"))
print (n)
# Integration limit for the first integration
a,b=0, 2*np.pi
h=(b-a)/n
# First integration
f_odd=4*sum ([f(a+i*h) for i in range(1, n, 2)])
f_even=2*sum ([f(a+i*h) for i in range(2, n-1, 2)])
I1_simp=(h/3)*(f(a)+f(b)+f_odd+f_even)
print('Value of first integral=',I1_simp)
x=np.linspace(a,b, num=n)
I1_simp_scipy=simps(f(x),x)
print('Value of first integral using simps function from Scipy library =', I1_simp_scipy
# Second integration
a1,b1=-5.0, 5.0
h1=(b1-a1)/n
f1_odd=4*sum ([f1(a1+i*h1) for i in range(1, n, 2)])
f1_even=2*sum ([f1(a1+i*h1) for i in range(2, n-1, 2)])
I2_simp=(h1/3)*(f1(a1)+f1(b1)+f1_odd+f1_even)
print('Value of first integral=',I2_simp)
x1=np.linspace(a1,b1, num=n)
I2_simp_scipy=simps(f1(x1),x1)
print('Value of first integral using simps function from Scipy library =', I2_simp_scipy
40
Value of first integral= -19.74028969475288
Value of first integral using simps function from Scipy library = -19.727365488719492
Value of first integral= 2.506626821938234
Value of first integral using simps function from Scipy library = 2.5066270343358035
In [ ]: