1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Apr 30, 2012 #1
    1. The problem statement, all variables and given/known data

    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.

    2. Relevant equations


    3. 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 (Text):
    for(i=0;i<500000;i++) {
        // 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="Gray"]//changing velocity components
        dvx=ax*dt;  //Change in velocity ...
        dvy=ay*dt;  // over one time step
        vx+=dvx;     // New velocty

    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: Apr 30, 2012
  2. jcsd
  3. May 1, 2012 #2


    User Avatar
    Homework Helper

    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.
  4. May 1, 2012 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
  5. May 1, 2012 #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.
  6. May 1, 2012 #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.
  7. May 1, 2012 #6


    User Avatar
    Homework Helper

    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?)
  8. May 1, 2012 #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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook