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 $f(x)$ and the x-axis from $x=a$ to $x=b$.

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 $[a,b]$ into $n$ subintervals of equal width (this assumption is unnecessary, but it will simplify things).

• We'll call this width $\Delta x$. Since all the widths are the same, we have $\Delta x=\frac{b-a}{n}$.

• We'll label the endpoints of the intervals $x_i$: $x_0=a$, $x_1=a+\Delta x$, $x_2=a+2\Delta x$, $\ldots$, $x_n=a+n\Delta x=b$.

• If each subinterval $[x_{i-1},x_{i}]$ is relatively small, then the function values on this subinterval are all approximately the same (i.e., $f(c)\approx f(d)$ for any $c$ and $d$ in the subinterval).

• Choose any $c_i$ in the subinterval $[x_{i-1},x_{i}]$.

• The area under the graph of $f$ on this subinterval is approximately $f(c_i)\Delta x$ (this is the area of the rectangle with one side along the x-axis from $x_{i-1}$ to $x_{i}$ and two vertical sides from the x-axis up to $f(c_i)$).

• To get the total area under the graph of $f$ on the interval $[a,b]$, we add up all our approximations: $Area\approx\sum_{i=1}^{n}f(c_i)\Delta x$

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

If $c_i$ 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 $LS$).

$LS=\sum_{i=1}^{n}f(x_{i-1})\Delta x=\sum_{i=0}^{n-1}f(x_{i})\Delta x$

(note that $LS$ depends on $n$).

If $c_i$ 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 $RS$).

$RS=\sum_{i=1}^{n}f(x_{i})\Delta x$

(note that $RS$ depends on $n$).

## Example 1

Here are pictures of the rectangles that make up the left and right Riemann sums for the function $f(x)=x^2-x+1$ from $x=0$ to $x=2$ with $n=5$ subintervals.

The animation below shows graphs of left Riemann sums for increasing values of $n$. 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 $n$ (the number of subintervals).

We then define the area as $Area=\lim_{n\to\infty}\sum_{i=1}^{n}f(c_i)\Delta x$

(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 $c_i$). In particular, this limit exists if the function $f$ is continuous on the interval $[a,b]$.

I have assumed that the function $f$ is positive on the interval $[a,b]$. If this is not the case, then some $f(c_i)$ may be negative, in which case $f(c_i)\Delta x$ 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:

$\int_a^bf(x)\, dx=\lim_{n\to\infty}\sum_{i=1}^{n}f(c_i)\Delta x$

This quantity is called the Definite Integral of the function $f$ from $x=a$ to $x=b$.

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.

$\displaystyle\sum_{k=0}^{10}k^2=385$

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

385

$\displaystyle\sum_{i=1}^5\frac{1}{i}=\frac{137}{60}$

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

137/60

For $f(x)=x^3$, $\displaystyle\sum_{n=0}^{20}\frac{f(n)}{f(n+1)}=\frac{3829036719783221149071997}{280346265322438720204800}\approx13.6582$.

%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 $f(x)=x^2-x+1$ and the x-axis from $x=0$ to $x=2$ using left and right Riemann sums with $n=5$, $n=10$, and $n=50$ subintervals.

First, the solution with $n=5$.

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 $n=10$.

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, $n=50$.

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 $n$ increases (they are both getting closer to the actual area).

The actual area is $\frac{8}{3}\approx 2.6667$. This gives you some idea of how big $n$ 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 $f(x)=\sin(x)$ and the x-axis from $x=0$ to $x=\frac{\pi}{2}$ using left and right Riemann sums with $n=50$ and $n=100$ subintervals.

[Note: The actual area is $1$.]

First, we'll use $n=50$.

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 $n=50$:

Now we'll use $n=100$.

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 $n=100$.