5.9 Approximate Integration

Mathematica script by Chris Parrish,
cparrish@sewanee.edu

Sources and references for some of these problems include
James Stewart, "Calculus: Concepts and Contexts," Second Edition, Brooks/Cole, 2001
Deborah Hughes-Hallett, Andrew M. Gleason, et. al., "Calculus," Second Edition, John Wiley & Sons, 1998
Robert Fraga, ed., "Calculus Problems for a New Century," The Mathematical Association of America, 1993

Mathematica Code for Some
Composite Numerical Integration Formulas (L, R, T, M, S)

Composite Rules -- This section contains Mathematica code only, not worked examples

Using Composite Numerical Integration Formulas (L, R, T, M)

Estimating

Hughes-Hallett, Gleason, et al, First Edition, Text Example, page 380

Let's approximate

Clear[steps,accuracy,left,right,trap,mid,table];

steps = {2,10,50,250};
accuracy = 5;

left[n_] := N[compositeLeftRule[f,a,b,n],accuracy];
right[n_] := N[compositeRightRule[f,a,b,n],accuracy];
trap[n_] := N[compositeTrapezoidRule[f,a,b,n],accuracy];
mid[n_] := N[compositeMidpointRule[f,a,b,n],accuracy];

table = Transpose[{steps,
Map[left,steps],
Map[right,steps],
Map[trap,steps],
Map[mid,steps]}];

TableForm[table,
TableSpacing -> {0,3}]

 n LEFT RIGHT TRAP MID 2 1.24996 1.20848 1.22922 0.0192431 10 0.461387 0.453092 0.45724 0.416894 50 0.432389 0.43073 0.43156 0.429996 250 0.430725 0.430393 0.430559 0.430497

Let's see what Mathematica gets for the numerical value of this integral.

NIntegrate[f[t],{t,a,b}]

Estimating

Hughes-Hallett, Gleason, et al, First Edition, Text Example, page 381

Let's approximate

Clear[n,firstTRAP20,secondMID20,firstMID20,secondTRAP20];

n = 20;

firstTRAP20  = N[compositeTrapezoidRule[f,a,cutPt,n],6];
secondTRAP20 = N[compositeTrapezoidRule[f,cutPt,b,n],6];

firstMID20  = N[compositeMidpointRule[f,a,cutPt,n],6];
secondMID20 = N[compositeMidpointRule[f,cutPt,b,n],6];

Print[firstTRAP20 + secondMID20," <= integral <= ",firstMID20 + secondTRAP20]

Let's see what Mathematica gets for the numerical value of this integral.

NIntegrate[f[z],{z,a,b}]

Estimating

Hughes-Hallett, Gleason, et al, First Edition, Exercise 7.6.1, page 382

Let's approximate

Clear[steps,accuracy,left,right,trap,mid,table];

steps = {1,2,4};
accuracy = 6;

left[n_] := N[compositeLeftRule[f,a,b,n],accuracy];
right[n_] := N[compositeRightRule[f,a,b,n],accuracy];
trap[n_] := N[compositeTrapezoidRule[f,a,b,n],accuracy];
mid[n_] := N[compositeMidpointRule[f,a,b,n],accuracy];

table = {Map[left,steps],
Map[right,steps],
Map[trap,steps],
Map[mid,steps]};

TableForm[table,
{"n=1","n=2","n=4"}},
TableSpacing -> {0,3}]

 n=1 n=2 n=4 LEFT 40. 40.7846 41.7116 RIGHT 51.225 46.3971 44.5179 TRAP 45.6125 43.5909 43.1147 MID 41.5692 42.6386 42.8795

Let's see what Mathematica gets for the numerical value of this integral.

NIntegrate[f[x],{x,a,b}]

Estimating

Hughes-Hallett, Gleason, et al, First Edition, Exercise 7.6.5, page 382

Let's approximate

Clear[steps,accuracy,left,right,trap,mid,table];

steps = {10,100};
accuracy = 6;

left[n_] := N[compositeLeftRule[f,a,b,n],accuracy];
right[n_] := N[compositeRightRule[f,a,b,n],accuracy];
trap[n_] := N[compositeTrapezoidRule[f,a,b,n],accuracy];
mid[n_] := N[compositeMidpointRule[f,a,b,n],accuracy];

table = {Map[left,steps],
Map[right,steps],
Map[trap,steps],
Map[mid,steps]};

TableForm[table,
{"n=10","n=100"}},
TableSpacing -> {0,3}]

 n=10 n=100 LEFT 0.140474 0.161324 RIGHT 0.188417 0.166118 TRAP 0.164446 0.163721 MID 0.163348 0.16371

Let's see what Mathematica gets for the numerical value of this integral.

NIntegrate[f[t],{t,a,b}]

Exercise: Comparison of Several
Numerical Integration Techniques (L, R, T, M)

Exercise: Comparing Several Numerical Integration Techniques

Your task is to approximate the integral, I, of y = Sqrt[x]
for x in the interval [0,1] using various composite methods
for numerical integration. To facilitate using calculators for
these comparisons, we will standardize on using just two
subdivisions of the interval of integration.

Composite Left Formula

Use the 2-fold lefthand-endpoint rule to approximate the integral I.

Illustrate this method by drawing an appropriate figure on the following graph.

Is your result an overestimate or an underestimate of the true value of the integral? Why?

Show[graph];

Solution

Clear[left,n,deltaX,x1];

n = 2;
deltaX = (b - a)/n //N;
x1 = a + deltaX;

left = deltax (f[a] + f[x1])

Composite Right Formula

Use the 2-fold righthand-endpoint rule to approximate the integral I.

Illustrate this method by drawing an appropriate figure on the following graph.

Is your result an overestimate or an underestimate of the true value of the integral? Why?

Show[graph];

Solution

Clear[right];

right = deltaX (f[x1] + f[b])

Composite Midpoint Formula

Use the 2-fold midpoint rule to approximate I.

Illustrate this method by drawing an appropriate figure on the following graph.

Is your result an overestimate or an underestimate of the true value of the integral? Why?

Show[graph];

Solution

Clear[mid];

m1 = a + deltaX/2;
m2 = m1 + deltaX;

mid = deltaX (f[m1] + f[m2])

Composite Trapezoid Formula

Use the 2-fold trapezoid rule to approximate I.

Illustrate this method by drawing an appropriate figure on the following graph.

Is your result an overestimate or an underestimate of the true value of the integral? Why?

Show[graph];

Solution

Clear[trap];

trap = deltaX (f[a] + 2 f[x1] + f[b])

Relative Errors

Construct a figure which provides a visual comparison of the actual errors involved
in making the above four approximations. Rank these errors in decreasing order.
There might be a bit of a challenge here in trying to find a picture of the error in the
Midpoint Method. How could we do that?

Show[GraphicsArray[{{graph,graph},
{graph,graph}}]];

Adding Simpson's Rule to the Collection (L, R, T, M, S)

Estimating

Hughes-Hallett, Gleason, et al, First Edition, Exercise 7.7.2, page 389

Let's estimate

Clear[f,x,a,b,steps,accuracy]

f[x_] := 1/x;

a = 1.;
b = 2.;

steps = 10;
accuracy = 10;

simp10 = N[compositeSimpsonsRule[f,a,b,steps],accuracy];
error  = N[simp10 - Log[2],accuracy];

Print["Simpson's Rule with n = 10 gives  ",simp10]
Print["The true value of the integral is ",Log[2]//N]
Print["              ... so the error is ",simp10 - Log[2]//N]

Estimating

Hughes-Hallett, Gleason, et al, First Edition, Exercise 7.7.11, page 390

Let's estimate

Clear[steps,accuracy,left,right,trap,mid,simp,table];

steps = {5,10};
accuracy = 6;

left[n_] := N[compositeLeftRule[f,a,b,n],accuracy];
right[n_] := N[compositeRightRule[f,a,b,n],accuracy];
trap[n_] := N[compositeTrapezoidRule[f,a,b,n],accuracy];
mid[n_] := N[compositeMidpointRule[f,a,b,n],accuracy];
simp[n_] := N[compositeMidpointRule[f,a,b,2 n],accuracy];

table = Transpose[{steps,
Map[left,steps],
Map[right,steps],
Map[trap,steps],
Map[mid,steps],
Map[simp,steps]}];

TableForm[table,
TableSpacing -> {0,3}]

 n LEFT RIGHT TRAP MID SIMP 5 0.438144 1.83814 1.13814 0.931623 0.982602 10 0.684884 1.38488 1.03488 0.982602 0.995631

Let's see what Mathematica gets for the numerical value of this integral.

NIntegrate[f[t],{t,a,b}]

Estimating

Hughes-Hallett, Gleason, et al, First Edition, Exercise 7.7.11, page 390

Let's estimate

Clear[steps,accuracy,left,right,trap,mid,simp,table];

steps = {5,10,50,100};
accuracy = 6;

left[n_] := N[compositeLeftRule[f,a,b,n],accuracy];
right[n_] := N[compositeRightRule[f,a,b,n],accuracy];
trap[n_] := N[compositeTrapezoidRule[f,a,b,n],accuracy];
mid[n_] := N[compositeMidpointRule[f,a,b,n],accuracy];
simp[n_] := N[compositeMidpointRule[f,a,b,2 n],accuracy];

table = Transpose[{steps,
Map[left,steps],
Map[right,steps],
Map[trap,steps],
Map[mid,steps],
Map[simp,steps]}];

TableForm[table,