!apt update -y
!apt install ffmpeg -y
import numpy as np
import sympy
import math
import matplotlib.pyplot as plt
from numpy.linalg import solve
from matplotlib.animation import FFMpegWriter
from matplotlib.patches import Circle
from matplotlib.patches import Rectangle
k= 10 #N/m
m1= 1 #kg
m2= 2 #kg
L= 1 #m#Setting Up Animation
fig, ax = plt.subplots()
Lo= 1 #m
g= 9.8 #m/s^2
dt= 0.001 #seconds
t_vec= np.arange(0, 10, dt) #For 10 seconds
x_vec= np.zeros(len(t_vec))
dx_vec= np.zeros(len(t_vec))
theta_vec= np.zeros(len(t_vec))
dtheta_vec= np.zeros(len(t_vec))
x_vec[0]= 0 #initial x-position
dx_vec[0]= 0 #initial x-velocity
theta_vec[0]= 1 #initial theta (radians)
dtheta_vec[0]= 0 #initial theta velocity (radians)
for i in range(1, len(t_vec)):
#spliiting apart the equations since they are VERY long
A= (x_vec[i-1]**2 - 2 * x_vec[i-1] * L * sympy.sin(theta_vec[i-1]) + L**2 * sympy.sin(theta_vec[i-1]) * sympy.sin(theta_vec[i-1]) + 1.5**2 - 3 * L * sympy.cos(theta_vec[i-1]) + L**2 * sympy.cos(theta_vec[i-1])*sympy.cos(theta_vec[i-1]))
ddx= ( (-0.5)*k * ( (A**(0.5)) - Lo) * (A**(-0.5)) * (2 * x_vec[i-1] - 2 * L * sympy.sin(theta_vec[i-1])) ) / (m2)
ddtheta=( ((-0.5)*k * ( (A**(0.5)) - Lo) * (A**(-0.5)) * ((-2) * x_vec[i-1] * L * sympy.cos(theta_vec[i-1]) + 3 * L * sympy.sin(theta_vec[i-1]) )) - m1 * g * L * sympy.sin(theta_vec[i-1]) ) / (m1 * L**2)
x_vec[i]= x_vec[i-1]+ dx_vec[i-1]*dt
theta_vec[i]= theta_vec[i-1]+ dtheta_vec[i-1]*dt
dx_vec[i]= dx_vec[i-1] + ddx*dt
dtheta_vec[i]= dtheta_vec[i-1] + ddtheta*dt
plt.plot(t_vec,x_vec)
plt.show()
plt.plot(t_vec,theta_vec)
plt.show()
p, = ax.plot([], [], color='black')
ground,= ax.plot([-5, 5], [-0.1, -0.1], color= 'brown')
spring, = ax.plot([], [], color= 'grey')
ax.set_xlim([-2, 2])
ax.set_ylim([-2, 2])
ax.set_xlabel("X-Value")
ax.set_ylabel("Y-Value")
ax.set_title("Pendulum and Spring")
video_title= "Simulation2"
mass1= Circle((0,0), radius= 0.1, color= 'blue')
mass2= Circle((0,0), radius= 0.1, color= 'green')
ax.add_patch(mass1)
ax.add_patch(mass2)
FPS= 20
sample_rate= int(1/FPS/dt)
dpi = 300
writerObj = FFMpegWriter(fps= FPS)
simulation_size= len(t_vec)
x_m1= np.zeros(simulation_size)
y_m1= np.zeros(simulation_size)
x_m2= np.zeros(simulation_size)
y_m2= np.zeros(simulation_size)
x_pendulum_arm = np.zeros(simulation_size)
y_pendulum_arm = np.zeros(simulation_size)
origin = (0, 1.5)
with writerObj.saving(fig, video_title+".mp4", dpi):
for i in range(0, simulation_size, sample_rate):
x_m1[i]= L*sympy.sin(theta_vec[i])
y_m1[i]= 1.5- L*sympy.cos(theta_vec[i])
x_m2[i]= x_vec[i]
x_data_points = [origin[0], x_m1[i]]
y_data_points = [origin[1], y_m1[i]]
p.set_data(x_data_points, y_data_points)
x_spring= [x_m1[i], x_m2[i]]
y_spring= [y_m1[i], y_m2[i]]
spring.set_data(x_spring, y_spring)
patch_center= x_m1[i], y_m1[i]
mass1.center= patch_center
patch_center= x_m2[i], y_m2[i]
mass2.center= patch_center
fig.canvas.draw()
writerObj.grab_frame()
from IPython.display import Video
Video("/work/Simulation2.mp4", embed=True, width=640, height=480)