python math_Python math.cos()用法及代码示例

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

用法: math.cos(x)

参数:

x:要传递给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()

输出:

in_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]

out_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]

73753c3b23fb647742e0f6ce3d4b4978.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值