-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsolve2dof.py
More file actions
79 lines (61 loc) · 1.33 KB
/
solve2dof.py
File metadata and controls
79 lines (61 loc) · 1.33 KB
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
import numpy as np
from math import sin
from scipy.linalg import eigh
from numpy.linalg import inv
from matplotlib import pyplot as plt
def F(t):
F = np.zeros(4)
F[1] = F0 * np.sin(omega*t)
return F
def G(y,t):
return A_inv.dot( F(t) - B.dot(y) )
def RK4_step(y, t, dt):
k1 = G(y,t)
k2 = G(y+0.5*k1*dt, t+0.5*dt)
k3 = G(y+0.5*k2*dt, t+0.5*dt)
k4 = G(y+k3*dt, t+dt)
return dt * (k1 + 2*k2 + 2*k3 + k4) / 6
# setup the parameters
m1, m2 = 2.0, 1.0
k1, k2 = 3.0, 2.0
delta_t = 0.1
time = np.arange(0.0, 80.0, delta_t)
F0 = 0.0
omega = 1.0
y = np.array([0, 0, 0, 1])
dof = 2
# setup matrices
K = np.array([[k1+k2, -k2],[-k2, k2]])
M = np.array([[m1, 0],[0, m2]])
I = np.identity(dof)
A = np.zeros((2*dof,2*dof))
B = np.zeros((2*dof,2*dof))
A[0:2,0:2] = M
A[2:4,2:4] = I
B[0:2,2:4] = K
B[2:4,0:2] = -I
# find natural frequencies and mode shapes
evals, evecs = eigh(K,M)
frequencies = np.sqrt(evals)
print frequencies
print evecs
A_inv = inv(A)
force = []
X1 = []
X2 = []
# numerically integrate the EOMs
for t in time:
y = y + RK4_step(y, t, delta_t)
X1.append(y[2])
X2.append(y[3])
force.append(F(t)[1])
# plot results
plt.plot(time,X1)
plt.plot(time,X2)
plt.plot(time,force)
plt.grid(True)
plt.xlabel('time (s)')
plt.ylabel('displacement (m)')
plt.title('Response Curves')
plt.legend(['X1', 'X2', 'Force'], loc='lower right')
plt.show()