Help Gravity with 3D coordinations

  • Context: Undergrad 
  • Thread starter Thread starter Sword7
  • Start date Start date
  • Tags Tags
    3d Gravity
Click For Summary

Discussion Overview

The discussion revolves around the calculation of gravitational forces in a 3D coordinate system, particularly in the context of simulating spacecraft motion around Earth. Participants explore vector calculations, the implementation of gravitational formulas, and the challenges faced in achieving accurate orbital simulations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their initial attempt to simulate gravity using a basic program but encounters issues with the spacecraft not achieving orbit.
  • Another participant suggests that the calculation of forces needs to be decomposed into components before updating positions, indicating a misunderstanding in the initial approach.
  • A subsequent post mentions a revised formula for gravitational force that incorporates unit vectors, but the participant still experiences issues with the spacecraft's trajectory.
  • Several participants discuss the correct formulation of gravitational forces, emphasizing the need for accurate representation of the force vector in relation to the position vector.
  • There is a discussion about the Euler method for numerical integration, with one participant suggesting that it may not yield accurate results and proposing alternative methods for better accuracy.
  • Questions arise regarding the conversion of position vectors to unit vectors, with participants sharing insights on how to achieve this mathematically.

Areas of Agreement / Disagreement

Participants express differing views on the correct formulation of gravitational forces and the methods for simulating motion. There is no consensus on the best approach to achieve accurate orbital dynamics, and multiple competing views remain regarding the implementation of vector calculations.

Contextual Notes

Some participants highlight limitations in the initial calculations, such as the need to represent both position and velocity, and the potential inaccuracies of the Euler method. The discussion also reveals dependencies on specific mathematical formulations and assumptions about vector components.

Who May Find This Useful

This discussion may be useful for individuals interested in computational physics, orbital mechanics, and numerical methods for simulating physical systems, particularly those new to vector mathematics and gravitational calculations.

Sword7
Messages
19
Reaction score
2
Hello folks,

I am new to this physics forum since I searched for that through Google and found this.

I am figuring out about gravity calculation with 3D coordinations (or vector). I wrote my small program to simulate gravity for flying around the planet Earth. I tried that but a spacecraft quickly went away from Earth instead of orbit. I searched for vector calculation for gravity through the Internet but no luck or not enough information. Does anyone have any complete information about 3D coordination/vector calculation for gravity?

I have two books called "Fundamental of Astrodynamics" and "Orbit Determination for Microcomputers". I still am figuring them out because I am new to vector calculations.

My program for 3D gravity simulation are:

double G = 6.67428e-11;
double M = 5.9742e+24;
double m = 100;
double px, py, pz;
double ax, ay, az;
double Fg, a, r;

px = 7000.0 * 1000;
py = 7000.0 * 1000;
pz = 7000.0 * 1000;

ax = 0.0;
ay = 14.0 * 1000;
az = 0.0;

for(idx = 0; idx < 10000; idx++) {
r = sqrt((px*px) + (py*py) + (pz*pz));
Fg = (G * M * m) / (r * r);
a = Fg / m;

ax += a; ay += a; az += a;
px -= ax; py -= ay; pz -= az;

printf("Spacecraft = %g,%g,%g: Fg = %g N (%g m/s)\n",
px, py, pz, Fg, a);
}

Thanks!
Sword7
 
Physics news on Phys.org
Sword7 said:
r = sqrt((px*px) + (py*py) + (pz*pz));
Fg = (G * M * m) / (r * r);
a = Fg / m;

ax += a; ay += a; az += a;

You calculate the full radius vector, which you use in your computation of the force vector, which is the full force vector (the one parallel to the radius vector). You cannot use this acceleration in the x y and z directions -- you have to decompose the accelerations (or forces) into components first before you calculate the new positions.
 
Hello folks,

Ok, I read my books again and learned about unit vector for gravity formula. I changed my code and tried again. Note: center of Earth is (0, 0, 0).

Fg = -( GMn / r^2 ) * (r / |r|);

:
:
r = sqrt((px*px) + (py*py) + (pz*pz));
Fgx = -(G * M * m * px) / (r*r*r);
Fgy = -(G * M * m * py) / (r*r*r);
Fgz = -(G * M * m * pz) / (r*r*r);

ax = Fgx / m;
ay = Fgy / m;
az = Fgz / m;

px += ax; py += ay; pz += az;
:
:

Results:

Spacecraft accelerated around Earth then quickly went away. At near center, its peak was near speed of light! Then it just started slow down but never return back. I think that spacecraft fell down to surface too fast instead of staying in orbit. However, there is another problem. When one of px/py/pz became negative, one of ax/ay/az jumped. Does anyone know any solution? That's why I want to write my own space simulator.

Thanks again,
Sword7
 
I think you're still calculating the component forces wrong, what you have is this:
[tex]F_{gx}=-\frac{GMm}{r^3}p_{x}[/tex]
The force of gravity is given by:
[tex]F_{g}=-\frac{GMm}{r^3}\vec{r}[/tex]
Or, equivilently,
[tex]F_{g}=-\frac{GMm}{r^2}\hat{r}[/tex]

