# Homework Help: Representing a radius vector in a Cartesian system for a simulation

1. May 15, 2013

### scienalc

1. The problem statement, all variables and given/known data

I need to develop a macroscopic gravity simulation software. I have problems with representing vectors in a programming language (c++), because I need do develop my own mathematical infrastructure for the project. I asked for the Cartesian coordinates, but would also have no objections to a polar system.

Also I would like to collect some opinion on the correctness of my equations.

2. Relevant equations

Law of Gravity : Fg = Gm1m2r-2
Newtons second Law : F = ma

3. The attempt at a solution

First, I tried to determine the velocity of the first object (m1):
F = ma = m$\frac{dv}{dt}$ (1)
Gm1m2r-2 = m1$\frac{dv}{dt}$ (2)
v = ∫Gm2r-2dt | integrated from 0 to t (3)

v(t) = Gm2r-2t (4)

Next, I tried similarly to get the dependency of the radius vector r on time:
v = $\frac{dr}{dt}$ (5)
dr = Gm2r-2tdt (6)
∫r2dr = ∫Gm2tdt (7)
$\frac{r3}{3}$ = Gm2$\frac{t2}{2}$ (8)

r(t) = ($\frac{3}{2}$Gm2t2)$\frac{1}{3}$ (9)

These results (if correct, of course) need to be supported by an infrastructure which will allow me to calculate the forces, distances, accelerations, velocities, ... between a number of objects. To me it seems the most easy way if I could from the above results somehow get rx, ry and using them vx and vy.

I hope I wasn't confusing.

Thanks a lot

Last edited: May 15, 2013
2. May 15, 2013

### voko

(1) is correct: a vector on the left, and a vector on the right.

(2) is wrong: a scalar on the left, and a vector on the right; can't be equal.

Even ignoring the scalar-vector mismatch, the transition from (3) to (4) is incorrect, because r is a function of t, so you can't assume it constant during integration.

3. May 16, 2013

### scienalc

Can you please clarify it a bit more for me. I assumed that in (2) is a situation like in the Law of Gravity, where we have a force (vector) on the left side and on the right we have the distance, a scalar, but one that is associated with a unit vector (in the opposite direction of the force), but of intensity r. Of course, a velocity vector and that unit vector are not aligned the same, but I assumed that if I broke the vectors down into their respective x and y components, the unit vectors of the coordinate system would align.

I see your point regarding the transition from (3) to (4). I'm finding myself in a circular problem then, I need r(t) for v(t) and vice versa. Can you provide a little guidance for me?

4. May 16, 2013

### voko

The proper notation should be this: $$m_1 \frac {d\vec{v}} {dt} = G \frac {m_1 m_2} {r^3} \vec{r}$$Can you see why this is correct?

This circular problem is called an ordinary differential equation. Does that sound familiar to you?

5. May 16, 2013

### scienalc

Yes, the notation is what I had in mind, the only thing that confuses me is that you used r cubed. Is that a typo or am I missing something obviously huge?

So, then I would have to regard the problem as a differential equation like:

Gm2 = r2(t)d2r/dt2

is that correct?

6. May 16, 2013

### voko

You mentioned that the force is aligned with the unit vector between the two masses. So we take $\vec{r}$ and to obtain the unit vector, we normalize it: $\frac {\vec{r}} {r}$. Do you see now where the cube comes from?

Again, this is correct only in one particular case: when the initial velocity is aligned with the distance between the masses. Which makes the problem 1D. I am not sure whether this is allowed in your case. If it is, go for it, as that simplifies things quite a bit. If not, you end up with a system of equations derived from the inversely cubed vector form. Alternatively, you could switch to polar coordinates, but that requires some preliminary derivation based on some conservation laws, and I am not sure if you are supposed to be able to do that.

7. May 16, 2013

### scienalc

This is the case.

