-
Notifications
You must be signed in to change notification settings - Fork 85
/
oscillator.py
101 lines (76 loc) · 1.76 KB
/
oscillator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'''
This is based on https://medium.com/analytics-vidhya/understanding-oscillators-python-2813ec38781d
Implementation of a forced damped harmonic oscillator.
'''
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
t = np.linspace(0,15,10000)
freq = 4
omega = 2 * np.pi * freq
y = [0,2] #y[0]=x and y[1]=v
def harmonic(t,y):
solution = [y[1],-omega*omega*y[0]]
return solution
sho = solve_ivp(harmonic, [0,1000], y0 = y, t_eval = t)
plt.plot(t,sho.y[0])
plt.ylabel("Position")
plt.xlabel("Time")
plt.title('SHO', fontsize = 20)
plt.show()
t = np.linspace(0,15,1000)
y = [1,1]
gamma = 4
freq = 4
omega = 2 * np.pi * freq
def sho(t,y):
solution = (y[1],(-gamma*y[1]-omega * omega *y[0]))
return solution
solution = solve_ivp(sho, [0,1000], y0 = y, t_eval = t)
plt.plot(t,solution.y[0])
plt.ylabel("Position")
plt.xlabel("Time")
plt.title('Damped Oscillator', fontsize = 20)
plt.show()
# example of numerical integration of 1D driven oscilla
from math import sin, sqrt
import matplotlib.pyplot as plt
# constants to give resonant frequency of about 1 /s
k = 2.0
m = 0.5
g = 0.05 # light damping
F = 1.0
w0 = sqrt(k/m)
w = 0.99 * w0
tmax = 150
dt = 0.01
t = 0
x = 0
v = 0
a = 0
# equation of motion
# m d2x/dt2 - 2 g dx/dt + kx = F sin wt
print('w0 = %f\n'%w0)
def force(x, v, t):
return -g*v - k*x + F*sin(w*t)
X = [0.]
T = [0.]
FF = [1.]
while t<tmax:
f = force(x,v, t)
a = f / m
x = x + v * dt + 0.5*a *dt*dt
v = v + a * dt
FF.append(F*sin(w*t))
t = t + dt
X.append(x)
T.append(t)
plt.figure()
plt.plot(T,X)
plt.plot(T, FF)
plt.title('amplitude response of lightly damped driven oscillator')
plt.legend(('displacement','force'))
plt.xlabel('time')
plt.show()