Approximate Integration Formulas

Clear[f,a,b,n]

Here are some rules for integration. Below each general rule, the rule is evaluated for a=0, b=1 and five intervals. The function f is left unspecified.

Right Hand Rule

R[n_,a_,b_] = (b-a)/n  Sum[f[a+(b-a)/n*i],{i,1,n}]

R[5,0,1]

MidPoint Rule

M[n_,a_,b_] = (b-a)/n  Sum[f[a+(b-a)/(2n)*(2i+1)],{i,0,n-1}]

M[5,0,1]

Left Hand Rule

L[n_,a_,b_] = (b-a)/n  Sum[f[a+(b-a)/n*i],{i,0,n-1}]

L[5,0,1]

Trapezoidal Rule

T[n_,a_,b_] = (b-a)/(2n) ( f[a]+f[b]+2Sum[f[a+(b-a)/n*i],{i,1,n-1}])

T[5,0,1]

Simpson's Rule

S[n_,a_,b_]=T[n/2,a,b]/3+2M[n/2,a,b]/3

S[5,0,1]

An Application

We can use these rules to numerically approximate integrals. For example, here is an integral Mathematica cannot do:

f[x_]=Exp[x^4+x^3]
Integrate[f[x],{x,0,1}]

Here, we use the midpoint rule to approximate the integral:

M[1000,0,1.]

We can construct a table of values and help us see how the approximation changes with increasing n:

Table[{n,M[n,0,1.]},{n,10,40,2}];
TableForm[%]

Table[{n,T[n,0,1.]},{n,10,40,2}];
TableForm[%]

Table[{n,S[n,0,1.]},{n,10,40,2}];
TableForm[%]

The Error

In the above example, we saw that the integral was about 1.88303 using Simpson's rule. Let's assume that is accurate, and sketch the errors associated with the Midpoint Rule, Trapeziod Rule, and Simpson's Rule.

We can construct a table of values and help us see how the approximation changes with increasing n:

midpointerror=Table[{n,Abs[M[n,0,1.]-1.88303]},{n,10,40,2}];
TableForm[midpointerror]
meplot=ListPlot[midpointerror,PlotStyle->{Green,PointSize[0.02]}]

traperror=Table[{n,Abs[T[n,0,1.]-1.88303]},{n,10,40,2}];
TableForm[lefterror]
teplot=ListPlot[traperror,PlotStyle->{Red,PointSize[0.02]}]

simperror=Table[{n,Abs[S[n,0,1.]-1.88303]},{n,10,40,2}];
TableForm[simperror]
seplot=ListPlot[simperror,PlotStyle->{Blue,PointSize[0.02]}]

Show[meplot,seplot,teplot,AxesLabel->{"n","Error"}]