Test 1

 Pay Notebook Creator: Tristan Tham 0 Set Container: Numerical CPU with TINY Memory for 10 Minutes 0 Total 0

Animate Rope¶

Here we will use FuncAnimation to generate a video of the following function from t = 1 to 200:

y = a + b x + c sin( (2 pi / d) (x + t / 25) )

where

a is the base intercept

b is the base slope

c is the sine amplitude

d is the sine period

In [ ]:
# Click the Blue Plane to preview this notebook as a CrossCompute Tool
base_intercept = 1
base_slope = 0.3
sine_amplitude = 0.2
sine_period = 3.14
target_folder = '/tmp'

In [ ]:
import numpy as np

x = np.arange(0, 10, 0.01)
sine_a = sine_amplitude
sine_b = 2 * np.pi / sine_period

def f(t):
return sum([
base_slope * x,
sine_a * np.sin(sine_b * (x + t / 25)),
base_intercept,
])

In [ ]:
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation

figure, axes = plt.subplots()
line, = axes.plot(x, f(0))

def initialize():
return line,

def animate(t):
line.set_ydata(f(t))
return line,

animation = FuncAnimation(
figure, animate, np.arange(1, 360), init_func=initialize,
interval=20, blit=True)

In [ ]:
from os.path import join
target_path = join(target_folder, 'animation.mp4')
animation.save(target_path)
print('animation_video_path = ' + target_path)