If I have n objects in a system and a new object A enters the system with its velocity v, obviously, everyone of the n objects would interact with A affecting its velocity. My plan was to calculate the isolated effect of each of the n objects on A and then superpose the results. I.e. to treat as if object A and the first object of n were alone in the plane and A was not moving. This way, I would get n+1 velocity vectors and would do a vector sum to determine the resultant velocity vector of object A. In that case, I could use the above differential equation (of course not for r, but for v) n times. Is that reasoning correct?

My issue started with the problem to interpret vectors in a plane. I can define a vector using its coordinates of start and end point, or using its length and angle, along with its starting points coordinates. Using those facts, I could work out an vector arithmetic infrastructure.

8. May 16, 2013

### voko

You have a general n-body problem. You might want to read up on that. In a nutshell, you can't treat it pair-wise except in some very special cases.

Apparently, you are supposed to integrate the equations of motion numerically. You are going to have n vector equations of the form $$\frac {d\vec{v}_i} {dt} = G \sum_{ j \ne i } \frac {m_j} {|\vec{r}_j - \vec{r}_i|^3} (\vec {r}_j - \vec{r}_i)$$which are then decomposed into 3n coupled ordinary second-order differential equations, which can be reduced to 6n coupled ordinary first-order differential equations, which you should be able to attack numerically.

9. May 16, 2013

### Staff: Mentor

You'll probably find it easier to keep all positions and velocities in Cartesian coordinates. Distances between objects ("r") can be found by the usual application of Pythagoras' theorem, so that the magnitude of the gravitational force can be found between pairs of interacting objects. Since the positions are already in components, the force can be easily divided into components, too. Components from all interacting bodies can be summed to yield a net force for a given instant, and hence a net acceleration (F=MA).

At this point, with the net forces and/or accelerations you apply an "integration step" to update positions and velocities. There are some simple but effective algorithms such as the Verlet or Leapfrog integrator (look 'em up), and more accurate but complex integrators like Runge-Kutta.

The simple integrators can be surprisingly accurate with a modest timestep if there are no very close interactions (collisions) of the objects. Numerical differential equation integration is practically a whole field of mathematics unto itself. A web search should turn up plenty of info and examples. Search for terms like: gravity simulation, orbit simulation, leapfrog integrator, to get started.

10. May 16, 2013

### D H

Staff Emeritus
Assuming that $\vec v = d\vec r/dt$, the proper notation should be this:$$m_1 \frac {d\vec{v}} {dt} = -G \frac {m_1 m_2} {r^3} \vec{r}$$ Your expression is missing a minus sign.

voko used Gm1m2/r2. Look at the units.

What voko did was to add direction to that scalar acceleration. Another way to write Newton's law of gravity is $$m_1 \frac {d\vec{v}} {dt} = -G \frac {m_1 m_2} {r^2} \hat{r}$$ The right hand side is the product of the scalar $Gm_1m_2/r^2$ with the unit vector that points along the gravitational force vector $-\hat r$. Multiply the right hand side by $r/r=1$ and you get the formulation voko wrote (with the sign correction).

11. May 16, 2013

### voko

It was not very obvious to me how r and v were defined, so I followed the original formulation.

In the one-body case, if r is the displacement from a gravitating center, the force must be indeed anti-parallel to it.

I think I have the correct signs in the formula for the general n-body case, though.

12. May 16, 2013

### scienalc

Thanks a lot, guys.

I looked up the n-body problem, seems to very well describe my problem. The relation voko provided is the one mentioned in various sources. Now, regarding the minus sign, I wonder why is the direction of the force/acceleration/velocity determined by rj-ri, and not the other way around? If we are calculating the acceleration/velocity of the i-th object in the presence of the gravitational influence of the j-th, shouldn't the vector be directed from i to j?

13. May 16, 2013

### voko

This is most easily seen if you assume that $\vec{r}_i = 0$. Which you can always do for one vector and one instant of time. Then the force will be directed toward the body pointed by $\vec{r}_j$, which is exactly as it should be.