NMM Ordinary Differential and Difference Equations¶

Before we dive in: a note on documentation¶

I started this week by documenting my work in a Google doc, including the formatting of mathematical formulas. Then I figured it's better to learn how to create mathematical equations in Jupyter. To start, this Medium article informed how to do this with Markdown. In addition, Jupyterbook provides documentation with a few examples. Jupyter uses MathJax for mathematical equations, which works with TeX/LaTex. Here is a list with commands and a quick LaTex syntax guide I found helpful. Lastly, a list with mathematical symbols. With the basics covered, let's get started!

A few shortcuts for working faster in Jupyter notebook:¶

  • Run cell: Shift + Enter
  • Run cell and create new cell below: Alt/Option + Enter
  • Change cell type to Markdown: M
  • Change cell type to code: Y
  • Full list here.

(2.1) Consider the motion of a damped, driven harmonic oscillator (such as a mass on a spring, a ball in a well, or a pendulum making small motions):¶

In [ ]:
m ̈x + γ ̇x + kx = eiωt . (4.58)

Let's first try to write this properly in JupyterLab. Using MathJax and LaTex, the same equation looks like this:

In [ ]:
$$
  m\ddot{x}+y\dot{x}+kx=e^{i\omega t}
$$

In Markdown, this will correctly output the desired mathematical function:

$$ m\ddot{x}+y\dot{x}+kx=e^{i\omega t} $$

Success! The next step is to quickly decipher the notations with this Wikipedia page on Derivatives in Newton’s notation, because I had never seen Newton’s notation for differentiation before.

Newton notation

(a) Under what conditions will the governing equations for small displacements of a particle around an arbitrary 1D potential minimum be simple undamped harmonic motion?¶

Let’s break this down. To my understanding, small displacements of a particle around an arbitrary 1D potential minimum are very tiny changes in dx/dy (or dx/dt) at a local minimum of a function plotted on two axes. The source is a 3Blue1Brown video, Differential equations, a tourist's guide. Additionally, I watched their full series on the essence of calculus and the essence of linear algebra because I lack education in those areas. (thanks for the recommendation, Jack!)

The simplest step-by-step resource on calculating waves and oscillations I could find is this article by David Morin. Chapter 1.1 goes into simple undamped harmonic motion, and provides equations for it:

Hooke’s Law:
$$F=-kx$$
Potential energy:
$$V(x)=\frac{1}{2}kx^2 $$

The author mentions that ‘the best tool for seeing what a function looks like in the vicinity of a given point is the Taylor series, so let’s expand V(x) in a Taylor series around $x_0$ (the location of the minimum)’. Here is another 3Blue1Brown video tutorial on what a Taylor series is and how to calculate it. In my own words, it is a method to approximate the trajectory of the curve at a certain domain using simple polynomial functions.

We start with:

$$ F=ma $$$$ F=m\frac{d^2x}{dt^2} $$$$ F=m\ddot{x} $$$$ m\ddot{x}=-kx $$

Then do a Taylor at 0: $$ V=V0+V'1+V'' $$

(b) Find the solution to the homogeneous equation, and comment on the possible cases. How does the amplitude depend on the frequency?¶

It is a homogeneous equation, if ‘every term involves either the unknown function or its derivatives’ (NMM, p.9). You get two results: real and imaginary.

Learing from the Wikipedia page of a Damped harmonic oscillator:

$\omega$ is called the "undamped angular frequency of the oscillator"

$ζ = \frac{c}{2 \sqrt{m k}}$ is called the "damping ratio".

The value of the damping ratio ζ critically determines the behavior of the system. A damped harmonic oscillator can be:

  1. Overdamped (ζ > 1): The system returns (exponentially decays) to steady state without oscillating. Larger values of the damping ratio ζ return to equilibrium more slowly.
  2. Critically damped (ζ = 1): The system returns to steady state as quickly as possible without oscillating (although overshoot can occur if the initial velocity is nonzero). This is often desired for the damping of systems such as doors.
  3. Underdamped (ζ < 1): The system oscillates (with a slightly different frequency than the undamped case) with the amplitude gradually decreasing to zero. The angular frequency of the underdamped harmonic oscillator is given by ω 1 = ω 0 1 − ζ 2 , the exponential decay of the underdamped harmonic oscillator is given by λ = ω 0 ζ

How does the amplitude depend on the frequency? Not at all.

(c) Find a particular solution to the inhomogeneous problem by assuming a response at the driving frequency, and plot its magnitude and phase as a function of the driving frequency for m = k = 1, γ = 0.1.¶

It is an inhomogeneous equation when there is a term that depends on the independent variable alone (i.e. a forcing term). (NNM, p.9). Real part solves imaginary part, imaginary part solves the real part.

This is what I know about springs in structural engineering. The spring constant k is dependent on material properties and dimensions of the structure:

$$ k_{eq}=\frac{EA}{L} $$

Where E = Young's modulus, A = cross-secional area of the bar, and L = length of the bar before loading. But we're going to forget that for this assignment, because our goal is to approximate the curve, not to solve it with an exact answer.

Below are screenshots from my Google doc. Let's start with an ansatz.

Derivatives

Next, plugging this in:

Algebra

The solution written again in LaTex:

$$ A=\frac{1}{-m\omega ^2+i \gamma \omega +k} $$

A is the amplitude.

Here are the numbers: $$ m = k = 1, γ = 0.1 $$

From David Morin's paper, we know that the (angular) frequency of the motion in a Hooke’s-law potential is:

$$ \omega = \sqrt{\frac{k}{m}} $$

