Shared13 - Riemann Sums Assignment / Riemann Sums Notes.sagewsOpen in CoCalc
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is

### Prerequisites:

• Intro to Sage

# Riemann Sums and Area Under a Curve

Suppose we want to know the area between the graph of a positive function and the x-axis from to .

For most functions, this region is not a shape whose area we can find simply using geometry. So we will approximate the area using a simple geometric shape: the rectangle. Now using only one rectangle would give us a pretty poor approximation most of the time, so we should use many rectangles. Here is the approach:

• Divide the interval into subintervals of equal width (this assumption is unnecessary, but it will simplify things).

• We'll call this width . Since all the widths are the same, we have .

• We'll label the endpoints of the intervals : , , , , .

• If each subinterval is relatively small, then the function values on this subinterval are all approximately the same (i.e., for any and in the subinterval).

• Choose any in the subinterval .

• The area under the graph of on this subinterval is approximately (this is the area of the rectangle with one side along the x-axis from to and two vertical sides from the x-axis up to ).

• To get the total area under the graph of on the interval , we add up all our approximations:

This is called a Riemann Sum, after German mathematician Bernhard Riemann (1826-1866).

If is chosen to be the left endpoint of each subinterval, then the resulting Riemann sum is called a Left Riemann Sum (I'll abbreviate it ).

(note that depends on ).

If is chosen to be the right endpoint of each subinterval, then the resulting Riemann sum is called a Right Riemann Sum (I'll abbreviate it ).

(note that depends on ).

## Example 1

Here are pictures of the rectangles that make up the left and right Riemann sums for the function from to with subintervals.

The animation below shows graphs of left Riemann sums for increasing values of . You can see that the rectangles begin to fill in the area under the curve.

## Definite Integrals

To get a better approximation, we make the subintervals smaller by increasing (the number of subintervals).

We then define the area as

(provided this limit exists).

It can be shown (although I won't do it) that this limit exists for many functions (and it does not depend on the choice of ). In particular, this limit exists if the function is continuous on the interval .

I have assumed that the function is positive on the interval . If this is not the case, then some may be negative, in which case would not be the area of the corresponding rectangle, but the opposite of this area (a negative number).

In calculus, we will decree that regions below the x-axis have negative area, while regions above the x-axis have positive area. Therefore, the area formula given above will still work, provided we think not of the geometric area of the region but of the "signed area."

Because of its importance, there is special notation for the limit of Riemann sums given above:

This quantity is called the Definite Integral of the function from to .

In this lab, we will compute values of Riemann sums using the "sum" command in Sage. Next time we will compute integrals using the "integral" command.

The sum command takes four arguments: sum(expression, variable of summation, starting value, ending value)

Make sure you declare your summation variable before using it.

Here some examples.

%var k
sum(k^2,k,0,10)

385

%var i
sum(1/i,i,1,5)

137/60

For , .

%var n
f(x)=x^3
sum(f(n)/f(n+1),n,0,20)
N(_)

3829036719783221149071997/280346265322438720204800 13.6582405168811

## Example 2

Approximate the area between the graph of and the x-axis from to using left and right Riemann sums with , , and subintervals.

First, the solution with .

f(x)=x^2-x+1  #function
a=0           #lower limit
b=2           #upper limit
n=5           #number of subintervals
dx=(b-a)/n    #delta x
%var i
LS=sum(f(a+i*dx)*dx,i,0,n-1) #calculates left sum; remember, x_i=a+i*dx
print 'The left Riemann sum is',N(LS)
RS=sum(f(a+i*dx)*dx,i,1,n)   #calculates right sum
print 'The right Riemann sum is',N(RS)

The left Riemann sum is 2.32000000000000 The right Riemann sum is 3.12000000000000

Now .

f(x)=x^2-x+1  #function
a=0           #lower limit
b=2           #upper limit
n=10          #number of subintervals
dx=(b-a)/n    #delta x
%var i
LS=sum(f(a+i*dx)*dx,i,0,n-1) #calculates left sum
print 'The left Riemann sum is',N(LS)
RS=sum(f(a+i*dx)*dx,i,1,n)   #calculates right sum
print 'The right Riemann sum is',N(RS)

The left Riemann sum is 2.48000000000000 The right Riemann sum is 2.88000000000000

Finally, .

f(x)=x^2-x+1  #function
a=0           #lower limit
b=2           #upper limit
n=50          #number of subintervals
dx=(b-a)/n    #delta x
%var i
LS=sum(f(a+i*dx)*dx,i,0,n-1) #calculates left sum
print 'The left Riemann sum is',N(LS)
RS=sum(f(a+i*dx)*dx,i,1,n)   #calculates right sum
print 'The right Riemann sum is',N(RS)

The left Riemann sum is 2.62720000000000 The right Riemann sum is 2.70720000000000

You can see that the left and right Riemann sums get closer together as increases (they are both getting closer to the actual area).

The actual area is . This gives you some idea of how big needs to be in order to get a good approximation.

Fortunately, there are much better ways to approximate areas! We'll talk about some of these in Math 206.

## Example 3

Approximate the area between the graph of and the x-axis from to using left and right Riemann sums with and subintervals.

[Note: The actual area is .]

First, we'll use .

f(x)=sin(x) #function
a=0         #lower limit
b=pi/2      #upper limit
n=50        #number of subintervals
dx=(b-a)/n  #delta x
%var i
LS=sum(f(a+i*dx)*dx,i,0,n-1) #calculates left sum
print 'The left Riemann sum is',N(LS)
RS=sum(f(a+i*dx)*dx,i,1,n)   #calculates right sum
print 'The right Riemann sum is',N(RS)

The left Riemann sum is 0.984209788675773 The right Riemann sum is 1.01562571521167

Here are graphs for the left and right Riemann sums with :

Now we'll use .

f(x)=sin(x) #function
a=0         #lower limit
b=pi/2      #upper limit
n=100       #number of subintervals
dx=(b-a)/n  #delta x
%var i
LS=sum(f(a+i*dx)*dx,i,0,n-1) #calculates left sum
print 'The left Riemann sum is',N(LS)
RS=sum(f(a+i*dx)*dx,i,1,n)   #calculates right sum
print 'The right Riemann sum is',N(RS)

The left Riemann sum is 0.992125456605633 The right Riemann sum is 1.00783341987358

Here are the graphs for .