Ⅰ 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()