Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

An example of using Sage to compute a left-hand, right-hand, and midpoint Riemann sum.

Views: 85

Riemann Sums Approximating Integrals

We will use Sage to generate Riemann sums to approximate some integrals.

We will need some list making commands in Sage. One useful command is range(n), which will generate a list of the positive integers from 0 to n-1:

range(7)
[0, 1, 2, 3, 4, 5, 6]

Next, we have what is called a list comprehension. If you type something like [f(x) for x in <values>], then you will get a list of everything that looks like f(x), where x is something in the list <values>. (This is a lot like sigma notation, except it generates a list instead of a sum.)

For example, to make a list of the squares of the integers from 0 to 10, you can enter the following:

[i^2 for i in range(11)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

(Remember that range(11) gives the values from 0 to 10.)

Finally, the function sum will add up the elements in a list. So the sum of the integers from 0 to 10 is:

sum([i^2 for i in range(11)])
385

Recall that we had a formula which said [ 1^2 + 2^2 + \dots + 10^2 = \frac{(10)(10+1)(2\cdot 10 + 1)}{6} ] We can confirm that this works:

(10*11*21)/6
385

A Left Hand Sum

Let's generate a left-hand sum with some number of subintervals for the integral [ \int_0^3 x-1 , dx ] In class, we used an area argument to show that this was 3/23/2. To be general, I will create variables for the endpoints of the interval (which I will call a and b) the number of subintervals (num), and the width of each subinterval (dx). (I will also use the function n when I create dx, so that the answer will be a decimal, and not be treated as an exact fraction. We are only approximating values here, so having Sage work with fractions is not helpful.)

a = 0 b = 3 num = 10 dx = n((b-a)/num)

Next, we need to make an array of xx-values on the left hand side of each subinterval. This is easy, since we just want to take a + i*dx for i between 0 and nn-1:

leftpoints = [a + i*dx for i in range(num)]

Finally, evaluate f(x)f(x) at each endpoint, multiply by dx, and add up the results:

f(x) = x-1 sum([f(value)*dx for value in leftpoints])
1.05000000000000

This is not a great approximation, but it is only using 10 subintervals. What is nice about what we have set up above is that we can make changes to the values and re-run the commands to get a better approximation. For example, here I will rerun the commands, but set num to be 100 instead:

a = 0 # Beginning of interval b = 3 # End of interval f(x) = x-1 # The function to integrate num = 10000 # Number of subintervals dx = n((b-a)/num) # Width of each subinterval leftpoints = [a + i*dx for i in range(num)] sum([f(value)*dx for value in leftpoints])
1.49955000000000

We see that the approximation is much better with 100 subintervals!

You can try changing num above to see what happens when you change to 1000 subintervals, or even 10000. (Please note: Although we would normally write 10,00010,000, Sage does not use commas like that!)

And guess what? If you change the values of a, b, and the definition of f(x) above, you can get a left hand sum for any function ff on any interval [a,b][a, b]!

A Right Hand Sum

To get a right hand sum instead of a left-hand sum, we just need to use the points a+(i+1)*dx instead of a + i*dx when we create the endpoints of the intervals. (In other words, the first endpoint is at a + 1*dx instead of a + 0*dx.) Otherwise, the commands are the same. Here's a right-hand sum with 10 subintervals for the same function:

a = 0 # Beginning of interval b = 3 # End of interval f(x) = x-1 # The function to integrate num = 100 # Number of subintervals dx = n((b-a)/num) rightpoints = [a + (i+1)*dx for i in range(num)] sum([f(value)*dx for value in rightpoints])
1.54500000000000

You could of course change the number of subintervals, or even change the function or endpoints to get different right-hand sums.

A Midpoint Sum

To get a midpoint sum, just the point halfway across each interval, or a + i*dx + dx/2:

a = 0 # Beginning of interval b = 3 # End of interval f(x) = x-1 # The function to integrate num = 100 # Number of subintervals dx = n((b-a)/num) midpoints = [a + i*dx + dx/2 for i in range(num)] sum([f(value)*dx for value in midpoints])
1.50000000000000

Hey, what do you know; the midpoint sum is exactly the value of the integral in this case. I wonder if that is a coincidence?