## GlowScript Tutorial 4: Mass on a Spring

Now for something fun. Let’s model a mass on a spring. Here is the situation.

Here we have a mass (m) hanging from a spring with an unstretched length *L *and a spring constant *k. *If the spring is stretched down like in the image on the right then there will be a force from the spring pulling up. There is also the gravitational force pulling on the mass.

Let’s start with the following setup.

This creates a platform at the top (holder), a ball and a spring. This is what it looks like when you run it.

I rotated the camera just a bit so that you could see how cool it looks. Now we need to move into the numerical calculation using the Momentum Principle. Here is the basic plan.

- Determine the amount the spring is stretched (or compressed).
- Calculate the spring force and the gravitational force on the mass.
- Use the force to calculate the new momentum after a short time interval.
- Use the momentum to update the position of the mass.
- Fix the location that the spring is attached to the mass.
- Update time, repeat.

Really, the only new part is determining the spring force. Let’s use the following expression for the force the spring exerts on the mass.

Here the vector *L* points from the part where the spring is attached to the platform to the mass and *L*_{0} is the unstretched length of the spring. Finally, *L*-hat is a unit vector in the direction of the spring (without this the spring force would just be a scalar).

Let me get you started on the loop with the calculations in it.

Here is the program in Glowscript (you can copy and paste it into your own version of the program). If everything is working right, it should look like this.

It works? Great. Now for the fun stuff. Here are some things to try.

- Go back to tutorial 3 and add a graph. Add a graph of the y-position vs. time. What about y-momentum vs. time?
- Is the solution correct? You can theoretically calculate the period of oscillation for a mass on a spring (just look up the formula). After you make a graph you can also measure the period of oscillation.
- In the example above, the mass started at the equilibrium position of the spring with a zero momentum. What if you change the initial conditions? Try giving it a non-zero momentum or start in a different position.
- What to see something cool? What if you give the mass a starting position that is not directly underneath the holder? Go ahead, try it. It’s awesome.
- What about the time interval. How big can you make it and still get the calculation to work?
- Try changing the order of the calculations – does that make a difference?
- If you change the amplitude of the oscillation, do you get a different period of oscillation? Should you?
- If you think you are really cool, see if you can get the mass to move around in a circle in the x-z plane.
- What if you add a drag force to the mass?
- In the above example, I have used a cylinder to represent the spring (because it’s simple). What about replacing the cylinder object with a helix object? Use the GlowScript help (upper right in GlowScript) to look up in the details of the helix object and put it in place of the cylinder.

**GlowScript Tutorial 5: Air Resistance**