/*************************************************************************/
/* Program file name: Ex1_7.java                                         */
/*                                                                       */
/* © Tao Pang 2006                                                       */
/*                                                                       */
/* Last modified: June 7, 2006                                           */
/*                                                                       */
/* (1) This Java program is created for the book, "An Introduction to    */
/*     Computational Physics, 2nd Edition," written by Tao Pang and      */
/*     published by Cambridge University Press on January 19, 2006.      */
/*                                                                       */
/* (2) No warranties, express or implied, are made for this program.     */
/*                                                                       */
/*************************************************************************/

// Program for Exercise 1.7.  The distance between Halley's comet
// and the Sun is calculated with the Euler algorithm.

import java.lang.*;
public class Ex1_7 {
  static final int n = 2000000, m = 20000;
  public static void main(String argv[]) {
    double t[] = new double [n];
    double r[] = new double [n];
    double h = 2.0/(n-1), k = 39.478428;

 // Initialization of the problem
    double x = 1.966843, y = 0, vx = 0, vy = 0.815795;
    t[0] = 0;
    r[0] = x;

 // Verlet algorithm for the position and velocity
    for (int i=0; i<n-1; ++i) {
      t[i+1]  = h*(i+1);
      x += h*vx;
      y += h*vy;
      double r2 = x*x+y*y;
      r[i+1] = Math.sqrt(r2);
      double r3 = r2*r[i+1];
      double gx = -k*x/r3;
      double gy = -k*y/r3;
      vx += h*gx;
      vy += h*gy;
    }
    for (int i=0; i<n-m; i+=m) {
      System.out.println(t[i]);
      System.out.println(r[i]);
      System.out.println();
    }
  }
}
