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

หาค่า cosine ของมุมใน Python


ภาพรวม

ใช้ numpy.cos() หา cosine ของมุม (เรเดียน) 2π = 360° คืนค่า scalar ถ้า input เป็น scalar

พารามิเตอร์

  • x: มุมในเรเดียน
  • out (optional): ndarray เก็บผลลัพธ์
  • where (optional): condition broadcast

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

1. นำเข้า NumPy

import numpy as np

2. คำนวณ cosine มุมต่างๆ (เรเดียน)

print("\nResult...", np.cos(np.pi/2.))   # 90°
print("\nResult...", np.cos(np.pi/3.))   # 60°
print("\nResult...", np.cos(np.pi/4.))   # 45°
print("\nResult...", np.cos(np.pi/6.))   # 30°
print("\nResult...", np.cos(0))          # 0°

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

import numpy as np
print("Get the Trigonometric cosine...")
print("\nResult...", np.cos(np.pi/2.))
print("\nResult...", np.cos(np.pi/3.))
print("\nResult...", np.cos(np.pi/4.))
print("\nResult...", np.cos(np.pi/6.))
print("\nResult...", np.cos(0))

ผลลัพธ์

Get the Trigonometric cosine...
Result... 6.123233995736766e-17
Result... 0.5000000000000001
Result... 0.7071067811865476
Result... 0.8660254037844387
Result... 1.0

คำอธิบาย: cos(90°)≈0, cos(60°)=0.5, cos(45°)=√2/2≈0.707, cos(30°)=√3/2≈0.866, cos(0°)=1