-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMAIN.py
76 lines (55 loc) · 1.51 KB
/
MAIN.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
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# This code will solve for the example at
# https://www.crcpress.com/downloads/K11527/MATLAB%20Resources.pdf
# For a Driven Mass Spring Damper system, driven with a sine wave
# assume a free mass spring damper system m*a+b*v+k*x=0
# a=acceleration, v= velocity, x=displacement
# Second order response parameters are:
# omega_n=sqrt(k/m) Natural Frequency
# zeta=b/(2*sqrt(k*m)) Damping ratio
# Define SI UNITS
meters = 1
seconds = 1
kg = 1
newtons = 1
centimeters = meters/100
# Define Model Parameters as per the PDF example
k = 100*newtons/meters
m = 1*kg
zeta = 0.1
omega_n = np.sqrt(k/m)
c = zeta*2*np.sqrt(m*k)
# Define input signal
omega = 2*omega_n
f_input = omega/(2*3.1415)
T_input = 1/f_input * seconds
fo = 100*newtons # Input signal amplitude
# Print important variables to the command line
print("resonance frequency is ", omega_n )
print("input frequency is ", omega )
# Define State Sapce model
A = [[0, 1], [-k/m, -c/m]]
B = [[0], [1]]
C = [[1, 0]]
D = 0
sys1 = signal.StateSpace(A, B, C, D)
# Define input and time matrix
t_start = 0
t_end = 5*seconds
N_samples = 5*50
t = np.linspace(0,t_end,N_samples)
# DEFINE INPUT
u = np.sin(omega*t)
u = fo*u
initial_conditions=[0.02, 0 ]
response = signal.lsim(sys1, u, t, initial_conditions) # Get response to time domain input u
t = response[0]
x = response[1]
# Plot the System response
plt.figure(1)
plt.plot(t, x)
plt.xlabel("time [s]")
plt.ylabel("x(t) [m]")
plt.show()