Orbit program - three objects (C programming, but other programmers could help)

Click For Summary

Homework Help Overview

The discussion revolves around a programming task involving simulating the motion of three bodies in a gravitational system using C. The original poster is attempting to create a program where two smaller objects orbit a larger central object, with one of the smaller objects' position being influenced by both the larger object and the central object. The problem is set in a two-dimensional space and raises questions about the physics involved in the simulation.

Discussion Character

  • Mixed

Approaches and Questions Raised

  • Participants explore the physics of gravitational interactions and the correct implementation of acceleration components in the code. There are discussions about the assumptions regarding mass and the fixed position of the central object. Some participants question the treatment of the coordinate system and the implications of using a non-inertial frame.

Discussion Status

The conversation has progressed with participants providing feedback on the code and discussing potential issues. The original poster has indicated that they resolved some issues related to the order of acceleration calculations. However, there are still concerns about the behavior of the simulation when the small object approaches the larger object, indicating ongoing exploration of the problem.

Contextual Notes

Participants note that the simulation is not intended to be an exact representation of celestial mechanics but rather a general gravitational simulation. There are mentions of specific software used for graphics, which may influence the implementation of the program.

Who Am I
Messages
89
Reaction score
2

Homework Statement



I'm trying to write a program with three bodies, two in orbit around a large central object. The main object orbits fine, that was dealt with earlier. I'm having trouble making a smaller object that is the only object whose position is dependent on the position of both of the other two masses.

I'm writing this in C, though I think anyone with some programming experience in physics should be able to give me a hand.

It's only being done in two dimensions.

This also might be more of a basic physics question than a programming question.

Homework Equations



F=Gm2m1/r2

The Attempt at a Solution



So, this is what I have inside my loop.

Everything in blue works. Orange is questionable, red is most likely the source of the error.

Code:
for(i=0;i<500000;i++) {
    putpoint_plot(x,y,10,1,3,0.6,1);  
    putpoint_plot(x2,y2,10,2,4,0.3,1);
    delay_plot(1);
    flush_plot();
    
    // distance magnitudes
    r=sqrt(x*x+y*y); //distance of large object to central object
    r1=sqrt(x2*x2+y2*y2); // distance of tiny object to central object
    r2=sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y)); //distance of tiny object to large object

    //acceleration components
     a=-1.0/r/r;
    
    ax=a*x/r;   // Components ...
    ay=a*y/r;   // of the acceleration
    
    a1=-1.0/r1/r1;  //acceleration of small object towards the massive central object
    a2=-0.5/r2/r2; //Acceleration of tiny object towards the large object
    
    ax2=a1*x2/r1+a2*(x-x2)/r2;
    ay2=a1*y2/r1+a2*(y-y2)/r2;

    //changing velocity components
    dvx=ax*dt;  //Change in velocity ...
    dvy=ay*dt;  // over one time step 
    dvx2=ax2*dt;
    dvy2=ay2*dt;    
    vx+=dvx;     // New velocty
    vy+=dvy;
    vx2+=dvx2;
    vy2+=dvy2;
etc. until the program returns the two positions.

Now, my difficulty is that I don't really know how to break up the acceleration into the x and y components for the second object using the method that is used above. As I stated, the acceleration of the main object works fine, but I don't quite what math was used to get there as I used this from an example.

So, the code in red is the part where I am breaking the two magnitudes of acceleration up into x and y components. The second portion is supposed to be the acceleration towards the moving object, which is really what's giving me trouble.

I'd definitely appreciate some help on this one. It's kind of tricky.
 
Last edited:
Physics news on Phys.org
hmm. I guess the first thing is to make sure I understand what you want the physics to be. So it looks like you want the large object to have half the mass of the central object. And you want the central object to be fixed at the centre of the coordinate system? And you want the motion of the large object to be affected only by the gravity of the central object? And you want the motion of the tiny object to be affected by the gravity of both the central object and the large object? I will assume these things to be true when I check through your code.

I've had a read-through of your code, and it all looks fine except for one thing in the red section. You've used (x-x2) and (y-y2) but it should have been (x2-x) and (y2-y), this is to get the correct direction of the gravitational force on the tiny object. You need to remember that a2 is negative, so the force is already attractive, so you should use (x2-x) and (y2-y) so that the force on the tiny object will be towards the large object.

Apart from that, the code looks good to me. Which is a good effort, because coding stuff like this can get very complicated very quickly, but you have kept it fairly neat. One bit which might be slightly confusing is that you use x2 and y2 as the coordinates of the tiny object, but you use r1 as the magnitude of this position. But it is neat enough, so I wouldn't worry about that.
 
Even the code in blue is questionable. Given your code, the central object has twice the mass of the large object, yet you are treating this coordinate system with the central object stuck at the origin as if it were an inertial frame. That central object is attracted to your large object, and by quite a bit. Think of Newton's third law.

You can pin the central object to the origin, but that means you need to modify your accelerations to account for this frame being non-inertial.
 
The central object being pinned isn't a source of error. This isn't meant to be an exact simulation of an object in space, but rather a general gravitational simulation where one object has a lot of inertia compared to another object.

The accelerations are easy to simply switch out.

And switching the two doesn't work. What I keep getting when I run the code is that after a certain point, the expected effects change. When the small object gets close to the main object, it acts normal. When it gets to the smaller object, it starts to reverse its acceleration. The larger the acceleration, the larger the range of the reversal is. Switching the two around only changes the direction of reversal.

The way I have it here makes it attract to the object until it gets fairly close. Then, if I switch them, it repels from the object until it gets close.

I don't know what I am missing here exactly. It looks right but clearly it isn't quite right.
 
Okay, I've got it fixed now.

I had everything right mathematically and conceptually. I was just reversing the order of my acceleration, so the code wasn't told the new acceleration until after it was broken up into different components.
 
Who Am I said:
Okay, I've got it fixed now.

I had everything right mathematically and conceptually. I was just reversing the order of my acceleration, so the code wasn't told the new acceleration until after it was broken up into different components.

excellent, good work. just out of curiosity, what software did you use for the graphics? (because a C compiler would enable you to write a program that outputs text, but I'm guessing you need some other software for graphics?)
 
Well, there's a graphics package called "philsplot" made by an astronomer at my university that is on the school computers.

It's cool, but the system is honestly a bit rubbish, since points you draw erase old points and what not (putting lines through the old lines). It's not a very versatile system. It took a long time to finesse the program to not put out an error where there are tails dragging around each of the objects that delete themselves too slowly.

I've now added a third object and made the small object do a gravitational slingshot off of both objects.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K