MATH 021 Z1  Lab 2  Tutorial

Riemann Sums and Integration
  
    This will prepare you to answer the questions found on lab 2.
    Do not hand this in, but go through the tutorial and the second lab
    will be much easier (follow the syntax provided in this tutorial).

Initialization

Evaluate the following initialization cell with Shift-Enter before you begin. (It will evaluate automatically if you answer Yes at the prompt.)

err="Interval is backwards. Fix it and try again.";
getlines[f_,{x_,a_,b_},n_,loc_]:=Module[{dx},dx=(b-a)/n;
	Table[Line[{{a + i*dx, 0},{a + i*dx, f/.{x->a + (i+loc)*dx}},
     {a + (i+1)*dx, f/.{x->a + (i+loc)*dx}},
     {a + (i+1)*dx, 0}}], {i, 0, n - 1}]];

PlotLeftRec[f_,{x_,a_, b_}, n_] :=If[a>b,Print[err],Show[Plot[f, {x, a, b},
     PlotRange->{{Min[0,a],Max[0,b]},All}],
     Graphics[{Darker[Green], Thick,getlines[f,{x,a, b}, n,0]}],
     AxesOrigin->{0,0},PlotLabel->ToString[n]<>" Left Rectangles",
     PlotRangeClipping->False]];

PlotRightRec[f_,{x_,a_, b_}, n_] :=If[a>b,Print[err],Show[Plot[f, {x, a, b},
      PlotRange->{{Min[0,a],Max[0,b]},All}],
      Graphics[{Red, Thick,getlines[f,{x,a, b}, n,1]}],AxesOrigin->{0,0},
      PlotLabel->ToString[n]<>" Right Rectangles",PlotRangeClipping->False]];

PlotMidRec[f_,{x_,a_, b_}, n_] :=If[a>b,Print[err],Show[Plot[f, {x, a, b},
      PlotRange->{{Min[0,a],Max[0,b]},All}],
      Graphics[{Purple, Thick,getlines[f,{x,a, b}, n,1/2]}],
      AxesOrigin->{0,0},PlotLabel->ToString[n]<>" Midpoint Rectangles",
      PlotRangeClipping->False]];

This defines functions for plotting area-approximating rectangles.

Example

Suppose we want to find the area between f(x)=3+(Sqrt[x-4]) and the x-axis, for x in the interval [4,12]. To get started, let’s plot the region.

f[x_]=3+(Sqrt[x-4])

Plot[f[x],{x,4,12},Filling->0,AxesOrigin->{0,0}]

Since we don’t (yet) know how to find the area exactly, we will approximate the shaded area with rectangles.

Left rectangles

We begin by partitioning the interval [4,12] into subintervals. On each subinterval we construct a rectangle with base along the x-axis and height equal to the value of f(x) at the left endpoint of the subinterval. These are called left rectangles.

Here's what our function looks like with 4 left rectangles, each of width 2.

PlotLeftRec[f[x],{x,4,12},4]

To find the total of the rectangle areas, we multiply the height of each rectangle by its width, and add them up.

f[4]*2 + f[6]*2 + f[8]*2 + f[10]*2

We can also enter it as the sum of f[x]*2, for x from 4 to 10 in increments of 2.

Sum[f[x]*2,{x,4,10,2}]

You can find a template for Sum on the calculus tab under Basic Commands in the Classroom Assistant palette. Make sure you put in the increment.

Wrap N[] around the sum to convert the answer to a decimal.

N[Sum[f[x]*2,{x,4,10,2}]]

Check Answer

The sum of 4 left rectangles for f(x) on the interval [4,12] is 35.7274. This is an underestimate of the true area under the curve (see plot).

Right rectangles

We can also approximate the area with right rectangles, that is, rectangles whose heights are equal to f(x) at the right endpoint of each subinterval. Here's what our function looks like with 4 right rectangles.

PlotRightRec[f[x],{x,4,12},4]

Find the sum of the right rectangle areas:

N[f[6]*2 + f[8]*2 +  f[10]*2 +  f[12]*2]

Or, as a sum:

N[Sum[f[x]*2,{x,6,12,2}]]

Check Answer

The sum of 4 right rectangles for f(x) on the interval [4,12] is 41.3843. This is an overestimate of the true area under the curve (see plot).

Midpoint rectangles

We can also approximate the area with midpoint rectangles, that is, rectangles whose heights are equal to f[x] at the midpoint of each subinterval. Here is what our function looks like with 4 midpoint rectangles.

PlotMidRec[f[x],{x,4,12},4]

Now find the sum of the Midpoint Rectangle areas:

N[f[5]*2 +  f[7]*2 + f[9]*2 +  f[11]*2]

Or as a sum:

N[Sum[f[x]*2,{x,5,11,2}]]

Check Answer

The sum of 4 midpoint rectangles for f(x) on the interval [4,12] is 39.2277.

Is the sum of the midpoint rectangle areas equal to the average of the left and right rectangles?

Let's increase the number of rectangles

25 left rectangles

PlotLeftRec[f[x],{x,4,12},25]

First, find the width of each rectangle.

The width of each rectangle = ((width of the region)/(#rectangles))

Δx=(((12-4))/25)

You can find Δ under Typesetting on the Classroom Assistant palette, or use the keyboard shortcut Esc-D-Esc. If you prefer, you can type dx in place of Δx.

Now find the sum of the rectangle areas.

We must find f[x]*Δx for each rectangle, beginning with x=4 (the left edge of the first rectangle) and ending at x=12-Δx (the left edge of the last rectangle). The x-values are spaced Δx apart, so we use Δx for the increment.

N[Sum[f[x]*Δx,{x,4,12-Δx,Δx}]]

25 right rectangles

PlotRightRec[f[x],{x,4,12},25]

Find the sum of the rectangle areas.

We must find f[x]*Δx for each rectangle, beginning with x=4+Δx (the right edge of the first rectangle) and ending at x=12 (the right edge of the last rectangle). The x-values are spaced Δx apart, so we use Δx for the increment.

N[Sum[f[x]*Δx,{x,4+Δx,12,Δx}]]

25 midpoint rectangles

PlotMidRec[f[x],{x,4,12},25]

Find the sum of the rectangle areas.

We must find f[x]*Δx for each rectangle, beginning with x=4+((Δx)/2) (the middle of the first rectangle) and ending at x=12-((Δx)/2) (the middle of the last rectangle). The x-values are spaced Δx apart, so we use Δx for the increment.

N[Sum[f[x]*Δx,{x,4+((Δx)/2),12-((Δx)/2),Δx}]]

Practice

For practice, approximate the area between g(x)=(1/x)  and the x-axis, for x in the interval [1,3], with:

(a) 30 left rectangles

(b) 30 right rectangles

(c) 30 midpoint rectangles

See below for answers.

Answers

30 left rectangles: 1.12116
30 right rectangles: 1.07672
30 midpoint rectangles: 1.09845