# Load necessary packages
import numpy as np
import matplotlib.pyplot as plt
Damped harmonic oscillator example
Define a damped harmonic oscillator + noise
def damped_harmonic_oscillator(omega,beta,noise,T0):
= 0.01;
dt = int(np.ceil(T0/dt))
T
= np.arange(0,T)*dt
t = np.zeros([T,1]) # Set up forcing,
F int(0.25*T)] = 1; # ... give it a kick 25% of way into simulation.
F[= np.zeros([T,1])
x = np.zeros([T,1])
y
0]=0
x[0]=0
y[
for i in range(0,T-1):
+1] = x[i] + dt*(y[i]);
x[i+1] = y[i] + dt*(-omega**2*x[i] - 2*beta*y[i] + F[i] + noise*np.random.randn());
y[i
return x,t
Simulate the model
= 1;
f = 2*np.pi*f;
omega = 1;
beta = 0;
noise = 10
T0 = damped_harmonic_oscillator(omega,beta,noise,T0);
x,t ; plt.plot(t,x)