0% found this document useful (0 votes)
7 views

problem_1_8

Uploaded by

ultapappu2
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)
7 views

problem_1_8

Uploaded by

ultapappu2
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/ 6

problem_1_8

November 7, 2024

[6]: import numpy as np


from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from IPython.display import Markdown

# Function to display text in Markdown format


def dsp_M(*args):
text = ' '.join(map(str, args))
display(Markdown(text)) # Use display to render Markdown in Jupyter

# External force function


def F_b(t):
return np.where(t > 4, 0, 400 * np.sin(2 * np.pi * t / 4))

# The system of ODEs


def crane_ode(t, y, m_b, m, l, g):
y_pos, y_vel, phi, phi_vel = y

# Equations of motion
denom = m_b + m - m * np.cos(phi)**2
y_acc = (F_b(t) + m * g * np.sin(phi) * np.cos(phi) + m * l * phi_vel**2 *␣
↪np.sin(phi)) / denom

phi_acc = -(y_acc * np.cos(phi) + g * np.sin(phi)) / l

return [y_vel, y_acc, phi_vel, phi_acc]

# Customize the y-axis tick labels to remove the negative sign


def custom_formatter(x, pos):
return '{:.4f}'.format(-x)

# Helper function for plotting


def plot_with_grid(ax, x, y, title, xlabel, ylabel):
ax.plot(x, y)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)

1
ax.grid(True)

def main():
# Parameters
m_b, m, l, g = 20, 100, 2, 9.81 # mass of bridge [kg], mass of payload␣
↪[kg], rope length [m], gravity [m/s^2]

t_f, n_t = 8, 100 # final time [s], number of time points

# Display parameters and initial conditions


dsp_M(f"#### Parameters\n",
f"Mass of the bridge: $m_b = {m_b}\ [kg]$<br>",
f"Mass of the payload: $m = {m}\ [kg]$<br>",
f"Length of the hoisting rope: $l = {l}\ [m]$<br>",
f"Acceleration due to gravity: $g = {g}\ [m/s^2]$<br>",
f"Final time: $t_f = {t_f}\ [s]$<br>",
f"Number of time points: $n_t = {n_t}$")

y0 = [0, 0, 0, 0] # Initial conditions

dsp_M("#### Initial Conditions\n",


f"Position of the bridge: $y(0) = {y0[0]}\ [m]$<br>",
f"Velocity of the bridge: $\dot{{y}}(0) = {y0[1]}\ [m/s]$<br>",
f"Sway angle: $\phi(0) = {y0[2]}\ [rad]$<br>",
f"Angular velocity: $\dot{{\phi}}(0) = {y0[3]}\ [1/s]$")

# Time interval
t_span = (0, t_f)
t_eval = np.linspace(0, t_f, n_t)

# Calculate F_b(t) values for plotting


F_b_values = F_b(t_eval)

# Plot external force F_b(t)


fig, ax = plt.subplots(figsize=(5, 3))
plot_with_grid(ax, t_eval, F_b_values, r'External Force ($F_b(t)$) vs␣
↪Time', 'Time [s]', r'$F_b(t)$ [N]')

plt.tight_layout()
plt.show()

# Solve the ODEs


solution = solve_ivp(crane_ode, t_span, y0, t_eval=t_eval, method='RK45',␣
↪args=(m_b, m, l, g))

# Extract results
y_pos, y_vel, phi, phi_vel = solution.y

# Calculate the trajectory of the payload

2
x_m = -l * np.cos(phi)
y_m = y_pos + l * np.sin(phi)

# Calculate the velocity magnitude of the payload


v_m = np.sqrt(y_vel**2 + 2 * y_vel * l * phi_vel * np.cos(phi) + (l *␣
↪phi_vel)**2)

dsp_M("### Results")

# Plot bridge position and velocity


fig, axs = plt.subplots(1, 2, figsize=(12, 3))
plot_with_grid(axs[0], solution.t, y_pos, 'Bridge Position (y) vs Time',␣
↪'Time [s]', 'y [m]')

plot_with_grid(axs[1], solution.t, y_vel, r'Bridge Velocity ($\dot{y}$) vs␣


↪Time', 'Time [s]', r'$\dot{y}$ [m/s]')

plt.tight_layout()
plt.show()

# Plot sway angle and angular velocity


fig, axs = plt.subplots(1, 2, figsize=(12, 3))
plot_with_grid(axs[0], solution.t, phi, r'Sway Angle ($\phi$) vs Time',␣
↪'Time [s]', r'$\phi$ [rad]')

plot_with_grid(axs[1], solution.t, phi_vel, r'Angular Velocity␣


↪$(\dot{\phi})$ vs Time', 'Time [s]', r'$\dot{\phi}$ [rad/s]')

plt.tight_layout()
plt.show()

# Plot trajectory and velocity magnitude of the payload


fig, axs = plt.subplots(1, 2, figsize=(12, 3))
axs[0].plot(y_m, x_m)
axs[0].set_title(r'Payload Trajectory ($y_m$, $x_m$)')
axs[0].set_xlabel(r'$y_m$ [m]')
axs[0].set_ylabel(r'$x_m$ [m]')
axs[0].grid(True)
axs[0].yaxis.set_major_formatter(ticker.FuncFormatter(custom_formatter))

plot_with_grid(axs[1], solution.t, v_m, 'Velocity of Payload ($v_m$) vs␣


↪Time', 'Time [s]', r'$v_m$ [m/s]')
plt.tight_layout()
plt.show()

if __name__ == "__main__":
main()

Parameters Mass of the bridge: 𝑚𝑏 = 20 [𝑘𝑔] Mass of the payload: 𝑚 = 100 [𝑘𝑔] Length of the
hoisting rope: 𝑙 = 2 [𝑚] Acceleration due to gravity: 𝑔 = 9.81 [𝑚/𝑠2 ] Final time: 𝑡𝑓 = 8 [𝑠] Number

3
of time points: 𝑛𝑡 = 100

Initial Conditions Position of the bridge: 𝑦(0) = 0 [𝑚] Velocity of the bridge: 𝑦(0)
̇ = 0 [𝑚/𝑠]
̇
Sway angle: 𝜙(0) = 0 [𝑟𝑎𝑑] Angular velocity: 𝜙(0) = 0 [1/𝑠]

0.0.1 Results

4
[2]:

<IPython.core.display.Markdown object>
<IPython.core.display.Markdown object>

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 123
120 plt.show()
122 if __name__ == "__main__":
--> 123 main()

Cell In[2], line 91, in main()


88 y_m = y_pos + l * np.sin(phi)
90 # Calculate the velocity magnitude of the payload
---> 91 v_m = np.sqrt(y_vel*2 + 2 * y_vel * l * phi_vel * np.cos(phi) + l2 *␣
↪phi_vel*2)

93 dsp_M("### Results")

5
95 # Plot bridge position and velocity

NameError: name 'l2' is not defined

[ ]:

You might also like