Calculus Explorations using Mathematica

Al Hibbard - Central College

e-mail homepage



Lab 16. We are bound to error: Numerical Integration

Goals for this lab

This lab pursues the question of integration from a numerical perspective. In addition to the rectangular and trapezoidal methods seen earlier, the Simpson method is introduced. Each of these methods have error bounds and we will explore these.

New Mathematica commands to (eventually) learn: Integrate, NIntegrate

A quick review

Clear[f, x]
f[x_] := -2(x + 1)(x - 1)(x - 3)
Plot[f[x], {x, 1, 3}, Filling -> Axis]
[Graphics:CEUMLb16gr1.gif]

LeftApprox[f[x], {x, 1, 3}, 8]
RightApprox[f[x], {x, 1, 3}, 8]
MidpointApprox[f[x], {x, 1, 3}, 8]
TrapezoidApprox[f[x], {x, 1, 3}, 8]
SimpsonApprox[f[x], {x, 1, 3}, 8]
[Graphics:CEUMLb16gr3.gif]
[Graphics:CEUMLb16gr4.gif]
[Graphics:CEUMLb16gr5.gif]
[Graphics:CEUMLb16gr6.gif]
[Graphics:CEUMLb16gr7.gif]

Error analysis of monotone functions

Clear[h, x]
h[x_] := x^2
ShowApproximations[h[x], {x, 1, 3}, left, 8];
ShowApproximations[h[x], {x, 1, 3}, right, 8];
[Graphics:CEUMLb16gr8.gif] [Graphics:CEUMLb16gr9.gif]

ShowLeftRightDifference[h[x], {x, 1, 3}, 8];
[Graphics:CEUMLb16gr10.gif]

Q3. What is the area of these blue rectangles? Note that they each have the same width. Furthermore, observe that the height of the top of the ith rectangle is the height of the bottom of the (i + 1)st rectangle. Therefore, imagine sliding them all together on one stack (say, on the first rectangle). How great is this height? Consequently, what is the area?

ShowApproximations[h[x], {x, 1, 3}, trapezoid, 8];
[Graphics:CEUMLb16gr12.gif]

ShowApproximations[Sin[x], {x, 1, 3}, trapezoid, 2];
ShowTrapError[Sin[x], {x, 1, 3}, 2];
[Graphics:CEUMLb16gr13.gif] [Graphics:CEUMLb16gr14.gif]

Error analysis, in general, for left and right methods

ShowApproximations[1/2 x, {x, 1, 5}, left, 6, PlotRange -> {1/2,10}, AxesOrigin -> {1,0}];
ShowApproximations[2 x, {x, 1, 5}, left, 6, PlotRange -> {1/2,10}, AxesOrigin -> {1,0}];
[Graphics:CEUMLb16gr15.gif] [Graphics:CEUMLb16gr16.gif]

Q6. Which function has more error? Why?

functlist = {1/10 x - 4, 1/4 x + 2, x, 5x - 3, -9x + 3, 15 x, -23 x + 1};
TableForm[Table[{functlist[[i]], est = NLeftApprox[functlist[[i]], {x,0,4}, 6], act = Integrate[functlist[[i]], {x, 0, 4}]//N, error = act - est,Abs[error]}, {i, 1, Length[functlist]}], TableHeadings -> {None, {"f", "L(6)", "I = exact", "I - L(6)", "|I - L(6)|"}}, TableAlignments -> {Automatic, Center}]
[Graphics:CEUMLb16gr17.gif]

TableForm[Table[{2^i, est = NLeftApprox[x, {x, 0, 4}, 2^i], act = Integrate[x, {x, 0, 4}]//N, Abs[act - est]}, {i, 0, 6}], TableHeadings -> {None, {"n","L(n)", "I = exact", "|I - L(n)|"}}, TableAlignments -> {Automatic, Center}]
[Graphics:CEUMLb16gr18.gif]

Error analysis for midpoint and trapezoid methods

Clear[h, x, a, b, n]
h[x_] := x^2
a = -3; b = 3; n = 8;
ShowApproximations[h[x], {x, a, b}, trapezoid, n];
ShowApproximations[h[x], {x, a, b}, midpoint, n];
{StringJoin["M(30) = ",NMidpointApprox[h[x], {x, a, b}, 30]//ToString], StringJoin["T(30) = ",
NTrapezoidApprox[h[x], {x, a, b}, 30]//ToString]}
[Graphics:CEUMLb16gr19.gif] [Graphics:CEUMLb16gr20.gif]
[Graphics:CEUMLb16gr21.gif]

RectToTrap;
[Graphics:CEUMLb16gr22.gif]

The Simpson Rule

Clear[f, x, a, b, n]
f[x_] := 895 - 1404x + 1282x^2 - 976x^3 + 499x^4 - 139x^5 + 19x^6 - x^7
a = 1; b = 6; n = 10;
ShowApproximations[f[x], {x, a, b}, simpson, n];
[Graphics:CEUMLb16gr23.gif]

TableForm[Table[{n,NLeftApprox[f[x],{x, a, b}, n],NRightApprox[f[x],{x, a, b}, n], NTrapezoidApprox[f[x],{x, a, b}, n],NMidpointApprox[f[x],{x, a, b}, n], NSimpsonApprox[f[x],{x, a, b}, n]}, {n, 8, 30, 4}], TableHeadings -> {None, {"n","L(n)","R(n)","T(n)","M(n)","S(n)"}}, TableAlignments ->{Automatic, Center}] 
[Graphics:CEUMLb16gr24.gif]

(Note that the midpoint column got cut off in part, as did the Simpson column in entirety.)


Return to the main page.

Go to the next lab.