Python 中的cos()函數

在本教程中,我們將討論 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-tw/n/242406.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:49
下一篇 2024-12-12 12:49

相關推薦

  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 蝴蝶優化演算法Python版

    蝴蝶優化演算法是一種基於仿生學的優化演算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化演算法Python版…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29

發表回復

登錄後才能評論