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

GlowScript 2.7 VPython
 
L0 = 5   #Spring length
k = 1   #Spring constant
 
track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7))
wall = box(pos = vec(-4.75,1,0), size = vec(0.5,2,2), color = color.gray(0.7))
block = box(pos = vec(0,1,0), size = vec(2,2,2), color = color.red)
spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos-
wall.pos, color = color.red)
spring.thickness = 0.1
 
L = block.pos - spring.pos
Lhat = L/mag(L)
s = mag(L) - L0
 
Fspring = vec(0,0,0)
Fnet = vec(0,0,0)
 
block.mass = 0.3
block.p = vec(1.05,0,0)
 
t = 0
dt = 0.05
 
while t < 100:
   rate(100)
 
   L = block.pos - spring.pos
   Lhat = L/mag(L)
   s = mag(L) - L0
 
   block.p = block.p + Fnet*dt
   block.pos = block.pos + block.p/block.mass*dt
 
   spring.axis = Lhat
   spring.size = vec(mag(L),0.75,0.75)
 
   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)

GlowScript 2.7 VPython
 
L0 = 5   #Spring length
k = 1   #Spring constant
c = 0.05   #Damping coefficient
 
stretch_initial = -3 #Negative means compress, positive means elongate
t_final = 100 #Use 3.4 for 2 full periods, use 100 for a long run
 
track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7))
wall = box(pos = vec(-5,1,0), size = vec(0.5,2,2), color = color.gray(0.7))
block = box(pos = vec(stretch_initial,1,0), size = vec(2,2,2), color = color.red)
spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos-wall.pos, color = color.red)
spring.thickness = 0.1
 
L = block.pos - spring.pos
Lhat = L/mag(L)
s = mag(L) - L0
 
Fspring = vec(0,0,0)
Fnet = vec(0,0,0)
 
block.mass = 0.3
block.p = vec(0,0,0)
 
Uspring=0.5*k*s**2
Kspring=0.5*block.mass*(mag(block.p/block.mass))**2
Espring=Uspring+Kspring
 
UGraph = series( color=color.red )
KGraph = series( color=color.blue )
EGraph = series( color=color.green )
 
t = 0
dt = 0.001
 
while t < t_final:
 
    rate(5000)
 
    L = block.pos - spring.pos
    Lhat = L/mag(L)
    s = mag(L) - L0
 
    Fspring=-1*k*s*Lhat
    Fnet=Fspring #(Linear damping)# -c*block.p/block.mass
 
    block.p = block.p + Fnet*dt
    block.pos = block.pos + block.p/block.mass*dt
 
    Uspring=0.5*k*s**2
    Kspring=0.5*block.mass*(mag(block.p/block.mass))**2
    Espring=Uspring+Kspring
 
    spring.axis = Lhat
    spring.size = vec(mag(L),0.75,0.75)
 
    UGraph.plot(t,Uspring)
    KGraph.plot(t,Kspring)
    EGraph.plot(t,Espring)
 
    t = t + dt 

Vertical Spring with Gravity Link

GlowScript 2.7 VPython
 
#System parameters: Length of spring at equilibrium (L0), spring constant (k), damping constant (c)
g = 10
mBlock = 0.3
L0 = 5
k = 500
c = 0.3
 
#Objects: track, wall, block, and spring
#track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7))
wall = box(pos = vec(0,5,0), size = vec(3,0.5,3), color = color.gray(0.7))
block = box(pos = vec(0,2.5,0), size = vec(2,2,2), color = color.red)
spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos-wall.pos, color = color.red)
spring.thickness = 0.1 #Spring thickness attribute
 
#Initializing system variables: Length of spring (L), length unit vector (Lhat), and stretch or displacement from equilibrium (s)
L = block.pos - spring.pos
Lhat = L/mag(L)
s = mag(L) - L0
 
#Initializing empty force vectors
Fspring = vec(0,0,0)
Fdamp = vec(0,0,0)
Fgrav = vec(0,0,0)
Fnet = vec(0,0,0)
 
#Block attributes
block.mass = mBlock
block.p = vec(0,0,0)
 
#Initialize data series for U, K, and E graphs. http://www.glowscript.org/docs/GlowScriptDocs/graph.html
Ugraph = series(color=color.green)
Kgraph = series(color=color.red)
Egraph = series(color=color.blue)
 
#Time and time step
t = 0
dt = 0.00005
 
#Trigger for program to begin with mouse click
print("Click the program window to begin.")
ev = scene.waitfor('click')
print("The program is running.")
 
#Begin update loop
while t<10:
 
    rate(5000)
 
    L = block.pos - spring.pos
    Lhat = L/mag(L)
    s = mag(L) - L0
 
    Fspring = -1*k*s*Lhat
    Fdamp = -1*c*block.p/block.mass
    Fgrav = block.mass*vec(0,-g,0)
    Fnet = Fspring+Fdamp+Fgrav
 
    block.p = block.p + Fnet*dt
    block.pos = block.pos + block.p/block.mass*dt
 
    spring.axis = Lhat
    spring.size = vec(mag(L),0.75,0.75)
 
    U = 0.5*k*s**2
    K = 0.5*block.mass*mag(block.p/block.mass)**2
    E = U + K
 
    Ugraph.plot(t,U)
    Kgraph.plot(t,K)
    Egraph.plot(t,E)
 
    t = t + dt
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?