# Start writin#Parabolic Constrained Harmonic Motion
#install ffmpeg
!apt update -y
!apt install ffmpeg -y
# Import Libraries:
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from matplotlib.animation import FFMpegWriter
from matplotlib.patches import Circle
# Bead Parameter
m = 1
# Gravity Constant
g = -9.81
# We need an array of time points from 0 to 10 in increments of 0.01 seconds
dt = 0.001
t_vec = np.arange(0,10,dt)
# Initialize a vector of zeros
x_vec = np.zeros(len(t_vec))
dx_vec = np.zeros(len(t_vec))
ddx_vec = np.zeros(len(t_vec))
y_vec = np.zeros(len(t_vec))
dy_vec = np.zeros(len(t_vec))
ddy_vec = np.zeros(len(t_vec))
# Set our initial condition
x_vec[0] = -1 # initial X location
dx_vec[0] = 0 # initial horizontal velocity
y_vec[0] = 1 # initial y location
dy_vec[0] = 0 # initial vertical Velocity
# Loop through time
# Euler's Method (approximately integrates the differential equation)
for i in range(1, len(t_vec)):
x_vec[i] = x_vec[i-1] + dx_vec[i-1]*dt
ddx_vec[i-1] = -(x_vec[i-1]*(200*(dx_vec[i-1]**2) + 981))/(50*(4*(x_vec[i-1]**2) + 1))
dx_vec[i] = dx_vec[i-1] + ddx_vec[i-1]*dt
y_vec[i] = x_vec[i]**2
# X positon vs. Time
fig1= plt.plot(t_vec, x_vec)
plt.title('X positon vs. Time')
plt.xlabel('Time')
plt.ylabel('X position')
plt.show()
# Y positon vs. Time
fig2 = plt.plot(t_vec, y_vec)
plt.title('Y positon vs. Time')
plt.xlabel('Time')
plt.ylabel('Y position')
plt.show()
# Setup Figure: Initialize Figure / Axe Handles
fig, ax = plt.subplots() # Initialize Figure and Axes
p, = ax.plot([], [], color='cornflowerblue') # Initialize Empty Plot
ax.axis('equal')
ax.set_xlim([-3, 3]) # X Lim
ax.set_ylim([-3, 3]) # Y Lim
ax.set_xlabel('X') # X Label
ax.set_ylabel('Y') # Y Label
ax.set_title('Parabolic Bead Simulation:') # Plot Title
video_title = "parabolic" # name of the mp4
# Initialize Patch:
c = Circle((-1, 1), radius=0.1, color='cornflowerblue') # Draws a circle of radius 0.1 at (X, Y) location (0, 0)
ax.add_patch(c) # Add the patch to the axes
# 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
# Create x and y locations
x_func = np.linspace(-2, 2, 2000)
y_func = x_func**2
# plot locations (for parabola)
fig3 = plt.plot(x_func, y_func)
plt.xlim([-2, 2])
plt.ylim([-0.5, 2.5])
plt.show()
simulation_size = len(t_vec)
# create animation
with writerObj.saving(fig, video_title+".mp4", dpi):
for i in range(0, simulation_size, sample_rate):
p.set_data(x_vec[i], y_vec[i])
patch_center = x_vec[i], y_vec[i]
c.center = patch_center
fig.canvas.draw()
writerObj.grab_frame()
from IPython.display import Video
Video("/work/parabolic.mp4",embed=True, width=640,height=480)