# Need Source Code In c For 2-Bodies Gravitational Problem

• Zaheer Ahmed
In summary, Zaheer wrote his own code to solve the 2-body gravitational problem, but it does not behave as expected. He is unsure where the problem is, but is hopeful that someone with more experience can help him out.
Zaheer Ahmed
Need Source Code In "c" For 2-Bodies Gravitational Problem

Hello,

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 anyone 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

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.

sounds like a (-) problem...search for stuff by "chris hecker"

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.

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

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 got to be a lot of code out there to get ideas from.

I wrote my own codes, they work correctly for 3 or more than 3 bodies but for 2bodies

Hello,

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

## 1. What is the basic concept behind the 2-Bodies Gravitational Problem?

The 2-Bodies Gravitational Problem is a mathematical problem that aims to calculate the motion of two objects under the influence of their mutual gravitational attraction. This problem is based on Newton's law of universal gravitation, which states that any two objects in the universe exert a force of attraction on each other.

## 2. Why is it important to have the source code in C for this problem?

C is a high-level programming language that is widely used in scientific and mathematical applications. Its efficient and optimized code allows for faster and more accurate calculations, making it ideal for solving complex problems such as the 2-Bodies Gravitational Problem.

## 3. Can the source code in C be modified for other gravitational problems?

Yes, the source code can be modified to solve other gravitational problems involving more than two bodies. However, the code may need to be adapted and optimized depending on the specific problem and its parameters.

## 4. How accurate is the source code in C for solving the 2-Bodies Gravitational Problem?

The accuracy of the source code depends on the input parameters and the precision of the calculations. With proper input and careful programming, the source code in C can provide highly accurate results for the 2-Bodies Gravitational Problem.

## 5. Are there any limitations to using the source code in C for this problem?

One limitation of using the source code in C for the 2-Bodies Gravitational Problem is that it assumes point masses, meaning that the size and shape of the bodies are not taken into account. This may not accurately reflect the real-world scenario and can affect the accuracy of the results. Additionally, the source code may not be suitable for solving highly complex or chaotic gravitational systems.

• Astronomy and Astrophysics
Replies
3
Views
949
• Astronomy and Astrophysics
Replies
6
Views
2K
• Programming and Computer Science
Replies
2
Views
447
Replies
3
Views
1K
• Classical Physics
Replies
2
Views
1K
Replies
1
Views
1K
• Classical Physics
Replies
86
Views
4K
• Astronomy and Astrophysics
Replies
1
Views
1K
• Programming and Computer Science
Replies
1
Views
996
• Programming and Computer Science
Replies
14
Views
1K