Approximating Definite Integrals

In this notebook we explore the right-sum, left-sum, and midpoint-sum methods for approximating definite integrals.
Recall that the definite integral can be approximated using summations:

(∫_a^b)f(x)dx ≈ (∑_(k=1)^n)f((Subscript[x, k]^( * )))∆x

where ∆x = ((b-a)/n) and (Subscript[x, k]^( * )) is chosen based on the method used.  The following Mathematica Modules allow the user to put in the name of the function, the upper and lower limits and the number of subintervals, then they calculate the three types of approximations used in calculus I.  Execute the following Mathematica command and then we will look at some examples

MidPointMethod[funct_,a_,b_,n_]:=
Module[{f=funct,x0=a,xn=b,n0=n},
dx=(xn-x0)/n0;
output=Sum[f[x0+(k-1/2)*dx],{k,1,n0}]*dx
]
RightSumMethod[funct_,a_,b_,n_]:=
Module[{f=funct,x0=a,xn=b,n0=n},
dx=(xn-x0)/n0;
output=Sum[f[x0+k*dx],{k,1,n0}]*dx
]
LeftSumMethod[funct_,a_,b_,n_]:=
Module[{f=funct,x0=a,xn=b,n0=n},
dx=(xn-x0)/n0;
output=Sum[f[x0+(k-1)*dx],{k,1,n0}]*dx
]

Example 1:
Approximate (∫_1^4)(x^2)dx using 4, 8, 16, and 32 subintervals and the midpoint-sum method.

f[x_]=x^2

For n=4 we have

M4=MidPointMethod[f,1,4,4]

As a decimal we get

N[M4]

Now the n=8, 16, and 32

M8=MidPointMethod[f,1,4,8]
N[M8]

M16=MidPointMethod[f,1,4,16]
N[M16]

M32=MidPointMethod[f,1,4,32]
N[M32]

Example 2:
Approximate (∫_0^2)sin(x)dx using both the right-sum and left-sum methods with n=4, 128, and 1024.  Show enough decimals to see any differences in the sums.

Since Sin is a predefined Mathematica function we can to the following:

left4=LeftSumMethod[Sin,0,2,4]
N[left4,16]
right4=RightSumMethod[Sin,0,2,4]
N[right4,16]

Note that in the following we suppress the output on the sums for brevity.

left128=LeftSumMethod[Sin,0,2,128];
N[left128,16]
right128=RightSumMethod[Sin,0,2,128];
N[right128,16]

left1024=LeftSumMethod[Sin,0,2,1024];
N[left1024,16]
right1024=RightSumMethod[Sin,0,2,1024];
N[right1024,16]