Search code examples
c++integral

Rectangle Method C++


I copied an example project from here, but I don't understand part of it: (i - 0.5). Why is (i - 0.5) being computed?

double rectangle_integrate(double a, double b, int subintervals, double (*function)(double))
{
   double result;
   double interval;
   int i;

   interval=(b-a)/subintervals;
   result=0;

   for(i=1;i<=subintervals;i++){
      result+=function(a+interval*(i-0.5));
   }
   result*=interval;

   return result;
} 

Solution

  • It represents the middle of the interval. When i is 1 the interval is 0 to 1, and 0.5 is the middle of it. Etc.