Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need Source Code In c For 2-Bodies Gravitational Problem

  1. May 4, 2005 #1
    Need Source Code In "c" For 2-Bodies Gravitational Problem


    My name is Zaheer. I am working on N-bodies gravitational problem, i made n-bodies program in "c" but the behaviour of the bodies is not correct as it is not making a complete circle, insted it is making a semi circle and coming back to the original position, if any one of you worked on this problem and can provide source codes for me for 2-bodies problem so that i can compare my program with the one that will be provided it will be a great help for me, i will be thankful to you. Regards Zaheer
  2. jcsd
  3. May 21, 2005 #2
    Sounds like a job for Google. I tried a search, and even found a post from you! The book An Introduction to Computer Simulation Methods has a whole chapter dedictated to coding the 2-body problem, using BASIC. Check it out if you get desperate and can read BASIC well enough to convert the code.
  4. May 22, 2005 #3
    sounds like a (-) problem...search for stuff by "chris hecker"
  5. May 23, 2005 #4


    User Avatar
    Staff Emeritus
    Science Advisor

    I could repeat my previous advice - work on a simpler problem first, it's all differential equations - but I'm not sure how much good that would do.

    A better understanding of the physics of a circular orbit might help, too - if you understand why a circular orbit is circular, you should be able to figure out where the code is going wrong. But I'm not sure how to "bottle up" such an understanding.

    Finding some code somewhere wouldn't really teach Z.A. anything - debugging the code he wrote would be far more productive, if he can manage to accomplish the task.

    Looking over other people's code might be instructive, but just lifting it wholesale won't be very educational.
  6. May 23, 2005 #5
    copied code would teach him stuff it he wanted to reprogramm it to test where he went wrong. I went through an entire 3D engine ...and i know the jist of how to organize it...and I thank Eberly, Irrlicht and the Ogre Engines
  7. May 24, 2005 #6
    Using the book I mentioned above, I coded a VB 2-body orbit function. It was super simple; the main loop has only 3 lines of code to calculate the position. There's gotta be a lot of code out there to get ideas from.
  8. May 26, 2005 #7
    I wrote my own codes, they work correctly for 3 or more than 3 bodies but for 2bodies


    Thanks for your valuable reply, i wrote my own codes using Leap Frog method and Rung-Kutta 4th order method, Considering my program,when i am working with two bodies (Sun-Earth) the behavior of Earth is not the same as expected, insted of making one complete rotation around the Sun in 53 weeks it is making more than two rotations. Whereas when i am working with three bodies, as well as with solar system, the behavior of Earth is as expected, Earth is making one complete rotation in 53 weeks, this shows that some where i am doing calculation mistake, my codes are available in the physics forms, as i already send one post by the name Zaheer A. If anyone want to refer please go though that. Thanks for your valuable time. Zaheer A
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook