///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Program file name: Millikan2.java                                     //
//                                                                       //
// © Tao Pang 2006                                                       //
//                                                                       //
// Last modified: January 18, 2006                                       //
//                                                                       //
// (1) This Java program is part of 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.     //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

// An example of directly fitting the Millikan data to
// p1(x)=a0+a1*x.

import java.lang.*;
public class Millikan2 {
  public static void main(String argv[]) {
    double x[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
      15, 16, 17, 18};
    double f[] = {6.558, 8.206, 9.880, 11.50, 13.14,
      14.81, 16.40, 18.04, 19.68, 21.32, 22.96, 24.60,
      26.24, 27.88, 29.52};
    int n = x.length-1;
    double c1 = 0, c2 = 0, c3 = 0, c4 = 0;
    for (int i=0; i<=n; ++i) {
      c1 += x[i];
      c2 += x[i]*x[i];
      c3 += f[i];
      c4 += f[i]*x[i];
    }
    double g = c1*c1-c2*(n+1);
    double a0 = (c1*c4-c2*c3)/g;
    double a1 = (c1*c3-c4*(n+1))/g;
    System.out.println("Fundamental charge: " + a1);
    System.out.println("Estimated error: " + a0);
  }
}
