Solving a Simple Harmonic Oscillator with Spreadsheets

Introduction

In this post, we’ll explore how to solve a simple harmonic oscillator using the Finite Difference Method (FDM) in spreadsheets. The simple harmonic oscillator is a fundamental concept in physics, described by a second-order differential equation that models various oscillatory systems, such as springs, pendulums, and circuits. While such equations can be challenging to solve analytically, numerical methods like FDM make it possible to approximate solutions with relative ease. By leveraging the power of spreadsheets, we can visualize the motion of the oscillator and gain deeper insights into its behavior—all without writing a single line of code.

The Method

The equation for a simple harmonic oscillator is a second order differential equation given by:

\begin{equation} m\frac{d^{2} x(t)}{dt^{2}} + c\frac{d x(t)}{dx} + kx(t) = 0 \end{equation}

Typically, when we have an equation of the first order, it is relatively simple to solve it using excel. Say for instance, we have an equation:

\begin{equation} \frac{d y}{dx} = f(x) \end{equation}

This could be solved with relative ease, by determining h, a constant value by which x increases, and solving it on a spreadsheet. The idea would be to assume that at infinitesimally small intervals, the curve behaves like straight line, and, hence, at any point k, the equation reduces to:

\begin{equation} \frac{d y}{dx} = \frac{f(k+h) – f(k)}{h} \end{equation}

The same principle applies when solving higher order differential equations. In the example given above:

\begin{equation} m\frac{d ^{2}x(t)}{dt} + c\frac{dx(t)}{dt} + kx(t) = 0 \end{equation}
\begin{equation} m\frac{d}{dt}\left( \frac{x_{t+1} – x_{t}}{h} \right) + c\left( \frac{x_{t+1} – x_{t}}{h} \right) + kx_{t} = 0 \end{equation}
\begin{equation} \frac{m}{h}\left( \left( \frac{x_{t+2} – x_{t+1}}{h} \right) – \left( \frac{x_t+1 – x_{t}}{h} \right) \right) + c\left( \frac{x_{t+1} – x_{t}}{h} \right) + kx_{t} = 0 \end{equation}
\begin{equation} m\left( \frac{x_{t+2} – 2x_{t+1} + x_{t}}{h^{2}} \right)+ c\left( \frac{x_{t+1} – x_{t}}{h} \right) + kx_{t} = 0 \end{equation}

Now, if we were to take all the unique x terms together

\begin{equation} \frac{m}{h^{2}}x_{t+2} + \left( \frac{c}{h} – \frac{2m}{h^{2}} \right)x_{t+1} + \left( \frac{m}{h^{2}} -\frac{c}{h} + k \right)x_{t} = 0 \end{equation}

Now, with the above simplification, it becomes relatively easy to solve a simple harmonic oscillator problem.

We will need some information. Let’s say, in this example:

\begin{equation} x(0) = 0 \end{equation}

and

\begin{equation} x'(0) = 1 \end{equation}

Since we will effectively looking for a value to x two time units removed from the initial condition, we can rewrite the above simplification as follows:

\begin{equation} x_{t+2} = \frac{-h^{2}}{m}\left( \left( \frac{c}{h} – \frac{2m}{h^{2}} \right)x_{t+1} + \left( \frac{m}{h^{2}} -\frac{c}{h} + k \right)x_{t} \right) \end{equation}

However, we are still missing the initial value of x(0.01). This can be easily obtained based on the formula:

\begin{equation} \frac{d x}{dt} = \frac{x_{0.01} – x_{0}}{0.01 – 0} \end{equation}

But we also know that x'(0) = 1. Substituting:

\begin{equation} x_{0.01} = 1\times 0.01 = 0.01 \end{equation}

Now, we have all the information needed to solve this problem. Let’s look at the spreadsheet embedded below.

CONCLUSION

In this post, we’ve explored how to use the Finite Difference Method (FDM) to solve the simple harmonic oscillator equation using spreadsheets. By breaking down the second-order differential equation into discrete steps, we’ve demonstrated how to apply this numerical method to approximate the solution iteratively. This approach not only makes complex equations more manageable but also leverages the familiar and accessible platform of Excel for numerical computation.

Using FDM, we transformed the differential equation into a set of algebraic equations that can be solved step-by-step. With the initial conditions defined and a clear method to propagate the solution forward, we are able to simulate the behavior of the oscillator over time. This showcases the power and flexibility of spreadsheets for solving higher-order differential equations, a task that might otherwise seem daunting.

By implementing this methodology, you can apply similar techniques to solve a variety of other dynamic systems, making Excel a surprisingly versatile tool for engineers and scientists. If you’d like to try this yourself, feel free to download the spreadsheet template and experiment with different parameters and initial conditions to see how the system responds.

Until next time!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *