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

Link

  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

Tasks

  1. Modify the code above to get your system to behave as an ideal simple harmonic oscillator. Ignore external forces such as gravity.
  2. Plot the potential, kinetic, and total energy of the mass-spring system as they relate to time (hint: use the plot command in GlowScript).
  3. (Optional 1): Add a linear damping effect to your system with a damping coefficient ā€œcā€.
  4. (Optional 2): Make your system into a vertical mass-spring system and include the force of gravity in your calculations.

Working Code

Link Horizontal Spring (with Optional Linear Damping)

  1. GlowScript 2.7 VPython
  2.  
  3. L0 = 5 #Spring length
  4. k = 1 #Spring constant
  5. c = 0.05 #Damping coefficient
  6.  
  7. stretch_initial = -3 #Negative means compress, positive means elongate
  8. t_final = 100 #Use 3.4 for 2 full periods, use 100 for a long run
  9.  
  10. track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7))
  11. wall = box(pos = vec(-5,1,0), size = vec(0.5,2,2), color = color.gray(0.7))
  12. block = box(pos = vec(stretch_initial,1,0), size = vec(2,2,2), color = color.red)
  13. spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos-wall.pos, color = color.red)
  14. spring.thickness = 0.1
  15.  
  16. L = block.pos - spring.pos
  17. Lhat = L/mag(L)
  18. s = mag(L) - L0
  19.  
  20. Fspring = vec(0,0,0)
  21. Fnet = vec(0,0,0)
  22.  
  23. block.mass = 0.3
  24. block.p = vec(0,0,0)
  25.  
  26. Uspring=0.5*k*s**2
  27. Kspring=0.5*block.mass*(mag(block.p/block.mass))**2
  28. Espring=Uspring+Kspring
  29.  
  30. UGraph = series( color=color.red )
  31. KGraph = series( color=color.blue )
  32. EGraph = series( color=color.green )
  33.  
  34. t = 0
  35. dt = 0.001
  36.  
  37. while t < t_final:
  38.  
  39. rate(5000)
  40.  
  41. L = block.pos - spring.pos
  42. Lhat = L/mag(L)
  43. s = mag(L) - L0
  44.  
  45. Fspring=-1*k*s*Lhat
  46. Fnet=Fspring #(Linear damping)# -c*block.p/block.mass
  47.  
  48. block.p = block.p + Fnet*dt
  49. block.pos = block.pos + block.p/block.mass*dt
  50.  
  51. Uspring=0.5*k*s**2
  52. Kspring=0.5*block.mass*(mag(block.p/block.mass))**2
  53. Espring=Uspring+Kspring
  54.  
  55. spring.axis = Lhat
  56. spring.size = vec(mag(L),0.75,0.75)
  57.  
  58. UGraph.plot(t,Uspring)
  59. KGraph.plot(t,Kspring)
  60. EGraph.plot(t,Espring)
  61.  
  62. t = t + dt

Vertical Spring with Gravity Link

  1. GlowScript 2.7 VPython
  2.  
  3. #System parameters: Length of spring at equilibrium (L0), spring constant (k), damping constant (c)
  4. g = 10
  5. mBlock = 0.3
  6. L0 = 5
  7. k = 500
  8. c = 0.3
  9.  
  10. #Objects: track, wall, block, and spring
  11. #track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7))
  12. wall = box(pos = vec(0,5,0), size = vec(3,0.5,3), color = color.gray(0.7))
  13. block = box(pos = vec(0,2.5,0), size = vec(2,2,2), color = color.red)
  14. spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos-wall.pos, color = color.red)
  15. spring.thickness = 0.1 #Spring thickness attribute
  16.  
  17. #Initializing system variables: Length of spring (L), length unit vector (Lhat), and stretch or displacement from equilibrium (s)
  18. L = block.pos - spring.pos
  19. Lhat = L/mag(L)
  20. s = mag(L) - L0
  21.  
  22. #Initializing empty force vectors
  23. Fspring = vec(0,0,0)
  24. Fdamp = vec(0,0,0)
  25. Fgrav = vec(0,0,0)
  26. Fnet = vec(0,0,0)
  27.  
  28. #Block attributes
  29. block.mass = mBlock
  30. block.p = vec(0,0,0)
  31.  
  32. #Initialize data series for U, K, and E graphs. http://www.glowscript.org/docs/GlowScriptDocs/graph.html
  33. Ugraph = series(color=color.green)
  34. Kgraph = series(color=color.red)
  35. Egraph = series(color=color.blue)
  36.  
  37. #Time and time step
  38. t = 0
  39. dt = 0.00005
  40.  
  41. #Trigger for program to begin with mouse click
  42. print("Click the program window to begin.")
  43. ev = scene.waitfor('click')
  44. print("The program is running.")
  45.  
  46. #Begin update loop
  47. while t<10:
  48.  
  49. rate(5000)
  50.  
  51. L = block.pos - spring.pos
  52. Lhat = L/mag(L)
  53. s = mag(L) - L0
  54.  
  55. Fspring = -1*k*s*Lhat
  56. Fdamp = -1*c*block.p/block.mass
  57. Fgrav = block.mass*vec(0,-g,0)
  58. Fnet = Fspring+Fdamp+Fgrav
  59.  
  60. block.p = block.p + Fnet*dt
  61. block.pos = block.pos + block.p/block.mass*dt
  62.  
  63. spring.axis = Lhat
  64. spring.size = vec(mag(L),0.75,0.75)
  65.  
  66. U = 0.5*k*s**2
  67. K = 0.5*block.mass*mag(block.p/block.mass)**2
  68. E = U + K
  69.  
  70. Ugraph.plot(t,U)
  71. Kgraph.plot(t,K)
  72. Egraph.plot(t,E)
  73.  
  74. t = t + dt
  75. print("the program is complete.")

Debrief Questions

  1. What challenges do you anticipate facing when teaching a problem like this? What challenges do you anticipate your students facing when working on a problem like this?
  2. What would be some strategies for overcoming these challenges in your classroom?
  3. What computational practices are used in this problem? What expectations do you have for your students with respect to these practices? How would you communicate these practices to your students?
  4. Which physics concepts would you want your students to learn from this problem?
  5. How is the minimally working program designed so that students are forced to engage with the physics concepts? As a teacher, how can you emphasize physics concepts while avoiding teaching strictly coding?

Other Follow-up Questions

  1. If you had to specify one or several learning objectives for this assignment, what would they be?
  2. In an assignment such as this one, what pedagogical affordances does computation offer over other forms teaching? What limitations does computation provide?
  3. Can you think of any ways to make this problem more relevant to your students/classroom?
  4. What types of questions would you ask your students to add depth to this problem?
  5. How can your program be made more or less accurate to model a true physical system? What might be some sources of error or discrepancy?
  • repository/cat_toy.txt
  • Last modified: 2021/04/07 22:43
  • by porcaro1