0% found this document useful (0 votes)
12 views5 pages

An Detailed Paper On Integration

The document provides a detailed lecture on numerical integration methods, specifically the Trapezoidal and Simpson's 1/3 methods, including their mathematical formulations and implementation in Python. It includes practical examples for evaluating integrals using both methods, along with comparisons to results obtained from the SciPy library. Additionally, it covers plotting the integrand functions using matplotlib for visual representation of the integration process.

Uploaded by

saundersgarrick9
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)
12 views5 pages

An Detailed Paper On Integration

The document provides a detailed lecture on numerical integration methods, specifically the Trapezoidal and Simpson's 1/3 methods, including their mathematical formulations and implementation in Python. It includes practical examples for evaluating integrals using both methods, along with comparisons to results obtained from the SciPy library. Additionally, it covers plotting the integrand functions using matplotlib for visual representation of the integration process.

Uploaded by

saundersgarrick9
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/ 5

Python Course: DSC4-P

Dr. Puja Mondal, Department of Physics, Vivekananda College, Thakurpukur, Kolkata-63

Lecture 10: Numerical Integration: Trapezoidal and Simson's 1/3 method

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

Evaluate the following integrations using the trapezoidal


rule
i) ∫ ii) ∫
2.5 π/4
2 4 1/4
(1 + x + x ) dx √tan(x) dx
0 0

taking the number of intervals as runtime input.

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)

# plot of the first integrand function


plt.plot(x,f(x),'--pg',label='f(x)=$(1+x^{2}+x^{4})^{1/4}$')
# linestyle=--, marker= 'p' (pentagon), color=green
plt.xlabel('x', size=20)
# set the size of x tick label font size
plt.xticks(size=15)
plt.ylabel('f(x)', size=20)
# set the size of y tick label font size
plt.yticks(size=15)
plt.legend(fontsize=14)
plt.show()

# plot of the second integrand function


plt.plot(x1,f1(x1),':*r',label='f1(x)=$\sqrt{tan(x)}$')
# linestyle=:, marker= '*', color=red
plt.xlabel('x1', size=20)
# set the size of x tick label font size
plt.xticks(size=15)
plt.ylabel('f1(x1)', size=20)
# set the size of y tick label font size
plt.yticks(size=15)
plt.legend(fontsize=14)
plt.show()

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.

In this method, we need two subintervals each of width h. The value of


the integration in the interval(-h,h) (consider the first two intervals) is
(ah + 6c) by considering the equation of parabola as
h 2

f (x) = ax
2
+ bx + c .
h
h h
2
∫ f (x) dx = (2ah + 6c) = [f (x0 ) + 4f (x1 ) + f (x2 ]
3 3
−h

where h = and we have three points (x in the two


xn −x0

n 0, x1 , x2 )

subintervals each of length h.

Summing over all subintervals, we get


xn
h
∫ f (x)dx = [f (x0 ) + 4(f (x1 ) + f (x3 )+. . . . +f (xn−1 )) + 2(f (x2 ) + f (x4 )+. . . . +f (xn−2 ))
3
x0

+ f (xn )]

Evaluate the following integrations using Simpson's 1/3 rule


i) ∫ ii) ∫
2π 5 2
2 −x /2
x sin(2x) dx e dx
0 −5

taking the number of intervals as runtime input.

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

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]


[2, 4, 6, 8, 10, 12, 14, 16, 18]

In [3]: import numpy as np


from scipy.integrate import simps
import matplotlib.pyplot as plt

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

# plot of the first integrand function


plt.plot(x,f(x),'--pg',label='f(x)=$x^{2}sin(2x)$')
# linestyle=--, marker= 'p' (pentagon), color=green
plt.xlabel('x', size=20)
# set the size of x tick label font size
plt.xticks(size=15)
plt.ylabel('f(x)', size=20)
# set the size of y tick label font size
plt.yticks(size=15)
plt.legend(fontsize=14)
plt.show()

# plot of the second integrand function


plt.plot(x1,f1(x1),':*r',label='f1(x)=$e^{-x^{2}/2}$')
# linestyle=:, marker= '*', color=red
plt.xlabel('x1', size=20)
# set the size of x tick label font size
plt.xticks(size=15)
plt.ylabel('f1(x1)', size=20)
# set the size of y tick label font size
plt.yticks(size=15)
plt.legend(fontsize=14)
plt.show()

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 [ ]:

You might also like