forked from rossy-awan/Computational-Physics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Center of Mass.py
28 lines (25 loc) · 1.26 KB
/
Center of Mass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Libraries
import matplotlib.pyplot as plt
import numpy as np
# Initialize and Declaration
m = [9, 1, 4, 4, 10, 5, 7, 10, 8, 0, 10, 2, 6, 2, 1, 7, 5, 1, 0, 1, 10, 3, 2, 7, 4, 9, 7, 9, 3, 1, 1, 0, 1, 9, 2, 3, 10, 5, 0, 10, 2, 8, 2, 4, 7, 8, 5, 7, 3, 9] # m
x = [-3, -23, -2, -3, -4, 1, -6, -10, -9, 16, 5, 15, -18, -4, 14, -8, 12, 12, 3, 3, -11, -8, -10, -9, 13, 11, 16, 20, 4, 20, -14, -11, 15, -25, -23, 0, 16, 22, -10, 22, 11, 15, -15, 9, -4, -22, 1, 11, 4, -12] # x
y = [25, 8, -5, 20, -9, 23, 10, 3, -21, 2, 15, -21, 23, 12, -10, -15, 13, 23, 17, 15, 14, 9, -18, -15, 9, 20, -11, 17, 22, -10, 14, -19, -13, 0, 23, 15, -9, 18, 13, 16, -21, 9, -17, 23, -3, -14, -21, -20, -22, 5] # y
z = [-10, 3, 6, 21, -5, 14, 3, -2, 17, -4, 4, 6, -3, 6, 0, -20, 22, 21, 5, -11, -20, -3, 17, 12, 21, -18, -20, 20, 9, -15, 9, -11, 1, -17, 12, -21, -2, -12, -16, 17, -8, -7, 23, 18, 0, 17, 16, -2, 20, 25] # z
# Calculation
M = sum(m)
X = np.dot(m, x) / M
Y = np.dot(m, y) / M
Z = np.dot(m, z) / M
CoM = [M, X, Y, Z]
print(CoM)
# Visualization 3D
fig = plt.figure()
ax = plt.axes(projection = "3d")
for i in range(len(m)):
ax.scatter3D(x[i], y[i], z[i], s = m[i], c = 'black')
ax.scatter3D(X, Y, Z, s = M)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()