Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
repository:cat_toy [2020/04/02 00:12]
porcaro1
repository:cat_toy [2021/04/07 22:43] (current)
porcaro1
Line 2: Line 2:
 ====Cat Toy==== ====Cat Toy====
 ===Premise=== ===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 [[https://​www.glowscript.org/​ | GlowScript]].+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=== ===Minimally Working Program===
-<code Python [enable_line_numbers="​true",​ highlight_lines_extra="​0"]>+[[https://​www.glowscript.org/#/​user/​porcaro1/​folder/​RepositoryPrograms/​program/​CatToy-Incomplete | Link]] 
 +<code Python [enable_line_numbers="​true",​ highlight_lines_extra="​7,​8,​24,​26,​27,​28,​30,​31,​32,​37,​45,​46,​51,​52,​53,​58,​59,​60"]>
  
 GlowScript 2.7 VPython GlowScript 2.7 VPython
Line 45: Line 46:
    ​spring.size = vec(mag(L),​0.75,​0.75)    ​spring.size = vec(mag(L),​0.75,​0.75)
  
-   t = t + dt</​code>​+   t = t + dt </​code>​
        
 ===Tasks=== ===Tasks===
   - Modify the code above to get your system to behave as an ideal simple harmonic oscillator. Ignore external forces such as gravity.   - 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).   - 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 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.   - (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=== ===Working Code===
-** +[[https://​www.glowscript.org/#/​user/​porcaro1/​folder/​RepositoryPrograms/​program/​CatToy-Solution1 | Link]] 
-<code Python [enable_line_numbers="​true",​ highlight_lines_extra=""​]>​+**Horizontal Spring (with Optional Linear Damping)** 
 +<code Python [enable_line_numbers="​true",​ highlight_lines_extra="​0"]>
 GlowScript 2.7 VPython GlowScript 2.7 VPython
  
Line 91: Line 94:
 t = 0 t = 0
 dt = 0.001 dt = 0.001
- 
  
 while t < t_final: while t < t_final:
Line 102: Line 104:
     ​     ​
     Fspring=-1*k*s*Lhat     Fspring=-1*k*s*Lhat
-    Fnet=Fspring+    Fnet=Fspring ​#(Linear damping)# -c*block.p/​block.mass
     ​     ​
     block.p = block.p + Fnet*dt     block.p = block.p + Fnet*dt
Line 118: Line 120:
     EGraph.plot(t,​Espring)     EGraph.plot(t,​Espring)
         ​         ​
-    t = t + dt <\code>+    t = t + dt </code> 
 +     
 +**Vertical Spring with Gravity** 
 +[[https://​www.glowscript.org/#/​user/​porcaro1/​folder/​RepositoryPrograms/​program/​CatToy-Solution2 | Link]] 
 +<code Python [enable_line_numbers="​true",​ highlight_lines_extra="​4,​12,​13,​14,​24,​25,​41,​42,​43,​44,​47,​57"​]>​ 
 +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."​)</​code>
 ===Debrief Questions=== ===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 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?
  • repository/cat_toy.1585786327.txt.gz
  • Last modified: 2020/04/02 00:12
  • by porcaro1