Beberapa contoh program python

Pemberian pak dedy

Program_1

1          import math
2          def time1(dist,v,n):
3          x=0                  # Initialize the position.
4          dx = dist/n       # Divide dist into n equal parts.
5          t=0                  # Initialize the time.
6          for i in range(n):
7          x = x+dx          # Change x.
8          dt=dx/v            # time=distance/speed
9          t=t+dt               # Keep track of elapsed time.
10         return t

>>>     time1(1.0,1.0,10)         # dist, v, n
0.99999999999999989

>>>     time1(1.0,1.0,1)           # dist, v, n
1.0


Program_2

1          import math
2          def time2(h,n):
3          g=9.8               # gravitational field
4          y=h                  # Initialize the height.
5          v=0                  # Initialize the velocity.
6          dy = -h/n          # Divide h into n equal parts.
7          t=0                   # Initialize the time.
8          for i in range(n):
9          y = y+dy          # Change y. (Note dy<0.)
10         v = -math.sqrt(2*g*(h-y)) # from cons. of energy
11         dt=dy/v            # dy and v are <0, so dt is >0
12         t=t+dt               # Keep track of elapsed time.
13         return t

>>> time2(1.0,10)       # h, n
0.35864270709233342

>>>     time2(1.0,10000) # h, n
0.44846664060793945














Program_3

1          import math
2          def time3(h,n):
3          g=9.8
4          y=h
5          v=0
6          dy = -h/n
7          t=0
8          for i in range(n):
9          y_old = y
10         y = y+dy
11         v_avg = -(math.sqrt(2*g*(h-y_old))+math.sqrt(2*g*(h-y)))/2.
12         dt=dy/v_avg
13         t=t+dt
14         return t

>>>     time3(1.0,1)     # h, n
0.45175395145262565



Program_4

1          import math
2          def timeb(a,b,c1,c2,n):
3          g=9.8
4          c3 = (b-c1*a-c2*a**2)/(a**3)
5          x=a
6          y=b
7          v=0
8          dx = -a/n
9          t=0
10         for i in range(n):
11         y_old = y
12         x = x+dx
13         y = c1*x+c2*x**2+c3*x**3
14         dy = y-y_old
15         v_avg = (math.sqrt(2*g*(b-y_old))+math.sqrt(2*g*(b-y)))/2.
16         ds = math.sqrt(dx**2+dy**2) # Pythagorean thm.
17         dt=ds/v_avg
18         t=t+dt
19         return t

>>>     b=1.
>>>     n=10000
>>>     c1=1.
>>>     c2=0.
>>>     timeb(a,b,c1,c2,n)
0.63887656499994161

>>>     c1=0.
>>>     c2=0.
>>>     timeb(a,b,c1,c2,n)
0.59458339947087069


Program_5

1          import math
2          def tmoon(vi,ri,rf,n):
3          bigg=6.67e-11   # gravitational constant
4          me=5.97e24      # mass of earth
5          mm=7.35e22     # mass of moon
6          rm=3.84e8         # earth-moon distance
7          r=ri
8          v=vi
9          dr = (rf-ri)/n
10         e=-bigg*me/ri-bigg*mm/(rm-ri)+.5*vi**2
11         t=0
12         for i in range(n):
13         u_old = -bigg*me/r-bigg*mm/(rm-r)
14         k_old = e - u_old
15         v_old = math.sqrt(2.*k_old)
16         r = r+dr
17         u = -bigg*me/r-bigg*mm/(rm-r)
18         k = e - u
19         v = math.sqrt(2.*k)
20         v_avg = .5*(v_old+v)
21         dt=dr/v_avg
22         t=t+dt
23         return t

>>>      re=6.378e6        # radius of earth
>>>      rm=1.74e6         # radius of moon
>>>      ri=re+3.363e5    # re+initial altitude
>>>      rf=3.8e8-rm       # earth-moon distance minus rm
>>>      vi=1.083e4        # initial velocity
>>>      tmoon(vi,ri,rf,1000)/3600. # convert seconds to hours
59.654047441976552


>>>      vi=1.2e4
>>>      tmoon(vi,ri,rf,1000)/3600.
18.177752636111677



Program_6

1          import math
2          def u(k,x):
3          return .5*k*x**2
4
5          def osc(m,k,a,n):
6          x=a
7          v=0
8          dx = -2.*a/n
9          t=0
10         e = u(k,x)+.5*m*v**2
11         for i in range(n):
12         x_old = x
13         v_old = v
14         x = x+dx
15         kinetic = e-u(k,x)
16         if kinetic<0. :
17         v=0.
18         print "warning, K=",kinetic,"<0"
19         else :
20         v = -math.sqrt(2.*kinetic/m)
21         v_avg = (v+v_old)/2.
22         dt=dx/v_avg
23         t=t+dt
24         return 2.*t

>>>     osc(1.,1.,1.,100000)
warning, K= -1.43707268307e-12 <0
6.2831854132667919

>>>     osc(1.,1.,2.,100000)
warning, K= -5.7482907323e-12 <0
6.2831854132667919



Program_7

1          import math
2          k=39.4784       # chosen to give a period of 1 second
3          m=1.
4          b=0.211           # chosen to make the results simple
5          x=1.
6          v=0.
7          t=0.
8          dt=.01
9          n=1000
10         for j in range(n):
11         x=x+v*dt
12         a=(-k*x-b*v)/m
13         if (v>0) and (v+a*dt<0) :
14         print "turnaround at t=",t,", x=",x
15         v=v+a*dt
16         t=t+dt



turnaround at t= 0.99 , x= 0.899919262445
turnaround at t= 1.99 , x= 0.809844934046
turnaround at t= 2.99 , x= 0.728777519477
turnaround at t= 3.99 , x= 0.655817260033
turnaround at t= 4.99 , x= 0.590154191135
turnaround at t= 5.99 , x= 0.531059189965
turnaround at t= 6.99 , x= 0.477875914756
turnaround at t= 7.99 , x= 0.430013546991
turnaround at t= 8.99 , x= 0.386940256644
turnaround at t= 9.99 , x= 0.348177318484



Kota Pempek , 18 Maret 2012


akhukum fillah arif zainurrohman

0 komentar:

Posting Komentar