/* This file contains the definitions of some numerical methods
   functions.

------------------------------------------------------------------*/

#include "NumMethods.h"

/*-----------------------------------------------------------------
RectangleMethod approximates the definite integral of a function 
   using the rectangle method with altitudes chosen at the 
   midpoints of the subintervals.

   Receive: FPtr, a pointer to the function being integrated
            Left, Right, the endpoints of the integration
            n, the number of rectangles to be used
   Return:  The approximate value of the definite integral of the
               function over the interval [Left, Right]
------------------------------------------------------------------*/

double RectangleMethod(FunctionPtr FPtr, 
                       double Left, double Right, int n)
{
   double
      DeltaX = (Right - Left) / n,
      MidPt = Left + DeltaX / 2.0,
      Sum = 0.0;

   for (int i = 1; i <= n; i++)    // for each rectangle
   {
      Sum += FPtr(MidPt);          // add f(midpoint) to Sum
      MidPt += DeltaX;             // move to next rectangle
   }

   return Sum * DeltaX;            // compute area approximation
}

// ... additional numerical methods function definitions
