Created
May 11, 2015 16:45
-
-
Save roger35972134/dba75a5ff4af74c119ef to your computer and use it in GitHub Desktop.
第二次python作業
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
列出巴斯卡三角形() | |
畫出二項分佈() | |
畫出巴松分佈() | |
def 列出巴斯卡三角形(): | |
Z=[[bCoef(n,k) for k in range(n+1)] for n in range(20)] | |
for z in Z: print(z) | |
from math import factorial, exp | |
def Comb(n,k): | |
y=factorial(n)//factorial(k)//factorial(n-k) | |
return y | |
def bCoef(n,k): | |
y=bCoef(n-1,k) +bCoef(n-1,k-1)if(0<k<n) else 1 | |
return y | |
from turtle import * | |
def 畫出二項分佈(): | |
C=Comb | |
n=100 | |
p=.1 | |
q=1-p | |
L=[(x,C(n,x) * p**x * q**(n-x)) for x in range(n+1)] | |
print('L= ',L) | |
最大的縱座標值=1.1*max([f for (x,f) in L]) | |
setworldcoordinates(0,0,n+1, 最大的縱座標值) | |
setheading(90) | |
for x,f in L: | |
penup() | |
goto(x,0) | |
color('red') | |
write('%d'%x) | |
pendown() | |
goto(x,f) | |
color('blue') | |
stamp() | |
write('%.4f'%f) | |
def 畫出巴松分佈(): | |
n=100 | |
a=10 | |
L=[(x,a**x /factorial(x) *exp(-a)) for x in range(n+1)] | |
print('L= ',L) | |
最大的縱座標值=1.1*max([f for (x,f) in L]) | |
setworldcoordinates(0,0,n+1,最大的縱座標值) | |
setheading(90) | |
for x,f in L: | |
penup() | |
goto(x,0) | |
color('magenta') | |
write('%d'%x) | |
pendown() | |
goto(x,f) | |
color('green') | |
stamp() | |
write('%.4f'%f) | |
if __name__=='__name__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment