04-A Mass on a Horizontal Spring

In this exercise you will model the motion of a mass connected to a horizontal spring.  One end of the spring is attached to a fixed wall and the other end of the spring is attached to the mass.

The starter code below draws the mass-spring system and shows the support wall and the horizontal surface.  The system is set up presently for a spring that is \(L_0 = 0.20\) m long with a force constant of \(k_s = 8.0\) N/m attached to a mass \(m = 0.20\) kg.  The initial conditions for the mass are presently such that the spring is unstretched and not moving. Pretty boring initial conditions that you will need to change.

You will notice that initially the mass just sits still.  This is due to the initial conditions and to the fact that the code describing the physics hasn’t been added yet. You will need to:

  • A – Enter the names of all students working together on this assignment.
  • B – Enter some non-trivial initial conditions for the block. Start off with an initial stretch of 5 cm = 0.05 m, and released from rest.
  • C – Replace the incorrect definition of the force on the mass by the spring with the proper expression that describes Hooke’s law in this situation.  You may wish to review the tutorial on Modeling a Spring Using Hooke’s Law.
  • D – Add lines of code to update the momentum block.p and the position block.pos of the block for each time step dt.

When finished with these steps you should find that the spring moves in a periodic manner. We call this type of motion simple harmonic motion. A graph showing the position and momentum of the spring and the force on the spring will be shown. When you place your cursor over different points on the graph you will see readouts of the values of x, p, F, and t. Use these values to help answer the questions below the starter code.

#
# Mass on a Horizontal Spring
#
# We will consider a mass m attached to a spring of stiffness ks and 
# unstretched length L0.  One end of the spring is attached to a wall
# and the other end is attached to the mass.  The mass moves along a
# horizontal, frictionless surface.
#
###########################################################################
#                                                                         #
# A - Enter the names of students working together on this assignment.    #
#                                                                         #
###########################################################################
students = "James Clerk Maxwell, Albert Einstein and Erwin Schrodinger"

# Define constants for the problem

L0 = 0.20           # Length of spring in meters
ks = 8.0            # Spring contant in N/m

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

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


# Define some size parameters for the system
a = 0.05            # Length of any side of the cubical block
b = 0.01            # Thickness of the wall and floor

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

#Display a yellow marker to show the equilibrium position of the system
equilibrium = cylinder(color=color.yellow, pos=vector(L0+a/2,0,0), axis=vector(0,L0/2,0), radius=L0/100, opacity=0.5)

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

# Draw the spring with it's left edge attached to the origin and with a length of L0.
# Make the size of the spring scale with the size of the mass for a nice visual effect.
#
# If the spring isn't stretched then spring.size.x = L0
spring = helix(pos=vector(0,0,0), size=vector(L0,a/3,a/3), coils=10, thickness=0.01,color=aluminum)

# Create a small block of edge 'a' on each side.
block = box(size=vector(a,a,a), color=darkblue)

# Set the mass of the block
block.m = 0.2                       # Mass of the block in kg

###########################################################################
#                                                                         #
# B - Define initial position and velocity of the block.  If the spring   #
#     isn't stretched initially then block.pos.x = L0 + a/2               #
#                                                                         #
###########################################################################
s0 = 0.00                           # Initial stretch of the spring in m
block.pos = vector(L0+a/2 + s0,0,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

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

# Create a graph object to show the Position, Momentum, and Force vs Time
graphTitle = students + ': m = ' + block.m + ' kg, ks = ' + ks + ' N/m, s0 = ' + s0 + ' m, v0 = ' + block.v.x + ' m/s'
graph(title = graphTitle, width = 800, height = 400,xtitle = 'Time (s)', fast = False)

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

# Iteratively solve for the position of the block as a funciton of time
while (t < 5):
    # Set the length of the spring to the position of the box minus 1/2 the width of the box.
    L = block.pos - a/2*vector(1,0,0)
    
    # Fix the size of the spring for display purposes
    spring.size.x = mag(L)
    
    # Use one or the other of the following lines.  The rate command will
    # allow the simulation to run freely.  The scene.pause() command will 
    # allow you to single step through the simulation using the mouse to
    # advance it forward one time step for each click.  Comment out the line
    # you do not want to use.
    #
    rate(100)
    #scene.pause()
    
    ##################################################################
    #                                                                #
    # C - Compute the force exerted on the block by the spring       #
    #                                                                #
    ##################################################################
    F = vector(0,0,0)
    
    ##################################################################
    #                                                                #
    # D - Update the momentum (block.p) and the position (block.pos) #
    #     of the block.                                              #
    #                                                                #
    ##################################################################
    
    

    # Increment the time for the plots and for stopping the loop
    t = t + dt
    
    PositionVsTime.plot(t, block.pos.x - (L0 + a/2))
    MomentumVsTime.plot(t, block.p.x)
    ForceVsTime.plot(t, F.x)