Hide code cell source
# IF USING JUPYTER NOTEBOOK:
#%matplotlib notebook
# IF USING JUPYTER LAB:
%matplotlib widget
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.integrate import odeint

Driven Harmonic Oscillator#

Author: Ludovic Charleux (ludovic.charleux@univ-smb.fr)

In this example, you will simulate an harmonic oscillator and compare the numerical solution to the closed form one.

Theory#

Read about the theory of harmonic oscillators on Wikipedia

Mechanical oscillator#

The case of the one dimensional mechanical oscillator leads to the following equation:

mx¨+μx˙+kx=mx¨d

Where:

  • x is the position,

  • x˙ and x¨ are respectively the speed and acceleration,

  • m is the mass,

  • μ the

  • k the stiffness,

  • and x¨d the driving acceleration which is null if the oscillator is free.

Canonical equation#

Most 1D oscilators follow the same canonical equation:

x¨+2ζω0x˙+ω02x=x¨d

Where:

  • ω0 is the undamped pulsation,

  • ζ is damping ratio,

  • x¨d=adsin(ωdt) is the imposed acceleration.

In the case of the mechanical oscillator:

ω0=km
ζ=μ2mk
omega0 = 2.0 * np.pi * 1.0
zeta = 0.1
ad = 1.0
omegad = 2.0 * np.pi * 1.2

Part 1: Coding#

First, you need to code: the harmonic oscillator oscillator using the standarde ODE formulation:

X˙=f(X,t)
def f(X, t):
    """
    Driven Harmonic oscillator.
    """
    return

Part 2: Solving#

Solve the ODE using odeint and plot it.

Part 3: Steady state amplitude#

Determine the steady state amplitude a(ωd) of the oscillator.

Part 4: Amplitude vs. drive pulsation#

Plot the evolution of the amplitude of the driven oscillator and compare it with the theory.