numpy.trapz()函数| Python
numpy.trapz()
函数使用复合梯形规则沿给定轴积分。
Syntax : numpy.trapz(y, x = None, dx = 1.0, axis = -1)
Parameters :
y : [array_like] Input array to integrate.
x : [array_like, optional] The sample points corresponding to the y values. If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.
dx : [scalar, optional] The spacing between sample points when x is None. The default is 1.
axis : [int, optional] The axis along which to integrate.
Return :
trapz: [float] Definite integral as approximated by trapezoidal rule.
代码#1:
# Python program explaining
# numpy.trapz() function
# importing numpy as geek
import numpy as geek
y = [1, 2, 3, 4]
gfg = geek.trapz( y )
print (gfg)
输出 :
7.5
代码#2:
# Python program explaining
# numpy.trapz() function
# importing numpy as geek
import numpy as geek
y = [1, 2, 3, 4]
x = [5, 6, 7, 8]
gfg = geek.trapz(y, x)
print (gfg)
输出 :
7.5
代码#3:
# Python program explaining
# numpy.trapz() function
# importing numpy as geek
import numpy as geek
y = [1, 2, 3, 4]
gfg = geek.trapz(y, dx = 2)
print (gfg)
输出 :
15.0