Scipy is the scientific computing module of Python providing in-built functions on a lot of well-known Mathematical functions. The scipy.integrate sub-package provides several integration techniques including an ordinary differential equation integrator.
Finding Integration using scipy.integrate
Numerical Integration is the approximate computation of an integral using numerical techniques. Methods for Integrating function given function object:
- quad - General Purpose Integration
- dblquad - General Purpose Double Integration
- nquad - General Purpose n- fold Integration
- fixed_quad - Gaussian quadrature, order n
- quadrature - Gaussian quadrature to tolerance
- romberg - Romberg integration
- trapz - Trapezoidal rule
- cumtrapz - Trapezoidal rule to cumulatively compute integral
- simps - Simpson’s rule
- romb - Romberg integration
- polyint - Analytical polynomial integration (NumPy)
(1) quad :
The function quad is provided to integrate a function of one variable between two points. The points can be +infinite or - infinite to indicate infinite limits.
Example:
Python
from scipy.integrate import quad
def f(x):
return 3.0*x*x + 1.0
I, err = quad(f, 0, 1)
print(I)
print(err)
Output :
2.0
2.220446049250313e-14
(2) dblquad :
This performs Double Integration with 2 arguments.
Example:
Python
from scipy.integrate import dblquad
area = dblquad(lambda x, y: x*y, 0, 0.5,
lambda x: 0, lambda x: 1-2*x)
print(area)
Output :
(0.010416666666666668, 4.101620128472366e-16)
(3) nquad :
Performs integration of n variables
Example:
Python
from scipy.integrate import nquad
def f(x, y, z):
return x*y*z
I = nquad(f, [[0, 1], [0, 5], [0, 5]])
print(I)
Output :
(78.12499999999999, 8.673617379884033e-13)
(4) fixed_quad :
With the help of scipy.integrate.fixed_quad() method, we can get the computation of a definite integral using fixed order gaussian quadrature
Example:
Python
# import scipy.integrate
from scipy import integrate
def func(x): return 3*x**3
# using scipy.integrate.fixed_quad() method
# n is the order of integration
gfg = integrate.fixed_quad(func, 1.0, 2.0, n=2)
print(gfg)
Output:
(11.25, None)
(5) quadrature :
With the help of scipy.integrate.quadrature() method, we can get the computation of definite integral using fixed tolerance gaussian quadrature
Example:
Python
# import scipy.integrate.
from scipy import integrate
def f(x): return 3*x**3
# using scipy.integrate.quadrature() method
g = integrate.quadrature(f, 0.0, 1.0)
print(g)
Output:
(0.7500000000000001, 2.220446049250313e-16)
(6) romberg :
With the help of scipy.integrate.romberg() method, we can get the romberg integration of a callable function from limit a to b
Example:
Python
# import numpy and scipy.integrate
import numpy as np
from scipy import integrate
f = lambda x: 3*(np.pi)*x**3
# using scipy.integrate.romberg()
g = integrate.romberg(f, 1, 2, show = True)
print(g)
Output:
Romberg integration of <function vectorize1.<locals>.vfunc at 0x0000003C1E212790> from [1, 2]
Steps StepSize Results
1 1.000000 42.411501
2 0.500000 37.110063 35.342917
4 0.250000 35.784704 35.342917 35.342917
The final result is 35.34291735288517 after 5 function evaluations.
35.34291735288517
(7) trapz :
numpy.trapz() function integrate along the given axis using the composite trapezoidal rule.
Ref : numpy.trapz() function | Python
Python
# Python program explaining
# numpy.trapz() function
# importing numpy as geek
import numpy as np
b = [2, 4]
a = [6, 8]
f = np.trapz(b, a)
print(f)
Output:
6.0
(8) cumtraz:
With the help of scipy.integrate.cumtrapz() method, we can get the cumulative integrated value of y(x) using composite trapezoidal rule.
Example:
Python
# import numpy and scipy.integrate.cumtrapz
import numpy as np
from scipy import integrate
a = np.arange(0, 5)
b = np.arange(0, 5)
# using scipy.integrate.cumtrapz() method
f = integrate.cumtrapz(b, a)
print(f)
Output:
[0.5 2. 4.5 8. ]
(9) simps:
With the help of scipy.integrate.simps() method, we can get the integration of y(x) using samples along the axis and composite simpson’s rule.
Example:
Python
# import numpy and scipy.integrate
import numpy as np
from scipy import integrate
a = np.arange(0, 5)
b = np.arange(0, 5)
# using scipy.integrate.simps() method
f = integrate.simps(b, a)
print(f)
Output:
8.0
(10) romb:
With the help of scipy.integrate.romb() method, we can get the romberg integration using samples of a function from limit a to b
Example:
Python
# import numpy and scipy.integrate
import numpy as np
from scipy import integrate
x = np.arange(0,5)
# using scipy.integrate.romb() method
f = integrate.romb(x)
print(f)
Output:
8.0
(11) polyint:
numpy.polyint(p, m) : Evaluates the anti – derivative of a polynomial with the specified order.
Ref : numpy.polyint() in Python
Python
# Python code explaining
# numpy.polyint()
# importing libraries
import numpy as np
# Constructing polynomial
p1 = np.poly1d([2,6])
p2 = np.poly1d([4,8])
a = np.polyint(p1, 1)
b = np.polyint(p2, 2)
print ("\n\nUsing polyint")
print ("p1 anti-derivative of order = 2 : \n", a)
print ("p2 anti-derivative of order = 2 : \n", b)
Output :
Using polyint
p1 anti-derivative of order = 2 :
2
1 x + 6 x
p2 anti-derivative of order = 2 :
3 2
0.6667 x + 4 x
Similar Reads
Integration
Integration, in simple terms, is a way to add up small pieces to find the total of something, especially when those pieces are changing or not uniform.Imagine you have a car driving along a road, and its speed changes over time. At some moments, it's going faster; at other moments, it's slower. If y
3 min read
Limit of Integration
Integration is a mathematical concept used to find the accumulation or total of a quantity, often represented by a function, over a specified interval. It involves finding the antiderivative (or indefinite integral) of a function.In simpler terms, integration helps calculate the area under a curve o
7 min read
Integration by Parts
Integration is the calculation of an integral which is used in maths for finding many useful quantities such as areas, volumes, displacement, etc., that occur due to a collection of small data which cannot be measured singularly. Integrals are denoted using " â« ".Integration NotationThe concept of i
6 min read
Integration by Parts
Integration by Parts or Partial Integration, is a technique used in calculus to evaluate the integral of a product of two functions. The formula for partial integration is given by:â« u dv = uv - â« v duWhere u and v are differentiable functions of x. This formula allows us to simplify the integral of
9 min read
Methods of Integration
Integration can be defined as the summation of values when the number of terms tends to infinity. It is used to unite a part of the whole. Integration is just the reverse of differentiation and has various applications in all spheres such as physics, chemistry, space, engineering, etc.Integration of
7 min read
Integral of Sec x
The integral of sec x is â«(sec x).dx = ln| sec x + tan x| + C. Integration of the secant function, denoted as â«(sec x).dx and is given by: â«(sec x).dx = ln| sec(x) + tan(x)| + C. Sec x is one of the fundamental functions of trigonometry and is the reciprocal function of Cos x. Learn how to integrate
6 min read
Integral of Sin x
Integral of sinx is -cos(x)+C, where C is the constant of integration. This result represents the area under the sine curve and reflects the periodic nature of the sine function, which repeats every 2Ï radians. This article delves into the integral of the sine function, offering a detailed explanati
10 min read
Integration in MATLAB
Integration is defined as the process of finding the anti derivative of a function. It is used to calculate area, volume, displacement, and many more. In this article, we will see how to perform integration on expressions in MATLAB. There are two types of Integration: Indefinite integral: Let f(x) b
3 min read
Integration of u/v dx
Integration is a way to add up small pieces to find a total. Imagine you're tracking how fast you're going while driving. If you know your speed at different times, integration helps you figure out how far you've traveled in total.In math, integration is the opposite of finding the rate of change (c
4 min read
Python | Scipy integrate.quad() method
With the help of scipy.integrate.quad() method, we can get the integration of a given function from limit a to b by using scipy.integrate.quad() method. Syntax : scipy.integrate.quad(func, a, b) Return : Return the integration of a polynomial. Example #1 : In this example we can see that by using sc
1 min read