/*
 * Integ.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `Java from Basics'
 * section: `Example - Integration'
 *
 * This program performs an integration of 1/(1+x) between x=1 and x=2
 * using the trapezium rule with 100 divisions. This is the first real
 * program documented. Note the `for' loop and the usage of `.0' on
 * numbers to stop integer division occurring.
 */
public class Integ
{
  public static void main(String[] argv)
  {
    double total, x, y;

    total = 0;

    // We are using the trapezium rule, so each end
    // point has a weighting of one.

    total += 1 / (1 + 1.0);  // LH end, x = 1
    total += 1 / (1 + 2.0);  // RH end, x = 2

    // whilst each middle point has weight two
    
    for (int i=1; i<=99; i++)
    {
      x = 1 + i/100.0;
      y = 1 / (x + 1);

      total += 2 * y;
    }

    // Finally, we multiply by half the step size
	
    total *= 0.005;

    System.out.println(total);
  }
}

