Center of Mass

Problem #12

Tags: special prg

Who solved this?

No translations... yet

Physics doesn't depend solely on whimsical formulas. Sometimes we need to involve some programming - typically when simulating some phenomenon, processing experimental data or when solution formulas require complicated integrations which are easier done in "numerically" rather than analytically.

This problem introduces us to programming. It's not going to be difficult, so we'll use the BASIC language (some dialect of it). It was created to allow students and researches quickly grasp programming without getting in too technical details. Nowadays it is less popular but its general syntax will be good intro to more advanced languages like Matlab / Octave or Python which you may encounter in your scientific career.

Problem Statement

You are an astrophysicist, studying some star cluster. You just received (after year of observations and measurements) a list of data for main stars comprising this cluster and as a first step, you want to find their Center of Mass - for the cluster it is a point of interest as they all rotate around this point!

The list contains total amount of stars in the first line. Then each next line contain 4 numbers describing one star - they are M X Y Z where M is the mass (relative to that of our Sun) and X Y Z are coordinates (well, in real life they would be astronomical rather than cartesian, but for now let them be). For example, list of 2 stars may look like this:

2
5 1 2 7
2.5 4 8 1

Which means the first star with mass of 5 suns is situated at X=1, Y=2, Z=7 and the second with mass 2.5 suns at X=4, Y=8, Z=1.

You need to find the center of the cluster - in other words, the center of mass of these stars. And print its 3 coordinates.

Instructions

You may know that to find center of mass you need to find average (or mean) value for each coordinate but with respect to masses - so that center is closer to heavier bodies. For this we multiply every coordinate by corresponding body weight, sum these "weighted" coordinates and divide it all by the total mass. E.g.

       M1 * X1  +  M2 * X2 + ... + Mn * Xn
Xc  =  -----------------------------------
               M1 + M2 + ... + Mn

Here we calculate X coordinate of the center by taking X1, X2, ..., Xn - corresponding X coordinates of stars 1, 2, ..., n multiplied by the masses M1, M2, ..., Mn - and then everything is divided by the total mass. In our case it gives:

       5 * 1  +  2.5 * 4
Xc  =  -----------------  =  2
            5 + 2.5

This makes sense: gravity center lies between 1 and 4 on X axis but should be twice closer to 1 because the star here is twice heavier. In the same manner we'll find Yc = 4, Zc = 5 - so the center point is (2, 4, 5).

Now, how to put this into the program?

As we don't know beforehand how many masses there are, it is a typical case where we use a loop - repeated execution. We shall firstly read in the first line, to know how many iterations should be performed - and then we run a loop for given number of operations. On every iteration we shall read in four values from the every next line and do some calculations on them.

To read a value, or several values into variable (or several variables) we use input keyword in basic, for example:

input N

input M, X, Y, Z

in the first case we read a single value into variable N (for example this can be useful for reading stars count in the first line), while the second example reads 4 variables - supposedly for mass and three coordinates. Obviously the first input should be before the repeating part (loop) while the second should be inside the repeating part.

Now about the loop. In most languages there is a convenient FOR loop used in cases when we know the number of iterations beforehand. In Basic exact syntax is like this

for I = 1 to 10
   ... do something
next I

Here I is a variable which will hold the current iteration number in case we need it (in this problem we don't, but syntax remains). As we need not 10 iterations but amount, which was read before into some variable, e.g. N, replace the "upper limit" 10 with this variable. At last next marks the end of the repeating part and in Basic it mentions variable on which we repeat.

Running your code

At this point you should be able to write some simple program, though a bit useless. Combine input of the stars count with the loop, so it performs exactly that number of iterations - and inside the loop for now put print statement, like this

print I*I

Now scroll the page down and find Run Basic button. You'll see there are two areas - with input data (where your program receives data from actually, where input reads from) - and for the answer. In this answer box your program will print some values. As we run I from 1 to some N we should see squares (I*I) of those values printed!

Completing your calculations

Now how to utilize all this our knowledge? The formula for center of mass is a ratio with numerator (above) and denominator (below), each of them consisting of a long sum, which in turn only could be calculated inside the loop. This means we need create "accumulating" variables which are initialized to 0 before the loop and on every iteration have the appropriate increment. For example, how do we compute total mass (which is in denominator):

sumM = 0

for ...

  ... read new values for M and others

  sumM = sumM + M

next ...

... do something with sumM holding total mass value

Regard this expression sumM = sumM + M - it is not a mathematical equality! Equal sign in this case is an assignment operator, so it tells to add M to current value of sumM and store result back into sumM variable (i.e. its value is updated or incremented by M).

Now you have enough knowledge to complete the problem. You'll need to calculate sumM and also "weighted sums" for each of 3 coordinates inside the loop (all this is done in a similar manner) - and then after the loop simply use print to output three values, e.g.

print 13, sumM, sumM/N

separate them with comma, so they are printed in a single line with spaces between. Of course replace these 13 etc with suitable expressions for X, Y and Z of a central point we look for!

You need to login to get test data and submit solution.