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

หาอนุพันธ์ของพหุนาม ตั้งค่าจำนวนอนุพันธ์และคูณสเกลแต่ละครั้งใน Python

บทนำ

ในการหาอนุพันธ์ของพหุนาม สามารถใช้เมธอด polynomial.polyder() ใน NumPy ของ Python เมธอดนี้คืนสัมประสิทธิ์พหุนามที่ถูกหาอนุพันธ์ m ครั้งตามแกนที่กำหนด พร้อมคูณสเกล (scl) ทุกครั้ง

พารามิเตอร์สำคัญ

  • c: อาร์เรย์สัมประสิทธิ์พหุนาม หากเป็นหลายมิติ แกนต่างๆ สอดคล้องกับตัวแปรต่างๆ
  • m: จำนวนอนุพันธ์ ต้องไม่ติดลบ (ค่าเริ่มต้น: 1)
  • scl: ตัวคูณสเกลแต่ละครั้ง ผลลัพธ์สุดท้ายคูณด้วย scl^m (ค่าเริ่มต้น: 1)
  • axis: แกนที่หาอนุพันธ์ (ค่าเริ่มต้น: 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)
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)

4. หาอนุพันธ์อันดับ 2 พร้อมสเกล -1

print("\nResult...\n", P.polyder(c, 2, scl=-1))

ตัวอย่างโค้ดสมบูรณ์

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)

# หาอนุพันธ์อันดับ 2 (m=2) พร้อมสเกล -1
print("\nResult...\n", P.polyder(c, 2, scl=-1))

ผลลัพธ์

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...
 [ 6. 24.]

สรุป

พหุนามต้นฉบับ 1 + 2x + 3x² + 4x³ การหาอนุพันธ์อันดับ 2 ได้ 6 + 24x การคูณสเกล -1 ทุกครั้ง (scl=-1, m=2) ให้ตัวคูณ (-1)² = 1 ดังนั้นผลลัพธ์จึงไม่เปลี่ยนเครื่องหมาย