SciPy - integrate.romberg() Method



The SciPy integrate.romberg() method is used to calculate the numerical integration. The method returns the function integral over the interval(a,b).

Syntax

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

romberg(func, a, b)

Parameters

This function accepts the following parameter −

  • func: This parameter is used for the integral performance.
  • a: This parameter represent the lower limit of integration.
  • b: This parameter represent the upper limit of integration.

Return value

This method returns the float value as result.

Example 1

Following is the basic demonstration of SciPy integrate.romberg() method that shows the intevals 0 and 1.

Here, we take the function f(x) = x2 for calculating the integration.

from scipy import integrate

def polynomial(x):
    return x**2

res = integrate.romberg(polynomial, 0, 1)
print("The result of integrating x^2 from 0 to 1:", res)

Output

The above code produces the following output −

The result of integrating x^2 from 0 to 1: 0.3333333333333333

Example 2

Here, we are using cos() function to perform the task of integration over the interval 0 and pi/2.

import numpy as np
import scipy.integrate as sp
def cosine(x):
    return np.cos(x)

res = sp.romberg(cosine, 0, np.pi/2)
print("The result of integrating cos(x) from 0 to /2:", res)

Output

The above code produces the following output −

The result of integrating cos(x) from 0 to /2: 0.63212055882857
scipy_reference.htm
Advertisements