Error Estimates - ErrorIntegral.nb

Error Estimates for Numerical Integration Methods

Input the function you want to integrate, the left and right endpoints "a" and "b", and the number "n" of sub-divisions, and execute the command below.
The command will output the integral approximations and the errors in the approximation for the various methods first in the case of n subdivisions and then in the case of 10n subdivisions. Afterwards it will output the factor by which the error decreases when the number of subdivisions increases from n to 10n.
From the theory we expect this factor to be about 10 for the Left and Right Riemann Sums, \(TraditionalForm
      \`about\ 10\^2 = 100\ for\ the\ Midpoint\ and\ the\ Trapezoidal\ Rule, 
      \ and\ about\ 10\^4\)=10,000 for Simpson's Rule.
If you compute the factors for a few examples, you will realize that these are very rough estimates for the factor. When the number of subdivisions is "too small", the points are sampled too much at random to give results close to the expaected ones, on the other hand, when the number of subdivisions is "too large", other considerations such as the round-off errors committed may distort the results.

The following command will only work for functions, for which Mathematica can symbolically compute the definite integral!

f[x_] = Sin[200  x]a = 0b = 2\(n = 100
; 
int = Integrate[f[x], {x, a, b}]; 

    listend = Table[f[x], {x, a, b, \((b - a)\)/n}]; 

    listmid = Table[f[x], {x, a + \((b - a)\)/\((2  n)\), b, \((b - a)\)/n}]; 
    
TableForm[
	
      Table[{{StringForm["[`` Subdivisions]", n], 
            "Integral Approximation", Error}, {"", "", ""}, {
            "Left Riemann Sum:", 
            leftriemann = 
              N[Apply[Plus, Drop[listend, \(-1\)]]\ \((b - a)\)/n], 
            err1 = Abs[leftriemann - int]}, 
          
{"Right Riemann Sum:", 
            rightriemann = N[Apply[Plus, Drop[listend, 1]]\ \((b - a)\)/n], 
            err2 = Abs[rightriemann - int]}, 
          
{"Midpoint Rule:", 
            midpoint = N[Apply[Plus, listmid] \((b - a)\)/n], 
            err3 = Abs[midpoint - int]}, 
          
{"Trapezoidal Rule:", 
            trapez = 1/2\ \((rightriemann + leftriemann)\), 
            err4 = Abs[trapez - int]}, 
          
{"Simpson's Rule:", simpson = 2/3\ midpoint\  + 1/3\ trapez, 
            err5 = Abs[simpson - int]}}]]\)\(n = 10  n; 
listend = Table[f[x], {x, a, b, \((b - a)\)/n}]; 

    listmid = Table[f[x], {x, a + \((b - a)\)/\((2  n)\), b, \((b - a)\)/n}]; 
    
TableForm[
	
      Table[{{StringForm["[`` Subdivisions]", n], 
            "Integral Approximation", Error}, {"", "", ""}, {
            "Left Riemann Sum:", 
            leftriemann = 
              N[Apply[Plus, Drop[listend, \(-1\)]]\ \((b - a)\)/n], 
            err10 = Abs[leftriemann - int]}, 
          
{"Right Riemann Sum:", 
            rightriemann = N[Apply[Plus, Drop[listend, 1]]\ \((b - a)\)/n], 
            err20 = Abs[rightriemann - int]}, 
          
{"Midpoint Rule:", 
            midpoint = N[Apply[Plus, listmid] \((b - a)\)/n], 
            err30 = Abs[midpoint - int]}, 
          
{"Trapezoidal Rule:", 
            trapez = 1/2\ \((rightriemann + leftriemann)\), 
            err40 = Abs[trapez - int]}, 
          
{"Simpson's Rule:", simpson = 2/3\ midpoint\  + 1/3\ trapez, 
            err50 = Abs[simpson - int]}}]]\)\(TableForm[
	
      Table[{{"", "Error decreases approximately by a factor:"\ }, {
            "", ""}, {"Left Riemann Sum:", Round[err1/err10]}, 
          
{"Right Riemann Sum:", Round[err2/err20]}, {"", ""}, 
          
{"Midpoint Rule:", Round[err3/err30]}, 
          
{"Trapezoidal Rule:", Round[err4/err40]}, {"", ""}, 
          
{"Simpson's Rule:", Round[err5/err50]}}]]
\)

Etc.

Author: H. Knaust (helmut@math.utep.edu)
Created: August 2000
Last Edits: August 21, 2000