ENGR387 Mini Project Code Kayra 67425975
ENGR387 Mini Project Code Kayra 67425975
In [1]:
import numpy as np # Grab all of the NumPy functions with nicknam
%matplotlib inline
In [2]:
# Plot the system response:
def plot(x):
# Set the plot size - 3x2 aspect ratio is best
fig = plt.figure(figsize=(6, 4))
ax = plt.gca()
plt.subplots_adjust(bottom=0.17, left=0.17, top=0.96, right=0.96)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Turn on the plot grid and set appropriate linestyle and color
ax.grid(True,linestyle=':',color='0.75')
ax.set_axisbelow(True)
# Plot response
plt.plot(t, x, linewidth=2, linestyle = '-', label=r'Response', color = "t
#plt.yticks([-1.5, -1, -0.5, 0, 0.5, 1, 1.5], ['', r'$-x_0$', '', '0', ''
# Adjust the page layout filling the page using the new tight_layout comma
plt.tight_layout(pad = 0.5)
#plt.savefig('FreeVibrationWithDamping.pdf')
In [3]:
# Define the System Parameters
m = 4.0 # kg
k = 2500 # N/m (Selected to give an undamped natrua
wn = np.sqrt(k/m) # Natural Frequency (rad/s)
localhost:8888/lab/tree/Downloads/ENGR387_Mini_Project_Python_Code_67425975.ipynb 1/8
11/13/21, 8:13 AM ENGR387_Mini_Project_Python_Code_67425975
if z < 1: # Only for underdamped system
wd = wn*np.sqrt(1 - z**2) # Damped natural frequency (rad/s)
In [5]:
# Define the System Parameters
m = 4.0 # kg
k = 2500 # N/m (Selected to give an undamped natrua
wn = np.sqrt(k/m) # Natural Frequency (rad/s)
localhost:8888/lab/tree/Downloads/ENGR387_Mini_Project_Python_Code_67425975.ipynb 2/8
11/13/21, 8:13 AM ENGR387_Mini_Project_Python_Code_67425975
t = np.linspace(0, 10, 501) # Time for simulation, 0-5s with 501 poin
localhost:8888/lab/tree/Downloads/ENGR387_Mini_Project_Python_Code_67425975.ipynb 3/8
11/13/21, 8:13 AM ENGR387_Mini_Project_Python_Code_67425975
# Define the initial conditions x(0) m and x_dot(0) m/s
x0 = np.array([0.1, 0])
# Define x(t)
x = (x0[0] + (x0[1] + wn*x0[0])*t)*np.exp(-wn*t)
plot(x)
In [8]:
# Define the System Parameters
m = 4.0 # kg
k = 2500 # N/m (Selected to give an undamped natrua
wn = np.sqrt(k/m) # Natural Frequency (rad/s)
localhost:8888/lab/tree/Downloads/ENGR387_Mini_Project_Python_Code_67425975.ipynb 4/8
11/13/21, 8:13 AM ENGR387_Mini_Project_Python_Code_67425975
C2 = (-x0[0]*wn*(z - np.sqrt(z**2 - 1)) - x0[1])/denom
x = C1*np.exp((-z + np.sqrt(z**2 - 1))*wn*t) + C2*np.exp((-z - np.sqrt(z**2 -
plot(x)
In [10]:
# plot multiple curves:
# Pass the responses as a list [x_1, x_2, ...]
def plot_multi(x):
# Set the plot size - 3x2 aspect ratio is best
fig = plt.figure(figsize=(6, 4))
ax = plt.gca()
plt.subplots_adjust(bottom=0.17, left=0.17, top=0.96, right=0.96)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Turn on the plot grid and set appropriate linestyle and color
ax.grid(True,linestyle=':',color='0.75')
ax.set_axisbelow(True)
# Plot response
colors = ["tab:orange", "tab:green", "tab:red"]
for i, j in enumerate(x):
plt.plot(t, j, linewidth=2, linestyle = '-', label=r'Response ' + str
#plt.yticks([-1.5, -1, -0.5, 0, 0.5, 1, 1.5], ['', r'$-x_0$', '', '0', ''
localhost:8888/lab/tree/Downloads/ENGR387_Mini_Project_Python_Code_67425975.ipynb 5/8
11/13/21, 8:13 AM ENGR387_Mini_Project_Python_Code_67425975
# Adjust the page layout filling the page using the new tight_layout comma
plt.tight_layout(pad = 0.5)
#plt.savefig('FreeVibrationWithDamping.pdf')
In [11]:
# Define the System Parameters
#Common Parameters
m = 4.0 # kg
k = 2500 # N/m (Selected to give an undamped natrua
wn = np.sqrt(k/m) # Natural Frequency (rad/s)
# 1: Underdamped
x_1 = np.exp(-z1*wn*t)*(x0[0]*np.cos(wd*t) + (z1*wn*x0[0] + x0[1])/wd * np.sin
# 2: Critically damped
x_2 = (x0[0] + (x0[1] + wn*x0[0])*t)*np.exp(-wn*t)
# 3: Overdamped
denom = 2*wn*np.sqrt(z3**2 - 1)
C1 = (x0[0]*wn*(z3 + np.sqrt(z3**2 - 1)) + x0[1])/denom
C2 = (-x0[0]*wn*(z3 - np.sqrt(z3**2 - 1)) - x0[1])/denom
x_3 = C1*np.exp((-z3 + np.sqrt(z3**2 - 1))*wn*t) + C2*np.exp((-z3 - np.sqrt(z3
plot_multi(responses)
localhost:8888/lab/tree/Downloads/ENGR387_Mini_Project_Python_Code_67425975.ipynb 6/8
11/13/21, 8:13 AM ENGR387_Mini_Project_Python_Code_67425975
#Responses
responsesNew = [x1, xNew1]
plot_multi(responsesNew)
localhost:8888/lab/tree/Downloads/ENGR387_Mini_Project_Python_Code_67425975.ipynb 7/8
11/13/21, 8:13 AM ENGR387_Mini_Project_Python_Code_67425975
In [ ]:
localhost:8888/lab/tree/Downloads/ENGR387_Mini_Project_Python_Code_67425975.ipynb 8/8