# Import Libraries:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FFMpegWriter
from matplotlib.patches import Circle
!apt update -y
!apt install ffmpeg -y
# Parameters
g = 9.8 # m/s2
m1 = 1 # kg
m2 = 2 # kg
L = 1 # m
k = 10 # N/m
h = 1.5 #m
dt = 0.001
t_vec = np.arange(0,10,dt)
# Zero Vectors
x_vec = np.zeros(len(t_vec))
dx_vec = np.zeros(len(t_vec))
ddx_vec = np.zeros(len(t_vec))
theta_vec = np.zeros(len(t_vec))
dtheta_vec = np.zeros(len(t_vec))
ddtheta_vec = np.zeros(len(t_vec))
y = np.zeros(len(t_vec))
# Initial Conditions
x_vec[0] = 0 # x position
theta_vec[0] = 1 # y position
dx_vec[0] = 0 # x velocity
dtheta_vec[0] = 0 # y velocity
# Eulers Method
for i in range(1, len(t_vec)):
theta_vec[i] = theta_vec[i-1] + dtheta_vec[i-1]*dt
ddtheta_vec[i-1] = -((g*np.sin(theta_vec[i-1]))/(L)) -
(k*(np.sqrt((L*np.sin(theta_vec[i-1]) - x_vec[i-1])**2
+ (h - L*np.cos(theta_vec[i-1]))**2) - L)*((L*np.sin(theta_vec[i-1])
- x_vec[i-1])*(L*np.cos(theta_vec[i-1]))
+ (h - L*np.cos(theta_vec[i-1]))*(L*np.sin(theta_vec[i-1]))))/
(m1*L**2*np.sqrt((L*np.sin(theta_vec[i-1]) - x_vec[i-1])**2
+ (h - L*np.cos(theta_vec[i-1]))**2))
dtheta_vec[i] = dtheta_vec[i-1] + ddtheta_vec[i-1]*dt
x_vec[i] = x_vec[i-1] + dx_vec[i-1]*dt
ddx_vec[i-1] = (k*(np.sqrt((L*np.sin(theta_vec[i-1])
- x_vec[i-1])**2 + (h - L*np.cos(theta_vec[i-1]))**2) - L)*(L*np.sin(theta_vec[i-1])
- x_vec[i-1]))/(m2*np.sqrt((L*np.sin(theta_vec[i-1]) - x_vec[i-1])**2
+ (h - L*np.cos(theta_vec[i-1]))**2))
dx_vec[i] = dx_vec[i-1] + ddx_vec[i-1]*dt
y[i] = -1.5
# X Position Plot
plt.plot(t_vec, x_vec)
plt.title('MOD HW 2B: X Position')
plt.show()
# Theta Plot
plt.plot(t_vec, theta_vec)
plt.title('MOD HW 2B: Theta')
plt.show()
# Initialize Figure and Axes
fig, ax = plt.subplots()
# Initialize Empty Plot
p, = ax.plot([], [], color='black')
q, = ax.plot([], [], color='black')
ax.axis('equal')
ax.set_xlim([-3, 3])
ax.set_ylim([-3, 3])
# Labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('MOD Simulation 2C:') # Plot Title
video_title = "MOD Simulation 2C" # name of the mp4
# Initialize Patch
c = Circle((0, -0.65), radius=0.2, color='red')
ax.add_patch(c) # Add the patch to the axes
b = Circle((0, -1.5), radius=0.2, color='blue')
ax.add_patch(b)
# Setup Animation Writer
FPS = 20 # Frames per second for the video
sample_rate = int(1 / (dt * FPS)) # Rate at which we sample the simulation to visualize
dpi = 300 # Quality of the Video
writerObj = FFMpegWriter(fps=FPS) # Video Object
# Cartesian location of pendulum
simulation_size = len(t_vec) # We use this alot, so lets store it.
x_pendulum_arm = np.zeros(simulation_size)
y_pendulum_arm = np.zeros(simulation_size)
origin = (0, 0) # Pin Joint (X, Y) Location
# Trajectories of pendulum
for i in range(0, simulation_size):
x_pendulum_arm[i] = L * np.sin(theta_vec[i]) + origin[0]
y_pendulum_arm[i] = origin[1] - L * np.cos(theta_vec[i])
# Plot and Create Animation
with writerObj.saving(fig, video_title+".mp4", dpi):
# We want to create video that represents the simulation
# So we need to sample only a few of the frames:
for i in range(0, simulation_size, sample_rate):
# Update Pendulum Arm
x_data_points = [origin[0], x_pendulum_arm[i]]
y_data_points = [origin[1], y_pendulum_arm[i]]
x2_data_points = [x_vec[i], x_pendulum_arm[i]]
y2_data_points = [y[i], y_pendulum_arm[i]]
p.set_data(x_data_points, y_data_points) # Update plot
q.set_data(x2_data_points, y2_data_points)
# Update Pendulum Patch:
patch_center = x_pendulum_arm[i], y_pendulum_arm[i]
c.center = patch_center
m_center = x_vec[i], y[i]
b.center = m_center
fig.canvas.draw()
# Grab and Save Frame:
writerObj.grab_frame()
from IPython.display import Video
Video("/work/MOD Simulation 2C.mp4", embed=True, width=640, height=480)
# You can find the video's path by right clicking on it in Notebooks & Files
# and selecting "Copy path to clipboard"