Friday, 5 December 2014

A floating ball dropped in a water current

While writing the previous script on simple harmonic motion I found a funny simulation.
Here it is, do you remember what happens when you drop a ball in flowing water? (Actually the fluid simulated looks something more dense than water).


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
def fun(x):
if x!=0:
return 2*np.sin(4/3*x+2)/x
else:
return 1
counter = 1
def animate(i):
global x,y,counter
counter += 0.2
ax1.clear()
plt.xlim(0,50)
plt.ylim(-2,2)
plt.plot([x for x in range(50)],[-0 for x in range(50)],lw=2,color="cyan")
plt.plot(counter,fun(counter),color="red",marker="o",ms=20)
ani = animation.FuncAnimation(fig,animate,interval=1)
plt.show()
view raw ball_water.py hosted with ❤ by GitHub

No comments:

Post a Comment