3-D Riemann Sums Illustration

Purpose:  This notebook uses an animation to demonstrate a Riemann Sums approximation for a double integral and then compares the approximated numerical values to the actual value.  It is designed as a pedagogical tool to help illustrate the concept of a double integral and its connection to the corresponding Riemann Sums.
      Developer:  This Mathematica code was developed by Major Glenn Sjoden at the US Air Force Academy's Department of Mathematical Sciences and later adapted to it's current format by Lt Col Steve Hadfield.
      File Name:  Riemann3d.nb
This notebook is stored without output to save on storage and download time, so use Kernel|Evaluation|Evaluate Notebook to reproduce the output.

3-D Riemann Sums Using Midpoints

The cell that follows defines a function called "midblocks" that is used to produce the 3D graphical image corresponding to a particular Riemann Sum approximate to the double integral.  The graphical image produced also shows the function and computes the numerical volume of the rectangular volumes of the Riemann Sum.  It assumes a floor of the xy-plane (z=0).
Invoking of this function is done with six positional variables as follows:
	midblocks[f, xvars, yvars, nn, mm, viewpoint]
where "f" is the expression for f(x,y); xvars and yvars are the independent variables and their ranges; nn and mm are the number of partitions for each of the two independent variables respectively; and viewpoint is the 3D ViewPoint triple that specifies the point the figures are viewed from.
NOTE:  You need to execute the cell below prior to using the midblocks command. 
WARNING:  You may get a few spelling warnings when you execute this definitional cell depending on how you have Mathematica set up.

\(\(midblocks[f_, xvars_, yvars_, \ nn_, mm_, \ view_] := 
        CompoundExpression[
Needs["Graphics`Master`"], 
cell[xo_, yo_, zo_, x1_, y1_, z1_, 
              colr_] := 
Graphics3D[
              Table[{FaceForm[
                    colr], 
Cuboid[{xo, yo, zo}, {x1, y1, 
                      z1}]}, {i, 0, 1, 2}, {j, 1, 1, 2}, {k, 1, 1, 2}]], 
	
          cellw[xo_, yo_, zo_, x1_, y1_, z1_, 
              colr_] := 
WireFrame[
              Graphics3D[
                Table[{colr, 
                    Cuboid[{xo, yo, zo}, {x1, y1, 
                        z1}]}, 
{i, 0, 1, 2}, {j, 1, 1, 
                    2}, {k, 1, 1, 2}]]], 
xlo = 
            xvars[\([2]\)], 
          xhi = xvars[\([3]\)], 
ylo = yvars[\([2]\)], 
          yhi = yvars[\([3]\)], 
dex = \((xhi - xlo)\)\/nn, 
          dey = \((yhi - ylo)\)\/mm, 
p3d = 
            Plot3D[f, xvars, 
              yvars, 
BoxRatios -> {1, 1, 1}, 
              AxesLabel -> {x, y, z}, 
              DisplayFunction -> Identity], 
RVol = 
            0, 
b3d = 
            Table[{xb = xa + dex; 
                yb = ya + dey; 
xavg = \(xa + xb\)\/2; 
                yavg = \(ya + yb\)\/2; 
zb = 
                  f /. {xvars[\([1]\)] -> xavg, yvars[\([1]\)] -> yavg}; 
                RVol = RVol + \((xb - xa)\) \((yb - ya)\)*
                      zb; 
cell[xa, ya, 0, xb, yb, zb, 
                  Blue]}, 
{xa, xlo, xhi - dex, dex}, {ya, 
                ylo, yhi - dey, dey}], \ 
          Show[{p3d, 
              b3d}, 
DisplayFunction -> $DisplayFunction, \ view], RVol // N];\)\)

The cell below demonstrates an example uses of the midblocks command and the Table command to produce a series of 3D graphics of the function and the Riemann Sum approximations.  The corresponding numerical values for each Riemann Sum approximation are also provided at the end.  The "{i, 2, 18, 4}" parameter on the Table command specifies that both variables are initially partitioned into 2 partitions and they go up by 4 more partitions each time until eventually getting to 18 partitions each.
Just double click on the first 3D graphic to view the animation.  
As expected you'll see that the numerical approximations converge to the corresponding double integral as more partitions are used.

Table[
      midblocks[2 - (x^2) - (y^2), {x, -1, 1}, {y, -1, 1}, i, i, 
        ViewPoint -> {2.568,  1.577,  1.540}], {i, 2, 18, 4}]

As you can tell from the numerical values above, the Riemann Sum approximations are converging.  Below, we compute the true double integral for comparison.

(∫_(-1)^1)(∫_(-1)^1)(2 - (x^2) - 
                (y^2)) \[DifferentialD]x \[DifferentialD]y // N