Lab 14: Riemann Sums

In this lab we will study Riemann sums in higher dimensions.

The following is an example of a one dimensional (x) Riemann sum, which you are probably familiar with from Calculus 2. One dimensional Riemann sums are used to approximate two dimensional objects (x, y).

_images/1dRiemann_sum.png

This can be expanded to two dimensions (x, y), to approximate three dimensional objects (x, y, z).

_images/2dRiemann_sum.png

Riemann sums of three dimensions ore more are difficult to visualize. Check out this Wikipedia article about Tesseracts if you want to learn more about four dimensional objects.

Task 1

Write a function, riemann_sum_1D(f, x_min, x_max, N, method), that takes as input a function of one variable (x), f, a minimum value of x, x_min, a maximum value of x, x_max, a number of sub-intervals to compute, N, and a Riemann method to use, method ("left", "right", "mid"), and computes the Riemann approximation of f on the input space [x_min, x_max].

Remember the formula for a one dimensional Riemann sum:

\[\sum_{i=1}^N f(x_i^*) \Delta x,\]

where Δx = (xMax - xMin)/N.

Raise a ValueError if the method is not "left", "right", or "mid" (use the error message f"Error: method ({method}) must be \"left\", \"right\", or \"mid\"").

Task 2

Write a function, riemann_sum_2D(f, x_min, x_max, y_min, y_max, N, method), that takes as input a function of two variables (x and y), f, a minimum value of x, x_min, a maximum value of x, x_max, a minimum value of y, y_min, a maximum value of y, y_max, a number of sub-intervals to compute, N, and Riemann method to use, method ("left", "right", mid).

Remember the formula for a two dimensional Riemann sum:

\[\sum_{j=1}^N \sum_{i=1}^N f(x_i^*, y_j^*) \Delta x \Delta y,\]

where Δx = (xMax - xMin)/N, Δy = (yMax - yMin)/N

Raise a ValueError if the method is not "left", "right", or "mid" (use the error message f"Error: method ({method}) must be \"left\", \"right\", or \"mid\"").

Task 3

Consider the integral of \(f(x, y) = x \sin(x + y)\) on the rectangle \([0, \pi/6] \times [0, \pi/3]\). First calculate the exact value of this integral by hand. Then make a plot that shows the error of the midpoint Riemann integral approximation as \(N\) ranges from \(1\) to \(100\). Remember to give your graph a title and label your axes.

Task 4

Write a function, riemann_sum_3D(f, x_min, x_max, y_min, y_max, z_min, z_max, N, method), that takes as input a function of three variables (x, y, and z), f, a minimum value of x, x_min, a maximum value of x, x_max, a minimum value of y, y_min, a maximum value of y, y_max, a minimum value of z, z_min, a maximum value of z, z_max, a number of sub-intervals to compute, N, and Riemann method to use, method ("left", "right", "mid").

Remember the formula for a three dimensional Riemann sum:

\[\sum_{k=1}^N \sum_{j=1}^N \sum_{i=1}^N f(x_i^*, y_j^*, z_k^*) \Delta x \Delta y \Delta z,\]

where Δx = (xMax - xMin)/N, Δy = (yMax - yMin)/N, Δz = (zMax - zMin)/N.

Raise a ValueError if the method is not "left", "right", or "mid" (use the error message f"Error: method ({method}) must be \"left\", \"right\", or \"mid\"").

Task 5

Write a function, riemann_sum(f, x_min, x_max, N, method), that takes as input a function of an n-dimensional variable (x), f, a vector of minimum values of the components of x, x_min, a vector of maximum values of the components of x, x_max, a number of sub-intervals to compute, N, and Riemann method to use, method ("left", "right", "mid").

Raise a ValueError if the method is not "left", "right", or "mid" (use the error message f"Error: method ({method}) must be \"left\", \"right\", or \"mid\"").