Differences

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

Link to this comparison view

Next revision
Previous revision
repository:solar_system_springs [2020/09/03 17:58]
porcaro1 created
repository:solar_system_springs [2021/04/07 23:30] (current)
porcaro1 [Answer Key]
Line 9: Line 9:
     *$F=-kx$     *$F=-kx$
 ===Code Manipulation=== ===Code Manipulation===
-  *creating/modifying existing code +  *Creating/modifying existing code 
-  *translating ​physical concepts and equations into code+  *Translating ​physical concepts and equations into code
 ---- ----
 ====Activity==== ====Activity====
 ===Handout=== ===Handout===
 ** Solar System Springs **  ** Solar System Springs ** 
 +{{ :​repository:​spring_spring.png?​nolink&​600|}}
  
 You and your team have the opportunity to board the Sol Spacecraft to explore the solar system. Should you choose to accept the mission, NASA requires you to study the action of springs on each of the planets. Some planets do not have a solid surface, so your experiment will be conducted as you hover deep within the planet'​s atmosphere. Your experiments will examine the effect of each planet'​s gravitational field on the oscillation of a spring. You also have another container filled with many different masses. You and your team have the opportunity to board the Sol Spacecraft to explore the solar system. Should you choose to accept the mission, NASA requires you to study the action of springs on each of the planets. Some planets do not have a solid surface, so your experiment will be conducted as you hover deep within the planet'​s atmosphere. Your experiments will examine the effect of each planet'​s gravitational field on the oscillation of a spring. You also have another container filled with many different masses.
Line 38: Line 39:
     - Case 5: The mass is between the equilibrium and bottom  ​     - Case 5: The mass is between the equilibrium and bottom  ​
   - Create a diagram showing the acceleration and velocity vectors for each of the aforementioned cases.   - Create a diagram showing the acceleration and velocity vectors for each of the aforementioned cases.
