# clase del 16

Oct 16th, 2020 (edited)
1,298
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2. import matplotlib.pyplot as plt
3. import matplotlib.animation as anim
4.
5. n = 2
6. lenght = 100
7. dt = 0.01
8. epsilon = 2250  # energía mínima
9. sigma = 26  # distancia a la cuál es 0
10.
11.
12. def u(r):
13.     return 4*epsilon*(pow(sigma/r, 12) - pow(sigma/r, 6))
14.
15.
16. def f(r):
17.     return 4*epsilon*(12*sigma**12/r**13 - 6*sigma**6/r**7)
18.
19.
20. def update(num, pos, pos1, pt):
21.     aceleraciones = np.zeros((n, 2))
22.     for i in range(n):
23.         acc = 0.0
24.         for j in range(n):
25.             if i != j:
26.                 d = np.linalg.norm(pos1[i] - pos1[j])
27.                 acc += f(d) * (pos1[i] - pos1[j])/d
28.                 pass
29.             pass
30.         aceleraciones[i] = acc
31.         pass
32.     pos1c = np.copy(pos1)
33.     for i in range(n):
34.         pos1[i] = 2*pos1[i] - pos[i] + aceleraciones[i]*dt**2
35.         for j in range(2):
36.             if pos1[i][j] < 0 or pos1[i][j] > lenght:  # si colide con los bordes
37.                 pos1[i][j] = pos1c[i][j]  # no avanza
38.         pass
39.     for i in range(n):
40.         pos[i] = pos1c[i]
41.         pass
42.     pt.set_data(pos1[:, 0], pos[:, 1])
43.     return pt
44.
45.
46. fig1 = plt.figure()
47.
48. pos = np.array([[30, 50], [70, 50]], dtype="float64")
49. vel = np.array([[0, 40], [0, -40]], dtype="float64")
50.
51. # primer paso
52. acc = 0.0
53. for i in range(n):
54.     for j in range(n):
55.         if i != j:
56.             d = np.linalg.norm(pos[i] - pos[j])
57.             acc += f(d) * (pos[i] - pos[j])/d
58. pos1 = pos + vel*dt + acc*dt**2/2
59.
60. pts, = plt.plot([], [], "bo", markersize=10)
61. plt.xlim(0, lenght)
62. plt.ylim(0, lenght)
63.
64. booru = anim.FuncAnimation(fig1, update, fargs=(pos, pos1, pts), interval=50)
65.
66. plt.show()
67.
RAW Paste Data