📜  Python| math.cos()函数

📅  最后修改于: 2022-05-13 01:55:27.600000             🧑  作者: Mango

Python| math.cos()函数

在Python中,数学模块包含许多数学运算,可以使用该模块轻松执行。 math.cos()函数返回作为参数传递的值的余弦。此函数中传递的值应以弧度为单位。

代码#1:

# Python code to demonstrate the working of cos()
     
# importing "math" for mathematical operations 
import math 
    
a = math.pi / 6
     
# returning the value of cosine of pi / 6 
print ("The value of cosine of pi / 6 is : ", end ="") 
print (math.cos(a)) 
输出:
The value of cosine of pi/6 is : 0.8660254037844387


代码#2:

# Python program showing 
# Graphical representation of 
# cos() function 
import math
import numpy as np
import matplotlib.pyplot as plt 
  
in_array = np.linspace(-(2 * np.pi), 2 * np.pi, 20)
  
out_array = []
  
for i in range(len(in_array)):
    out_array.append(math.cos(in_array[i]))
    i += 1
  
   
print("in_array : ", in_array) 
print("\nout_array : ", out_array) 
  
# red for numpy.sin() 
plt.plot(in_array, out_array, color = 'red', marker = "o") 
plt.title("math.cos()") 
plt.xlabel("X") 
plt.ylabel("Y") 
plt.show() 
输出: