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

In summary, the programmer is 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. He is using the equations F=Gm2m1/r2 and r1=sqrt(x2*x2+y2*y2). The first part is working, but the second is giving him trouble. He needs help understanding what he needs to do to make this work.
  • #1
Who Am I
89
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
    [COLOR="Blue"] a=-1.0/r/r;[/COLOR]
    
   [COLOR="Blue"] ax=a*x/r;   // Components ...
    ay=a*y/r;   // of the acceleration[/COLOR]
    
[COLOR="orange"]    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
    [/COLOR]
    [COLOR="red"]ax2=a1*x2/r1+a2*(x-x2)/r2;
    ay2=a1*y2/r1+a2*(y-y2)/r2;
[/COLOR]
    [COLOR="Gray"]//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;
[/COLOR]
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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?)
 
  • #7
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.
 

1. What is the purpose of the "Orbit program - three objects"?

The "Orbit program - three objects" is a computer program written in the C programming language that simulates the movement of three objects in space. It is designed to demonstrate the principles of orbital mechanics and can be used for educational or scientific purposes.

2. How does the program work?

The program uses Newton's laws of motion and principles of gravitation to calculate the positions and velocities of the three objects at each time step. It takes into account the masses, initial positions, and velocities of the objects, as well as any external forces acting upon them, to simulate their movement in a three-dimensional space.

3. Can I input my own values for the three objects?

Yes, the program allows users to input their own values for the masses, initial positions, and velocities of the three objects. This can be done through the program's interface or by modifying the code itself.

4. Are there any limitations to the program?

As with any computer program, there are limitations to what the "Orbit program - three objects" can simulate. It is based on simplified models of orbital mechanics and may not accurately represent real-world scenarios. Additionally, the program may not be able to handle extremely large or small values, which could affect the accuracy of the simulation.

5. Can other programmers contribute to the program?

Yes, the "Orbit program - three objects" is open-source and can be accessed and modified by other programmers. Contributions, suggestions, and improvements are welcome and can help to enhance the functionality and accuracy of the program.

Similar threads

  • Introductory Physics Homework Help
Replies
11
Views
771
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Introductory Physics Homework Help
Replies
16
Views
3K
  • Introductory Physics Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Astronomy and Astrophysics
Replies
4
Views
1K
Back
Top