Python

Scipy Dblquad() Function

Today, we are going to learn one of the important functions that are used to get the double integration of the provided function in python language. But before directly jumping on the main topic, let us take a quick look at the basics of the scipy library so that every aspect of the scipy and double integration function will be clear in our mind.

SciPy is a Python package that is open-source and accessible that is used for technical and scientific computation. It consists of a bundle of useful features and numerical methods built using Python’s NumPy module. By giving the user advanced functions and classes for data manipulation and presentation, it significantly enhances the capabilities of an interactive Python session.

Introduction

In python’s scipy package, ordinary differential equations(ODEs) are one of the integration methods that can be solved using the scipy.integrate sub-package in scipy. In scipy, we can perform general-purpose double integration using the scipy.integrate.dblquad technique. To incorporate a function of one variable between two positions, use the quad() function. In Mathematics, double integration means the ability to obtain the equation of the elastic curve that makes this approach a formidable tool for resolving a beam’s deflection and slope anywhere at the point.

Syntax:

Here is the syntax of one of the functions of the python scipy library dblquad() function. To solve the double integration by using the scipy library, we will first understand the writing style and the implementation dblquad() function in python. First, we will write the name of the python library which we are using “scipy” and then we concatenated the integrate() function with it because we want to implement integration on the input value. Then, we concrete the dblquad function which will tell the compiler that we want to perform the double integration on the input values. In the dblquad() function, we will pass the input variable so that we get the desired output.

Parameters:

In the dblquad() function, we will pass the parameters so that we get the double integration. There are some required parameters and some optional parameters in it.

Required Parameter:

These are the parameters that we must have to pass in the dblquad() function brackets.

func: The Python functions to perform the double integration so we must take a minimum of two arguments, y and x, both of which must be variables.

a and b: It is Integration’s upper and lower bounds of x where a<b and the type of the a and b must be float type.

gfun and hfun: gfun represents the lower boundary curve and hfun represents the upper boundary of the curve y that is the function of x and in return we will get the single value in floating points.

Optional Parameters:

These are the optional parameters of the dblquad() function, if we did not pass these parameters it did not affect the output of the function.

arg: It is used to sequence the extra parameters of the function.

epsabs: When integrating the 1-D quadrature internally, the absolute tolerance is directly transferred.

epsrel: When integrating 1-D quadrature internally, the relative tolerance is directly transferred.

Return Value:

In return, we will get the double resultant integration of the input values.

Example 01:

This is the example in which we have to perform the double integration on the given example and store the result in the “I” variable.

Now, let’s start implementing the example of the dblquad() function of scipy. To start writing the code, we first have to use the python compiler so that we can write and execute the code. One more thing is that the compiler must be compatible with both the SciPy and NumPy libraries of python. The Python modules that we want to use in the program must be installed. Here, we are going to implement the SciPy module as well as the NumPy module. So, first, we installed these modules.

After this, we will import the first library which we use to get the value of “pi” which is the numpy library. So, we will import the NumPy library first. Next, we must provide the name of the library we wish to import, in this case, NumPy, as well as its alias, np.

Now, we will import the second module of the program which is the SciPy module. First, we will write the “from” keyword which will tell the python compiler that we only want to import the sub package of the module. Then, we will write the name of the module which we are going to use in the program which is the SciPy module. After this, we will write the keyword “import” informing the compiler that we intend to integrate the library into the program. Then, we will write the library name which is “integrate”. It is the sub-library of the SciPy library.

import numpy as np

from scipy import integrate

func = lambda y, x: x*y**2

xlo = 0

xhi = 2

ylo = 0

yhi = np.pi/2

I , err = integrate.dblquad(func, xlo, xhi, ylo, yhi)

print ("I   = ", I)

print ("error = ", err)

Writing the actual line of code, we wish to use in the application comes after importing the NumPy and SciPy libraries. We have declared a variable named “func” and we will assign the operation to the “func” variable which we want to implement. In the func variable, first, we will define the lambda function which is the built-in function and we pass the variable “y” in it. Then, we will take another variable in the “func” variable which is “x” and we will perform the multiplication operation. So first we will multiply the variable “y” with itself and then the result will multiply with the “x” variable.

Then, we declared two variables “xlo” and “xhi”. The “xlo” is the lower bound value of the d(x) integral and “xhi” is the upper bound value of the d(x) integral. We will again declare two more variables where we store the upper bound and lower bound values of the d(y) integral. In the next line, we will call the double integral function which is integrated.dblquad() function and we will pass the variables in it. Then, we used the print() statement so that we can print the result on the user screen. Let us see the output of the above-implemented example:

Conclusion

In this article, we have learned about one of the functions of the SciPy library which is the dblquad() function by using python language. We have also learned about the writing style and implementation of the function through a simple example with a detailed explanation of every line of code so that the user can easily understand the functionality of the dblquad() function.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content