Ⅰ python中计算三角函数之cos()方法的使用简介
这篇文章主要介绍了Python中计算三角函数之cos()方法的使用简介,是Python入门的基础知识,需要的朋友可以参考下
cos()方法返回x弧度的余弦值。
语法
以下是cos()方法的语法:
cos(x)
注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math的静态对象来调用这个函数。
参数
x
--
这必须是一个数值
返回值
此方法返回-1
到
1之间的数值,它表示角度的余弦值
例子
下面的例子展示cos()方法的使用
?
1
2
3
4
5
6
7
8#!/usr/bin/python
import
math
print
"cos(3)
:
",
math.cos(3)
print
"cos(-3)
:
",
math.cos(-3)
print
"cos(0)
:
",
math.cos(0)
print
"cos(math.pi)
:
",
math.cos(math.pi)
print
"cos(2*math.pi)
:
",
math.cos(2*math.pi)
当我们运行上面的程序,它会产生以下结果:
?
1
2
3
4
5cos(3)
:
-0.9899924966
cos(-3)
:
-0.9899924966
cos(0)
:
1.0
cos(math.pi)
:
-1.0
cos(2*math.pi)
:
1.0
Ⅱ 如何用python表示三角函数
Python编码下面的三角函数包括以下种类:acos(x)//返回x的反余弦弧度值。asin(x)//返回x的反正弦弧度值。atan(x)//返回x的反正切弧度值。atan2(y,x)//返回给定的X及Y坐标值的反正切值。cos(x)//返回x的弧度的余弦值。hypot(x,y
描述
sin()返回的x弧度的正弦值。
语法
以下是sin()方法的语法:
importmath
math.sin(x)
注意:sin()是不能直接访问的,需要导入math模块,然后通过math静态对象调用该方法。
参数
x--一个数值。
返回值
返回的x弧度的正弦值,数值在-1到1之间。
实例
以下展示了使用sin()方法的实例:
#!/usr/bin/python
import math
print "sin(3) : ", math.sin(3)
print "sin(-3) : ", math.sin(-3)
print "sin(0) : ", math.sin(0)
print "sin(math.pi) : ", math.sin(math.pi)
print "sin(math.pi/2) : ", math.sin(math.pi/2)
以上实例运行后输出结果为:
sin(3) : 0.14112000806
sin(-3) : -0.14112000806
sin(0) : 0.0
sin(math.pi) : 1.22460635382e-16
sin(math.pi/2) : 1
总结
以上就是本文关于Python入门之三角函数sin()函数实例详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:python正则表达式re之compile函数解析、Python中enumerate函数代码解析、简单了解Python中的几种函数等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!
Ⅲ python三角函数怎么输入度数
python三角函数输入度数:acos(x) //返回x的反余弦弧度值。 asin(x) //返回x的反正弦弧度值。 atan(x) //返回x的反正切弧度值。
def read_cell(x,y):if cell_type(x,y)==4: #4是真值类型(bool),return "TRUE" if cell_value(x,y)==1 else "FALSE"。
elif cell_type(x,y)==2: #2是数字类型(number),return str(cell_value(x,y)),else:#其他类型不再一一列举,用到时再做增加。
python三角函数规范的代码:
Python采用强制缩进的方式使得代码具有较好可读性。而Python语言写的程序不需要编译成二进制代码。Python的作者设计限制性很强的语法,使得不好的编程习惯(例如if语句的下一行不向右缩进)都不能通过编译。其中很重要的一项就是Python的缩进规则。
一个和其他大多数语言(如C)的区别就是,一个模块的界限,完全是由每行的首字符在这一行的位置来决定(而C语言是用一对大括号{}来明确的定出模块的边界,与字符的位置毫无关系)。通过强制程序员们缩进,Python确实使得程序更加清晰和美观。
Ⅳ 如何用python表示三角函数如题,能不能
Python编码下面的三角函数包括以下种类:
12345678910
acos(x) //返回x的反余弦弧度值。 asin(x) //返回x的反正弦弧度值。 atan(x) //返回x的反正切弧度值。 atan2(y, x) //返回给定的 X 及 Y 坐标值的反正切值。 cos(x) //返回x的弧度的余弦值。 hypot(x, y) //返回欧几里德范数 sqrt(x*x + y*y)。 sin(x) //返回的x弧度的正弦值。 tan(x) //返回x弧度的正切值。 degrees(x) //将弧度转换为角度,如degrees(math.pi/2) , 返回90.0 radians(x) //将角度转换为弧度
下面介绍了Python计算三角函数之asin()方法的使用(其它只需替换上述方法即可),返回x的反正弦,以弧度表示,代码如下:
12345678910111213
#!/usr/bin/python import math print "asin(0.64) : ", math.asin(0.64) print "asin(0) : ", math.asin(0) print "asin(-1) : ", math.asin(-1) print "asin(1) : ", math.asin(1) #运行结果如下:asin(0.64) : 0.694498265627asin(0) : 0.0asin(-1) : -1.57079632679asin(1) : 1.57079632679
Ⅳ python matplotlib数据作图
一、使用numpy生成长度为100的等差序列作为自变量,命名为x,并使用三角函数SiNx和cosx**2生成两个因变量,命名为y和Z。
Ⅵ Python如何画cos和sin的图啊
import
numpy
as
np
import
matplotlib.pyplot
as
plt
#从-π到π取201个变量(取得多点线就圆滑一点,这是源码写的例子201不知道有啥特殊之处,反正取100也行)
x
=
np.linspace(-np.pi,
np.pi,
201)
#计算201个变量对应的值连成线
plt.plot(x,
np.sin(x))
plt.plot(x,
np.cos(x))
#将画成的图显示出来
plt.show()
大概就是这个意思,有错误的还请指教
Ⅶ 如何在python中表达三角函数,比如sin,tan
在python中,有一个math mole,你可以import math,
里面有math.sin(),math.cos(),math.asin()和math.acos()四个函数.
有了这四个函数你就可以求函数值和角度了.
注意:括号里面填的数值,要用弧度制.
Ⅷ python计算三角函数的问题
[1]
[1,
1]
[1,
2,
1]
[1,
3,
3,
1]
[1,
4,
6,
4,
1]
[1,
5,
10,
10,
5,
1]
执行你那个生成器,并生成6行杨辉三角的数据
经过观察你就会发现这个列表推导式[l[i-1]
+
l[i]
for
i
in
range(len(l))]是产生每一行的杨辉三角数据的。
l[i-1]+l[i]是根据前一行指定索引位置的杨辉三角数据,产生新的一行的数据
Ⅸ 如何用python完成:用自顶向下设计方法编写程序:在屏幕上打印三角函数y = sin(x)的图像。
I wrote this in Tkinter for you, in case you don't know Tkinter, it is a built-in mole for most python versions.
If you want a commandline version, you can ask me, but tell you what, since those values are all
float numbers, so it's hard to get a precise graph in commandline window.
Well, in this version, I enlarged each element's position by 40 and then change them to integer, guess this is an enrable loss of precision.
#
frommathimportradians
frommathimportsin
fromTkinterimport*
pos=[]
xPos=0
centerX=0
centerY=0
fordeginrange(-360,361,10):
pos.append([xPos,int(40*(sin(radians(deg))))])#1000toobigformyscreen
xPos+=1
ifdeg==0:
centerX=xPos-1
centerY=pos[-1][1]
root=Tk()
root.title('trianblegraphfrom-180to180')
width,height=550,450
mHei=height/2
mWid=width/2
canvas=Canvas(root,width=width,height=height)
canvas.create_line(0,mHei,width,mHei)#xaxis
canvas.create_line(mWid,0,mWid,height)#yaxis
xStep=(width-150)/len(pos)
yStep=(height-150)/len(pos)
radius=3
#themiddlepoint(sin(0)
canvas.create_oval(mWid-radius,mHei-radius,mWid+radius,mHei+radius,fill='green')
printpos
printxStep,yStep,centerX,centerY
#exit(0)
foriinpos:
ifi[0]==centerX:#centerprocessedalready.
continue
x=mWid+xStep*(i[0]-centerX)
#yissmaller,thebiggerthevalue,souseminus
y=mHei-yStep*(i[1]-centerY)
canvas.create_oval(x-radius,y-radius,x+radius,y+radius,fill='green')
canvas.pack()
root.mainloop()