Below is an animation of the motion of a mass hanging on a spring. As it moves up and down the total energy of the system expresses itself as changing amounts of kinetic energy, potential energy due to gravity, and potential energy of a spring.
Click this button to examine the computer code that runs this animation.
Here's the code that runs the above animation.
You can change the code, if you like, and then click the following 'Reevaluate code' button. The program will then work as per your changes. Of course, your changes, especially random changes, can introduce errors, miscalculations, and browser crashes. If you need to get things back to their original condition, just reload this page using your browser's reload button.
The intention here is to conveniently show the inner workings of this program so that you understand how the diagram is drawn.
Click the 'Code' button again to close this section.
The different levels for the spring potential, gravitational potential, and kinetic energies are highlighted at the appropriate times during the motion.
At the top of the motion:
The spring is stretched the least, so, the potential energy of the spring is at a minimum.
The mass is lifted as high as it will go, so, the potential energy due to gravity is at a maximum.
The mass is stopped, so, its kinetic energy is zero.
In the middle of the motion:
Some energy is stored in the spring, since it is stretched to some degree; that is, there is some potential energy in the spring.
Some energy is stored in the gravitational field, since the object is lifted above ground; that is, there is some potential energy due to gravity.
The mass has reached its maximum speed; so, the kinetic energy of the mass is at a maximum.
At the bottom of the motion:
The potential energy of the spring is at a maximum since the spring is stretched at its maximum extension.
The potential energy due to gravity is at a minimum, since the mass is lifted the least amount above ground. In fact, if ground level is imagined at the bottom of the motion, the potential energy due to gravity is zero at this point of the motion.
The kinetic energy of the mass is zero, since the mass is temporarily stopped.
So, while the mass moves up and down energy changes forms between potential energy due to the spring, potential energy due to gravity, and kinetic energy. However, the total energy of the system will remain constant, according to the law of the conservation of energy.
Of course, for a real mass moving with a real spring, the motion will eventually come to a stop. This is because energy is slowly radiated by the system as heat. Over time this 'energy leak' will drain energy from the system, and the mass will quit moving.