Exercise guide for demonstrating approximations to integrals

Programming hidden here

Left endpoint approximation

leftrect[n_]:=Module[{X,dX,rectangles,boundaries,RightRect, leftsum},functionplot:=Plot[f[x],{x,a,b},DisplayFunction->Identity,PlotRange->All];RightRect[g_,x_,d_]:=Line[{{x,0},{x+d,0},{x+d,g[x]},{x,g[x]},{x,0}}];dX=((b-a)/n);X=Table[a+((i (b-a))/n),{i,0,n}];rectangles:=Graphics[{GrayLevel[0.9`],Table[Rectangle[{X[[i]],0},{X[[i+1]],f[X[[i]]]}],{i,1,n}]}];boundaries:=Graphics[Table[RightRect[f,X[[j]],dX],{j,1,n}]];leftsum=N[(∑_(i=1)^n)f[X[[i]]] dX,10];label=leftsum "=  sum  ";Show[rectangles,boundaries,functionplot,PlotLabel->Style[Framed[label],16,Blue,Background->Lighter[Yellow]],DisplayFunction->$DisplayFunction,AspectRatio-> 1,Axes-> True]];

Midpoint approximation

midrect[n_]:=Module[{X,dX,rectangles,boundaries,MidRect, midsum},functionplot:=Plot[f[x],{x,a,b},DisplayFunction->Identity,PlotRange->All, Axes->True];MidRect[g_,x_,d_]:=Line[{{x,0},{x+d,0},{x+d,g[x+(d/2)]},{x,g[x+(d/2)]},{x,0}}];dX=((b-a)/n);X=Table[a+((i (b-a))/n),{i,0,n}];rectangles:=Graphics[{GrayLevel[0.9`],Table[Rectangle[{X[[i]],0},{X[[i+1]],f[X[[i]]+(dX/2)]}],{i,1,n}]}];boundaries:=Graphics[Table[MidRect[f,X[[j]],dX],{j,1,n}]];midsum=N[(∑_(i=1)^n)f[X[[i]]+(dX/2)] dX,10];label=midsum "= sum  ";Show[rectangles,boundaries,functionplot,PlotLabel->Style[Framed[label],16,Blue,Background->Lighter[Yellow]],DisplayFunction->$DisplayFunction,AspectRatio-> 1, Axes->True]];

Right endpoint approximation

rightrect[n_]:=Module[{X,dX,rectangles,boundaries,RightRect, rightsum},functionplot:=Plot[f[x],{x,a,b},DisplayFunction->Identity,PlotRange->All];RightRect[g_,x_,d_]:=Line[{{x,0},{x+d,0},{x+d,g[x+d]},{x,g[x+d]},{x,0}}];dX=((b-a)/n);X=Table[a+((i (b-a))/n),{i,0,n}];rectangles:=Graphics[{GrayLevel[0.9`],Table[Rectangle[{X[[i]],0},{X[[i+1]],f[X[[i+1]]]}],{i,1,n}]}];boundaries:=Graphics[Table[RightRect[f,X[[j]],dX],{j,1,n}]];rightsum=N[(∑_(i=1)^n)f[X[[i+1]]] dX,10];label=rightsum "=  sum  ";Show[rectangles,boundaries,functionplot,PlotLabel->Style[Framed[label],16,Blue,Background->Lighter[Yellow]],DisplayFunction->$DisplayFunction,AspectRatio-> 1, Axes->True]];

Trapezoid approximation

trapezoids[n_]:=Module[{X,dX,Trap,regions,boundaries, trapsum},functionplot:=Plot[f[x],{x,a,b},DisplayFunction->Identity,PlotRange->All];dX=((b-a)/n);X=Table[a+((i (b-a))/n),{i,0,n}];Trap[g_,x_,d_]:=Line[{{x,0},{x+d,0},{x+d,g[x+d]},{x,g[x]},{x,0}}];regions:=Graphics[{GrayLevel[0.9`],Table[Polygon[{{X[[i]],0},{X[[i]],f[X[[i]]]},{X[[i+1]],f[X[[i+1]]]},{X[[i+1]],0}}],{i,1,n}]}];boundaries:=Graphics[Table[Trap[f,X[[j]],dX],{j,1,n}]];trapsum=N[(1/2) dX (f[a]+2 (∑_(i=2)^n)f[X[[i]]]+f[b]),10];label=trapsum "= sum of area of trapezoids";Show[regions,boundaries,functionplot,PlotLabel->Style[Framed[label],16,Blue,Background->Lighter[Yellow]],DisplayFunction->$DisplayFunction,AspectRatio-> 1]];

Numerical rules

Clear[LeftSum, leftsum, RightSum, rightsum, MidpointSum, midsum, TrapezoidSum, trapezoidsum,n]
leftsum[n_]:=Module[{dX},dX=((b-a)/n);N[(∑_(i=0)^(n-1))dX f[a+i dX],10]]
midsum[n_]:=Module[{dX},dX=((b-a)/n);N[(∑_(i=0)^(n-1))dX f[a+(i+0.5) dX],10]]

rightsum[n_]:=Module[{dX},dX=((b-a)/n);N[(∑_(i=1)^n)dX f[a+i dX],10]]


trapezoidsum[n_]:=Module[{dX},dX=((b-a)/n);N[(∑_(i=0)^(n-1))dX 0.5 (f[a+i dX]+f[a+(i+1) dX]),10]]

Available commands for drawing approximating collections of rectangles and calculating approximating sums for integrals:

These commands assume that you have defined a function f and a, b.  See examples below.

leftrect[n]    partitions [a, b] into n subintervals of equal length,  uses the left endpoints as (Subscript[x, j]^( * ))  ;  draws a collection of rectangles illustrating the approximation to the integral.

rightrect[n]    same thing, but with right endpoints instead of left endpoints.

midrect[n]  same thing, but with midpoints instead of left endpoints.

leftsum[n]    This is like leftrect,  but no drawing is made,  only the approximating sum is calculated.

rightsum[n]   Same as leftsum,  but with right endpoints instead of left endpoints.

midsum[n]  Same as leftsum, but with midpoints instead of left endpoints.

Example:

f[x_] :=  Sin[x]
a = 0;
b = (3/2)  Pi;

leftrect[40]

leftsum[40]

rightrect[40]