Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ทำการ integral ตาม axis 0 ด้วยกฎ trapezoidal แบบ composite ใน Python


ภาพรวม

ใช้ numpy.trapz() ทำ integration ตาม axis=0 ด้วย composite trapezoidal rule คืนค่า definite integral ของอาร์เรย์ n-D ตาม axis เดียว

ขั้นตอนการทำงาน

1. นำเข้า NumPy

import numpy as np

2. สร้างอาร์เรย์ 3x3

arr = np.arange(9).reshape(3, 3)

3. แสดงข้อมูลอาร์เรย์

print("Our Array...\n", arr)
print("\nDimensions of our Array...\n", arr.ndim)
print("\nDatatype of our Array object...\n", arr.dtype)

4. Integrate ตาม axis=0

print("\nResult (trapz)...\n", np.trapz(arr, axis=0))

ตัวอย่างโค้ดเต็ม

import numpy as np
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)
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.]

คำอธิบาย: Integrate แต่ละ column (axis=0) ผลลัพธ์เป็น 1-D array ขนาด 3 ค่า