Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

SciPy – Integration

  • Last Updated : 26 Mar, 2021

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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

  • 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:

Python3




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:

Python3




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:

Python3




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:

Python3




# 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:

Python3






# 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:

Python3




# 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

Python3




# 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:

Python3




# 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:

Python3






# 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:

Python3




# 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

Python3




# 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :