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

อนุพันธ์พหุนามและตั้งค่าอนุพันธ์ใน Python

หากต้องการหาอนุพันธ์ของพหุนาม (differentiate a polynomial) ให้ใช้เมธอด polynomial.polyder() ใน Python NumPy

คืนค่าสัมประสิทธิ์พหุนาม c ที่ถูกหาอนุพันธ์ m ครั้งตามแกน axis ที่แต่ละรอบผลลัพธ์จะคูณด้วย scl (scaling factor สำหรับการเปลี่ยนตัวแปรเชิงเส้น) อาร์กิวเมนต์ c เป็นอาร์เรย์สัมประสิทธิ์จากอันดับต่ำไปสูง ตัวอย่าง [1,2,3] แทนพหุนาม 1 + 2x + 3x² ในขณะที่ [[1,2],[1,2]] แทน 1 + 1x + 2y + 2xy หาก axis=0 คือ x และ axis=1 คือ y

เมธอดคืนค่าสัมประสิทธิ์พหุนามของอนุพันธ์

พารามิเตอร์

  • c: อาร์เรย์สัมประสิทธิ์พหุนาม หากหลายมิติ แกนต่างๆ ตรงกับตัวแปรต่างๆ
  • m: จำนวนครั้งของการหาอนุพันธ์ ต้องเป็นค่าบวกหรือศูนย์ (default: 1)
  • scl: ตัวคูณสเกลสำหรับแต่ละครั้งของการหาอนุพันธ์ ผลลัพธ์คูณด้วย scl**m (default: 1)
  • axis: แกนที่หาอนุพันธ์ (default: 0)

ขั้นตอนการใช้งาน

1. นำเข้าไลบรารีที่จำเป็น

import numpy as np
from numpy.polynomial import polynomial as P

2. สร้างอาร์เรย์สัมประสิทธิ์พหุนาม (1 + 2x + 3x² + 4x³)

c = np.array([1, 2, 3, 4])

3. แสดงอาร์เรย์สัมประสิทธิ์

print("Our coefficient Array...\n", c)

4. ตรวจสอบมิติ

print("\nDimensions of our Array...\n", c.ndim)

5. ตรวจสอบชนิดข้อมูล

print("\nDatatype of our Array object...\n", c.dtype)

6. ตรวจสอบรูปร่าง

print("\nShape of our Array object...\n", c.shape)

7. หาอนุพันธ์อันดับ 3 ด้วย polynomial.polyder(c, 3)

print("\nResult...\n", P.polyder(c, 3))

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

import numpy as np
from numpy.polynomial import polynomial as P

# สร้างอาร์เรย์สัมประสิทธิ์พหุนาม: 1 + 2x + 3x² + 4x³
c = np.array([1, 2, 3, 4])

# แสดงอาร์เรย์สัมประสิทธิ์
print("Our coefficient Array...\n", c)

# ตรวจสอบมิติ
print("\nDimensions of our Array...\n", c.ndim)

# ตรวจสอบชนิดข้อมูล
print("\nDatatype of our Array object...\n", c.dtype)

# ตรวจสอบรูปร่าง
print("\nShape of our Array object...\n", c.shape)

# หาอนุพันธ์อันดับ 3
print("\nResult...\n", P.polyder(c, 3))

ผลลัพธ์ที่ได้

Our coefficient Array...
 [1 2 3 4]
Dimensions of our Array...
 1
Datatype of our Array object...
 int64
Shape of our Array object...
 (4,)
Result...
 [24.]

คำอธิบายเพิ่มเติม

พหุนามต้นฉบับ: P(x) = 1 + 2x + 3x² + 4x³

อนุพันธ์อันดับ 1: P'(x) = 2 + 6x + 12x²

อนุพันธ์อันดับ 2: P''(x) = 6 + 24x

อนุพันธ์อันดับ 3: P'''(x) = 24

ผลลัพธ์ [24.] ตรงกับค่าคงที่ 24

สูตรทั่วไป: หาก P(x) = Σ aₙxⁿ แล้ว P⁽ᵐ⁾(x) = Σ aₙ·n!/(n-m)! · xⁿ⁻ᵐ

พารามิเตอร์ scl มีประโยชน์สำหรับการเปลี่ยนตัวแปร เช่น หาอนุพันธ์ตาม u = ax แล้ว scl = a

ฟังก์ชันคู่คือ polyint() สำหรับการหาอินทิกรัล (integration)

อนุพันธ์พหุนามและตั้งค่าอนุพันธ์ใน Python

ใช้ polynomial.polyder() หาอนุพันธ์อันดับ 3 ของพหุนาม 1+2x+3x²+4x³ ได้ผลลัพธ์ [24.] (ค่าคงที่ 24)