Definition and computation of the definite integral

Important:  To view this notebook, first go to the "kernel" menu, choose "evaluation" and then "evaluate notebook".  I have removed all the output from this notebook to conserve space.

Initialization cell.  Execute this group of cells before beginning

Left endpoint approximation

leftrect[n_] := 
        Block[{X, dX, rectangles, boundaries, RightRect}, 
          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 of  rectangle areas "; 
          Show[rectangles, boundaries, functionplot, PlotLabel -> label, 
            DisplayFunction -> $DisplayFunction];];

Midpoint approximation

midrect[n_] := 
        Block[{X, dX, rectangles, boundaries, MidRect}, 
          functionplot := 
            Plot[f[x], {x, a, b}, DisplayFunction -> Identity]; 
          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  of rectangle areas"; 
          Show[rectangles, boundaries, functionplot, PlotLabel -> label, 
            DisplayFunction -> $DisplayFunction];];

Right endpoint approximation

rightrect[n_] := 
        Block[{X, dX, rectangles, boundaries, RightRect}, 
          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 of area of rectangles "; 
          Show[rectangles, boundaries, functionplot, PlotLabel -> label, 
            DisplayFunction -> $DisplayFunction];];

Trapezoid approximation

trapezoids[n_] := 
        Block[{X, dX, Trap, regions, boundaries}, 
          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 -> label, 
            DisplayFunction -> $DisplayFunction];];

Numerical rules

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

Double click on each of the following closed cells  in order to be able to read the examples.

Example:  Find an approximation to the area under the curve y = (e^(-x)) from x = 1 to x = 5.

f[x_] := x (E^(-x))
a = 0; 
b = 5;

leftrect[20]
leftrect[100]
leftrect[200]

TableForm[Table[{n, LeftSum[n]}, {n, 100, 1000, 200}]]

Thus the area is approximately  0.959476.

We can apply the same procedure (computing Riemann sums,and testing what happens as the width of the subintervals goes to zero) for functions which are not necessarily non-negatiive.The result,which is called the integral can be regarded as an algebraic sum of signed areas:

Example:  Find the approximate value of the definite integral (∫_(-1)^2)((e^x) - 
              (x^2)) dx.

f[x_] := (E^x) - (x^2)
a = -1; 
b = 2;

leftrect[20]
leftrect[100]
leftrect[200]

TableForm[Table[{n, LeftSum[n]}, {n, 100, 2000, 200}]]

Thus the approximate value of the definite integral(∫_(-1)^2)((e^x) - (x^2))dx is 4.02

Exercises:

Following these examples, find approximate values for the following areas or definite integrals:
1)  The area between the x axis and the graph of y = (x^4) + 1, between x = -1 and x = 3.

2) The definite integral (∫_(-2)^6)((x^3) - 4 (x^2) + 2 x + 
                5) dx.

3) The area between the two curves y = (x^(2   ))- 1 and y =  - (x^(2   ))+ 1 (The two curves intersect at x = -1 and at x = 1.)  As explained in class, the answer is the integral  (∫_(-1)^1) 2 x^2 dx.

4)  The volume inside the cone z = (Sqrt[(x^2) + (y^2)]) , from z = 0 to z = 3.  As explained in class, the answer is the integral (1/4) π (∫_0^3) 
                  (z^2) dz.

5)  The area between the curves y = (x^(2   )) - 1 and y = -3 (x^2) + 2 x + 1.
Hints:  What are the points of intersection of the two curves? Which curve is above the other on the interval between the two points of intersection?

6) The volume inside the paraboloid  z = (x^2) + (y^2) , from z = 0 to z = 3.  As explained in class, the answer is the integral π (∫_0^3) z dz.