I get different plots for each value that I wrote in for loop's upper range as I expected. But I would like to animate the plot from range (0,0) an goes to (0,15) as the upper limit changes 1 by 1, by using matplotlib animation function animation.FuncAnimation(). So there will be 16 frames total in the animation. I messed up with the animation part, so I'm pasting the code that gives 1 plot output. Thanks in advance!
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation g = 1.0 def distance(x1,y1,x2,y2): r = np.sqrt((x2-x1)**2+(y2-y1)**2) return r def gravit(m1,m2,r): f = g*m1*m2/(r**2) return f def angle(y2, y1, x2, x1): ydif = y1-y2 xdif = x1-x2 angle = np.arctan2(ydif,xdif) return angle m1 = 100 x1, y1 = 0,0 m2 = 1 x2, y2 = -15,-10 vx1 = 1 vy1 = 0 ax1 = 0 ay1 = 0 vx2 = 2 vy2 = 3.9 ax2 = 0 ay2 = 0 x1coor = [x1] y1coor = [y1] x2coor = [x2] y2coor = [y2] for t in range(0,10): #This value of 10 should be change from 0 to 15 in the animation r = distance(x1,y1,x2,y2) fx1 = gravit(m1, m2, r) * np.cos(angle(y2,y1,x2,x1)) fy1 = gravit(m1, m2, r) * np.sin(angle(y2,y1,x2,x1)) ax2 = fx1/m2 vx2 = vx2 + ax2 ay2 = fy1/m2 vy2 = vy2 + ay2 x2 = x2 + vx2 + 0.5*ax2 y2 = y2 + vy2 + 0.5*ay2 x1 = x1 + vx1 + 0.5*ax1 y1 = y1 + vy1 + 0.5*ay1 x1coor.append(x1) y1coor.append(y1) x2coor.append(x2) y2coor.append(y2) plt.axes().set_aspect('equal') plt.axis([-30,30,-30,30]) plt.plot(x1coor,y1coor, '-.', color='blue') plt.plot(x2coor,y2coor, '-.', color='black') plt.scatter(x1,y1,s=m1*20, color='blue') plt.scatter(x2,y2,s=m2*20, color='red')