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

Dynamics Homework (2)

Uploaded by

matiandylan
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)
8 views8 pages

Dynamics Homework (2)

Uploaded by

matiandylan
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

-

Hompwork 3)

1) M
,
K
,
lo ,

a) two is = x
o

b) w =
Guz
Epo =
wir
changes
2 ~

Tr
> /
= wx r

Uplo =
Far + rur r =
Gu
-

+roo Vo =
-On ,

>
-

aplo =
j +
ro +
rbG + ro + rU
26
-

I Ur + + roo-r n
=
( -vGY) r
+ (2 + r) no
m
pub un
4
FBD

Tri Fs

-
K(r -
1) =
(i -

vGYm (1)

i =
ro -Cr-ho
To
o =
(zio + r)m (2)

=
-Li
import numpy as np
import matplotlib.pyplot as plt

m = 1.0
k = 2.0
l_initial = 0.1
theta_initial = 0.0
theta_dot_initial = np.pi / 2
r_initial = l_initial
r_dot_initial = 0.0

# Time parameters
total_time = 10.0
dt = 0.01

# Function to calculate derivatives


def derivatives(theta, theta_dot, r, r_dot):
theta_double_dot = -(2 * r_dot * theta_dot) / r
r_double_dot = r * theta_dot**2 - (k / m) * (r - l_initial)
return theta_dot, theta_double_dot, r_dot, r_double_dot

# Euler's method
num_steps = int(total_time / dt) + 1
time_values = np.linspace(0, total_time, num_steps)

theta_values = np.zeros(num_steps)
theta_dot_values = np.zeros(num_steps)
r_values = np.zeros(num_steps)
r_dot_values = np.zeros(num_steps)

# Initial conditions
theta_values[0] = theta_initial
theta_dot_values[0] = theta_dot_initial
r_values[0] = r_initial
r_dot_values[0] = r_dot_initial

# Euler's method integration


for i in range(1, num_steps):
theta_dot, theta_double_dot, r_dot, r_double_dot = derivatives(
theta_values[i - 1], theta_dot_values[i - 1], r_values[i - 1], r_dot_values[i - 1]
)

theta_values[i] = theta_values[i - 1] + dt * theta_dot


theta_dot_values[i] = theta_dot_values[i - 1] + dt * theta_double_dot
r_values[i] = r_values[i - 1] + dt * r_dot
r_dot_values[i] = r_dot_values[i - 1] + dt * r_double_dot

# Plotting
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
fig.suptitle('Numerical Simulation of Planar Pendulum on a Spring')

# Subplot 1: Theta vs. time


ax1.plot(time_values, theta_values, label='Theta')
ax1.set_xlabel('Time (s)')
ax1.set_ylabel('Theta (rad)')
ax1.legend()

# Subplot 2: Radius vs. time


ax2.plot(time_values, r_values, label='Radius')
ax2.set_xlabel('Time (s)')
ax2.set_ylabel('Radius (m)')
ax2.legend()

plt.show()

# Trajectory plot
fig_trajectory, ax_trajectory = plt.subplots(figsize=(8, 8))
ax_trajectory.set_title('Trajectory of the Pendulum')
ax_trajectory.set_xlabel('X (m)')
ax_trajectory.set_ylabel('Y (m)')

x_values = r_values * np.sin(theta_values)


y_values = r_values * np.cos(theta_values)
ax_trajectory.plot(x_values, y_values, label='Trajectory')

ax_trajectory.legend()
plt.show()

You might also like