3 bodies problem-programming in C

  • Thread starter d_m
  • Start date
  • Tags
    bodies
In summary, the conversation discusses a programming problem involving calculating the movement of a third mass in a gravitational field created by two fixed masses. The program needs to be written in C and use allegro or GTK+ for the graphical part. The user must input the values for the masses, distances, and initial speed and position of the third mass. The program will then use equations to calculate the position and speed of the third mass over time. The biggest issue is with the programming aspect, especially for someone who is not proficient in C.
  • #1
d_m
2
0
Hello.

I have been folowing this forum for a while, but this is the 1st time I post. If I make any spelling mistakes, forgive me because I'm not an english born speaker.

My problem is the following, I have to write this program in C, and the graphical part is to be made in allegro or GTK+.

The program has 2 fixes masses, m1 and m2, over a rect horizontal line in the middle of the window, in which both the value of the masses as well as their distances are given by the user, and a 3rd mass, m3. The program will have to trace the movement of m3 in the gravitical field created by m1 and m2, oce the user gives the initial speed and position of m3 in the xy field.

I already know that i must 1st calculate the resultant force in m3, using F = G (m1.m2)/r2. Then I think I will have to use the equation F = m (d2 x(t))/(dt2) to get the position and speed of m3, but I'm not sure about how this is done.

My biggest problem is not about the mathematical/physical part of the problem, is about the programming part, because I'm not very good in programming in C.

Can anyone help me with this?

Thank you
 
Physics news on Phys.org
  • #2
So far I have only done this:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

main(int argc, char* argv[])
{
double x[101];
double y[101];
double m1;
double m2;
double ax[101];
double ay[101];
double x0;
double y0;
double vx0;
double vy0;
double dt;
int n;
FILE *output;

output = fopen("data.dat", "w");

sscanf(argv[1], "%f", &m1);
sscanf(argv[2], "%f", &m2);
sscanf(argv[3], "%f", &x0);
sscanf(argv[4], "%f", &y0);
sscanf(argv[5], "%f", &vx0);
sscanf(argv[6], "%f", &vy0);

dt=0.005;

x[0]=x0;
y[0]=y0;
x[1]=x0 + vx0*dt;
y[1]=y0 + vy0*dt;

printf("%lf %lf\n", x[1], y[1]);

for(n=1; n < 100; ++n)
{
ax[n]= ?
ay[n]= ?

x[n+1]= ?
y[n+1]= ?

fprintf(output, "%f %f\n", x[n+1],y[n+1]);

}
printf("%f %f", ax[1], ay[1]);

fclose(output);

return 0;
}



I'm not sure what i have tu put in ax[n], ay[n], x[n+1] and y[n+1], and I'm also sure about where I will use the gravitational field formula.
 
  • #3
for sharing your problem with us. I would approach this problem by first breaking it down into smaller, more manageable parts. The first step would be to familiarize myself with the programming language C and the libraries I will be using (Allegro or GTK+). This could involve researching online, reading tutorials, or seeking guidance from more experienced programmers.

Next, I would focus on the mathematical/physical aspect of the problem. As you mentioned, the first step would be to calculate the resultant force on m3 using the formula F = G (m1.m2)/r2. Then, I would use the equation F = m (d2 x(t))/(dt2) to calculate the position and speed of m3 at different time intervals. This could involve using loops and arrays in the programming language to keep track of the changing values.

I would also recommend breaking down the problem into smaller steps and testing each step as you go along. This will help you identify any errors or bugs in your code and make it easier to troubleshoot.

Additionally, I would suggest seeking help from other programmers or joining online forums or communities where you can ask for advice or guidance. Collaboration and learning from others is a valuable tool in problem-solving.

In conclusion, tackling a complex problem like the 3 bodies problem in programming can be challenging, but by breaking it down into smaller steps and seeking help when needed, you can successfully complete the task. Good luck with your project!
 

1. What is the 3 bodies problem in programming?

The 3 bodies problem in programming refers to a mathematical problem in which the behavior of three objects in space, such as planets, are governed by the laws of physics. It is a complex problem that can be solved using numerical methods in programming languages like C.

2. How do you approach solving the 3 bodies problem in C?

To solve the 3 bodies problem in C, one approach is to use numerical integration methods, such as the Runge-Kutta method, to solve the equations of motion for the three bodies. This involves breaking down the problem into smaller time steps and calculating the positions and velocities of the bodies at each step.

3. What are some challenges in programming the 3 bodies problem in C?

One challenge in programming the 3 bodies problem in C is the precision and accuracy of calculations. Due to the complex nature of the problem, small errors in calculation can quickly accumulate and lead to incorrect results. Another challenge is optimizing the code for efficiency, as the problem involves a large number of calculations.

4. Can the 3 bodies problem be solved exactly in C?

No, the 3 bodies problem cannot be solved exactly in any programming language. This is due to the chaotic nature of the problem, where even small changes in initial conditions can lead to drastically different outcomes. Therefore, numerical methods are used to approximate the solution.

5. What are some real-world applications of the 3 bodies problem in programming?

The 3 bodies problem has applications in fields such as astrophysics, celestial mechanics, and space exploration. It can be used to model the motion of planets, moons, and other celestial bodies, and to predict their future positions and interactions. It also has applications in engineering, such as predicting the trajectories of satellites and spacecraft.

Similar threads

  • Introductory Physics Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
4
Views
966
  • Programming and Computer Science
Replies
25
Views
2K
Replies
12
Views
349
  • Introductory Physics Homework Help
2
Replies
45
Views
2K
Replies
7
Views
1K
  • Classical Physics
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
10
Views
2K
Back
Top