在本教程中,我們將討論 Python 中的三角餘弦(cos) 函數。我們將討論我們可以用來在 Python 程序中實現 cos()
函數的模塊。我們還將學習在程序中使用 cos()
函數繪製圖形。所以,讓我們從我們可以導入到程序中使用 cos()
函數的模塊開始。
cos()
函數的 Python 模塊
在 Python 中,我們有一個math
模塊,可以用來導入和實現 cos()
函數以及程序中其他重要的數學運算。除了math
模塊,我們還可以使用 Python 的 numpy 模塊來實現程序中的 cos()
函數。我們將一個接一個地學習使用兩個模塊,即math
模塊和 numpy 模塊,分別在程序中實現 cos()
函數。
方法 1:math
模塊中的 cos()函數
Python 的math
模塊附帶了許多重要的數學值和運算,cos()函數就是其中之一。我們可以使用math
模塊的 cos()函數在程序中實現三角 cos 值。 math.cos() 函數返回我們在函數內部給出的自變量的三角餘弦值,即餘弦值。我們在函數中作為參數給出的值應該是弧度。
語法-
下面是在 Python 程序中使用 math.cos()函數的語法:
math.cos(a)
參數:這裡參數 a =弧度值。
返回值:**math . cos()**函數返回我們在函數中給定的以弧度表示的參數‘a’的餘弦值。
讓我們通過下面的示例程序來了解math
模塊的 cos() 函數在 Python 中的使用:
示例-
# 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()函數中給出‘a’作為以下參數類型:
- 我們可以在函數中給出一個以弧度表示的單值參數。
- 我們還可以在函數中給出一個包含多個弧度值的數組作為參數。
返回類型:numpy . cos()函數將返回給定數字的餘弦值。
讓我們通過下面的示例程序來了解 numpy 模塊的 cos()函數在 Python 中的使用:
示例-
# 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\. ]
示例- 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]
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/242406.html