Advertisement
Guest User

shell oscillator

a guest
Oct 15th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #
  2. # Set program constants
  3. #
  4. BobMass = .02
  5. SpringConstant = .7
  6. DampingBeta = 0
  7. InitialStretch = .2
  8. EquilLength = .5
  9. ExpectedPeriod = 2*pi*sqrt(BobMass/SpringConstant)
  10. #
  11. scene2 = canvas(title='Damped Oscillator',caption='Animated Display',
  12. center=vector(0.50,0,0), background=color.white)
  13. #
  14. bob = sphere(pos=vector(0,0,0),radius=0.05,color=color.red)
  15. wall = box(pos=vector(0,0,0),size=vector(0.05,.1,.1),color=color.blue)
  16. spring = helix(pos=wall.pos,axis=bob.pos-wall.pos,radius=0.01,thickness=.004,coils=10,color=color.green)
  17. #
  18. # equilibrium position of the end of the spring.
  19. #
  20. length = vector(EquilLength,0,0)
  21. stretch = vector(InitialStretch,0,0)
  22. #
  23. # Set the initial position of the bob
  24. #
  25. bob.pos = wall.pos + length + stretch
  26. bob.mom = vector(0,0,0)
  27. #
  28. # Input Parameters needed in the program. Be sure to
  29. # choose sensible values.
  30. #
  31. bob.mass = BobMass
  32. spring.ks = SpringConstant
  33. beta = DampingBeta
  34. #
  35. print('Damped Mass on a Spring')
  36. #
  37. # Time step and total elapsed time
  38. #
  39. dt = 0.005
  40. t = 0.0
  41. #
  42. # Used to look for zero crossings to measure the period.
  43. #
  44. told = 0.0
  45. xold = bob.pos.x
  46. #
  47. # Setup a graph window to plot things in
  48. #
  49. s='<b>Mass and Spring: Graph</b>'
  50. #
  51. # Move the mouse over the graph to explore its interactivity.
  52. # Drag a rectangle in the graph to zoom. Examine the icons at the upper right.
  53. # Click the "Reset axes" icon to restore. Drag along the bottom or left to pan.
  54. #
  55.  
  56. graph(title=s, xtitle='Time',ytitle='Energy', xmas=10, ymax=.05, ymin=-.05, x=0, y = 500, width=500, height=300)
  57. #
  58.  
  59. drawKE = gcurve(color=color.cyan,label='Kinetic Energy')
  60. drawPE = gcurve(color=color.blue,label='Potential Energy')
  61. drawTE = gcurve(color=color.magenta,label='Total Energy')
  62. #
  63. while(t<10):
  64. rate(100)
  65. t += dt
  66. #
  67. # spring.stretch = block.pos-wall.pos
  68. #
  69. spring.force = -SpringConstant*(bob.pos-length)
  70. bob.mom = bob.mom + spring.force*dt
  71. bob.pos = bob.pos + bob.mom*(dt/bob.mass)
  72. spring.axis = bob.pos-wall.pos
  73.  
  74. KE = .5*((bob.mom.x)**2/bob.mass)
  75. PE = .5*SpringConstant*(bob.pos.x-length.x)**2
  76. TE = KE + PE
  77.  
  78. drawKE.plot(pos=(t,KE))
  79. drawPE.plot(pos=(t,PE))
  80. drawTE.plot(pos=(t,TE))
  81. #
  82. # Check for a zero crossing
  83. #
  84. xnew = bob.pos.x - wall.pos.x - length.x
  85. if xnew*xold <= 0:
  86. period = 2*(t - told)
  87. if told != 0:
  88. scene2.caption=('Expected period is',ExpectedPeriod,' actual period is',period,'.')
  89. told = t
  90. xold = xnew
  91. #
  92. # Plot the x-coordinate of the block as a function of time.
  93. #
  94. #
  95. print("Expected Period (sec)",ExpectedPeriod)
  96. print("Actual Period (sec)",period)
  97. print('All Done')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement