Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Math 241
Views: 511
License: OTHER
Image: ubuntu2004
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

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)f(x) and the x-axis from x=ax=a to x=bx=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][a,b] into nn subintervals of equal width (this assumption is unnecessary, but it will simplify things).

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

  • We'll label the endpoints of the intervals xix_i: x0=ax_0=a, x1=a+Δxx_1=a+\Delta x, x2=a+2Δxx_2=a+2\Delta x, \ldots, xn=a+nΔx=bx_n=a+n\Delta x=b.

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

  • Choose any cic_i in the subinterval [xi1,xi][x_{i-1},x_{i}].

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

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

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

If cic_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 LSLS).

LS=i=1nf(xi1)Δx=i=0n1f(xi)ΔxLS=\sum_{i=1}^{n}f(x_{i-1})\Delta x=\sum_{i=0}^{n-1}f(x_{i})\Delta x

(note that LSLS depends on nn).

If cic_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 RSRS).

RS=i=1nf(xi)ΔxRS=\sum_{i=1}^{n}f(x_{i})\Delta x

(note that RSRS depends on nn).

Example 1

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

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

We then define the area as Area=limni=1nf(ci)ΔxArea=\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 cic_i). In particular, this limit exists if the function ff is continuous on the interval [a,b][a,b].

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

abf(x)dx=limni=1nf(ci)Δx\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 ff from x=ax=a to x=bx=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.

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

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

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

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

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

First, the solution with n=5n=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=10n=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=50n=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 nn increases (they are both getting closer to the actual area).

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

[Note: The actual area is 11.]

First, we'll use n=50n=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=50n=50:

Now we'll use n=100n=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=100n=100.