0% found this document useful (0 votes)
43 views8 pages

ENGR387 Mini Project Code Kayra 67425975

This document contains Python code that models and plots the response of an underdamped, critically damped, and overdamped spring-mass system. It defines system parameters like mass, stiffness, damping ratio, and initial conditions. For each damping case, it calculates the system response, plots the response over time, and labels the appropriate case.

Uploaded by

capturemrah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views8 pages

ENGR387 Mini Project Code Kayra 67425975

This document contains Python code that models and plots the response of an underdamped, critically damped, and overdamped spring-mass system. It defines system parameters like mass, stiffness, damping ratio, and initial conditions. For each damping case, it calculates the system response, plots the response over time, and labels the appropriate case.

Uploaded by

capturemrah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

11/13/21, 8:13 AM ENGR387_Mini_Project_Python_Code_67425975

In [1]:
import numpy as np # Grab all of the NumPy functions with nicknam

%matplotlib inline

# Import the plotting functions


import matplotlib.pyplot as plt

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)

# Change the axis units to serif

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)

# Define the X and Y axis labels


plt.xlabel('Time (s)', fontsize=22, labelpad=5)
plt.ylabel('Position (m)', fontsize=22, labelpad=10)

# Plot response
plt.plot(t, x, linewidth=2, linestyle = '-', label=r'Response', color = "t

# plot the decay envelope (only for underdamped system)


if z < 1:
X_0 = np.sqrt(x0[0]**2 + ((z*wn*x0[0] + x0[1])/wd)**2)
decay_env = X_0 * np.exp(-z*wn*t)

plt.plot(t, decay_env, linewidth=1.0, linestyle = '--', color = "tab:b


plt.plot(t, -decay_env, linewidth=1.0, linestyle = '--', color = "tab

#plt.yticks([-1.5, -1, -0.5, 0, 0.5, 1, 1.5], ['', r'$-x_0$', '', '0', ''

# # Create the legend, then fix the fontsize


leg = plt.legend(loc='upper right', fancybox=True)
ltext = leg.get_texts()
plt.setp(ltext,fontsize=14)

# Adjust the page layout filling the page using the new tight_layout comma
plt.tight_layout(pad = 0.5)

#plt.savefig('FreeVibrationWithDamping.pdf')

fig.set_size_inches(9, 6) # Resize the figure for better display in the no

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)

c = 1 # Define a desired damping ratio


z = c/(2*wn*m) # calculate the damping coeff. to create i

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)

# Set up simulation parameters


t = np.linspace(0, 10, 501) # Time for simulation, 0-5s with 501 poin

# Define the initial conditions x(0) m and x_dot(0) m/s


x0 = np.array([0.1, -10])

# Determine the nature of the response based on the value of zeta


if z < 1:
print('Run case 1: Underdamped system')
elif z == 1:
print('Run case 2: Critically damped system')
elif z > 1:
print('Run case 3: Overdamped system')

Run case 1: Underdamped system


Case 1: Underdamped System
In [4]:
# Define x(t)
x = np.exp(-z*wn*t)*(x0[0]*np.cos(wd*t) + (z*wn*x0[0] + x0[1])/wd * np.sin(wd*
plot(x)

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)

c = 200 # Define a desired damping ratio


z = c/(2*wn*m) # calculate the damping coeff. to create i

if z < 1: # Only for underdamped system


wd = wn*np.sqrt(1 - z**2) # Damped natural frequency (rad/s)

# Set up simulation parameters

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

# Define the initial conditions x(0) m and x_dot(0) m/s


x0 = np.array([0.1, -10])

# Determine the nature of the response based on the value of zeta


if z < 1:
print('Run case 1: Underdamped system')
elif z == 1:
print('Run case 2: Critically damped system')
elif z > 1:
print('Run case 3: Overdamped system')

Run case 2: Critically damped system


Case 2: Critically Damped System
In [6]:
# Define x(t)
x = (x0[0] + (x0[1] + wn*x0[0])*t)*np.exp(-wn*t)
plot(x)

Case 2 : Critically Damped System while velocity is less than 1


In [7]:
# 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)

c = 200 # Define a desired damping ratio


z = c/(2*wn*m) # calculate the damping coeff. to create i

if z < 1: # Only for underdamped system


wd = wn*np.sqrt(1 - z**2) # Damped natural frequency (rad/s)

# Set up simulation parameters


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)

c = 1000 # Define a desired damping ratio


z = c/(2*wn*m) # calculate the damping coeff. to create i

if z < 1: # Only for underdamped system


wd = wn*np.sqrt(1 - z**2) # Damped natural frequency (rad/s)

# Set up simulation parameters


t = np.linspace(0, 10, 501) # Time for simulation, 0-5s with 501 poin

# Define the initial conditions x(0) m and x_dot(0) m/s


x0 = np.array([0.1, -10.0])
# Determine the nature of the response based on the value of zeta
if z < 1:
print('Run case 1: Underdamped system')
elif z == 1:
print('Run case 2: Critically damped system')
elif z > 1:
print('Run case 3: Overdamped system')

Run case 3: Overdamped system


Case 3: Overdamped System
In [9]:
denom = 2*wn*np.sqrt(z**2 - 1)
C1 = (x0[0]*wn*(z + np.sqrt(z**2 - 1)) + x0[1])/denom

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)

