Skip to content

Instantly share code, notes, and snippets.

@rene-d
Created October 10, 2018 02:46
Show Gist options
  • Save rene-d/9295ac4088b8e9477a99179c1e447472 to your computer and use it in GitHub Desktop.
Save rene-d/9295ac4088b8e9477a99179c1e447472 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
# http://ifcuriousthenlearn.com/blog/2015/06/18/center-of-mass/
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#set plot sizes
plt.rcParams['figure.figsize'] = (10, 2) # (width, height)
plt.rcParams['font.size'] = 20
plt.rcParams['legend.fontsize'] = 16
x = np.array([ 1, 7, 7, 11, 2.5])
y = np.array([10, 19, 10, 14.5, -0.2])
m = np.array([20, 10, 10, 7, 5 ])
cgx = np.sum(x * m) / np.sum(m)
print('The center of mass in x is %f' % cgx)
cgy = np.sum(y * m) / np.sum(m)
print('The center of mass in y is %f' % cgy)
plt.rcParams['figure.figsize'] = (6, 10) # (width, height)
plt.scatter(x,y,s=m);
plt.scatter(cgx, cgy, color='k', marker='+', s=1e4);
plt.title('Center of Gravity')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment