(C) Copyright 1994 D.C.M. Burbulla and  C.T.J. Dodson
       Notebook to support the book
     Self-Tutor for Computer  Calculus

Using Mathematica 2

by
     D. C. M. Burbulla and C. T. J. Dodson, University of Toronto
 Published by:    
    Prentice-Hall Canada  (English)   ISBN 0130152803     and 
     Prentice-Hall Japan  (Japanese) ISBN4-8101-8558-3 C3055 P2880E

The Definite Integral

Chap7.m

riemann/: riemann[f_, {a_, b_}, n_, choice_:1] := 
          N[(b - a)*Sum[f[(1 - choice)*(a + (i - 1)*(b - a)/n) + 
	  choice*(a + i*(b - a)/n)], {i, 1, n}]/n]

riemann::usage = "riemann[f, {a, b}, n, choice] calculates the 
numerical value of the Riemann sum obtained by dividing 
the interval {a, b} into n equal
subintervals and evaluating f at the chosen
point of each subinterval. The chosen point is determined by a number between 0 
and 1, ranging from the left end point to the right end point of each subinterval. 
The default choice is 1, ie. choosing the right end point of each subinterval."

 
er/: er[f_, {a_, b_}, n_, choice_:1] := 
Abs[NIntegrate[f[x], {x, a, b}] - riemann[f, {a, b}, n, choice]]

er::usage = "er[f, {a, b}, n, choice] computes the absolute value of the
difference between the value of the definite integral of f 
on the interval {a, b} and the value of the Riemann 
sum approximation riemann[f, {a, b}, n, choice]."

 
definitetransform/: 
definitetransform[integrand_, {x_, a_, b_}, u_, substitution_] := 
{newintegrand = integrand/D[substitution, x] /. substitution -> u, 
u == substitution, {substitution /. x -> a, substitution /. x -> b}}
 
definitetransform::usage = 
"definitetransform[integrand, {x, a, b}, u, substitution] 
applies the change of variables u = substitution (in terms
of x) to the definite integral of the given integrand (in
terms of x) on the interval {a, b} and outputs a list 
with three elements: the first element is the new integrand 
(newintegrand) in terms of u (and possibly still x); 
the second element is an equation giving u in terms of
x; the third element is a list of replacements of the new limits of
integration in terms of u."


s/: s[f_, {a_, b_}, x_, choice_:1] := 
	If[a <= x <= b, f[(1 - choice)*a + choice*b], 0]

 
randompartition/: randompartition[{a_, b_}, n_] := P = 
   Union[Table[Random[Real, {N[a], N[b]}], {n - 1}], {N[a], N[b]}]

randompartition::usage = "randompartition[{a, b}, n] 
produces a random partition of the interval {a, b} 
into at most n subintervals by randomly
picking n - 1 points in {a, b}. The partition is set equal to P."
regularpartition/: regularpartition[{a_, b_}, n_] := P = Block[{i},
    Table[N[a + i (b - a)/n], {i, 0, n}]]
regularpartition::usage = "regularpartition[{a, b}, n] partitions 
the interval {a, b} into n equally spaced subintervals. 
The partition is set equal to P."


refine/: refine[partition_, n_] := P = Union[Table[Random[Real, 
{Min[N[partition]], Max[N[partition]]}], {n}], N[partition]]

refine::usage = "refine[partition, n] randomly inserts n 
(possibly not distinct) points into the given partition. 
The new partition is set equal to P."


arbitraryriemann/: 
  arbitraryriemann[f_, P_, choice_:1] := Block[{i}, 
Sum[(Union[N[P]][[i + 1]] - Union[N[P]][[i]])*f[(1 - choice)*Union[N[P]][[i]] + 
choice*Union[N[P]][[i + 1]]], {i, 1, Length[P] - 1}]]

arbitraryriemann::usage = "arbitraryriemann[f, P, choice] 
calculates the Riemann sum of f for the partition P by evaluating 
f at the chosen point of each subinterval of P. The chosen point is determined by 
a number between 0 and 1, ranging from the left end point to the right end point 
of each subinterval. The default choice is 1, ie. choosing the right end point of each 
subinterval."

 
ear/: ear[f_, P_, choice_:1] := Block[{x}, Abs[NIntegrate[f[x], 
{x, Min[N[P]], Max[N[P]]}] - arbitraryriemann[f, P, choice]]]

ear::usage = "ear[f, P, choice] calculates the difference between 
the value of the defintite integral of f on the interval 
{Min[P], Max[P]} and arbitraryriemann[f, P, choice]."


norm/: norm[P_] := 
Max[Drop[Union[N[P]], 1] - Drop[Union[N[P]], -1]]

norm::usage = "norm[P] is the norm of the partition P."

 
viewapprox/: viewapprox[f_, P_, choice_:1] := 
(one = Block[{x}, Plot[f[x], {x, Min[N[P]] - 0.1, Max[N[P]] + 0.1},
           DisplayFunction -> Identity]];
    two = Block[{i, x}, 
Plot[Release[Table[s[f, {Union[N[P]][[i]], Union[N[P]][[i + 1]]}, x, choice],
         {i, Length[P] - 1}]], {x, Min[N[P]] - 0.1, Max[N[P]] + 0.1},
PlotRange -> All, 
PlotPoints -> 50, DisplayFunction -> Identity]];
      Block[{x}, Show[two, one, PlotLabel ->
         {f[x], (Length[P] - 1)*"intervals" , N[norm[P]]*"= norm", 
  If[Chop[ear[f, P, choice]] == 0, "no error", ear[f, P, choice]*"= error"]},
  PlotRange -> All, DisplayFunction -> $DisplayFunction]])

viewapprox::usage = "viewapprox[f, P, choice] shows the Riemann sum 
approximation to the definite integral of f on the interval 
{Min[P], Max[P]} in terms of the given partition P and the indicated choice of 
point in each subinterval. The default choice is to evaluate f at the right end point 
of each subinterval. The PlotLabel includes the function f, the number
of subintervals, the norm of the partition, and the absolute 
error of the Riemann approximation. 
If P is a partition with some points extremely
close together - as may happen with a random partition - 
the narrrow rectangles on these short subintervals may 
be missed in the output."

  
average/: average[f_, {a_, b_}] := Integrate[f[x], {x, a, b}]/(b - a)
 
average::usage = "average[f, {a, b}] is the average value of f on the
interval {a, b}."

Partitions and Riemann Sums

Example 1.

?regularpartition

?randompartition

regularpartition[{2, 6}, 8]

randompartition[{2, 6}, 8]

?riemann

?er

riemann[Sin, {0, Pi}, 8]

er[Sin, {0, Pi}, 8]

riemann[Sin, {0, Pi}, 16]

er[Sin, {0, Pi}, 16]

Example 2.

?viewapprox

viewapprox[Sin, regularpartition[{0, Pi}, 8] ];

viewapprox[Sin, randompartition[{0, Pi}, 8] ];

viewapprox[ArcTan, regularpartition[{0, 6}, 10], 0.5];

The Fundamental Theorem of Calculus

We shall consider two examples. In the first example we shall use a function
 for which anantiderivative is well known; in the second example we shall use a
function for which no elementary antiderivative is known. First, we use f(t)=t^2.

F[x_] := NIntegrate[t^2, {t, 0, x}]

plot1=Plot[F[x], {x, 0, 2}, PlotRange -> All];

Clear[f]; f[x_] := x^2

viewantiderivative[f, {0, 2}, 0];

Show[%, plot1];

Next we use f(x)= cos(cos x)

F[x_] := NIntegrate[Cos[Cos[t]], {t, 0, x}]

plot2=Plot[F[x], {x, 0, Pi}];

Clear[f]; f[x_] := Cos[Cos[x]]

viewantiderivative[f, {0, Pi}, 0];

Show[%, plot2];

Of course, The Fundamental Theorem of Calculus is built in
to  Mathematica:

Clear[f]; D[Integrate[f[t], {t, 0, x}], x]

Also built in is the method of evaluating definite integrals if an
antiderivative, F, of the integrand is known

Clear[F]; Integrate[D[F[x], x], {x, a, b}]

Sometimes we need a transformation of variables:

?definitetransform

definitetransform[x Sqrt[9 + x^2], {x, 0, 4}, u, 9 + x^2]

Plot[x Sqrt[9 + x^2], {x, 0, 4}, AspectRatio -> Automatic];

Plot[newintegrand, {u, 9, 25}, AspectRatio -> Automatic];

Show[%%, %];

As you can see, the area of the region that represents the value of the
original definite integral appears equal to the area of the region that
represents the new integral after the change of variables. (But this
is not completely obvious, since the two regions have different shapes.)