SciPy - integrate.fixed_quad() Method



The SciPy integrate.fixed_quad() method operates the fixed order of gaussian quadrature for numerical integration. The Gaussian quadrature is defined by the set of point within an interval.

Syntax

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

fixed_quad(custom_func, int_val1, int_val2)

Parameters

This function accepts the following parameters −

  • custom_func: This parameter is used to set for calculation of specific integration over interval.
  • int_val1: This parameter define the lower limit of integration.
  • int_val2: This parameter define the upper limit of integration.

Return value

The method returns mixed of different data values() into a tuple form.

Example 1

Following is the basic example that illustrate the usage of SciPy integrate.fixed_quad() method.

import scipy.integrate as integrate

def simple_polynomial(x):
    return x**2

res = integrate.fixed_quad(simple_polynomial, 0, 1)
print(f"Integral of x^2 from 0 to 1: {res}")

Output

The above code produces the following output −

Integral of x^2 from 0 to 1: (0.33333333333333326, None)

Example 2

This program demonstrate the exponential function integration which can be used over the interval [0, 2].

import scipy.integrate as integrate
import numpy as np

def exp_fun(x):
    return np.exp(x)

res = integrate.fixed_quad(exp_fun, 0, 2)
print(f"Integral of e^x from 0 to 2: {res}")

Output

The above code produces the following output −

Integral of e^x from 0 to 2: (6.389056096688674, None)

Example 3

Below the program shows sine function integration which range interval is [0, pi].

import scipy.integrate as integrate
import numpy as np

def sine_function(x):
    return np.sin(x)

res = integrate.fixed_quad(sine_function, 0, np.pi)
print(f"Integral of sin(x) from 0 to pi: {res}")

Output

The above code produces the following output −

Integral of sin(x) from 0 to pi: (2.0000001102844727, None)
scipy_reference.htm
Advertisements