Physics 120 – A Mass Oscillating on a Horizontal Spring. Goal: Today’s laboratory exercise will have you model a mass on a spring both with and without sliding friction between the mass and the horizontal surface using VPython. Part 1: Mass oscillating on a horizontal spring with no friction Open the VPython program mass_spring_No_Friction_CodeforLab.py. From previous laboratory exercises involving VPython you should recognize the initial lines of calling the graphics import commands. In addition we define the coefficient of kinetic friction mu (initially set to 0.15) and the acceleration due to gravity. The coefficient of friction is a parameter that you can change later in the program (part 2). The next set of commands makes a graph widow and sets the scale for the graph windows. You won’t be changing these commands during the execution of the program. scene.title = 'Mass-Spring System' scene.height = 300 scene.width = 600 scene.autoscale = 0 scene.range = (2,2,2) The next set of commands in the program are used to create a graph that plots the position of the mass and the velocity of the mass as functions of position. p_v = gdisplay(x=500,y=400,xtitle='t (s)', ytitle='x (m)/v (m/s)') This defines the graph to display and it’s called p_v for position and velocity. The attributes set the horizontal and vertical widths of and allow you to title the x and y-axes respectively. You could call these anything that you want; here they are labeled for you. The gcurve command with the attribute color tells the program to create a curve (that you’ll call later in the program to actually make a curve on your graph) with the color specified. You could change the colors if you wanted to reflect your personal taste. position = gcurve(color=color.red) velocity = gcurve(color=color.blue) Next you will see commands that will create graphs of the kinetic, potential and total energy and will title the graph that is created with the x- and y-axis titles of time (in seconds) and Energy (in Joules). The kinetic energy here is going to be a curve that is colored red, the potential energy a curve colored blue and the total energy (the sum of the kinetic and potential energies) colored green. energy = gdisplay(x=500,xtitle='t (s)', ytitle='Energy (J)') kinetic_energy = gcurve(color=color.red) potential_energy = gcurve(color=color.blue) total_energy = gcurve(color=color.green) The last set of user-defined commands creates the floor and the wall that the spring is attached to and oscillating on. Further one defines the floor and the wall to be boxes with dimensions specified by the length, height, and width attributes. The position specifies the location of the floor and the wall. floor = box(pos=(0,-0.25,0), length=3, height=0.10, width=1) wall = box(pos=(-1.55,0,0), length=0.10, height=0.60, width=1). This sets up the basics of the VPython code. For the remainder of the program you will modify the given code so that you can model the motion of a mass connected to the end of a horizontal spring oscillating on a frictinless surface. You will generate the kinetic, elastic potential and total energy in the system and plot them graphically. Then you will modify the program you created to incorporate the effects of sliding friction into your program. The first program you want to make will be able to model the motion of the mass on the end of horizontal spring with stiffness k oscillating on a horizontal frictionless surface. You are going to modify the program that you have open to make a block of mass 0.5kg move back and forth for a total of 5 seconds. Define initial velocity to be v i = 0,0,0 ms and define the block’s momentum based on the velocity. Your program should fill in the code for the following lines. block = box(pos=(0.75,0,0), length=0.5, height=0.5, width=0.5,€color=color.red) block.m = block.v = block.p = Next we will define the spring and the stiffness of the spring, or the spring constant. The program has the following code: spring = helix(pos=(-1.5,0,0), axis=block.pos-wall.pos, radius=0.1, thickness=0.02, coils=10, color=color.blue) This defines the spring to be a helix (a coil) with the end that the mass will be attached to located at the position of the block minus the wall. The location of the end of the spring will change if either the wall or the block changes position. The wall will not change its position, but the block will as the motion progresses. The spring has an axis (defined to be along the horizontal of the spring) to be this difference in positions. Again, if either the block or wall changes position the axis will have an appropriate length and stay connected to the mass. The radius is used to define the diameter of the spring (2r in this case) of the spring, and coils define the number of coils in the spring. The attribute color defines the color of the spring, blue in this case. Modify your program and define the value of k to be the stiffness of the spring in N/m, with a number here to be between zero and one hundred. k= Next, we define the initial time to be t = 0. What would an appropriate time step be to see the motion of the block on the end of the spring? Input this value into the program as dt. dt = Now, the while loop should be familiar to you from previous VPython laboratory exercises. The while loop will be used to update the kinetic, elastic potential and total energy associated with the mass-spring system as well as the momentum of the block. The next lines of the program are given below. Determine the appropriate expressions for the kinetic energy of the mass, the elastic potential energy in the spring, and the total energy of the mass-spring system. Call these expressions K, U, and E and fill in the appropriate lines of code below and in your program. while t<5: # Plot the position and velocity position.plot(pos=(t,block.pos.x)) velocity.plot(pos=(t,block.p.x/block.m)) # Calculate the energies K= U= E= Hint: You will need to calculate the magnitude of the momentum (or of the velocity) vector to calculate the kinetic energy. The magnitude of a vector is given by vector_name.mag2 (which will determine the square magnitude of the vector. And to square an individual component of a vector, call the attribute and use **2. # Plot the energies kinetic_energy.plot(pos=(t,K)) potential_energy.plot(pos=(t,U)) total_energy.plot(pos=(t,E)) Here there are some new VPython commands. The commands position.plot(pos=(t,block.pos.x)) and velocity.plot(pos=(t,block.p.x/block.m)) define the plot parameters for the position and velocity of the block respectively. For the position, we have the x-axis equaling time, and the y-axis, the x- or horizontal position of the block, block.pos.x. The parameters for the velocity plot are the time t, and the xcomponent of the block’s velocity, block.p.x / block.m. The last set of commands under the #plot the energies heading above will plot the kinetic, potential, and total energy at the correct time t in the program with the values of the kinetic, potential, and total energies calculated by the program. Now we need to calculate and update the momentum of the mass on the end of the spring. We have the code: # Calculate the force and update the momentum and position of the block F_s = F_net = block.p = block.pos = Define the spring force F_s as an appropriate vector using your value of the spring constant from earlier and then use the derivative form of the momentum principle to update the momentum of the block subject to the net force (the spring force) and the position of the block using the derivative form of the position update. Lastly in this program, the spring axis needs to be updated, so that as the block’s position changes the position of the end of the spring stays attached to the mass. This statement should be in the while loop so that as the block moves the end of the spring moves with the block. Then we need to increment the time step and set the rate. You shouldn’t change these parameters. # Update the spring axis spring.axis = block.pos - wall.pos # Update the time and set the rate t = t + dt rate(500) Save your program and each lab group email it to your instructor with the title Mass_Spring_NO_Friction_each of your initials_Date.py and make sure you comment your program. Run your program and verify that it runs. You should have a graphics window that shows a wall, horizontal surface, and a red block attached to a blue spring. The mass should oscillate on the end of the spring. You should also have a two graph windows. One plots the position and velocity of the block as functions of time and the second plots the kinetic, elastic potential and total energy versus time. Now, vary the value of k and see what happens. What is the effect of varying k on the period of oscillation? Change the value of the mass and determine the effect on the period of oscillation. Are the effects as expected from your previous experimentations? Provide your answer as a comment in your program. Determine the period of the oscillation of the mass on the end of the spring using your plot of the velocity (or position) versus time. You can click on the graph window to determine the time. If the program runs successfully, you should have a red mass on the end of a blue spring that oscillates back and forth for 5 seconds. The graph should have a constant value for the energy and the potential energy U and kinetic energy K should sum to the total energy E. Make sure your programs runs. This will determine the first part of your lab grade. Part 2: Sliding Friction Open the program Mass_Spring_SLIDING_Friction_each of your initials_Date.py and make sure you comment your program as you go and save it often. Here you will keep the format of the program the same except that now you will incorporate sliding friction into your code. To incorporate sliding friction, we’ll use the coefficient of friction mu command that you put in the beginning of your previous program. You will need to modify some of the following commands: Define a value for the spring constant k (or leave it the same value you had in your previous program. k= Define an appropriate time step, dt. What did you use in the last program? Is it reasonable to use the same time step here? dt = while t<5: # Plot the position and velocity position.plot(pos=(t,block.pos.x)) velocity.plot(pos=(t,block.p.x/block.m)) # Calculate the energies K= U= E= Write code for the kinetic, elastic potential and total energies above. If you are stuck, think about what did you did in the last program. Do you want the expressions for the kinetic, potential, and total energies to change? Modify the program accordingly to calculate K, U and E. Now for the next set of code in your program and shown below, you can keep the same expressions since you want to make the same plot, except that you want to now include friction in your calculation. # Plot the energies kinetic_energy.plot(pos=(t,K)) potential_energy.plot(pos=(t,U)) total_energy.plot(pos=(t,E)) Now, you need to determine the spring force and the frictional force. Determine the appropriate expressions for these forces and enter them into your program and below. # Calculate the force and update the momentum and position of the block F_s = F_f_mag = if block.p.x <= 0: F_f = if block.p.x > 0: F_f = F_net = block.p = block.pos = # Update the spring axis spring.axis = block.pos - wall.pos # Update the time and set the rate t = t + dt rate(500) In the code above you’ll notice two new statements. These are the if statements. The if statement is a conditional expression. Why would you need a conditional expression for the frictional force? Think about why this might be the case and comment in the program the answer to this question. Here you need to provide the condition for the if statement. To do this, what is the direction of the force of friction as the block moves from xmax towards the equilibrium point? What happens to the frictional force as the mass passes through the equilibrium point heading towards –xmax? How could you incorporate these conditions into the if statement? Remember that force is a vector quantity. Determine the expression for the net force vector and update the block’s momentum and position accordingly. The reminder of the code should stay the same. Run your program and verify that it runs. You should have a graphics window that shows a wall, horizontal surface, and a red block attached to a blue spring. The mass should oscillate on the end of the spring. You should also have a two graph windows. One plots the position and velocity of the block as functions of time and the second plots the kinetic, elastic potential and total energy versus time. What do you notice about the kinetic, elastic potential and total energy as time goes on in the presence of sliding friction? Provide your answer as a comment in the program. Vary the value of mu and see what happens. Is there an effect of mu on the period of oscillation? Explain the effect of varying mu on the period. What happens if mu is very large (close to 1)? What happens if mu is very small (close to zero)? Determine the period of the masses oscillation on the end of the spring for the same values of k, mass and mu that you used in the last program. Put in a comment that gives your values of mu, mass, and k when you determine the period of oscillation. Is the period of oscillation the same or does it differ? Should the period of oscillation change? What happens to the total energy as time increments? What happens to the potential and kinetic energies as time increments? Do they still sum to the total energy? Answer these questions as comments in your program. Save your program and each lab group email it to your instructor with the title Mass_Spring_Friction_each of your initials_Date.py and make sure you comment your program. If the program runs successfully, you should have a red mass on the end of a blue spring that oscillates back and forth for 5 seconds. The total energy should dampen out over time and the block should stop at the equilibrium position. Make sure your programs runs. This will determine the second part of your lab grade.
© Copyright 2026 Paperzz