SciPy - integrate.quadrature() Method



The SciPy integrate.quadrature() method is used to calculate the numerical integration. In data analysis, we use this method to perform the various tasks such as calculating area under curve, evaluate the definite integral and solving differential equation.

Syntax

Following is the syntax of the SciPy integrate.quadrature() method −

quadrature(func, arg_1, arg_2)

Parameters

This function accepts the following parameters −

  • func: This defines the custom function to integrate.
  • arg_1: This determine the lower limit of integration interval.
  • arg_2: This determine the upper limit of integration interval.

Return value

This method return the float value as result.

Example 1

Following is the SciPy integrate.quadrature() method which illutrate the simple polynomial function which is f(x) = x2 over the interval of [0, 1].

from scipy import integrate

# define the function to integrate
def func(x):
   return x**2

# perform the integration
res, err = integrate.quadrature(func, 0, 1)

# display the result
print("Integral:", res)
print("Error estimate:", err)

Output

The above code produces the following output −

Integral: 0.33333333333333337
Error estimate: 0.0

Example 2

Here, we demonstrate the trigonometric function by defining sin() function and set the inteval between 0 to pi.

In mathematics, we represent the above sentence as f(x) = sin(x) over the interval of [0, ]

import numpy as np
import scipy.integrate as sp

# define the function to integrate with trigonometric function
def fun(x):
    return np.sin(x)

# perform the integration
res, err = sp.quadrature(fun, 0, np.pi)
print("Integral:", res)
print("Error estimate:", err)

Output

The above code produces the following output −

Integral: 2.0000000000017897
Error estimate: 5.245188727798222e-10

Example 3

The program demonstrate the integrate of function with extra parameter which is f(x) = a * x + b over the interval of [0, 2] where a = 3 and b = 4.

import numpy as np
import scipy.integrate as sp
def fun(x, a, b):
    return a * x + b

res, err = sp.quadrature(fun, 0, 2, args=(4, 5))
print("Integral:", res)
print("Error estimate:", err)

Output

The above code produces the following output −

Integral: 18.0
Error estimate: 0.0
scipy_reference.htm
Advertisements