Your formula isn't directly in either of these forms, so I think what you should use is something like this:
[tex]F_{gx}=-\frac{GMm}{p_{x}^2}\hat{p_{x}}[/tex]

Which should correctly yield the force in the x dimension (the unit vector isn't really necessary in your calculations though).
 
Hello,

Hmm. I noticed that nice math fonts that you tried to write. How do I write equations by using math formula/symbols fonts?

Ok, I was looking for unit vector information through my books and Internet but not much information like r-> or r^. How convert position vectors to unit vector? Does anyone have any good information about unit vector because I am very new to vector math?

Thanks again,
Sword7
 
You can use "LaTex". Some people have software that converts "WYSIWYG" to LaTex form but it is not difficult to do it "by hand" (and I think it produces a cleaner file). Look at
https://www.physicsforums.com/showthread.php?t=8997

A "unit vector" is simply a vector of length 1. To convert any (non-zero) vector to a unit vector in the same direction, divide each component by the length of the vector.
 
Nabeshin said:
The force of gravity is given by:
[tex]F_{g}=-\frac{GMm}{r^3}\vec{r}[/tex]
That is exactly what he is doing now.

Your formula isn't directly in either of these forms, so I think what you should use is something like this:
[tex]F_{gx}=-\frac{GMm}{p_{x}^2}\hat{p_{x}}[/tex]
:bugeye: Don't do that!

======================================================

Sword7, acceleration is the second derivative of position. Acceleration changes velocity, not position. Velocity changes position. The problem is that you are not representing velocity. You need to represent both the position and velocity of the object.

There are many ways to propagate state over time. You are using the simplest such technique, the Euler method. Euler integration is never going to be particularly accurate. That said, there are several variants even on the Euler method for a second order differential equation. Basic Euler uses

[tex]\aligned<br /> {\boldsymbol a}(t_{n}) &=<br /> -\,\frac {GM}{||{\boldsymbol x}(t_{n})||^3}{\boldsymbol x}(t_{n}) \\<br /> {\boldsymbol x}(t_{n+1}) &=<br /> {\boldsymbol x}(t_{n}) + {\boldsymbol v}(t_{n})\Delta t \\<br /> {\boldsymbol v}(t_{n+1}) &=<br /> {\boldsymbol v}(t_{n}) + {\boldsymbol a}(t_{n})\Delta t<br /> \endaligned[/tex]

In other words, calculate the acceleration based on the position, update the position vector using the current velocity vector, and update the velocity vector using the acceleration just calculated.

A slight variation called the symplectic Euler method or semi-implicit Euler method will give much better results than basic Euler:

[tex]\aligned<br /> {\boldsymbol a}(t_{n}) &=<br /> -\,\frac {GM}{||{\boldsymbol x}(t_{n})||^3}{\boldsymbol x}(t_{n}) \\<br /> {\boldsymbol v}(t_{n+1}) &=<br /> {\boldsymbol v}(t_{n}) + {\boldsymbol a}(t_{n})\Delta t \\<br /> {\boldsymbol x}(t_{n+1}) &=<br /> {\boldsymbol x}(t_{n}) + {\boldsymbol v}(t_{n+1})\Delta t<br /> \endaligned[/tex]

In other words, calculate the acceleration based on the position, update the velocity vector using the acceleration just calculated, and update the position vector using the updated velocity vector.

There are much, much better techniques than either of these. Ask if you are interested.
 
D H said:
That is exactly what he is doing now.

Is it? It looks to me like he's multiplying by the component rather than the full radius vector. The equation I provided was for the force along the radius vector, not yet in components. The two are certainly not equivilent, but it may be the case that what he's doing turns out to be correct and I'm simply not seeing it.

:bugeye: Don't do that!

...Why not?
 
Nabeshin said:
Is it? It looks to me like he's multiplying by the component rather than the full radius vector.
As you noted, a correct expression for the force due to gravity is

[tex]F_{g}=-\frac{GMm}{r^3}\vec{r}[/tex][/QUOTE]

Denote the components of the position vector as [itex]p_x, p_y, p_z[/i[/itex]. Then [itex]r^2 = {p_x}^2+{p_y}^2+{p_z}^2[/tex]. The <i>x,y,z</i> components of the force vector are thus<br /> <br /> [tex]\aligned<br /> F_x &=-\frac{GMm}{r^3}p_x \\<br /> F_y &=-\frac{GMm}{r^3}p_y \\<br /> F_z &=-\frac{GMm}{r^3}p_z<br /> \endaligned[/tex]<br /> <br /> This is exactly what Sword7 did.<br /> <br /> <blockquote data-attributes="" data-quote="Nabeshin" data-source="post: 2086150" class="bbCodeBlock bbCodeBlock--expandable bbCodeBlock--quote js-expandWatch"> <div class="bbCodeBlock-title"> Nabeshin said: </div> <div class="bbCodeBlock-content"> <div class="bbCodeBlock-expandContent js-expandContent "> <blockquote data-attributes="" data-quote="D H" data-source="post: 2085918" class="bbCodeBlock bbCodeBlock--expandable bbCodeBlock--quote js-expandWatch"> <div class="bbCodeBlock-title"> D H said: </div> <div class="bbCodeBlock-content"> <div class="bbCodeBlock-expandContent js-expandContent "> <blockquote data-attributes="" data-quote="Nabeshin" data-source="post: 2085613" class="bbCodeBlock bbCodeBlock--expandable bbCodeBlock--quote js-expandWatch"> <div class="bbCodeBlock-title"> Nabeshin said: </div> <div class="bbCodeBlock-content"> <div class="bbCodeBlock-expandContent js-expandContent "> Your formula isn't directly in either of these forms, so I think what you should use is something like this:<br /> [tex]F_{gx}=-\frac{GMm}{p_{x}^2}\hat{p_{x}}[/tex] </div> </div> </blockquote><img src="https://www.physicsforums.com/styles/physicsforums/xenforo/smilies/oldschool/bugeye.gif" class="smilie" loading="lazy" alt=":bugeye:" title="Bug Eye :bugeye:" data-shortname=":bugeye:" /> Don't do that! </div> </div> </blockquote>...Why not? </div> </div> </blockquote>Because it's wrong.<br /> <br /> Consider the case [itex]p_x=p_y=0, p_z=r\ne0[/itex]. You're equation would yield infinite values for the <i>x-</i> and <i>y-</i> components of the force vector.[/itex]
 
  • #10
D H said:
As you noted, a correct expression for the force due to gravity is

[tex]F_{g}=-\frac{GMm}{r^3}\vec{r}[/tex]

Denote the components of the position vector as [itex]p_x, p_y, p_z[/i[/itex]. Then [itex]r^2 = {p_x}^2+{p_y}^2+{p_z}^2[/tex]. The <i>x,y,z</i> components of the force vector are thus<br /> <br /> [tex]\aligned<br /> F_x &=-\frac{GMm}{r^3}p_x \\<br /> F_y &=-\frac{GMm}{r^3}p_y \\<br /> F_z &=-\frac{GMm}{r^3}p_z<br /> \endaligned[/tex]<br /> <br /> This is exactly what Sword7 did.Because it's wrong.<br /> <br /> Consider the case [itex]p_x=p_y=0, p_z=r\ne0[/itex]. You're equation would yield infinite values for the <i>x-</i> and <i>y-</i> components of the force vector.[/itex]
[itex] <br /> Ok I see now. <br /> <br /> <blockquote data-attributes="" data-quote="" data-source="" class="bbCodeBlock bbCodeBlock--expandable bbCodeBlock--quote js-expandWatch"> <div class="bbCodeBlock-content"> <div class="bbCodeBlock-expandContent js-expandContent "> Spacecraft accelerated around Earth then quickly went away. At near center, its peak was near speed of light! Then it just started slow down but never return back. I think that spacecraft fell down to surface too fast instead of staying in orbit. However, there is another problem. When one of px/py/pz became negative, one of ax/ay/az jumped. Does anyone know any solution? That's why I want to write my own space simulator. </div> </div> </blockquote><br /> This sounds to me like your step size is too large, and so the when the p value jumped suddenly from -500 to 500 you see a large jump in one of the a's. As far as your object going near the speed of light, what are your initial conditions? Because that just sounds wrong. (Do you have a condition that stops the program if r<Re?)[/itex]
 
  • #11
Read the second part of post #7. He is not representing velocity.
 
  • #12
D H said:
Read the second part of post #7. He is not representing velocity.

Wow how did I miss that. Sorry.
 
  • #13
Nabeshin said:
Wow how did I miss that. Sorry.

'Cause its hidden. In the first post, he was representing velocity, but he called it ax, ay, az. The problem there was that he has updating the velocity incorrectly by using acceleration as a scale. In Sword7's next post (post #3), he corrected the calculation of acceleration as a vector rather than a scalar, but messed things up by getting rid of velocity.
 
  • #14
Hello again,

Good news! Huston, we finally got my spacecraft in orbit...

I finally resolved a problem with my gravity calculation after I fixed my code. I corrected initial velocity setting from 14km/s to 7km/s and finally got spacecraft in orbit instead of runaway. That was elliptic orbit. I now recongized that 14km/s is too fast for Earth orbit.

I recongized that I miswrote my equations above and now mean that 'Ax += (Fgx / m); ...', not 'Ax += a; ...'. I enhanced my code by adding Vx, Vy, and Vz variable for current velocity updating.

Also, I added crash detection (r < Re). I set all initial velocity to zero and ran it. It fell down to Earth surface like dropping a ball and my code printed "Crash!" and exit. It worked so well.

Everything worked so well. I now am working on n-body equations that are affecting orbit path and 3D animations right now. Later I will add orbital determination and atmosphere techniques for air-resistance (re-entry, launches, and orbit decay).

DH, I saw one of your postings said that much better techniques than that technique from my astrodynamics book. Yes, I am interested in that.

Thank for your help.
Sword7
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K