Integration is one of the most fundamental mathematical operations that we perform. We use it to find the area under a curve, to find the volume of a solid, and to solve differential equations.

In Python, scipy.integrate provides an interface for performing numerical integration using several different methods. It also provides an ordinary differential equation solver that can be used to solve problems in ODE and PDE theory.

What Is SciPy Integrate?

SciPy Integrate is a Python library for numerical integration. It provides several functions for integrating functions, transforming derivatives, and solving differential equations. 

It also includes some algorithms for finding roots of univariate polynomials, as well as some basic linear algebra operations such as matrix multiplication and inversion.

Learn From The Best in The Data Science Business!

Caltech Data Science BootcampExplore Course
Learn From The Best in The Data Science Business!

SciPy Integration Functions

Quad

The function quad is provided to integrate a function of one variable between two points. The points can be infinite or negative infinity, indicating that the integrand has limits of +infinite and -infinite.

Example:

from scipy.integrate import quad

def f(x):

return 5.0*x*x + 1.0

I, err = quad(f, 0, 1)

print(I)

print(err)

dblquad 

dblquad integrates functions with two arguments.

Example:

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)

Nquad

The nquad function performs the integration of n variables.

Example:

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)

Fixed_quad

The scipy.integrate.fixed_quad() method provides the computation of a definite integral using fixed-order Gaussian quadrature.

Example:

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)

Quadrature

The scipy.integrate.quadrature() method can be used to compute the definite integral using fixed-tolerance Gaussian quadrature

Example:

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

romberg 

Using the scipy.integrate.romberg() method, we can integrate a callable function from a to b.

Example:

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)

trapz 

The numpy.trapz() function uses the composite trapezoidal rule to integrate along a given axis.

Example:

import numpy as np 

b = [2, 4] 

a = [6, 8] 

f = np.trapz(b, a) 

print(f) 

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

cumtrapz 

The scipy.integrate.cumtrapz() method can be used to find the cumulative integrated value for \( y(x) \) using the composite trapezoidal rule.

Example:

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)

simps 

By using the scipy.integrate.simps() method, we can use Simpson’s rule to get an estimate of the integral of y(x).

Example:

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)

romb 

The scipy.integrate.romb() method can be used to get a Romberg integration of a function from a to b, using samples of the function.

Example:

import numpy as np 

from scipy import integrate 

x = np.arange(0,5) 

# using scipy.integrate.romb() method 

f = integrate.romb(x) 

print(f) 

polyint 

The numpy.polyint() function evaluates the anti-derivative of a polynomial with the specified order.

Example:

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) 

Single Integrals

The quad() function is a mathematical tool that makes numerical integration possible. It allows us to approximate the area under a curve using discrete points on the curve.

The syntax for the quad() function is scipy.integrate.quad(f,a,b), where:

f - Function name to be integrate

a-It is a lower limit.

b- It is a upper limit.

Example:

import scipy.integrate

from numpy import exp

f= lambda x:exp(-x**2)

i = scipy.integrate.quad(f, 0, 1)

print i

Free Course: Python Libraries for Data Science

Learn the Basics of Python LibrariesEnroll Now
Free Course: Python Libraries for Data Science

Multiple Integrals

The double integral problem can be solved using the scipy.integrate.dblquad(func,a,b,gfun,hfun).

The first argument func is the name of the function to be integrated, a and b are the lower and upper limit of the x variable, while gfun and hfun are names of functions that define the lower and upper limit of the y variable.

Benefits of SciPy Integrate

SciPy Integrate is a powerful tool that can be used to perform calculations, make plots and analyze data. It has many different applications in science, engineering, mathematics and other fields.

1. It’s Open Source

SciPy Integrate is free and open source software, which means it's easy to use and highly customizable. You can use it for free with no restrictions or limitations on how long you can use it or where you can use it.

2. Its Functionality Is Extensive

SciPy Integrate includes many different functions for performing calculations and making plots. It also includes tools to help you analyze data sets and graphs so that you can better understand what the data is telling you about your experiment or project. 

This functionality makes SciPy Integrate a powerful tool for scientists who want to do more than just calculate numbers by hand; they want to analyze their results as well!

Enroll in the Professional Certificate Program in Data Science to learn over a dozen of data science tools and skills, and get exposure to masterclasses by Purdue faculty and IBM experts, exclusive hackathons, Ask Me Anything sessions by IBM.

Conclusion

The Professional Certificate Program in Data Science is designed for professionals who want to learn about data science. The program will provide you with the skills you need to make informed decisions about your company's use of data.

The program is designed to equip you with the skills required to succeed in data science roles across industries. You will learn how to analyze data using advanced machine-learning techniques and build predictive models that can be used to solve real-world problems.

FAQs

1. What is SciPy integration?

Scipy integration is a function that can be used to integrate any function of one or more variables. The purpose of the integration is to find the area under the curve of a given function. It can be used in many different applications, including math, physics, and engineering.

2. What are simps from scipy integrate?

Simps are the symbolic form of the numerical integration for scipy. They are based on Simpson's rule, which is a simple and fairly accurate way to calculate an approximation of the area under a curve.

3. How does integrating quad work?

Integrating quad works by combining the most important elements of quadratic equations, namely solving for the x-intercept and writing in the form of ax^2 + bx + c = 0. Once you can do this, you can find the solutions to any quadratic equation.

4. How does SciPy integrate with Python?

SciPy integrates very well with Python. It offers a lot of functionality for scientific computing in Python, and it's quite easy to use.

SciPy is a collection of open source software for mathematics, science, and engineering. It includes modules for linear algebra, optimization, integration, statistics and more. SciPy also provides many other useful features that make it easy to program with Python.

5. How do I install SciPy integration?

To install SciPy integration, you must first install Python. You can download it here: https://www.python.org/downloads/

Once you have installed Python, you can use pip to install the package:

pip3 install scipy

6. Should I use NumPy or SciPy?

If you're looking to do data analysis and visualization, NumPy is the way to go. NumPy is an extension of Python that allows for fast array manipulation, which is useful when you're working with large datasets. It also provides a lot of built-in functions, including linear algebra and Fourier transforms.

If you're looking for more advanced mathematics, though, SciPy is a great choice. SciPy provides tools for solving differential equations and performing numerical integration, as well as for computing integrals and integrating functions.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors