Advertisement
Guest User

Untitled

a guest
Jun 14th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from scipy.integrate import odeint
  4. from matplotlib.animation import FuncAnimation
  5. from functools import partial
  6.  
  7. #system
  8. def van_der_pol(coords, t, mu, evens, odds, ones):
  9.     x, y = coords*evens, coords*odds
  10.     x_shift = np.roll(x, 1) #for use in computation
  11.     y_prime = mu*(1-x_shift**2)*y-x_shift
  12.     return np.roll(y, -1)+y_prime
  13.  
  14. #initialize window for blitting
  15. def init(fig, axes, scatter):
  16.     plt.xlim([-3.0, 3.0])
  17.     plt.ylim([-3.0, 3.0])
  18.     return scatter,
  19.  
  20. #animates one frame
  21. def animate(t, sols, scatter, odds, evens):
  22.     print(t)
  23.  
  24.     scatter.set_offsets(np.array([np.trim_zeros(sols[t]*evens), np.trim_zeros(sols[t]*odds)]))
  25.     return scatter,
  26.    
  27. #constants
  28. mu = 0.5
  29.  
  30. #initial data
  31. M = 10
  32. axis = np.linspace(-3.0, 3.0, M)
  33. init_conds = np.array([(x, y) for x in axis for y in axis])
  34. init_conds = init_conds.reshape(2*M**2)
  35. evens, odds = np.array([1 if i % 2 == 0 else 0 for i in range(2*M**2)]), np.array([1 if i % 2 == 1 else 0 for i in range(2*M**2)]) #will be used to extract the x and y coordinates
  36. ones = np.ones(2*M**2)
  37.  
  38. #solving
  39. total = 10.0
  40. delta = 0.1
  41. N = int(total/delta)
  42. t = np.linspace(0.0, total, N)
  43. sols = odeint(van_der_pol, init_conds, t, args=(mu, evens, odds, ones))
  44.  
  45. #setup
  46. fig, axes = plt.figure(), plt.axes(frameon=True)
  47. scatter = plt.scatter([], [], animated=True)
  48.  
  49. #animation
  50. anim = FuncAnimation(fig, func=animate, frames=N, init_func=partial(init, fig, axes, scatter), blit=True, fargs=(sols, scatter, odds, evens), repeat=False)
  51. anim.save("out.mp4", fps=40)
  52. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement