An example of using Sage to compute a left-hand, right-hand, and midpoint Riemann sum.
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:
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:
(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:
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:
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 . 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.)
Next, we need to make an array of -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 -1:
Finally, evaluate at each endpoint, multiply by dx
, and add up the results:
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:
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 , 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 on any interval !
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:
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
:
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?