Area between the curve y = f(x) and the x-axis using a Trapezoidal Approximation

ByJohn Luker

Small changes were made in this file by the Editor when the file was converted to Mathematica 3.

Brief Description:  The lesson successively draws the curve, shows the area, breaks the region into trapezoids, computes the area of one trapezoid, sums the areas of all the trapezoids and then computes the area using NIntegrate.

1.  Define a curve y = f(x) and take a look at its graph on a specified domain.

Clear[f,x,jump,a,b,c,d]
a = 0;												
(*left limit of domain*)
b = 1.5;												
(*right limit of domain*)
c = -1;												
(*lower limit of range*)
d = 1;												
(*upper limit of range*)
f[x_] = Sin[Pi x];
curveplot = Plot[f[x],{x,a,b},PlotRange->{c,d},
																PlotStyle->{Thickness[.006]},
																AxesOrigin->{0,0},
																AxesLabel->{"x","y"}];

2.  Take a look at what constitutes the area between the curve y = f(x) and the x-axis.

Clear[number,jump,x,block,blockplot,k,ii,vline,
						vlineplot]
number = 15;
jump = (b-a)/number;
xpoints = Table[{x,0},{x,a,b,jump}]
points = Table[{x,f[x]},{x,a,b,jump}];

block[k_] := Graphics[{GrayLevel[.75],
																		Polygon[{xpoints[[k-1]],
																		xpoints[[k]],
                  points[[k]],
                  points[[k-1]]}]}]

blockplot[m_] := Table[block[ii],{ii,2,m}]

vline[k_]     := Graphics[Line[
																									{xpoints[[k]],points[[k]]}]]

vlineplot[m_] := Table[vline[ii],{ii,2,m}]

Do[
			Show[curveplot,blockplot[m]],{m,2,number+1}]

3.  To approximate the area we will divide the region into a series of trapezoids, find the area of each individual trapezoid and then sum these areas.  First, define the number of trapezoids.

Clear[number,jump]
number = 9
jump = (b-a)/number

4.  Now divide the x-axis into equally spaced regions, the number of regions equaling the number of trapezoids

xpoints = Table[{x,0},{x,a,b,jump}]
xpointsplot = ListPlot[xpoints,
													         	PlotStyle->{RGBColor[0,0,1],
													                     	PointSize[.015]},
													         	DisplayFunction->Identity]
Show[curveplot,xpointsplot];

5.  Next we will compute and plot f(x) for each x.

points = Table[{x,f[x]},{x,a,b,jump}];
pointplot = ListPlot[Join[points,xpoints],
												PlotStyle->{RGBColor[0,0,1],PointSize[.015]},
												DisplayFunction-> Identity]
brokenlineplot = Graphics[Line[points]];
Show[curveplot,pointplot];

6.  Now we can complete the trapezoids by joining appropriate points.

pointplot = ListPlot[Join[points,xpoints],
												PlotStyle->{RGBColor[0,0,1],PointSize[.015]},
												DisplayFunction-> Identity]
brokenlineplot = Graphics[Line[points]];
Clear[vline]
vline[k_] := Graphics[Line[{xpoints[[k]],points[[k]]}]];
Clear[ii]
vlineplot = Table[vline[ii],{ii,1,Length[xpoints]}];
	
Show[curveplot,pointplot,brokenlineplot,vlineplot];

7.  Once the trapezoids have been formed we can compute the area of a specific trapezoid, say the 3rd trapezoid.

Clear[trap]
trap[x_] := Graphics[{GrayLevel[.75],
												Polygon[{{x,0},{x,f[x]},{x+jump,f[x+jump]},{x+jump,0}}]}]
Show[curveplot,pointplot,brokenlineplot,vlineplot,trap[2 jump]];
Area = N[(jump (f[2 jump] + f[2 jump+jump]))/2]

8.  The idea, of course, is to compute the area of all the trapezoids and then sum them.

Area = N[Sum[jump (f[x] + f[x+jump])/2,{x,0,1-jump,jump}]]

9.  Finally we can let Mathematica compute the area using its internal numerical routine and compare the answer to the one we obtained.

NIntegrate[f[x],{x,0,1}]