3 bodies problem-programming in C

  • Thread starter Thread starter d_m
  • Start date Start date
  • Tags Tags
    bodies
Click For Summary
SUMMARY

The discussion focuses on implementing a C program to simulate the motion of a third mass (m3) influenced by two fixed masses (m1 and m2) using gravitational physics. The user inputs the masses and initial conditions, while the program calculates the resultant force on m3 using the formula F = G (m1.m2)/r². The poster seeks assistance with programming aspects, particularly in calculating acceleration and updating positions in the simulation loop. The provided code snippet outlines the initial setup but lacks the implementation of the physics calculations for ax[n], ay[n], x[n+1], and y[n+1].

PREREQUISITES
  • Understanding of Newton's law of gravitation
  • Familiarity with C programming language
  • Basic knowledge of numerical methods for simulating motion
  • Experience with file I/O in C for data output
NEXT STEPS
  • Implement gravitational force calculations in the C program using F = G (m1.m2)/r²
  • Calculate acceleration components ax[n] and ay[n] based on the resultant force
  • Update position arrays x[n+1] and y[n+1] using the calculated velocities
  • Explore Allegro or GTK+ for graphical representation of the simulation
USEFUL FOR

Students and developers interested in physics simulations, C programmers looking to enhance their skills, and anyone working on graphical applications using Allegro or GTK+.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 45 ·
2
Replies
45
Views
4K
  • · Replies 97 ·
4
Replies
97
Views
17K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
8
Views
10K
  • · Replies 12 ·
Replies
12
Views
2K