3 bodies problem-programming in C

  • Thread starter Thread starter d_m
  • Start date Start date
  • Tags Tags
    bodies
AI Thread Summary
The user seeks assistance in programming a simulation of the three-body problem in C, specifically focusing on the movement of a third mass influenced by two fixed masses. They have outlined the need to calculate the gravitational force using the formula F = G(m1*m2)/r² and to derive the position and velocity of the third mass using F = m(d²x/dt²). The user has provided initial code but is uncertain about how to implement the calculations for acceleration and position updates in the simulation. They are looking for guidance on completing the programming aspects, particularly in populating the arrays for acceleration and position. The discussion highlights a blend of physics and programming challenges in creating the simulation.
d_m
Messages
2
Reaction score
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
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.
 
Back
Top