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

Homework Help: 3 bodies problem-programming in C

  1. Jan 3, 2010 #1

    d_m

    User Avatar

    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
     
  2. jcsd
  3. Jan 4, 2010 #2

    d_m

    User Avatar

    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 gravitional field formula.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook