02-C Mass on a Vertical Spring

The following snippet of code can be used as a starting point to model a mass on a vertical spring. Copy the code below and paste it into a new GlowScript program window.

  1. Study the beginning lines of code and notice how parameters such as the spring stiffness ks and the spring length L0 are defined at the start of the program before the loop.
  2. Notice that the time t is initialized to zero and the size of the time step dt is defined outside the loop.
  3. Inside the loop the rate(100) command tells the program to execute the contents of the loop 100 times each second. Since we have set dt = 0.01 seconds the program should run in near real time. When we add graphs or other features later the program may actually run slower.
  4. Notice the loop is defined to run for five seconds.
  5. Notice that the time is incremented on each iteration of the loop with an equation of the form t = t + dt inside the loop. This might seem like a silly algebraic equation since the t would cancel on each side of the equation. However, this is not an algebraic equation. It is an assignment which means that the value of the right hand side is evaluated and then assigned to the variable on the left hand side. It would be equivalent to the equation \(t_{new} = t_{old} + dt\).
  6. Finally, notice that there are three area that require your attention. In part A, you must set the initial conditions for the block (it’s initial position and velocity). In part B, you need to define the forces on the mass due to the Earth’s gravity and due to the spring. The tutorial Modeling a Spring Using Hooke’s Law will be helpful here. Finally, in part C you need to use the momentum principle and the position update formula to update the momentum and position of the block.
#
# Mass on a Vertical Spring
#
# We will consider a mass m attached to a spring of stiffness ks and 
# unstretched length L0.  The spring is vertical with one end of the spring 
# attached to the floor and the other end attached to the mass.  

# Define constants for the problem

L0 = 0.20           # Length of spring in meters
ks = 8.0            # Spring contant in N/m
g  = 9.8            # Strength of gravitational attraction in N/kg

# Define some colors for the spring, block and floor
aluminum = vector(0x84, 0x87, 0x89)/0xFF
darkblue = vector(0x21, 0x43, 0x65)/0xFF
brown    = vector(0x65, 0x43, 0x21)/0xFF

# Put the right end of the spring at the center of the scene and zoom in a bit
scene.center = vector(0,L0,0)
scene.width = 300
scene.height = 600
scene.align = "left"
scene.range  = L0
scene.background = vector(0.4, 0.4, 0.4)

# Display a set of coordinate axes using narrow cylinders that will scale with the spring size
xaxis=cylinder(color=color.white, pos=vector(-L0/2,0,0), axis=vector(L0,0,0), radius=L0/100, opacity=0.5)
yaxis=cylinder(color=color.white, pos=vector(0,0,0), axis=vector(0,2*L0,0), radius=L0/100, opacity=0.5)
zaxis=cylinder(color=color.white, pos=vector(0,0,-L0/2), axis=vector(0,0,L0), radius=L0/100, opacity=0.5)

# Define some size parameters for the system
a = 0.05            # Horizontal dimension of the block
b = 0.01            # Thickness of the floor

# Draw the floor, placing the origin at the point of attachment of the spring.
floor = box(pos=vector(0,-b/2, 0), size=vector(3*a, b, 3*a), color=brown)

# Draw the block
block = box(size=vector(a,a/10,a), color=darkblue, opacity=0.5)

# Define the mass of the block in kg
block.m = 0.06  

################################################################################
#                                                                              #
# A - Set the initial conditions of the block and compute its initial momentum #
#                                                                              #
################################################################################

# Initial position of block will be (0,L0,0) if the spring isn't stretched or compressed yet
block.pos = vector(0, L0, 0)    # Initial position of the mass in m
block.v = vector(0, 0, 0)       # Initial velocity of the mass in m/s

block.p = block.m * block.v     # Initial momentum of the mass in kg m/s

# Draw the spring with it's bottom edge attached to the origin and with a length defined by the block's position
# Make the size of the spring scale with the size of the mass for a nice visual effect.
spring = helix(pos=vector(0,0,0), axis=block.pos, coils=10, thickness=0.01, radius=a/4, color=aluminum)

# Define graphs for the position, momentum, and force versus time
graphTitle = 'Horizontal Mass Spring System: m = ' + block.m + ' kg, ks = ' + ks + ' N/m'
graph(title = graphTitle, width = 800, height = 560, xtitle = 'Time (s)', fast = False, align="right")

PositionVsTime = gcurve(color = color.blue,  width = 2, label = 'Position, y [m]')
MomentumVsTime = gcurve(color = color.red,   width = 2, label = 'Momentum, p.y [kg m/s]')
ForceVsTime    = gcurve(color = color.green, width = 2, label = 'Force, F [N]')

t  = 0                          # Initialize the time counter
dt = 0.01                       # Time step in seconds

while (t<5):
    rate(100)
    
    # Set the length of the spring to the position of the block.
    # Note:  We are ignoring the thickness of the block.
    L = block.pos
    
    # Fix the size of the spring for display purposes
    spring.axis = L
    
    ############################################################################
    #                                                                          #
    # B - Compute the forces exerted on the block                              #
    #                                                                          #
    ############################################################################
    
    # Gravitational force on the block by the Earth
    Fg = vector(0, 0, 0)
    
    # Force on the block by the spring
    Fs = vector(0, 0, 0)
    
    # Net force on the block
    Fnet = Fg + Fs
    
    ############################################################################
    #                                                                          #
    # C - Update the block's momentum (block.p) and position (block.pos)       #
    #                                                                          #
    ############################################################################
    #block.p = 
    #block.pos = 
    
    # Increment the time for the plots and for stopping the loop
    t = t + dt
    
    PositionVsTime.plot(t, block.pos.y)
    MomentumVsTime.plot(t, block.p.y)
    ForceVsTime.plot(t, Fnet.y)

Use your program to investigate the following questions.

  1. What is the effect on the motion when you change the initial conditions (position and velocity) of the block?
  2. What is the effect on the motion when you change the spring constant?
  3. What is the effect on the motion when you change the mass of the block?