Advertisement
Guest User

Untitled

a guest
May 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #No.11 #Calculate the period of simple harmonic oscillator
  2.  
  3. import numpy as np
  4. from pylab import plot, show, grid, xlabel, ylabel
  5. import matplotlib.pyplot as plt
  6.  
  7. def main ():
  8. #Total time
  9. T = 10.0 #how far the graph will go in seconds
  10. #number of steps
  11. N = 100
  12. #Time step size in sec
  13. E = 0.1
  14.  
  15. #create array with steps+time step value from 0to time final
  16. #t = np.linspace(0.0,t,N+1)
  17. #print t
  18.  
  19. #allocating arrays for velocity and position
  20. v=np.zeros(N+1)
  21. x=np.zeros(N+1)
  22. a=np.zeros(N+1)
  23. t=np.linspace(0.0, T, N+1)
  24. rev=0
  25. period=0
  26.  
  27. #set the initial value for velocity and position
  28. v[0]=0.0
  29. x[0]=1.0
  30. a[0]=-1.0
  31. v[1]=v[0]+a[0]*E/2
  32. t[0]=0.0
  33.  
  34. #looping for position and velocity
  35. for i in range(1,N):
  36. t[i]=t[i-1]+E
  37. x[i]=x[i-1]+E*v[i]
  38. a[i]=-x[i]
  39. v[i+1]=v[i]+E*a[i]
  40.  
  41. if x[i]*x[i-1] < 0:
  42. if rev == 0:
  43. time_old=t[i]
  44. else:
  45. period[rev]=2*(t(i)-time_old)
  46. print period[rev]
  47. time_old=t[i]
  48. #rev=rev+1
  49. #print period[rev]
  50.  
  51. period_mean=np.mean(period[rev])
  52. error=np.std(period[rev])/np.sqrt(rev)
  53. print ('Average period = ',period_mean, error)
  54.  
  55.  
  56.  
  57. #plotting graph position vs time
  58. plt.figure(1)
  59. plt.plot (t, x, 'r')
  60. plt.xlabel('time', fontsize=16)
  61. plt.ylabel('position', fontsize=16)
  62. plt.grid(True)
  63. plt.show()
  64.  
  65. if __name__ == "__main__":
  66. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement