This is an old revision of the document!


Modeling 1D Mass-Spring System and Energy

Cat Toy

Premise

After a long day of work, you return home to be greeted by your favorite feline companion, TicTac. Most days, you would usually play with TicTac by tossing his favorite toy (a bright, red block stuffed full of catnip) around the room and watching him chase it from one end to the other. Unfortunately, today you are just too tired to throw TicTac's block. Instead you decide to fix the block to a spring so that TicTac can get his exercise with as little effort as possible on your end. You attach the apparatus to a wall, giving the block a slight push, and it begins oscillating back and forth. TicTac is more excited than ever, as his block is now moving in a way that he never knew possible. Deciding that you are curious about this unique form of motion, you choose to computationally model the motion in GlowScript.

Minimally Working Program

  1. GlowScript 2.7 VPython
  2.  
  3. L0 = 5 #Spring length
  4. k = 1 #Spring constant
  5.  
  6. track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7))
  7. wall = box(pos = vec(-4.75,1,0), size = vec(0.5,2,2), color = color.gray(0.7))
  8. block = box(pos = vec(0,1,0), size = vec(2,2,2), color = color.red)
  9. spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos-
  10. wall.pos, color = color.red)
  11. spring.thickness = 0.1
  12.  
  13. L = block.pos - spring.pos
  14. Lhat = L/mag(L)
  15. s = mag(L) - L0
  16.  
  17. Fspring = vec(0,0,0)
  18. Fnet = vec(0,0,0)
  19.  
  20. block.mass = 0.3
  21. block.p = vec(1.05,0,0)
  22.  
  23. t = 0
  24. dt = 0.05
  25.  
  26. while t < 100:
  27. rate(100)
  28.  
  29. L = block.pos - spring.pos
  30. Lhat = L/mag(L)
  31. s = mag(L) - L0
  32.  
  33. block.p = block.p + Fnet*dt
  34. block.pos = block.pos + block.p/block.mass*dt
  35.  
  36. spring.axis = Lhat
  37. spring.size = vec(mag(L),0.75,0.75)
  38.  
  39. t = t + dt
  • repository/cat_toy.1585784061.txt.gz
  • Last modified: 2020/04/01 23:34
  • by porcaro1