/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. Support by National Science Foundation */ // IntegGauss.java: Gauss quadrature, need include gauss method import java.io.*; // Location of PrintWriter public class IntegGauss { static final double max_in = 1001; // Numb intervals static final double vmin = 0., vmax = 1.; // Int ranges static final double ME = 2.7182818284590452354E0 ; // Euler's const public static void main(String[] argv) throws IOException, FileNotFoundException { int i; double result; PrintWriter t = new PrintWriter( new FileOutputStream("IntegGauss.dat"), true); for ( i=3; i <= max_in; i += 2) { result = gaussint(i, vmin, vmax); t.println("" + i + " " + Math.abs(result-1 + 1/ME)); } System.out.println("Output in IntegGauss.dat"); } // f(x) public static double f (double x) {return (Math.exp(-x));} public static double gaussint (int no, double min, double max) { int n; double quadra = 0.; double w[] = new double[2001], x[] = new double[2001]; Gauss.gauss (no, 0, min, max, x, w); // Returns pts & wts for ( n=0; n < no; n++ ) { quadra += f(x[n])*w[n]; } // Calculate integral return (quadra); } }