SciPy - integrate.trapezoid() Method



The SciPy integrate.trapezoid() method is used to find the approximate value of integral function using trapezoid rule. There are two rules of trapezoid −

  • The function value at equal space points.
  • The width of the interval.

Syntax

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

trapezoid(y, x)

Parameters

This method accepts two parameter −

  • y: This parameter is used to set the value of function to be integrate.
  • x: This also define the same.

Return value

This method returns float values.

Example 1

Following is the basic example that shows the usage of SciPy integrate.trapezoid() method.

import numpy as np
from scipy import integrate

x = np.linspace(0, 10, 100)
y = x**2
integral = integrate.trapezoid(y, x)
print("The resultant value is ", integral)

Output

The above code produces the following result −

The resultant value is  333.35033840084344

Example 2

This program defines two arrays to the respective variable and pass these variable to trapezoid() to calculate the integral result.

import numpy as np
from scipy.integrate import trapezoid
x = np.array([0, 1, 2, 5, 6])
y = np.array([0, 1, 4, 25, 36])
integral = trapezoid(y, x)
print("The resultant value is ", integral)

Output

The above code produces the following result −

The resultant value is  77.0

Example 3

Below the program calculate the multiple integration along with an axis.

import numpy as np
from scipy.integrate import trapezoid
y = np.array([[0, 11, 64, 93, 16], [0, 17, 87, 27, 64]])
x = np.array([0, 1, 24, 34, 4])
integral = trapezoid(y, x, axis=1)
print("The resultant value is ", integral)

Output

The above code produces the following result −

The resultant value is  [ 18.  409.5]
scipy_reference.htm
Advertisements