title: Restricted 3 body simulation not working correctly
body: <p>I am currently trying to do an assignment where i have to write a simulation for the restricted 3 body gravitational problem, with two fixed masses and one test mass. The information i have been given on the problem is: <a href="http://www.flickr.com/photos/91029993@N07/8269806430/in/photostream" rel="nofollow noreferrer">Check out this link</a> and here is my program so far:</p>
  
  <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  int main (int argc, char* argv[])
  {
      double dt=0.005, x[20000],y[20000],xv,yv,ax,ay,mneg,mpos,time,radius=0.01;
      int n,validation=0;
      FILE* output=fopen("proj1.out", "w");
  
      printf("\n");
  
  
      if((argv[1]==NULL) || (argv[2]==NULL) || (argv[3]==NULL) || (argv[4]==NULL) ||     (argv[5]==NULL) || (argv[6]==NULL)) 
      {
          printf("************************ ERROR! ***********************\n");
          printf("**     Not enough comand line arguments input.       **\n");
          printf("**     Please run again with the correct amount (6). **\n");
          printf("*******************************************************\n");
          validation=1;
          goto VALIDATIONFAIL;
      }
  if((sscanf(argv[1], "%lf", &amp;mneg)==NULL) || (sscanf(argv[2], "%lf", &amp;mpos)==NULL) || (sscanf(argv[3], "%lf", &amp;x[0])==NULL) ||
  (sscanf(argv[4], "%lf", &amp;y[0])==NULL) || (sscanf(argv[5], "%lf", &amp;xv)==NULL) || (sscanf(argv[6], "%lf", &amp;yv)==NULL) )
  {
      printf("************************* ERROR! ************************\n");
      printf("** Input values must be numbers. Please run again with **\n");                 
      printf("** with numerical inputs (6).                          **\n");
      printf("*********************************************************\n");
      validation=1;
    goto VALIDATIONFAIL;
  }
  
  sscanf(argv[1], "%lf", &amp;mneg);
  sscanf(argv[2], "%lf", &amp;mpos);
  sscanf(argv[3], "%lf", &amp;x[0]);
  sscanf(argv[4], "%lf", &amp;y[0]);
  sscanf(argv[5], "%lf", &amp;xv);
  sscanf(argv[6], "%lf", &amp;yv);
  
  
      x[1]=x[0]+(xv*dt);
      y[1]=y[0]+(yv*dt);
  
  for(n=1;n&lt;10000;n++)
      {
          if(x[n-1]&gt;=(1-radius) &amp;&amp; x[n-1]&lt;=(1+radius) &amp;&amp; y[n-1]&gt;=(0-radius) &amp;&amp; y[n-1]&lt;=(0+radius))
          {
              printf("Test mass has collided with M+ at (1,0), Exiting...\n");
              goto EXIT;
          }
          else if(x[n-1]&gt;=(-1-radius) &amp;&amp; x[n-1]&lt;=(-1+radius) &amp;&amp; y[n-1]&gt;=(0-radius) &amp;&amp; y[n-1]&lt;=(0+radius))
          {
              printf("Test mass has collided with M- at (-1,0), Exiting...\n");
              goto EXIT;
          }
          else
          {
              double dxn = x[n] + 1;
              double dxp = x[n] - 1;
              double mnegdist = pow((dxn*dxn + (y[n]*y[n])), -1.5);
              double mposdist = pow((dxp*dxp + (y[n]*y[n])), -1.5);
  
              ax =  -(mpos*dxp*mposdist+mneg*dxn*mnegdist);
              ay =  -(mpos*y[n]*mposdist+mneg*y[n]*mnegdist); 
  
  
              x[n+1]=((2*x[n])-x[n-1] +(dt*dt*ax));
              y[n+1]=((2*y[n])-y[n-1]+(dt*dt*ay));
  
  
              fprintf(output, "%lf %lf\n",x[n-1], y[n-1]); 
          }
      }
  VALIDATIONFAIL: 
  printf("\n");
  return(EXIT_FAILURE);   
  
  EXIT:
  return(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>My program is working to certain extent but i am getting some weird problems that i hope someone can help me with. </p>
  
  <p>The main issue is that when the test mass gets to a point in its trajectory when it should go off and start to orbit about the other mass it instead just shoots off on a straight line to infinity! at first i thought it was that the masses were colliding so i put in the radius check, but in some cases this does work, in some cases it doesn't, and in some cases the masses collide earlier on before the trajectory goes wrong anyway so this clearly isn't the issue. I am not sure if i have explained that all too well so here is a picture to show you what i mean. (the simulation on the right is from <a href="http://www.upscale.utoronto.ca/PVB/Harrison/Flash/Chaos/ThreeBody/ThreeBody.html" rel="nofollow noreferrer">here</a>)</p>
  
  <p><img src="https://i.stack.imgur.com/qsA2a.png" alt="line"></p>
  
  <p>However, this is not always the case, sometimes instead of going in a straight line, the trajectory just goes crazy when it should go over to the other mass, like this: 
  <img src="https://i.stack.imgur.com/aXyBA.png" alt="crazy"></p>
  
  <p>I really have absolutely no idea whats going on i have spent days trying to figure this out but just cant seem to get anywhere, so any help in identifying where my problem is would be very much appreciated.</p>