# This code block installs ffmpeg to your Deepnote Machine
# Allows Deepnote to write MP4
!apt update -y
!apt install ffmpeg -y
# Import Libraries:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FFMpegWriter
from matplotlib.patches import Circle
from matplotlib.patches import Rectangle
from matplotlib.pyplot import *
from numpy import *
# Part b: ------------------------------------------------------------
# Simulate this system using Euler’s method (time step = 0.001
# seconds) in Python with initial conditions, x = -1m, dx/dt = 0m/s,
# over the course of 10 seconds
# Parameters
g = 9.81 # m/s^2
m = 1 # kg
# Time array
dt = 0.001
t_vec = np.arange(0,10,dt)
# Initialize a vector of zeros
vec_size = len(t_vec)
x_vec = np.zeros(vec_size)
dx_vec = np.zeros(vec_size)
y_vec = np.zeros(vec_size)
lam_vec = np.zeros(vec_size)
# Pole End effector Location for Animation:
x_pole = np.zeros(vec_size)
y_pole = np.zeros(vec_size)
# Initial Conditions
x_vec[0] = -1
y_vec[0] = 1
lam_vec[0] = 0
# Initial Pole End effector Location:
y_offset = 0 # The Y Location of where the Cart and Pole are connected
x_pole[0] = x_vec[0] #+ L * np.sin(theta_vec[0])
y_pole[0] = y_vec[0] #y_offset #- L * np.cos(theta_vec[0])
# Euler Simulation: Using Matrix Form (A * x = B)
# Initialize A and B:
A = np.array([[m, 0, 0], [0, m, 1], [0, 1, 0]])
b = np.array([[0], [-m*g], [0]])
for i in range (1, vec_size):
# Replace non constant values
A [0, 2] = -2 * x_vec[i-1]
A [2, 0] = -2 * x_vec[i-1]
b [2] = 2 * dx_vec [i-1]**2
# Solve for ddx, ddy, and lambda
[ddx, ddy, lam] = np.linalg.solve(A, b)
# Euler 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] = x_vec[i]**2
lam_vec[i] = lam
# x position vs time plotting
plt.plot(t_vec, x_vec)
plt.title("x position vs time")
plt.show()
# y position vs time plotting
plt.plot(t_vec, y_vec)
plt.title("y position vs time")
plt.show()
# constraint forces vs time plotting
plt.plot(t_vec, lam_vec)
plt.title("constraint forces vs time")
plt.show()
# Part c: ------------------------------------------------------------
# Animate the motion from Problem 1b, showing a bead sliding on a
# wire (at 20 frames per second).
# Setup Figure:
fig, ax = plt.subplots()
p, = ax.plot([], [], color='royalblue')
min_lim = -5
max_lim = 5
ax.axis('equal')
ax.set_xlim([min_lim, max_lim])
ax.set_ylim([min_lim, max_lim])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Bead on a string Simulation:')
title = "simulation"
# Setup Animation Writer:
FPS = 20
sample_rate = int(1 / (dt * FPS)) # Real Time Playback
dpi = 300
writerObj = FFMpegWriter(fps=FPS)
# Create Parabola
x = linspace(-1.5,1.5,5000)
y = x**2
plot(x,y)
xlabel("x axis")
ylabel("y axis")
# Initialize Patch: (Cart)
width = 0.4 # Width of Cart
height = width / 2 # Height of Cart
xy_cart = (x_vec[0], y_vec[0]) # Bottom Left Corner of Cart
r = Rectangle(xy_cart, width, height, color='cornflowerblue') # Rectangle Patch
ax.add_patch(r) # Add Patch to Plot
# Animate:
with writerObj.saving(fig, title + ".mp4", dpi):
for i in range(0, vec_size, sample_rate):
# Update Cart Patch:
r.set(xy =(x_vec[i]-0.2, y_vec[i]-0.1))
#c.set(xy =(x_vec[i], y_vec[i]), radius = 0.1)
# Update Drawing:
fig.canvas.draw()
# Save Frame:
writerObj.grab_frame()
from IPython.display import Video
Video("/work/simulation.mp4", embed=True, width=640, height=480)