Last active
          August 29, 2015 14:23 
        
      - 
      
- 
        Save thanasi/be379a441ddbf05350ab to your computer and use it in GitHub Desktop. 
    create an animation and display it in iPython
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ## based on https://wakari.io/nb/url///wakari.io/static/notebooks/Lecture_4_Matplotlib.ipynb | |
| ## create double pendulum animation frames using matplotlib | |
| from matplotlib import animation | |
| from scipy.integrate import odeint | |
| # solve the double pendulum motion | |
| g = 9.82; L = 0.5; m = 0.1 | |
| def dx(x, t): | |
| x1, x2, x3, x4 = x[0], x[1], x[2], x[3] | |
| dx1 = 6.0/(m*L**2) * (2 * x3 - 3 * cos(x1-x2) * x4)/(16 - 9 * cos(x1-x2)**2) | |
| dx2 = 6.0/(m*L**2) * (8 * x4 - 3 * cos(x1-x2) * x3)/(16 - 9 * cos(x1-x2)**2) | |
| dx3 = -0.5 * m * L**2 * ( dx1 * dx2 * sin(x1-x2) + 3 * (g/L) * sin(x1)) | |
| dx4 = -0.5 * m * L**2 * (-dx1 * dx2 * sin(x1-x2) + (g/L) * sin(x2)) | |
| return [dx1, dx2, dx3, dx4] | |
| x0 = [pi/2, pi/2, 0, 0] # initial state | |
| t = linspace(0, 10, 250) # time coordinates | |
| x = odeint(dx, x0, t) # solve the ODE problem | |
| ## animate the motion | |
| fig, ax = plt.subplots(figsize=(5,5)) | |
| ax.set_ylim([-1.5, 0.5]) | |
| ax.set_xlim([1, -1]) | |
| pendulum1, = ax.plot([], [], color="red", lw=2) | |
| pendulum2, = ax.plot([], [], color="blue", lw=2) | |
| def init(): | |
| pendulum1.set_data([], []) | |
| pendulum2.set_data([], []) | |
| def update(n): | |
| # n = frame counter | |
| # calculate the positions of the pendulums | |
| x1 = + L * sin(x[n, 0]) | |
| y1 = - L * cos(x[n, 0]) | |
| x2 = x1 + L * sin(x[n, 1]) | |
| y2 = y1 - L * cos(x[n, 1]) | |
| # update the line data | |
| pendulum1.set_data([0 ,x1], [0 ,y1]) | |
| pendulum2.set_data([x1,x2], [y1,y2]) | |
| anim = animation.FuncAnimation(fig, update, init_func=init, frames=len(t), blit=True) | |
| anim.save('animation.mp4', fps=20); | |
| ## and then display it | |
| from IPython.display import HTML | |
| video = open("animation.mp4", "rb").read() | |
| video_encoded = video.encode("base64") | |
| video_tag = '<video controls alt="test" src="data:video/x-m4v;base64,{0}">'.format(video_encoded) | |
| HTML(video_tag) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment