Riemann Sums

Integral calculus: Interpreting Riemann sums in terms of area

Complete Code

Download file: RiemannSums.pg

POD for Macro Files

PG problem file

Explanation

DOCUMENT();

loadMacros(
    'PGstandard.pl', 'PGML.pl', 'parserPopUp.pl', 'PGtikz.pl',
    'PGcourse.pl'
);

Preamble

The parserPopUp.pl macro is used to create drop down menus, and the PGtikz.pl macro is used to produce the graphs.

$c  = random(9, 13);       # a constant for scaling the function
$f  = Compute("x^2/$c");
$a  = random(2, 5);        # left endpoint of interval
$b  = $a + 2;              # right endpoint of interval
$n  = 4;                   # number of rectangles
$dx = ($b - $a) / $n;

# Generate arrays of interval endpoints and function values.
for my $k (0 .. $n) {
    $x[$k] = $a + $k * $dx;
    $y[$k] = $f->eval(x => $x[$k]);
}

# Compute the Riemann sums.
$sumLeft  = 0;
$sumRight = 0;
for my $k (0 .. $n - 1) {
    $sumLeft  += $y[$k] * $dx;
    $sumRight += $y[ $k + 1 ] * $dx;
}

# Graph of the Left Riemann sum rectangles
$graph1 = createTikZImage();
$graph1->tikzLibraries('arrows.meta');
$graph1->BEGIN_TIKZ
\tikzset{>={Stealth[scale = 2]}}
\Large % Make the fonts a little bigger.
\filldraw[
    draw = LightBlue,
    fill = white,
    rounded corners = 10pt,
    thick,
    use as bounding box
] (-1, -1) rectangle (9, 9);
\draw[->] (-1, 0) -- (9, 0) node[above left, outer sep = 3pt] {\(x\)};
\foreach \x in {1, ..., 8} \draw (\x, 5pt) -- (\x, -5pt) node [below] {\(\x\)};
\draw[->] (0, -1) -- (0, 9) node[below right, outer sep = 3pt] {\(y\)};
\foreach \y in {1, ..., 8} \draw (5pt, \y) -- (-5pt, \y) node[left] {\(\y\)};
\draw[<->] plot[domain = -1:9] (\x, {\x * \x / $c});
\filldraw[draw = blue, fill = blue!50!white, fill opacity = 0.5]
    ($x[0], 0) rectangle ($x[1], $y[0]);
\filldraw[draw = blue, fill = blue!50!white, fill opacity = 0.5]
    ($x[1], 0) rectangle ($x[2], $y[1]);
\filldraw[draw = blue, fill = blue!50!white, fill opacity = 0.5]
    ($x[2], 0) rectangle ($x[3], $y[2]);
\filldraw[draw = blue, fill = blue!50!white, fill opacity = 0.5]
    ($x[3], 0) rectangle ($x[4], $y[3]);
END_TIKZ

# Graph of the right Riemann sum rectangles
$graph2 = createTikZImage();
$graph2->tikzLibraries('arrows.meta');
$graph2->BEGIN_TIKZ
\tikzset{>={Stealth[scale = 2]}}
\Large % Make the fonts a little bigger.
\filldraw[
    draw = LightBlue,
    fill = white,
    rounded corners = 10pt,
    thick,
    use as bounding box
] (-1, -1) rectangle (9, 9);
\draw[->] (-1, 0) -- (9, 0) node[above left, outer sep = 3pt] {\(x\)};
\foreach \x in {1, ..., 8} \draw (\x, 5pt) -- (\x, -5pt) node [below] {\(\x\)};
\draw[->] (0, -1) -- (0, 9) node[below right, outer sep = 3pt] {\(y\)};
\foreach \y in {1,...,8} \draw (5pt, \y) -- (-5pt, \y) node[left] {\(\y\)};
\draw[<->] plot[domain = -1:9] (\x, {\x * \x / $c});
\filldraw[draw = blue, fill = blue!50!white, fill opacity = 0.5]
    ($x[0], 0) rectangle ($x[1], $y[1]);
\filldraw[draw = blue, fill = blue!50!white, fill opacity = 0.5]
    ($x[1], 0) rectangle ($x[2], $y[2]);
\filldraw[draw = blue, fill = blue!50!white, fill opacity = 0.5]
    ($x[2], 0) rectangle ($x[3], $y[3]);
\filldraw[draw = blue, fill = blue!50!white, fill opacity = 0.5]
    ($x[3], 0) rectangle ($x[4], $y[4]);
END_TIKZ

$leftEstimateDropDown = DropDown(
    [
        'an overestimate of',
        'equal to',
        'an underestimate of',
        'there is ambiguity'
    ],
    2
);

$rightEstimateDropDown = DropDown(
    [
        'an overestimate of',
        'equal to',
        'an underestimate of',
        'there is ambiguity'
    ],
    0
);

$leftRiemannSumAltText =
    "The graph of a parabola opening up with vertex at the point (0, 0) is "
    . "shown. Rectangles above the intervals from $x[0] to $x[1], $x[1] to "
    . "$x[2], $x[2] to $x[3], and $x[3] to $x[4] with heights $y[0], $y[1], "
    . "$y[2], and $y[3], respectively are also shown.";

$rightRiemannSumAltText =
    "The graph of a parabola opening up with vertex at the point (0, 0) is "
    . "shown. Rectangles above the intervals from $x[0] to $x[1], $x[1] to "
    . "$x[2], $x[2] to $x[3], and $x[3] to $x[4] with heights $y[1], $y[2], "
    . "$y[3], and $y[4], respectively are also shown.";

Setup

First generate a random constant for the function and the interval endpoints. Note that you should be careful to choose ranges for the parameters such that all possibilities work well in the graphs.

Then compute the left and right Riemann sums, first by storing the endpoints of the intervals that partition [$a, $b] in the array @x and the corresponding function values in the array @y, and then summing over the arrays.

Next, generate the graphs of the function with the rectangles whose areas are summed in the Riemann sums.

Next, construct drop down menu answers that ask the student to relate the Riemann sum estimates to the area of the region specified in the problem.

Finally, alternate texts are generated that describe the Riemann sum images for unsighted users that are using a screen reader.

BEGIN_PGML
Suppose [``f(x) = \frac{x^2}{[$c]}``].

a. The rectangles in the graph below illustrate a left endpoint Riemann sum for
[`f(x)`] on the interval [`[$a] \leq x \leq [$b]`].  The value of this left
endpoint Riemann sum is [_]{Real($sumLeft)->cmp(weight => 45)}{5} and it is
[_]{ $leftEstimateDropDown->cmp(weight => 5) } the area of the region enclosed
by [`y = f(x)`], the [`x`]-axis, and the vertical lines [`x = [$a]`] and
[`x = [$b]`].

>>[![$leftRiemannSumAltText]!]{$graph1}{250}<<  
>>Left endpoint Riemann sum<<

b. The rectangles in the graph below illustrate a right endpoint Riemann sum for
[`f(x)`] on the interval [`[$a] \leq x \leq [$b]`].  The value of this right
endpoint Riemann sum is [_]{ Real($sumRight)->cmp(weight => 45) }{5} and it is
[_]{ $rightEstimateDropDown->cmp(weight => 5) } the area of the region enclosed
by [`y = f(x)`], the [`x`]-axis, and the vertical lines [`x = [$a]`] and
[`x = [$b]`].

>>[![$rightRiemannSumAltText]!]{$graph2}{250}<<  
>>Right endpoint Riemann sum<<
END_PGML

Statement

The default avg_problem_grader used in this problem allows answers to be assigned weights, i.e., given precedence in the problem score when the problem is graded. The answers are assigned weights by passing the weight flag to the cmp method.

Note that there are two spaces at the end of the line >>[!left Riemann sum!]{$graph1}{250}<< as well as at the end of the equivalent line for the right Riemann sum (these may not be visible on this page). Those spaces are a PGML line break, and make it so that the image caption is closer to the image than a paragraph break (an empty line in PGML) would give.

It is also important to provide alternate texts for unsighted users that are using a screen reader that give detailed information as to what is shown in the image.

BEGIN_PGML_SOLUTION
a. The left endpoint Riemann sum is
[```
    f([$x[0]]) \cdot 0.5 + f([$x[1]]) \cdot 0.5 + \cdots + f([$x[ $n - 1]]) \cdot 0.5
    = ([$y[0]] + [$y[1]] + \cdots + [$y[ $n-1 ]]) \cdot 0.5 = [$sumLeft].
```]

b. The right endpoint Riemann sum is
[```
    f([$x[1]]) \cdot 0.5 + f([$x[2]]) \cdot 0.5 + \cdots + f([$x[ $n ]]) \cdot 0.5
    = ([$y[1]] + [$y[2]] + \cdots + [$y[ $n ]]) \cdot 0.5  = [$sumRight].
```]
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.