That means the following in this example: $$ \omega = \sqrt{\frac{1}{1}} $$ $$ \omega = 1 $$

Plotting the function¶

The next step is to plot the function. To quickly prototype and experiment with mathematical functions, I used this free, online tool. Here is a guide on integrating this in Jupyter.

Next up: plotting with Python. In class, it was recommended to do this with Python & SciPy & matplotlib. This Medium article helps in getting a better understanding of how to do this. For this task, I'm going to try working with NumPy (Numerical Python) and matplotlib visualization for Python. Both have extensive documentation. Matplotlib offers a quickstart guide and a couple of helpful cheatsheets to get started.

In [134]:
import numpy as np 
import matplotlib.pyplot as plt


X = np.linspace(0, 4*np.pi, 1000) # This creates a line at (start, stop, number of samples to generate)
Y = np.sin(X) # Adding a sinus function of X


plt.plot(X, Y)
plt.ylabel("Position")
plt.xlabel("Time")
plt.title("Oscillator experiment", fontsize = 14)

plt.show()

Next, let's plot the assignment.

In [107]:
import numpy as np 
import matplotlib.pyplot as plt

k = 1
m = 1
gamma = 0.1

omega = np.linspace(0, 2) # Draw a line from x=0 to x=2

A = 1 / (-m * omega * omega + 1j * gamma * omega + k) # Formula with complex number j instead of i


A_magnitude = np.abs(A) # In absolute values (https://numpy.org/doc/stable/reference/generated/numpy.absolute.html)
A_phase = np.angle(A) # In angles (https://numpy.org/doc/stable/reference/generated/numpy.angle.html)


plt.figure()
plt.plot(omega, A_magnitude) 
plt.ylabel("Position")
plt.xlabel("Frequency $\omega$")


plt.plot(omega, A_phase)
plt.ylabel("Position")
plt.xlabel("Frequency $\omega$")
plt.title('Damped Harmonic Oscillator', fontsize = 16)


plt.show()

That's a first! Let's explore the possiblities for making the plot look a little nicer.

In [132]:
import numpy as np 
import matplotlib.pyplot as plt

k = 1
m = 1
gamma = 0.1

omega = np.linspace(0, 2, 1000) # Increasing step resolution for a smoother curve

A = 1 / (-m * omega * omega + 1j * gamma * omega + k) # Formula with complex number j instead of i


A_magnitude = np.abs(A) # In absolute values (https://numpy.org/doc/stable/reference/generated/numpy.absolute.html)
A_phase = np.angle(A) # In angles (https://numpy.org/doc/stable/reference/generated/numpy.angle.html)


plt.figure()
plt.plot(omega, A_magnitude, color="purple", label="magnitude")  # Adding color and label
plt.xlabel("Frequency $\omega$")

plt.plot(omega, A_phase, color="pink", label="phase") # Adding color and label
plt.title('Damped Harmonic Oscillator', fontsize = 14)

plt.legend() # Adding a legend
plt.text(1.5,1.75, 'magnitude') # Or annotate with inline text
plt.text(1.5,-2.25, 'phase') 


plt.show()

(d) For a driven oscillator the Q or Quality factor is defined as the ratio of the center frequency to the width of the curve of the average energy (kinetic + potential) in the oscillator versus the driving frequency (the width is defined by the places where the curve falls to half its maximum value). For an undriven oscillator the Q is defined to be the ratio of the energy in the oscillator to the energy lost per radian (one cycle is 2π radians). Show that these two definitions are equal, assuming that the damping is small. How long does it take the amplitude of a 100 Hz oscillator with a Q of 109 to decay by 1/e?¶

Kinetic energy + potential energy

The potential energy stored in a simple harmonic oscillator at position x is (wikipedia):

$$ U=\frac{1}{2}kx^2 $$

Kinetic energy:

$$ E=\frac{1}{2}mv^2 $$

The motion of a simple harmonic oscillator is characterized by its period

$$ T = 2 \pi/\omega $$

The time for a single oscillation or its frequency

$$ f = 1/T $$

Kinetic energy + potential energy

The potential energy stored in a simple harmonic oscillator at position x is (wikipedia):

$$ U=\frac{1}{2}kx^2 $$

The motion of a simple harmonic oscillator is characterized by its period

$$ T = 2 \pi/\omega $$

The time for a single oscillation or its frequency

$$ f = 1/T $$

(e) Now find the solution to equation (4.58) by using Laplace transforms. Take the initial condition as x(0) = ̇x(0) = 0.¶

As a reminder, the equation is:

$$ m\ddot{x}+y\dot{x}+kx=e^{i\omega t} $$

Here's a video tutorial on how to solve a damped harmonic oscillator with Laplace transforms.

Next:

  1. Replace the derivative with s.
  2. Rearrange function.
  3. Look up the function in the Laplace table.

(f) For an arbitrary potential minimum, work out the form of the lowest-order correction to simple undamped unforced harmonic motion.¶

Answer

(2.2) Explicitly solve (and try to simplify) the system of differential equations for two coupled harmonic oscillators (see Figure 4.2; don’t worry about the initial transient), and then find the normal modes by matrix diagonalization.¶

Answer

(2.3) A common simple digital filter used for smoothing a signal is¶

$$ y(k)=\alpha y(k-1)+(1-\alpha)x(k) $$

where α is a parameter that determines the response of the filter. Use z-transforms to solve for y(k) as a function of x(k) (assume y(k < 0) = 0). What is the amplitude of the frequency response?¶

Answer

Meme of the week

Meme week 2 - imaginary numbers

Click here to go back to the main page.