Damped harmonic oscillator example

# Load necessary packages
import numpy as np
import matplotlib.pyplot as plt

Define a damped harmonic oscillator + noise

def damped_harmonic_oscillator(omega,beta,noise,T0):

    dt = 0.01;
    T  = int(np.ceil(T0/dt))

    t = np.arange(0,T)*dt
    F = np.zeros([T,1])         # Set up forcing,
    F[int(0.25*T)] = 1;         # ... give it a kick 25% of way into simulation.
    x = np.zeros([T,1])
    y = np.zeros([T,1])

    x[0]=0
    y[0]=0
    
    for i in range(0,T-1):
        x[i+1] = x[i] + dt*(y[i]);
        y[i+1] = y[i] + dt*(-omega**2*x[i] - 2*beta*y[i] + F[i] + noise*np.random.randn());

    return x,t

Simulate the model

f     = 1;
omega = 2*np.pi*f;
beta  = 1;
noise = 0;
T0    = 10
x,t   = damped_harmonic_oscillator(omega,beta,noise,T0);
plt.plot(t,x);