In this example, you will simulate an harmonic oscillator and compare the numerical solution to the closed form one.
Read about the theory of harmonic oscillators on Wikipedia
The case of the one dimensional mechanical oscillator leads to the following equation:
$$ m \ddot x + \mu \dot x + k x = m \ddot x_d $$Where:
Most 1D oscilators follow the same canonical equation:
$$ \ddot x + 2 \zeta \omega_0 \dot x + \omega_0^2 x = \ddot x_d $$Where:
In the case of the mechanical oscillator:
$$ \omega_0 = \sqrt{\dfrac{k}{m}} $$$$ \zeta = \dfrac{\mu}{2\sqrt{mk}} $$First, you will focus on the case of an undamped free oscillator ($\zeta = 0$, $\ddot x_d = 0$) with the following initial conditions:
$$ \left \lbrace \begin{split} x(t = 0) = 1 \\ \dot x(t = 0) = 0 \end{split}\right. $$The closed form solution is: $$ x(t) = a\cos \omega_0 t $$
In [1]:
# Setup
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
# Setup
f0 = 1.
omega0 = 2. * np.pi * f0
a = 1.
In [2]:
# Complete here
#t =
#xth =
Solve the problem introduced in question 1 with the Euler integrator.
Steps:
In [ ]:
Calculate and plot the kinetic energy $E_c$, the potential energy $E_p$ and the total energy $E_t = E_c + E_p$, comment the result.
Steps:
In [ ]:
Plot the effect of the number time steps $n_t$ on the error $e$.
Steps:
In [ ]:
In [ ]: