+
Simple Hanging Harmonic Oscillator
Developed by K. Roos  Published July 17, 2016
In this set of exercises the student builds a computational model of a hanging massspring system that is constrained to move in 1D, using the simple Euler and the EulerCromer numerical schemes. The student is guided to discover, by using the model to produce graphs of the position, velocity, and energy of the mass as a function of time, that the Euler algorithm does not conserve energy, and that for this simple oscillatory system, a modified algorithm (EulerCromer) is necessary to avoid artificial behavior in the model.
Subject Area  Mechanics 

Levels  First Year and Beyond the First Year 
Available Implementations  C/C++, Fortran, Glowscript, IPython/Jupyter Notebook, Mathematica, MATLAB, Python, Spreadsheet, and Easy Java Simulations 
Learning Objectives 
Students who complete this set of exercises will
 be able to build a model of a simple hanging harmonic oscillator using the Euler algorithm (**Exercises 1 and 2**);
 be able to build a model of a simple hanging harmonic oscillator using the EulerCromer algorithm (**Exercises 4 and 5**);
 be able to produce graphs of the positon, velocity, and total energy as a function of time from the results of their computational model (**Exercises 15**);
 be able to assess the accuracy of two different computational algorithms (Euler and EulerCromer) by comparing results from the different algorithms to each other and to the exact analytical solution (**Exercises 15**);
 discover that they bloody well can't use the simple Euler method when modeling an oscillatory system (**Exercises 13**).

These exercises are not tied to a specific programming language. Example implementations are provided under the Code tab, but the Exercises can be implemented in whatever platform you wish to use (e.g., Excel, Python, MATLAB, etc.).
#### Exercise 1: Euler Algorithm Model of a SHO
Build a computational model of a simple hanging harmonic oscillator using the Euler method. Use realistic values for the parameters (i.e., spring constant $k$ and attached mass $m$, such as would be encountered in a typical introductory mechanics laboratory exercise. Also, assume that the mass of the spring is negligible compared to the attached mass, and that the harmonic oscillator has been stretched vertically downward a distance $y_0$, relative to its hanging equilibrium position and released from rest. Use the model to produce graphs of the position and velocity of the mass as a function of time, and compare these with the exact functions for the position and velocity,
$$y(t)=y_{0}\cos\left(\sqrt{\frac{k}{m}}t\right)$$
and
$$v(t)={\sqrt{\frac{k}{m}}y_{0}\sin\left(\sqrt{\frac{k}{m}}t\right)},$$
that result from solving Newton's 2nd Law analytically. Does the angular frequency match that expected for a simple harmonic oscillator of mass $m$ an spring constant $k$?
#### Exercise 2: Artificial Behavior with the Euler Algorithm
You may (should!) have noticed that something is not right with the Euler model of your hanging oscillator. Describe in detail the artificial behavior you observe in your model, and explain why it doesn't represent a realistic oscillating mass. Recall that in the Euler method, the accuracy of the solution can be increased by using a smaller value of $\Delta t$. Can you get rid of the artificial behavior by making $\Delta t$ smaller?
#### Exercise 3: Energy in the Euler Algorithm Model of a SHO
Modify your model to produce a graph of the total energy of the oscillator as a function of time. Describe in detail what happens to the energy, and the artificial behavior observed. Can this artificial behavior in the energy be corrected by making $\Delta t$ smaller? What can you conclude about using the Euler method to model a simple harmonic oscillator?
#### Exercise 4: EulerCromer Algorithm Model of a SHO
Build a model of the hanging oscillator using the modified Euler, or EulerCromer, numerical method. Compare the results you obtain (i.e. position and velocity vs. time) with those obtained from the simple Euler method, and with the exact solution. Comment in detail on your results.
#### Exercise 5: Energy in the EulerCromer Algorithm Model of a SHO
Modify your model to produce a graph of the total energy as a function of time. Is energy conserved for the EulerCromer algorithm?
Download Options
Share a Variation
Did you have to edit this material to fit your needs? Share your changes by
Creating a Variation
Credits and Licensing
K. Roos, "Simple Hanging Harmonic Oscillator," Published in the PICUP Collection, July 2016.
The instructor materials are ©2016 K. Roos.
The exercises are released under a Creative Commons AttributionNonCommercialShareAlike 4.0 license