=====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=== [[https://www.glowscript.org/#/user/porcaro1/folder/RepositoryPrograms/program/CatToy-Incomplete | 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=== - Modify the code above to get your system to behave as an ideal simple harmonic oscillator. Ignore external forces such as gravity. - Plot the potential, kinetic, and total energy of the mass-spring system as they relate to time (hint: use the plot command in GlowScript). - (Optional 1): Add a linear damping effect to your system with a damping coefficient "c". - (Optional 2): Make your system into a vertical mass-spring system and include the force of gravity in your calculations. {{ :repository:cattoy.png?nolink|}} ===Working Code=== [[https://www.glowscript.org/#/user/porcaro1/folder/RepositoryPrograms/program/CatToy-Solution1 | 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** [[https://www.glowscript.org/#/user/porcaro1/folder/RepositoryPrograms/program/CatToy-Solution2 | 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=== - 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? - What would be some strategies for overcoming these challenges in your classroom? - 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? - Which //physics concepts// would you want your students to learn from this problem? - 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=== - If you had to specify one or several learning objectives for this assignment, what would they be? - In an assignment such as this one, what pedagogical affordances does computation offer over other forms teaching? What limitations does computation provide? - Can you think of any ways to make this problem more relevant to your students/classroom? - What types of questions would you ask your students to add depth to this problem? - 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?