-  - Run the [[https://​trinket.io/​glowscript/​bf533ef826 | minimally working code]]. The code allows the user to input the value for the spring constant ($k$) and change the mass of the crate ($m$) using a slider. What problem(s) do you see with the simulation?+  - Run the minimally working code (below). The code allows the user to input the value for the spring constant ($k$) and change the mass of the crate ($m$) using a slider. What problem(s) do you see with the simulation?
  
 Fortunately,​ the code is not entirely complete; you have a fantastic opportunity to fix it! Modify the code to make the spring oscillate appropriately. Also, add some lines of code so that you can change the acceleration due to gravity and perform a check to make sure the value is in the "​down"​ direction (the "​up"​ direction is set as positive). Make sure that the acceleration due to gravity is reported in the window that prints the mass and spring constant. Once you have verified that your code is running properly, complete the following questions. Fortunately,​ the code is not entirely complete; you have a fantastic opportunity to fix it! Modify the code to make the spring oscillate appropriately. Also, add some lines of code so that you can change the acceleration due to gravity and perform a check to make sure the value is in the "​down"​ direction (the "​up"​ direction is set as positive). Make sure that the acceleration due to gravity is reported in the window that prints the mass and spring constant. Once you have verified that your code is running properly, complete the following questions.
Line 52: Line 53:
  
 ===Code=== ===Code===
 +[[https://​www.glowscript.org/#/​user/​porcaro1/​folder/​RepositoryPrograms/​program/​SolarSystemSprings-Incomplete | Link]]
 <code Python [enable_line_numbers="​true"​]>​ <code Python [enable_line_numbers="​true"​]>​
 GlowScript 2.7 VPython GlowScript 2.7 VPython
Line 157: Line 159:
 ====Answer Key==== ====Answer Key====
 ===Handout=== ===Handout===
 +==Pre-coding Questions==
 +  - 
 +  - 
 +  - There are 2 major problems with the code:
 +    - The program does not stop, despite there being code alluding to a 20 second run-time (line 5)
 +    - The spring does not oscillate, but rather extends infinitely. This is due to the "​Fnet"​ in line 69 only including the force of gravity but not the force of the spring ​
 +
 +==Post-coding Questions==
 +  - {{:​repository:​spring_graphs.png?​nolink&​600|}}
 +  - When the mass is at the top of its oscillation,​ the velocity is at a minimum and its acceleration is at a maximum (the force of gravity and the force of the compressed spring are pulling and pushing the mass down)
 +  - When the mass is at the equilibrium point, its velocity is at a maximum, and its acceleration is at a minimum (the spring and the force of gravity counteract each other and the net force is 0) 
 +  - When the mass is at the bottom of its acceleration,​ the velocity is at a minimum and the acceleration is at a maximum value (although the force of gravity is still pulling the mass downwards, the force of the stretched spring pulling the mass upwards will result in a net force that causes the same magnitude acceleration as when the spring is at the top of its oscillation)
 +  - Increasing the spring constant affects the stiffness of the spring. The higher the constant, the stiffer the spring is. A higher spring constant results in smaller amplitude for position, but higher amplitudes for velocity and acceleration. Additionally,​ the frequency of the oscillation increases, and therefore the period decreases
 ===Code=== ===Code===
-<code Python [enable_line_numbers="​true",​ highlight_lines_extra=""​]>​+[[https://​www.glowscript.org/#/​user/​porcaro1/​folder/​RepositoryPrograms/​program/​SolarSystemSprings-Solution | Link]] 
 +<code Python [enable_line_numbers="​true",​ highlight_lines_extra="​8,​9,​10,​11,​63,​64,​69,​74,​84"]> 
 +GlowScript 2.7 VPython 
 + 
 +running=True ​    #Sets up condition for pause/run button 
 + 
 +totalTime=20 #in seconds 
 + 
 +#​Variables/​Objects 
 +g=float(prompt("​Acceleration due to Gravity?: ")) 
 +while g>0: 
 +  g=0 
 +  g=float(prompt("​ERROR:​ Acceleration due to Gravity is down: ")) 
 +k=int(prompt("​Spring constant?: "​))#​prompt allows user to set spring constant 
 + 
 +#Graphs below measure position, velocity, and acceleration as a function of time 
 +f4 = graph(xmin=0,​xmax=totalTime,​xtitle='<​i>​t</​i>​ in seconds'​) 
 +f5 = gcurve(color=color.green,​ label='​position',​fast=True) 
 +f6 = gcurve(type='​scatter',​ color=color.blue,​ label='​velocity',​fast=True) 
 +f7 = gcurve(type='​scatter',​ color=color.red,​ label='​acceleration',​fast=True) 
 +f1 = graph(xmin=0,​xmax=totalTime,​xtitle='<​i>​t</​i>​ in seconds'​) 
 +f1 = gcurve(color=color.green,​ label='​position',​fast=True) 
 +f2 = graph(xmin=0,​xmax=totalTime,​xtitle='<​i>​t</​i>​ in seconds'​) 
 +f2 = gcurve(type='​scatter',​ color=color.blue,​ label='​velocity',​fast=True) 
 +f3 = graph(xmin=0,​xmax=totalTime,​xtitle='<​i>​t</​i>​ in seconds'​) 
 +f3 = gcurve(type='​scatter',​ color=color.red,​ label='​acceleration',​fast=True) 
 + 
 +#The mass and spring hang from the ceiling 
 +ceiling = box(pos=vec(0,​11.5,​0),​size=vec(8,​1,​1),​ color=color.red) 
 + 
 +Mass = box(pos=vector(0,​-10,​0),​size=vec(2,​2,​2),​velocity=vector(0,​0,​0),​color=color.green,​mass=1) 
 + 
 +def setmass(s):​ 
 +  return 
 +#Using a slider for the mass allows the user to change the mass, ranging from 1 to 5 kg 
 +mass = slider( min=1, max=5, value=1,​label='​Mass'​ ,​align='​right',​step=0.5,​ length=220, bind=setmass) 
 + 
 +pivot = vector(0,​10,​0) 
 + 
 +spring = helix(pos=pivot,​axis=Mass.pos-pivot,​radius=0.8,​coils=20,​constant=k,​thicnkess=0.1,​color=color.green) 
 +#When you hang a mass from a spring, the spring stretches and the equilibrium point gets moved down 
 +eq = vector(0,​Mass.mass*g/​k,​0) 
 + 
 +#We are starting with time t=0, and changing every .05 seconds 
 +t = 0 
 +dt = .05 #if time step is smaller than this, then completion time will not be the total time duration. 
 +print('​mass = 1 kg | ', '​spring constant = ', k,'​N/​m',​ ' | gravity is:',​g,​ '​m/​s/​s'​) 
 + 
 +#The code below allows the user to pause/run the program by pushing a button at the top 
 +def Run(b): 
 +      global running 
 +      running = not running 
 +      if running: 
 +        b.text = "​Pause"​ 
 +        running=True 
 +      else:  
 +        b.text = "​Run"​ 
 +        running=False 
 +button(text="​Pause",​ pos=scene.title_anchor,​ bind=Run) ​  
 + 
 +#In the while loop below, the forces (gravity, spring, and net) are being updated 
 +#Also, the acceleration,​ velocity, and position of the box are being updated 
 + 
 +while True: 
 +    if t > totalTime:​ 
 +      running=False 
 +   
 +    if running==True:​ 
 +      frameRate = 1/dt 
 +      rate(frameRate) 
 +      Fg = vec(0,​Mass.mass*g,​0) 
 +      if eq.y < Mass.pos.y:​ 
 +        Fs = vec(0, -k*mag(Mass.pos-eq),​0) 
 +      else: 
 +        Fs = vec(0, k*mag(Mass.pos-eq),​0) 
 +      Fnet = Fg + Fs 
 +      acc = Fnet/​(Mass.mass) 
 + 
 +       
 +      Mass.velocity = Mass.velocity+acc*dt 
 +      Mass.pos = Mass.pos+Mass.velocity*dt 
 +      spring.axis = Mass.pos-spring.pos 
 + 
 +       
 +      if mass.value != Mass.mass:​ 
 +        print('​mass =  ', mass.value,'​kg'​ ,'| spring constant = ', k,'​N/​m',​ '| gravity is:', g, '​m/​s/​s'​) 
 +     
 +      Mass.mass=mass.value 
 +     
 +     
 +      f1.plot(t,​Mass.pos.y) 
 +      f2.plot(t,​Mass.velocity.y) 
 +      f3.plot(t,​acc.y) 
 +      f5.plot(t,​Mass.pos.y) 
 +      f6.plot(t,​Mass.velocity.y) 
 +      f7.plot(t,​acc.y) 
 + 
 +   
 +      t = t + dt 
 +    if running==False:​ 
 +       
 +      frameRate =1/dt 
 +      rate(frameRate) 
 +      acc = (eq-Mass.pos)*(spring.constant/​Mass.mass)*0 
 +      Mass.velocity = Mass.velocity 
 +      Mass.pos = Mass.pos 
 +      spring.axis = spring.axis
 </​code>​ </​code>​
  
 ---- ----
 ====See Also==== ​ ====See Also==== ​
 +  * [[bungee_jump | Bungee Jump]]
  • repository/solar_system_springs.1599155907.txt.gz
  • Last modified: 2020/09/03 17:58
  • by porcaro1