Skip to content

Instantly share code, notes, and snippets.

@awesomebytes
Last active December 7, 2023 13:39
Show Gist options
  • Save awesomebytes/39a4ba6c64956a1aa9bd to your computer and use it in GitHub Desktop.
Save awesomebytes/39a4ba6c64956a1aa9bd to your computer and use it in GitHub Desktop.
Compute inertia matrix for simple solids: cube, sphere and cylinder
#/usr/bin/env python
# Based on:
# http://mathworld.wolfram.com/MomentofInertia.html
def get_cube_inertia_matrix(mass, x, y, z):
"""Given mass and dimensions of a cube return intertia matrix.
:return: ixx, ixy, ixz, ixy, iyy, iyz, ixz, iyz, izz
From https://www.wolframalpha.com/input/?i=moment+of+inertia+cube"""
ixx = (1.0 / 12.0) * (y**2 + z**2) * mass
iyy = (1.0 / 12.0) * (x**2 + z**2) * mass
izz = (1.0 / 12.0) * (x**2 + y**2) * mass
ixy = 0.0
ixz = 0.0
iyz = 0.0
return [[ixx, ixy, ixz], [ixy, iyy, iyz], [ixz, iyz, izz]]
def get_sphere_inertia_matrix(mass, radius):
"""Given mass and radius of a sphere return inertia matrix.
:return: ixx, ixy, ixz, ixy, iyy, iyz, ixz, iyz, izz
From https://www.wolframalpha.com/input/?i=inertia+matrix+sphere
"""
ixx = iyy = izz = (2.0 / 5.0) * radius**2 * mass
ixy = 0.0
ixz = 0.0
iyz = 0.0
return [[ixx, ixy, ixz], [ixy, iyy, iyz], [ixz, iyz, izz]]
def get_cylinder_inertia_matrix(mass, radius, height):
"""Given mass and radius and height of a cylinder return inertia matrix.
:return: ixx, ixy, ixz, ixy, iyy, iyz, ixz, iyz, izz
https://www.wolframalpha.com/input/?i=inertia+matrix+cylinder&rawformassumption=%7B%22C%22,+%22cylinder%22%7D+-%3E+%7B%22Solid%22%7D
"""
ixx = (1.0 / 12.0) * (3.0 * radius**2 + height**2) * mass
iyy = (1.0 / 12.0) * (3.0 * radius**2 + height**2) * mass
izz = (1.0 / 2.0) * radius**2 * mass
ixy = 0.0
ixz = 0.0
iyz = 0.0
return [[ixx, ixy, ixz], [ixy, iyy, iyz], [ixz, iyz, izz]]
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print "Inertia matrix as:"
print "[[ixx, ixy, ixz], [ixy, iyy, iyz], [ixz, iyz, izz]]"
print "Cube inertia of cube mass 1.0 and all dimensions 1.0:"
print get_cube_inertia_matrix(1.0, 1.0, 1.0, 1.0)
print "Sphere inertia of mass 1.0 and radius 1.0:"
print get_sphere_inertia_matrix(1.0, 1.0)
print "Cylinder inertia of mass 1.0 and radius 1.0 and height 1.0:"
print get_cylinder_inertia_matrix(1.0, 1.0, 1.0)
elif len(sys.argv) == 3:
print get_sphere_inertia_matrix(float(sys.argv[1]),
float(sys.argv[2]))
elif len(sys.argv) == 4:
print get_cylinder_inertia_matrix(float(sys.argv[1]),
float(sys.argv[2]),
float(sys.argv[3]))
elif len(sys.argv) == 5:
print "Cube inertia matrix:"
print get_cube_inertia_matrix(float(sys.argv[1]),
float(sys.argv[2]),
float(sys.argv[3]),
float(sys.argv[4]))
else:
print "Get the inertia matrix of simple shapes."
print "Usage:"
print "For a sphere:"
print sys.argv[0] + " mass radius"
print "For a cylinder:"
print sys.argv[0] + " mass radius height"
print "For a cube:"
print sys.argv[0] + " mass x y z"
print "\nFor example:\n" + sys.argv[0] + "1.0 1.0 1.0 1.0"
print "[[0.16666666666666666, 0.0, 0.0], [0.0, 0.16666666666666666, 0.0], [0.0, 0.0, 0.16666666666666666]]"
@awesomebytes
Copy link
Author

I believe there is a mistake in the cylinder calculation at izz = (1.0 / 12.0) * radius**2 * mass. It should be 1.0/2.0 and not divided by 12.0. Double checked with SolidWorks.

Good catch! Editing it right now.

@rguyonneau
Copy link

rguyonneau commented Dec 7, 2023

Hi,

Thanks for the script and the links!

According to the link you provide, for the computation of the inertia of a cylinder, don't you have a 4* in front of the height for ixx and iyy?

ixx = (1.0 / 12.0) * (3.0 * radius**2 + height**2) * mass -> ixx = (1.0 / 12.0) * (3.0 * radius**2 + 4*height**2) * mass
iyy = (1.0 / 12.0) * (3.0 * radius**2 + height**2) * mass ->  iyy = (1.0 / 12.0) * (3.0 * radius**2 + 4*height**2) * mass

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment