/** A class of code for computing a numerical integral
 *
 * @author Duncan Buell
 *
 * written: 14 October 2007
 *
**/
public class ComputeIntegral
{

/* **************************************************************************** */
/** Put in a constructor just in case I want one later.
**/
  public ComputeIntegral()
  {
  }
   
/* **************************************************************************** */
/** compute the integral
**/
  public double doIntegral(double inputLeftEnd, double inputRightEnd, int segmentCount)
  {
    double leftEnd,rightEnd,returnValue,addinArea,width;
    MyFunction theFunc = new MyFunction();

    if(inputLeftEnd > inputRightEnd)
    {
      System.out.printf("ERROR: inputLeftEnd %f is larger than inputRightEnd %f%n",
                          inputLeftEnd,inputRightEnd);
      System.exit(0);
    }

    width = (inputRightEnd-inputLeftEnd)/segmentCount;
//    System.out.printf("%ninterval width is %f%n", width);

    leftEnd = inputLeftEnd;
    rightEnd = leftEnd + width;
    returnValue = 0.0;

    while(leftEnd < inputRightEnd)
    {
      addinArea = theFunc.myEval(leftEnd) * width; 
      returnValue += addinArea;
//      System.out.printf("left,right,addin,total %f %f %f %f%n",
//                         leftEnd,rightEnd,addinArea,returnValue);

      leftEnd = leftEnd + width;
      rightEnd = rightEnd + width;
    }

    return returnValue;
  } // public double doIntegral(double leftEnd, double rightEnd, int segmentCount)
  
} // public class ComputeIntegral

