### Energy in a Spring-Mass Oscillator

In this assignment you will add energy calculations and graphs to your program that predicted the motion of a mass on a spring. Retrieve a working copy of your program that made a mass oscillate on a horizontal spring. You can get your copy from where you saved it in WebAssign.

Remove the statements in your spring-mass program for graphing position.

Use the following:

Mass = 0.0265 kg
Spring stiffness = 1.3 N/m

Currently, Python interprets "1/2" as zero, because it does integer division and discards the remainder. This is inappropriate in physics computations and can lead to bugs that are hard to find, and in the future Python will interpret "1/2" as 0.5. To get this behavior now, put the following line at the start of your program (there are two underlines before "future" and two underlines after "future"):

from  __future__  import division

#### Plot graphs of energy

Before the "while" loop, prepare to make graphs of energy. The following statements go near the start of the program, after the statement "from visual import *":

from visual.graph import *
scene.y = 400 # move spring-mass display window below graph

And these lines go before the start of the "while" loop:

Kgraph = gcurve(color=color.cyan)
Ugraph = gcurve(color=color.yellow)
KplusUgraph = gcurve(color=color.red)

Inside the "while" loop calculate and plot the various energy terms as a function of time. You can omit the constant term in the potential energy and just plot (1/2)kss2 for the spring energy. There is a VPython function for finding the magnitude of a vector, which is convenient to use in this program:

pmag = mag(ball.p) # or whatever you've called the ball's momentum
K = (calculate the current kinetic energy of the ball)
U = (calculate the current potential energy of the spring-mass system)
Kgraph.plot(pos=(t,K))
Ugraph.plot(pos=(t,U))
KplusUgraph.plot(pos=(t,K+U))

#### Check that the program is running correctly

Set your while statement so that you graph two entire periods. Note that the kinetic energy has two maxima in every period.

(1) What do you expect for the graph of K+U? If the graph does not behave as you expect, check your calculations.
(2) What effect is there on the graph of K+U when you make the time step be quite large for computational speed, deltat = 0.03 seconds?
(3) What effect is there on the graph of K+U when you make the time step be quite small for computational accuracy, deltat = 0.0001 seconds?

Evidently a graph of K+U is a sensitive indicator of the accuracy of a computation.

(4) When the stretch is a maximum, is the kinetic energy zero? Small? Large?
(5) When the stretch is a minimum, is the kinetic energy zero? Small? Large?
(6) When the kinetic energy of the system increases, what should happen to the potential energy of the system? Is that what your graph shows?
(7) When the kinetic energy of the system decreases, what should happen to the potential energy of the system? Is that what your graph shows?

#### Energy "loss"

The energy of a real spring-mass oscillator decreases with time, due to sliding friction or air resistance. One kind of friction is particularly easy to model: "viscous" friction, which occurs for very small, slowly moving objects in air, or for ordinary-sized objects moving through thick oil. The magnitude of the force of viscous friction is proportional to the speed, and the direction is opposite to the direction of the velocity, so the viscous friction force can be represented by (-b) times the velocity, where b is a positive constant. Add viscous friction to your model, with b = 0.02 N·s/m. Observe carefully the shape of the graphs and answer the following questions. The energy "loss" is energy transferred to the surroundings from the spring-mass system.

(a) Is the rate of energy "loss" large when the kinetic energy is large?
(b) Is the kinetic energy large when the speed is large?
(c) Is the rate of energy "loss" large when the potential energy is large?
(d) Is the rate of energy "loss" large when the viscous force is large?
(e) Is the potential energy large when the speed is large?

You will submit your program to WebAssign, and there will be some questions about what you observe.