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

ผสานรวมโดยใช้กฎสี่เหลี่ยมคางหมูคอมโพสิตใน Python


ในการรวมเข้ากับแกนที่กำหนดโดยใช้กฎสี่เหลี่ยมคางหมูแบบผสม ให้ใช้เมธอด numpy.trapz() หากระบุ x การผสานจะเกิดขึ้นตามลำดับองค์ประกอบ - จะไม่ถูกจัดเรียง เมธอดจะคืนค่าอินทิกรัลที่แน่นอนของอาร์เรย์ 'y' =n มิติตามแกนเดียวโดยประมาณโดยกฎสี่เหลี่ยมคางหมู หาก 'y' เป็นอาร์เรย์ 1 มิติ ผลลัพธ์จะเป็นค่าทศนิยม หาก 'n' มากกว่า 1 ผลลัพธ์จะเป็นอาร์เรย์มิติ 'n-1'

พารามิเตอร์ที่ 1 y คืออาร์เรย์อินพุตที่จะรวมเข้าด้วยกัน พารามิเตอร์ตัวที่ 2 x คือจุดตัวอย่างที่สัมพันธ์กับค่า y ถ้า x เป็น none จะถือว่าจุดตัวอย่างเป็น dxapart ที่มีระยะห่างเท่าๆ กัน ค่าเริ่มต้นคือไม่มี พารามิเตอร์ตัวที่ 3 dx คือระยะห่างระหว่างจุดตัวอย่างเมื่อ x ไม่มี ค่าเริ่มต้นคือ 1 พารามิเตอร์ที่ 4 แกนคือแกนที่จะรวมเข้าด้วยกัน

ขั้นตอน

ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import numpy as np

การสร้างอาร์เรย์ numpy โดยใช้เมธอด array() เราได้เพิ่มองค์ประกอบประเภท int -

arr = np.array([20, 35, 57, 70, 85, 120])

แสดงอาร์เรย์ -

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

ตัวอย่าง

import numpy as np

# Creating a numpy array using the array() method
# We have added elements of int type
arr = np.array([20, 35, 57, 70, 85, 120])

# 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))

ผลลัพธ์

Our Array...
[ 20 35 57 70 85 120]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Result (trapz)...
317.0