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

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


ภาพรวม

ใช้ numpy.trapz() ทำ integration ตาม axis=1 ด้วย composite trapezoidal rule คืนค่า definite integral ตาม 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=1

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

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

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=1))

ผลลัพธ์

Our Array...
[[0 1 2]
 [3 4 5]
 [6 7 8]]
Dimensions of our Array...
2
Datatype of our Array object... int64
Result (trapz)...
[ 2.  8. 14.]

คำอธิบาย: Integrate แต่ละ row (axis=1): row 0: (0+1)/2 + (1+2)/2 = 0.5 + 1.5 = 2, row 1: 8, row 2: 14