在本教程中,我们将讨论Python中的三角余弦(cos)函数。我们将讨论可以在Python程序中使用的模块,以实现cos函数。我们还将学习如何在程序中使用cos函数绘制图形。让我们首先看看可以导入程序以使用cos函数的模块。

Python中的cos函数模块

在Python中,我们有一个可以用来导入和实现cos函数以及其他重要数学运算的math模块。除了math模块,我们还可以使用Python的numpy模块来实现程序中的cos函数。我们将依次学习两个模块的使用,即math模块和numpy模块,来分别实现程序中的cos函数。

方法1:math模块中的cos()函数

Python的math模块包含许多重要的数学值和操作,其中cos()函数就是其中之一。我们可以使用math模块的cos()函数来在程序中实现三角余弦值。math.cos()函数返回我们在函数中给定的参数的三角余弦值,即余弦值的度数。而且,我们在函数中给定的参数的值应该以弧度为单位。

语法 -

以下是在Python程序中使用math.cos()函数的语法:

math.cos(a)  

参数: 在这里,参数a = 弧度值

返回值: math.cos()函数返回函数中给定的弧度值'a'的余弦值。

让我们通过以下示例程序来了解Python中math模块的cos()函数的使用:

示例 -

# Import math module  
import math  
# Define an input radian value  
x = math.pi / 12  
# Printing cosine value for respective input value  
print ("The cosine value of pi / 12 value as given is : ", end ="")    
  
print (math.cos(x))  

输出:

The cosine value of pi / 12 value as given is: 0.9659258262890683

方法2:Numpy模块中的cos()函数

除了math模块,我们还可以使用numpy模块来实现程序中的三角余弦值。为此,我们可以使用numpy模块中的cos()函数,该函数会在输出中返回数学余弦值。与math.cos()函数一样,使用numpy模块的cos()函数时,我们必须在函数中以弧度值为参数。

以下是在Python程序中使用numpy.cos()函数的语法:

numpy.cos(a)  

参数: 我们可以在numpy.cos()函数中使用以下参数类型:

  • 我们可以在函数中给出以弧度为单位的单个值参数。
  • 我们还可以将包含多个弧度值的数组作为参数传递给函数。

返回类型: numpy.cos()函数将返回给定数字的余弦值。

让我们通过以下示例程序来了解Python中numpy模块的cos()函数的使用:

示例 -

# importing numpy module as jtp in program  
import numpy as jtp  
# defining multiple input values in a single array  
ValArray = [0, jtp.pi / 4, jtp.pi / 7, jtp.pi/9, jtp.pi/12, jtp.pi/5]  
# printing input array in output  
print ("Values given in the input array: \n", ValArray)  
# using cos() function to get cosine values  
CosArray = jtp.cos(ValArray)  
# printing cos values in output  
print ("\nRespective Cosine values for input array values: \n", CosArray)  

输出:

Values given in the input array: 
 [0, 0.7853981633974483, 0.4487989505128276, 0.3490658503988659, 0.2617993877991494, 0.6283185307179586]

Respective Cosine values for input array values: 
 [1.         0.70710678 0.90096887 0.93969262 0.96592583 0.80901699]

绘制余弦值的图形表示

到目前为止,我们已经学习了在Python程序中使用numpy和math模块的cos()函数。现在,我们将同时使用numpy和math模块以及cos()函数来绘制余弦值的图形表示。我们可以通过以下两种方式进行图形表示:

  • 直接导入和实现cos()函数以及numpy和math模块
  • 使用numpy和math模块对cos()函数进行迭代

让我们通过在Python程序中使用这两种方法并在输出中绘制图形来了解两种方法的实现。

示例 - 1:直接导入和实现cos()函数以及numpy和math模块

# importing numpy module as jtp  
import numpy as jtp  
# importing matplotlib module as mlt  
import matplotlib.pyplot as mlt  
  
# Defining an array containing radian values  
RadValArray = jtp.linspace(-(2*jtp.pi), 2*jtp.pi, 20)  
# cosine values for respective array value  
CosValArray = jtp.cos(RadValArray)  
  
# printing values in output  
print("Radian values in the array: ", RadValArray)  
print("\nRespective cos values of array: ", CosValArray)  
  
# using plot() function with variables  
mlt.plot(RadValArray, CosValArray, color = 'blue', marker = "*")  
mlt.title("Graphical representation of cos function")  
mlt.xlabel("X-axis")  
mlt.ylabel("Y-axis")  
  
# plotting graph in output  
mlt.show()  

输出:

Radian values in the array:  [-6.28318531 -5.62179738 -4.96040945 -4.29902153 -3.6376336  -2.97624567
 -2.31485774 -1.65346982 -0.99208189 -0.33069396  0.33069396  0.99208189
  1.65346982  2.31485774  2.97624567  3.6376336   4.29902153  4.96040945
  5.62179738  6.28318531]

Respective cos values of array:  [ 1.          0.78914051  0.24548549 -0.40169542 -0.87947375 -0.9863613
 -0.67728157 -0.08257935  0.54694816  0.94581724  0.94581724  0.54694816
 -0.08257935 -0.67728157 -0.9863613  -0.87947375 -0.40169542  0.24548549
  0.78914051  1.        ]

268-1.png

示例 - 2:使用numpy和math模块迭代cos()函数

# importing math module  
import math  
# importing numpy module as jtp  
import numpy as jtp  
# importing matplotlib module as mlt  
import matplotlib.pyplot as mlt  
  
# Defining an array containing radian values  
RadValArray = jtp.linspace(-(2*jtp.pi), 2*jtp.pi, 20)  
# Empty array for cosine values  
CosValArray = []  
  
#Iterating over the cos values array  
for j in range(len(RadValArray)):   
    CosValArray.append(math.cos(RadValArray[j]))   
    j += 1  
  
# printing respective values in output  
print("Radian values in the array: ", RadValArray)  
print("\nRespective cos values of array: ", CosValArray)  
  
# using plot() function with variables  
mlt.plot(RadValArray, CosValArray, color = 'orange', marker = "+")  
mlt.title("Graphical representation of cos function")  
mlt.xlabel("X-axis")  
mlt.ylabel("Y-axis")  
  
# plotting graph in output  
mlt.show()  

输出:

Radian values in the array:  [-6.28318531 -5.62179738 -4.96040945 -4.29902153 -3.6376336  -2.97624567
 -2.31485774 -1.65346982 -0.99208189 -0.33069396  0.33069396  0.99208189
  1.65346982  2.31485774  2.97624567  3.6376336   4.29902153  4.96040945
  5.62179738  6.28318531]

Respective cos values of array:  [1.0, 0.7891405093963934, 0.2454854871407988, -0.40169542465296987, -0.8794737512064891, -0.9863613034027223, -0.6772815716257412, -0.08257934547233249, 0.5469481581224268, 0.9458172417006346, 0.9458172417006346, 0.5469481581224268, -0.0825793454723316, -0.6772815716257405, -0.9863613034027223, -0.8794737512064893, -0.40169542465296987, 0.2454854871407988, 0.7891405093963934, 1.0]

268-2.png

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter学习指南, Tkinter进阶教程, Tkinter编程, Tkinter进阶Tkinter指南