Bead on Paraboloid: HW 1.3
Import Stuff
# Install ffmpeg (allows us to create videos)
!apt update -y
!apt install ffmpeg -y
import numpy as np
import matplotlib.pyplot as plt
import sympy
from matplotlib.animation import FFMpegWriter
from matplotlib.patches import Circle, Rectangle
from mpl_toolkits import mplot3d
Simulation of Bead on Paraboloid
# Parameters
m = 1.0 # mass of bead (kg)
g = 9.8 # gravity
# Simulate System:
x0 = 1, 0, 0, 1 # x, dx, y, dy
dt = 0.001
sim_time = 10
time = np.arange(0, sim_time, dt)
sim_length = len(time)
# Initialize Arrays:
y_vec = np.zeros(sim_length)
dy_vec = np.zeros(sim_length)
x_vec = np.zeros(sim_length)
dx_vec = np.zeros(sim_length)
z_vec = np.zeros(sim_length)
# Evaluate Initial Conditions:
x_vec[0] = x0[0]
dx_vec[0] = x0[1]
y_vec[0] = x0[2]
dy_vec[0] = x0[3]
z_vec[0] = x_vec[0]**2 + y_vec[0]**2
# Euler Integration:
for i in range(1, sim_length):
# Evaluate Dynamics:
ddx = -(g*x_vec[i-1] - 2*x_vec[i-1]*dx_vec[i-1]**2 - 2*x_vec[i-1]*dy_vec[i-1]**2)/(2*x_vec[i-1]**2+2*y_vec[i-1]**2)
ddy = -(g*y_vec[i-1] - 2*y_vec[i-1]*dx_vec[i-1]**2 - 2*y_vec[i-1]*dy_vec[i-1]**2)/(2*x_vec[i-1]**2+2*y_vec[i-1]**2)
# Euler Step Integration:
x_vec[i] = x_vec[i-1] + dx_vec[i-1] * dt
dx_vec[i] = dx_vec[i-1] + ddx * dt
y_vec[i] = y_vec[i-1] + dy_vec[i-1] * dt
dy_vec[i] = dy_vec[i-1] + ddy * dt
z_vec[i] = x_vec[i]**2 + y_vec[i]**2
plt.plot(time,x_vec)
plt.title("X versus time")
plt.show()
plt.plot(time,y_vec)
plt.title("Y versus time")
plt.show()
# fig = plt.figure()
# ax = plt.axes(projection='3d')
# ax.plot3D(x_vec, y_vec, z_vec)
Animation
# Create Animation:
# Setup Figure: Initialize Figure / Axe Handles
fig, ax = plt.subplots()
lb, ub = -1, 1
ax.axis('equal')
ax.set_xlim([lb, ub])
ax.set_xlabel('X') # X Label
ax.set_ylabel('Y') # Y Label
ax.set_title('Bead on Paraboloid Simulation:')
video_title = "simulation"
# Setup Animation Writer:
FPS = 20
sample_rate = int(1 / (dt * FPS))
dpi = 300
writerObj = FFMpegWriter(fps=FPS)
# Initialize Patch: Pendulum 1 and 2
bead = Circle((0, 0), radius=0.1, color='cornflowerblue', zorder=10)
ax.add_patch(bead)
# Plot and Create Animation:
with writerObj.saving(fig, video_title+".mp4", dpi):
for i in range(0, sim_length, sample_rate):
# Draw Pendulum Arm:
x_pendulum_arm = x_vec[i]
y_pendulum_arm = y_vec[i]
# Update Pendulum Patches:
pendulum_center = x_vec[i], y_vec[i]
bead.center = pendulum_center
# Update Drawing:
fig.canvas.draw()
# Grab and Save Frame:
writerObj.grab_frame()