Rectangle method
In mathematics, specifically in integral calculus, the rectangle method (also called the midpoint or mid-ordinate rule) computes an approximation to a definite integral, made by finding the area of a collection of rectangles whose heights are determined by the values of the function.
Specifically, the interval over which the function is to be integrated is divided into equal subintervals of length . The rectangles are then drawn so that either their left or right corners, or the middle of their top line lies on the graph of the function, with bases running along the -axis. The approximation to the integral is then calculated by adding up the areas (base multiplied by height) of the rectangles, giving the formula:
where is defined to be either , or , giving the approximation based on the top-left corner, the top-right corner or the middle of the top line respectively.
As n gets larger, this approximation gets more accurate. In fact, this computation is the spirit of the definition of the Riemann integral and the limit of this approximation as is defined and equal to the integral of on if this Riemann integral is defined. Note that this is true regardless of which is used, however the midpoint approximation tends to be more accurate for finite .
-
Top-left corner approximation
-
Midpoint approximation
-
Top-right corner approximation
Error
For a function which is twice differentiable, the approximation error in each section of the midpoint rule decays as the cube of the width of the rectangle. (For a derivation based on a Taylor approximation, see Midpoint method)
for some in . Summing this, the approximation error for intervals with width is less than or equal to
where is the number of nodes
in terms of the total interval, we know that so we can rewrite the expression:
for some in .
C example program
#include <stdio.h>
#include <math.h>
double f(double x){
return sin(x);
}
double rectangle_integrate(double a, double b, int subintervals){
double result;
double interval;
int i;
interval=(b-a)/subintervals;
result=0;
for(i=1;i<=subintervals;i++){
result+=f(a+interval*(i-0.5));
}
result*=interval;
return result;
}
int main(void){
double integral;
integral=rectangle_integrate(0,2,100);
printf("Integral: %f \n",integral);
return 0;
}
Fortran Example Program
Program Calc
Double Precision f,y,a,b,J,mult,sum,c1,c2,ci
Sum=0
c2=0
ci=0
Print*,'Enter the start and end of the interval'
Read*,a,b
If (b.gt.a) then
goto 1
Else
goto 2
End If
1 Do J=a,b,.00000001
c1=J
Y=F(((c1+c2)/2))
Mult=Y*.00000001
Sum=sum+mult
c2=c1
End Do
2 Do J=a,b,-.00000001
c1=J
Y=F(((c1+c2)/2))
Mult=Y*.00000001
Sum=sum+mult
c2=c1
End Do
Print*,Sum
3 Format (F20.5)
End
Double Precision Function f(x)
Double Precision x
F=(4)/((x**2)+1)
Return
End
MATLAB implementation
The Rectangle method can be implemented in MATLAB as follows:
function [I] = riemann(f,a,b,n)
%f=name of function, a=start value, b=end value, n=number of
%iterations
%made by Carolina, Niklas, Benjamin and Damiano
h=(b-a)./n;
S=f(a);
i=1:1:n-1;
x=a+h.*i;
y=f(x);
S=S+sum(y);
S=S+f(b);
%%I think it is wrong, because it is divided into n rectangles, but sum n+1 times.
%%We can either sum f(a) or f(b) for top-right corner or top left corner, but can't both. Am I right?
I=h.*S;
end