Mathematica Labs | Denis Shubleka

Subject: Calculus

Topic: Riemann Sums

Goal: Use Mathematica to compute Riemann Sums

Task 1

Recall that a Riemann Sum is an approximation of the definite integral, often
set up as a sum of areas of rectangles (left, right, midpoint etc.) or trapezoids.
The Sum function makes the computation easy. For example, the following command
adds all the squares of integers from 1 to 10, including 1 and 100.

Sum[(i^2),{i,1,10}]

Alternatively, we can also use the Sigma operation from the palette:

(∑_(i=1)^10)(i^2)

The definition of a right-hand Riemann sum using n rectangles on a closed 
interval [a, b], is given by: (∑_(i=1)^n)f[(Subscript[x, i])]Δx, d where:
n = number of rectangles
Δx=((b-a)/n)
(Subscript[x, i])=a+i*Δx

In the following example we are going to estimate the definite integral of Sin[x]
on the interval [1, 4].

f[x_]:=Sin[x]
a=1;
b=4;

Next, we introduce functions for Δx and (Subscript[x, i]), and use them to define functions for
the left and right hand Riemann sums:

Δx[n_]:=(b-a)/n;

x[i_,n_]=a+i*Δx[n];
rightRiemann[n_]:=(∑_(i=1)^n)f[x[i,n]]*Δx[n]//N
leftRiemann[n_]:=(∑_(i=0)^(n-1))f[x[i,n]]*Δx[n]//N

We compute the two sums for f(x)=Sin[x] on [1, 4], using fifty subdivisions:

rightRiemann[50]
leftRiemann[50]

, and then compare them with the definite integral, after converting to
decimal form, as shown below.

(∫_1^4)Sin[x]\[DifferentialD]x

N[%,6]

To obtain a plot of the net area, type and execute the following:

Plot[f[x],{x,1,4},Filling->Axis]

Are the approximations close to the definite integral’s value? Explain how
one can improve the estimate.

We conclude this Task with a table summary of right-hand Riemann Sums, for
several values of n:

mydata =Table[{n,rightRiemann[n]},{n,10, 200, 10}];
mydataWithHeadings=Prepend[mydata,{"n","Right Riemann Sum"}];
Text@Grid[mydataWithHeadings,Alignment->Left,Dividers->{Center,2->True}]

Your turn: write two new functions that compute the Midpoint and Trapezoidal
Sums. Feel free to use a new function as an example.

Related Exercises/Notes:

ap-calc.github.io