使用Python中的复合梯形规则沿轴0积分
要使用复合梯形规则沿给定轴积分,请使用numpy.trapz()方法。如果提供x,则积分将沿其元素依次进行——它们不会被排序。该方法返回沿单个轴由梯形规则近似的'y'=n维数组的定积分。如果'y'是一维数组,则结果为浮点数。如果'n'大于1,则结果为'n-1'维数组。
第一个参数y是要积分的输入数组。第二个参数x是对应于y值的样本点。如果x为None,则假定样本点均匀分布,相隔dx。默认值为None。第三个参数dx是x为None时样本点之间的间距。默认值为1。第四个参数axis是要积分的轴。
步骤
首先,导入所需的库:
import numpy as np
使用arange()方法创建一个numpy数组。我们添加了整型元素:
arr = np.arange(9).reshape(3, 3)
显示数组:
print("Our Array...\n",arr)检查维度:
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型:
print("\nDatatype of our Array object...\n",arr.dtype)要使用复合梯形规则沿给定轴积分,请使用numpy.trapz()方法:
print("\nResult (trapz)...\n",np.trapz(arr, axis = 0))
示例
import numpy as np
# Creating a numpy array using the arange() method
# We have added elements of int type
arr = np.arange(9).reshape(3, 3)
# Display the array
print("Our Array...\n",arr)
# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)
# To integrate along the given axis using the composite trapezoidal rule, use the numpy.trapz() method
print("\nResult (trapz)...\n",np.trapz(arr, axis = 0))输出
Our Array... [[0 1 2] [3 4 5] [6 7 8]] Dimensions of our Array... 2 Datatype of our Array object... int64 Result (trapz)... [ 6. 8. 10.]
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP