I have code for a simple n-body problem:
GravityForce[pos1_, pos2_] := (pos1 - pos2)/
EuclideanDistance[pos1, pos2]^2;
GravityForce[p_, p_] := {0, 0, 0};
Forces[pos_List] := With[{n = Length[pos]},
Mean /@ Table[GravityForce[pos[[i]], pos[[j]]], {i, n}, {j, n}]
];
n = 10;
{vel, pos} = RandomReal[{-20, 20}, {2, n, 3}];
vel += -7 Forces[pos] - 10 (GravityForce[#, sun] & /@ pos);
pos += vel
Is there some way to make this simpler?
pos += vel??! What units are you working in here? – march Oct 13 '16 at 16:35Tablecan be done asOuter[GravityForce, pos, pos]. I think you need to divide byrinGravityForceif you intend to direct your force along a unit vector by the way (although studying your physics problem is outside the scope of this forum..) – george2079 Oct 13 '16 at 16:56EuclideanDistance[pos1, pos2]^3not^2... – BlacKow Oct 13 '16 at 18:05Table, soMean/@Outer[]– george2079 Oct 13 '16 at 19:221arg is needed, sorry I forgot that. As a physics matter shouldn't it beTotalnotMean? – george2079 Oct 13 '16 at 20:11pos2-pos1is fine for direction, but you need to divide it by its own norm to get the unit vector. – Oct 14 '16 at 13:50