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
Next revision Both sides next revision
repository:cat_toy [2020/04/02 00:46]
porcaro1 [Cat Toy]
repository:cat_toy [2020/04/02 00:57]
porcaro1 [Cat Toy]
Line 50: Line 50:
   - 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|}} {{ :​repository:​cattoy.png?​nolink|}}
 +
 ===Working Code=== ===Working Code===
 +**Horizontal Spring (with Optional Linear Damping)**
 <code Python [enable_line_numbers="​true",​ highlight_lines_extra="​0"​]>​ <code Python [enable_line_numbers="​true",​ highlight_lines_extra="​0"​]>​
 GlowScript 2.7 VPython GlowScript 2.7 VPython
Line 90: Line 92:
 t = 0 t = 0
 dt = 0.001 dt = 0.001
- 
  
 while t < t_final: while t < t_final:
Line 118: Line 119:
         ​         ​
     t = t + dt </​code>​     t = t + dt </​code>​
 +    ​
 +**Vertical Spring with Gravity**
 +<code Python [enable_line_numbers="​true",​ highlight_lines_extra="​0"​]>​
 +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.txt
  • Last modified: 2021/04/07 22:43
  • by porcaro1