% area_under_curve_grid.m
%
%  Estimate the area between the x axis and the curve y=x^2 for 0<=x<=1.
%
%  Strategy: use a grid, that is, a regularly-spaced set of x values.  
%  The x values break up the interval [0,1] into n intervals, of width 1/n.
%  This also breaks the area under the curve into n strips.
%  We approximate the area of a strip by multiplying the width of
%  the strip by a height, in other words, (1/4) * x^2.
%
%  In calculus, this is called a "right Riemann sum".
%
n = input ( '  Enter the number of intervals to use: ' );

area = 0.0;

for i = 1 : n
%
%  The i-th interval is [ (i-1)/n, i/n ].
%  We could choose any x value in this interval to sample.
%  Here, we choose the rightmost value (i/n).
%
  x = i / n;
  y = x^2;
  area_strip = (1/n) * y;
  area = area + area_strip;
end

fprintf ( '  Estimated area under y=x^2 is %g\n', area );
fprintf ( '  Exact area is %g\n', 1 / 3 );

