# Calculating second moment of inertia for a region

Let's say I want to calculate the moment of inertia of a half circle around its centroid. I set up a function to calculate the inertial moment like so:

inertialMoment[reg_?RegionQ, axis_InfiniteLine] :=
Module[{df = RegionDistance[axis]},
Integrate[df[{x, y}], {x, y} \[Element] reg]]


and then define my geometry as follows:

yc = RegionCentroid[Disk[{0, 0}, r, {0, Pi}]][]
axis = InfiniteLine[{{-1, yc}, {1, yc}}];


So now I do

inertialMoment[Disk[{0, 0}, r, {0, Pi}], axis]


and I get: Now, I think I understand what the message says, and my function works fine if I use a fixed axis, such as

axis = InfiniteLine[{-1, 0}, {1, 0}];


but it doesn't make much sense to have this restriction to an explicitly defined location for this axis. Is there a way around this?

P.S.: I know I can get moments of inertia using the MomenOfInertia function; the above is just an example.

• You can actually do this: df[{x_, y_}] = RegionDistance[axis, {x, y}] ( the integral is not returning now, but that's a different issue ) – george2079 Sep 2 '16 at 20:25
• @george2079: Perhaps I'm missing something, but with your definition of df I get an error message about how "only assignments to symbols are allowed". – Pirx Sep 2 '16 at 20:36
• Did you mean to write InfiniteLine[{{-1, yc}, {1, yc}}] (with an extra {})? This gives a line through the two points specified. – mikado Sep 2 '16 at 20:41
• @Mikado: Ah, yes, you're right. Fixed my post, but the change makes no difference in the outcome. – Pirx Sep 2 '16 at 20:44
• Wait...I failed to notice my df gives a patently wrong result for the distance (!) – george2079 Sep 2 '16 at 21:01

You can reasonably expand the definition of RegionDistance to cover this case:

Unprotect[RegionDistance];
RegionDistance[InfiniteLine[{u_List, v_List}]] := Function[{x},
Module[{p, d},
p = Normalize[u - v];
d = x - u;
Norm[d - (p.d) p]]]
Protect[RegionDistance];


This then allows you to evaluate the expressions you gave without problem

inertialMoment[reg_?RegionQ, axis_InfiniteLine] :=
Module[{df = RegionDistance[axis]},
Integrate[df[{x, y}], {x, y} ∈ reg]]

yc = RegionCentroid[Disk[{0, 0}, r, {0, Pi}]][]
axis = InfiniteLine[{{-1, yc}, {1, yc}}];
(* (4 r)/(3 π) *)


And it now works with symbolic r

Assuming[r > 0, inertialMoment[Disk[{0, 0}, r, {0, Pi}], axis]]
(* (4 (8 Sqrt[-16 + 9 π^2] r^3 +
9 π^2 Sqrt[-16 + 9 π^2] r^3 -
54 π^2 r^3 ArcSec[(3 π)/4]))/(81 π^3) *)

• Awesome, thanks! – Pirx Sep 2 '16 at 20:52