# Change the axis units to serif

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)

# Define the X and Y axis labels


plt.xlabel('Time (s)', fontsize=22, labelpad=5)
plt.ylabel('Position (m)', fontsize=22, labelpad=10)

# 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', ''

# # Create the legend, then fix the fontsize


leg = plt.legend(loc='upper right', fancybox=True)
ltext = leg.get_texts()
plt.setp(ltext,fontsize=14)

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')

fig.set_size_inches(9, 6) # Resize the figure for better display in the no

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)

#Values for case 1


c1 = 1 # Define a desired damping ratio
z1 = c1/(2*wn*m) # calculate the damping coeff. to create i

#Values for case 2


c2 = 200 # N/m (Selected to give an undamped natrua
z2 = c2/(2*wn*m) # Natural Frequency (rad/s)

#Values for case 3


c3 = 400 # N/m (Selected to give an undamped natrua
z3 = c3/(2*wn*m) # Natural Frequency (rad/s)

if z1 < 1: # Only for underdamped system, in other wo


wd = wn*np.sqrt(1 - z1**2) # Damped 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

responses = [x_1,x_2, x_3]

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

PART D: plotting the graph


In [12]:
# Define the System Parameters
#Common Parameters
m = 4.0 # kg
k = 2500 # N/m (Selected to give an undamped natr
wn = np.sqrt(k/m) # Natural Frequency (rad/s)

#Values for case 1


c1 = 1 # Define a desired damping ratio
z1 = c1/(2*wn*m) # calculate the damping coeff. to create

#Values for case 1 but the damping constant is halved


cNew1 = (1 / 2) # Define a desired damping ratio
zNew1 = cNew1/(2*wn*m) # calculate the damping coeff. to create

if z1 < 1: # Only for underdamped system


wd1 = wn*np.sqrt(1 - z1**2) # Damped natural frequency (rad/s)

if zNew1 < 1: # Only for underdamped system


wdNew1 = wn*np.sqrt(1 - zNew1**2) # Damped natural frequency (rad/s)

#Define x(t) for case 1


x1 = np.exp(-z1*wn*t)*(x0[0]*np.cos(wd1*t) + (z1*wn*x0[0] + x0[1])/wd1 * np.si

#Define x(t) for case 1 but for halved damping constant


xNew1 = np.exp(-zNew1*wn*t)*(x0[0]*np.cos(wdNew1*t) + (zNew1*wn*x0[0] + x0[1]

#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

You might also like