4  Integration

4.1  Limits of Sums and the Area Under a Curve

Plot[(x^2),{x,0,1},Filling->Axis]

Plot[{(x^2),(((Floor[10 x]/10))^2)},{x,0,1}, PlotStyle->{Red, Black}, Filling->{1->{Axis, Opacity[.1]}, 2->{Axis, Opacity[.1]}}]

?Sum

Sum[ ((i-1)/10)^2*(1/10), {i,1,10}] //N

The form of the preceding expression is the Input form (that can be typed entirely with ASCII characters in the simplest of word processsors such as Notepad (with basic text formatting).  by copying the preceding formula into the next cell, then marking its right bracket with the mouse, then clicking on the Menu Item Cell, then clicking on Convert To, then clicking on StandardForm, we obtain the following form

N[(∑_(i=1)^10)(1/10) (((((i-1)/10)))^2)]

that could be typed using the BasicInput palette.

Plot[{(x^2),((( Floor[10 x+1]/10))^2)},{x,0,1},PlotStyle->{Red, Black}, Filling->{2->{Axis, Opacity[.1]}, 1->{Axis, Opacity[.1]}}]

?Sum

Sum[ (i/10)^2*(1/10), {i,1,10} ] //N

N[(∑_(i=1)^10)(1/10) ((((i/10)))^2)]

Plot[{(x^2),((((1/10) (Floor[10 x]+0.5)))^2)},{x,0,1},PlotStyle->{Red, Black}, Filling->{2->{Axis, Opacity[.1]}, 1->{Axis, Opacity[.1]}}]

Sum[ ((i - .5)/10)^2*(1/10), {i,1,10}]

(∑_(i=1)^10)(1/10) (((((i-.5)/10)))^2)

To the Limit!

areaR[n_] = Sum[ (i/n)^2*(1/n), {i,1,n}]

The preceding evaluation shows that Mathematica has the ability to compute symbolic sums.  For example, we have

Sum[i,{i,1,n}]

Sum[ i^2, {i,1,n}]

Sum[i^3, {i,1,n}]

Sum[i^4, {i,1,n}]

and etc.

Table[ areaR[n]//N, {n,10,100,10}]

areaR[n]

Limit[ areaR[n], n->Infinity]

Example 4.1.1   (on page 104)

Clear[f, areaR]; f[x_] = x^3-5x^2+8x-4

Plot[f[x],{x,1,2},Filling->Axis]

areaR[n_] := Sum[ f[ 1 + i/n ]*(1/n), {i,1,n}]

Table[ areaR[n] //N, {n, 10,100, 30}]  // TableForm

areaR[n] //Simplify

Limit[ areaR[n], n->Infinity]

Example 4.1.2    (on page 105)

Clear[ f, areaR ];  f[x_] = Sin[x]

Plot[f[x],{x,0,π},Filling->Axis]

areaR[n_] := Sum[ f[ 0 + (i*Pi/n) ]*(Pi/n), {i,1,n}]

Table[ areaR[n]//N, {n, 10, 100, 10}]

areaR[n]

Limit[ areaR[n], n->Infinity]

4.2  The Definite Integral

Example 4.2.1  (on page 107)

Clear[ f, areaR ]; f[x_] = 2 - x^2

Show[Plot[f[x],{x,0,1},Filling->Axis],Plot[f[x],{x,-1.25,1.25}], PlotRange->All]

a = 0; b = 1;

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

intApprox[100]//N

intApprox[n]  //Simplify

Limit[ intApprox[n], n->Infinity]

Example 4.2.2   (on page 108)

Clear[ f, a, b, intApprox ];  f[x_] = x^3 - 1

Show[Plot[f[x],{x,0,1}, Filling->Axis],Plot[f[x],{x,-1,1.5}],PlotRange->{{-1,1.5},{-1.5,0.5}},AspectRatio->Automatic]

a = 0; b = 1;

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

intApprox[100]//N

intApprox[n]

Limit[ intApprox[n], n->Infinity]

Example 4.2.3   (on page 109)

Clear[ f, a, b, intApprox ];  f[x_] = x^2 - 1

Show[Plot[f[x],{x,0,2},Filling->Axis],Plot[f[x],{x,-1.5,3},Filling->Axis],PlotRange->{{-1.5,3},{-1,3.25}},AspectRatio->Automatic]

a = 0; b = 2;

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

intApprox[100] //N

intApprox[n]

Limit[ intApprox[n], n->Infinity]

a=0; b=1

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

area1 = - Limit[ intApprox[n], n->Infinity]

a =1; b=2

intApprox[n] = Sum[ f[a + i(b-a)/n ]*(b-a)/n, {i,1,n}]

area2 = Limit[ intApprox[n], n->Infinity]

area1 + area2

Animating the Convergence of Riemann Sums

integralMovie[func_,{x_,a_,b_},{ymin_,ymax_},sumRule_]:=Module[ 
{apprx,f,stepfn,nrects,dx,plot}, 
f[t_]=func/.x->t;
stepfn[t_,n_]=f[a+(Ceiling[n(t-a)/(b-a)] - sumRule)(b-a)/n]; 
Animate[ dx=N[(b-a)/2^k];
aprox = Sum[ f[a+(i-sumRule)*dx]*dx, {i,1,2^k}]; Show[ Plot[ {f[x], stepfn[x,2^k]}, {x,a,b}, PlotPoints->200, PlotStyle->{{Red}, {Black}}, Filling->Axis, PlotRange->{ymin,ymax}, PlotLabel->{2^k, apprx}]], {k,1,8,1}] ];

f[x_] = x^3 - 4x^2 - x + 13

integralMovie[ f[x], {x, 0, 4}, {0, 13}, 0]

4.3  The Fundamental Theorem of Calclulus  (on page 113)

Example 4.3.1   (on page 113)

Clear[ f, a, b];  f[x_] = x

a = 0

Phi[x_] = Limit[ Sum[ f[ a + i(x-a)/n ]*(x-a)/n, {i,1,n}], n->Infinity]

Plot[ {f[x], Phi[x]}, {x, 0, 3} ]

Table[ {x, Phi[x]}, {x,  0, 4}]//TableForm

Phi[x]

Example 4.3.2   (on page 114)

Clear[f, a, Phi];  f[x_] = 2x - x^2

a = 0

Phi[x_] = Limit[ Sum[ f[a + i(x-a)/n]*(x-a)/n, {i,1,n}], n->Infinity]

Plot[ {f[x], Phi[x]}, {x, 0, 3}, PlotRange->All]

Phi[x]

?=!=

Example 4.3.3    (on page 114)  Animating the Graph of Phi

areaMovie[func_,{x_,a_,b_},{ymin_,ymax_}]:=Module[ {f}, f[t_]= func /. x->t; 
int[t_] = Assuming[ a<t<b, Integrate[ f[x],{x,a,t} ]   ];
If[ Head[int[t]] =!=Integrate, 
         Animate[ Show[  
            Plot[f[t], {t,a,b}],
            Plot[ f[t], {t,a,x}, Filling->Axis, FillingStyle->{Lighter[Red], Lighter[Green]}],
              Plot[ int[t], {t,a,x}, PlotStyle->{Blue, Thickness[.005]}],
              Graphics[{Red, PointSize[.015], Point[{x, int[x]}]}], 
               Graphics[{Gray, Line[{{x,ymin}, {x, ymax}}]}], 
               PlotRange->{{a, b}, {ymin, ymax}}], {x,a+.001,b}]]]

areaMovie[ x*Exp[-x/3]*Sin[x], {x, 0, 20}, {-1.5, 2}]

areaMovie[ x*Sin[x^2], {x, 0, Sqrt[3*Pi]}, {-3,3}]

areaMovie[ x*Sin[x^2], {x, 0,6}, {-3,3}]

The Fundamental Theorem of Calculus   (on page 115)

Example 4.3.4      (on page 116)

Clear[f, a, Phi]

f[x_] = x + x^2 + x^3

a=1;

Phi[x_] = Limit[ Sum[ f[ a + i(x-a)/n ]*(x-a)/n, {i,1,n}], n->Infinity]

Phi'[x] //Simplify

Example 4.3.5

Clear[f, a, Phi]

Integrate[ x^2*Sqrt[x+1],{x,-1,1}]

bigF[x_] = Integrate[x^2*Sqrt[x+1],x]

bigF[1] - bigF[-1]

Plot[ x^2*Sqrt[x+1],{x,-1,1},Filling->Axis]

Definite Integrals with Integrate    (on page 117)

Integrate[ 1/(x^2 + 1), {x, 1, 3}]

%//N

Recall that you can change from InputForm (that which can be typed with Microsoft Notepad) to StandardForm (where the BasicMathInput palette is useful) or vice versa by using  Convert To 
under the Cell Menu.

Integrate[Sin[3*x]*Cos[2*x], {x, 0, Pi}]

(∫_0^π)Sin[3 x] Cos[2 x]\[DifferentialD]x

Variable Limits of Integration

Integrate[1/t^2, {t,1,x}]

Assuming[ x>0, Integrate[1/t^2,{t,1,x}] ]

Special Functions as Antiderivatives

Integrate[ Sin[x^2], x]

Integrate[ Exp[-x^2], x]

FresnelS[1] //N

Plot[ FresnelS[x], {x, -5, 5}]

More on Initial-Value Problems   (on page 118)

Example 4.3.6

y[t_] = 2 + Integrate[ E^(-s)*Sin[Pi*s], {s, 1, t}]

Example 4.3.7   (on page 119)

yprime[t_] = -1 + Integrate[ s*E^(-s), {s, 0, t}]

y[t_] = 1 + Integrate[ yprime[s], {s, 0, t}]

4.4   Numerical Integration

The Midpoint Rule

midptApprox[ func_, {x_, a_, b_}, n_] := 
Module[{f, h = (b-a)/n}, f[t_] := func /. x->t; 
		h*Sum[ f[a + i*h], {i,.5,n-.5}] ]

midptApprox[ Sin[x], {x, 0, Pi/2}, 16]

The Trapezoidal Rule   (on pages 120-121)

trapez[ func_, {x_, a_, b_}, n_] := Module[ {f, h = N[(b-a)/n]},
		f[t_] := func /. x-> t; 
		(h/2)*( f[a] + 2*Sum[ f[a+i*h], {i,1,n-1} ] + f[b] ) ]

Example 4.4.1

Clear[f]

f[x_] = Cos[ Sin[x] ]^2

midptApprox[ f[x], {x,0,2}, 4]

trapez[ f[x], {x,0,2}, 4]

linapprox=Interpolation[ Table[{x,f[x]},{x,0,2,0.5}],InterpolationOrder->1];

Plot[ linapprox[x],{x,0,2}]

Show[  Graphics[ Line/@ ({{#,0},{#,f[#]}} &) /@ {.5,1.,1.5,2}]]

Show[ Plot[ {f[x], linapprox[x]}, {x,0,2}, PlotStyle->Black, Filling->{{2->{Axis, {Lighter[Yellow, .7]}}},
{1->{{2}, {Green, Red}}}}, PlotRange->{0,1}],
       Graphics[ Line/@ ({{#,0},{#,f[#]}} &) /@ {.5,1.,1.5,2}]]

Simpson's Rule   (on pages 121-122)

simpson[ func_, {x_, a_, b_}, n_] := Module[ {f, h = (b-a)/n, wt},
		wt[i_] := 3.+ (-1)^(i-1);  f[t_] := func /. x -> t;
		If[ EvenQ[n], (h/3)( f[a] + Sum[  wt[i]*f[a+i*h], {i,1,n-1}] + f[b]), "n must be even"]]

f[x_] = 1

simpson[ f[x], {x,0,5}, 20]

Example 4.4.2  (page 122)

Clear[f]

f[x_] = Cos[ Sin[x] ]^2

simpson[ f[x], {x, 0, 2}, 4]

order2 = InterpolationOrder->2

quadapprox[z_]:=Evaluate[If[z≤1,Interpolation[Table[{t,f[t]},{t,0,1,0.5`}],InterpolationOrder->2][x],Interpolation[Table[{t,f[t]},{t,1,2,0.5`}],InterpolationOrder->2][x]]]

Note that our textbook has a serious misprint at the previous step.  Namely, our author has x appear in merely one place.

Plot[{f[x],quadapprox[x]},{x,0,2},PlotRange->{0,1}, Filling->{{2->{Axis, {Lighter[Yellow, .7]}}}, {1->{{2},{Green, Red}}}}]

Error Bounds     (at the beginning of page 123)

Example 4.4.3    (on page 123)

f[x_] = Cos[ Sin[x] ]^2

trapez[ f[x], {x, 0, 2}, 4]

midptApprox[ f[x], {x, 0, 2}, 4]

D2f[x_] = D[ f[x], {x,2}] //Simplify

Plot[ Abs[ D2f[x]], {x, 0, 2}, PlotRange->All]

2(2-0)*.5^2/12

2(2-0)*.5^2/24

Example 4.4.4   (on page 124)

f[x_] = Cos[Sin[x]]^2

simpson[ f[x], {x, 0, 2}, 4]

D4f[x_] = D[ f[x], {x, 4}] //Simplify

Plot[ Abs[ D4f[x] ], {x, 0, 2}, PlotRange->All]

16(2-0)*.5^4/180

Example 4.4.5

f[x_] = E^(-x^2/2)

D2f[x_] = D[ f[x], {x, 2}]//Simplify

Plot[ Abs[ D2f[x] ], {x, 1, 3} ]

sol1 = FindRoot[ Evaluate[ D[ D2f[x], x] ], {x, 1.7}]

bigK = Abs[ D2f[x] ] /.sol1

midptErrorBound[n_] = bigK*(3-1)^3/(24*n^2)

Table[{n, midptErrorBound[n]}, {n, 53, 57}] //TableForm

?NumberForm

NumberForm[ midptApprox[ f[x], {x, 0, 3}, 55], 12]

trapezErrorBound[n_] = bigK*(3-1)^3/(12n^2)

Table[{n, trapezErrorBound[n]}, {n, 75,80}]//TableForm

NumberForm[ trapez[ f[x], {x, 0, 3}, 78], 12]

D4f[x_] = D[ f[x], {x, 4}]//Simplify

Plot[ Abs[ D4f[x] ], {x, 1, 3} ]

sol2 = FindRoot[ Evaluate[ D[D4f[x], x ] ], {x, 1.4} ]

bigM = Abs[ D4f[x] ] /. sol2

simpsonErrorBound[n_] = bigM*(3-1)^5/(180n^4)

Table[ {n, simpsonErrorBound[n]}, {n, 6, 14, 2}]//TableForm

NumberForm[ simpson[ f[x], {x, 0, 3}, 10], 12]

Mathematica's NIntegrate   (on page 127)

NIntegrate[ Cos[ Sin[x] ]^2, {x, 0, 2} ]

The following    comparison    function is your teacher's supplement to the textbook to aid in appreciating more highly Mathematica's NIntegrate command.

midptApprox[ func_, {x_, a_, b_}, n_] := 
Module[{f, h = (b-a)/n}, f[t_] := func /. x->t; 
		h*Sum[ f[a + i*h], {i,.5,n-.5}] ]

trapez[ func_, {x_, a_, b_}, n_] := Module[ {f, h = N[(b-a)/n]},
		f[t_] := func /. x-> t; 
		(h/2)*( f[a] + 2*Sum[ f[a+i*h], {i,1,n-1} ] + f[b] ) ]

simpson[ func_, {x_, a_, b_}, n_] := Module[ {f, h = (b-a)/n, wt},
		wt[i_] := 3.+ (-1)^(i-1);  f[t_] := func /. x -> t;
		If[ EvenQ[n], (h/3)( f[a] + Sum[  wt[i]*f[a+i*h], {i,1,n-1}] + f[b]), "n must be even"]]

comparison[ func_, {x_,a_,b_}, n_] := Module[{f}, f[t_] = func/. x->t;
Print["Exact = ", Integrate[ f[t],{t,a,b}]];
Print["Exact as a decimal = ", N[ Integrate[f[t],{t,a,b}] ,14] ];
Print["Mathematica's NIntegrate = ", NIntegrate[f[t],{t,a,b}]];
Print["Midpoint Approximation = ", midptApprox[ f[t], {t,a,b} ,n] ];
 Print["Trapezoidal Approximation = ", trapez[ f[t], {t,a,b},n] ];
Print["Simpson's Approximation = ",  simpson[ f[t], {t,a,b},n] ] ]

comparison[x^3,{x,0,3},12]

comparison[Exp[-x^2],{x,0,33},12]

comparison[x^5*Sin[x],{x,0,33},12]

comparison[x^5*Sin[x],{x,0,33},100]

Mathematica' s NIntegrate is generally superior to the other numerical techniques.  It is likely based on a sophisticated algorithm that does not rely on subintervals of equal length.  It chooses more points at which to evaluate the function over those portions of the interval where the functional values are changing more rapidly.

Functions Given by Data

trapezData[ pts_] := 
.5*Sum[ (pts[[i,2]]+pts[[i+1,2]])*(pts[[i+1,1]]-pts[[i,1]]), {i,1,Length[pts]-1}]

Example 4.4.6

data = 
{{0,.275}, {.2,.510}, {.5,.916},{.65,1.186},{1.,1.615},{1.3, 2.23},{1.5,2.703},{1.75,2.781},{2.,2.607},{2.2,2.562},{2.4,2.554},{2.75,2.556},{3.,2.332},{3.3,1.97},{3.5,1.727},{3.7,1.641},{4,1.56},{4.25,1.658},{4.6,1.777},{4.7,1.686},{5.1,1.430},{5.2,1.095},{5.4,.771},{5.7,.493},{5.9,.383},{6.2,.366},{6.5,.369},{6.75,.306},{7.,.201},{7.1,.138},{7.5,.146},{7.7,.166},{8.,.137},{8.2,.0737},{8.6,.0378},{8.75,.0529},{9.1,.0768},{9.2,.0638},{9.5,.0235},{9.7,.00152},{10.,.0162} };

ListPlot[data,Joined->True,PlotRange->All]

trapezData[data]

The Formula for    simpsonData[ yList_, h_] := etc.   at the bottom of page 128 needs to be corrected.
As presented, its right member is the right member of the following expression.

MISPRINTEDsimpsonData[ yList_, h_] := 
Module[{wt, n=Length[yList]-1}, wt[i_] = 3. +(-1)^(i-1);
		If[ EvenQ[n], (h/3.)*(yList[[1]] + Sum[ wt[i]*yList[[i]], {i,2,n}]+yList[[n+1]]),
			"length of data list must be odd"]]

To see that it involves a mispring, apply it to the constant function  f(x) =  1 on the interval 0 ≤ x ≤ 5 with 
                               yList = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
consisting of twenty-one 1's and h = .25 ;  the area should be 5.

Illustration for misprinted formula.

ylist = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }

Length[ ylist ]

5./20.

MISPRINTEDsimpsonData[ ylist, .25 ]

To correct the formula, simply replace   3. +(-1)^(i-1)   with   3. + (-1)^i   .

CORRECTEDsimpsonData[ yList_, h_] := 
Module[{wt, n=Length[yList]-1}, wt[i_] = 3. +(-1)^i;
		If[ EvenQ[n], (h/3.)*(yList[[1]] + Sum[ wt[i]*yList[[i]], {i,2,n}]+yList[[n+1]]),
			"length of data list must be odd"]]

Now, for the preceding illustration, we have

CORRECTEDsimpsonData[ ylist, .25 ]

Example 4.4.7   (on page 129)

data = { .0404, .0835, .0752, .162, .275, .370, .661, .916, 1.186,1.615,2.033,2.303,2.381,2.407,2.562,2.554,2.556,2.332,1.927,1.641,1.515,1.358,1.077,.886,.830,.795,.671,.493,.383, .366, .361, .306, .201, .138, .146, .1370, .0737, .0808, .0529, .0768, .0638 };

Length[data]

ListPlot[data,Joined->True,Ticks->{None,Automatic}]

MISPRINTEDsimpsonData[ data, .25 ]

CORRECTEDsimpsonData[ data, .25 ]

4.5  Improper Integrals   (starting on page 130)

Example 4.5.1

int1 = Assuming[ t>1,Integrate[ x^(-3/2), {x,1,t}] ]

Limit[ int1, t->Infinity]

Plot[x^(-3/2),{x,1,15},PlotRange->{{0,15}, All},Filling->Axis]

Example 4.5.2

int2 = Assuming[ 0<t<1, Integrate[ 1/Sqrt[ E^x - 1], {x,t,1}] ]

lim2 = Limit[ int2, t->0, Direction->-1]

N[ lim2 ]

?PlotRange

Plot[(1/(Sqrt[(E^x)-1])),{x,0,1},PlotRange->{0,8},Filling->Axis]

Example 4.5.3

int3 = Assuming[ 0<t<Pi/2,Integrate[ Tan[x], {x, 0, t} ] ]

Limit[ int3, t-> Pi/2, Direction->1]

Show[ Plot[Tan[x],{x,-2,2}] , Plot[ Tan[x], {x,0,Pi/2}, Filling->Axis, PlotRange->{-12,12}]]

Example 4.5.4  ( It would seem that the author of our textbook should use the  Assuming  command here.)

Plot[1/Sqrt[ Sqrt[x]*(1- Sqrt[x]) ], {x,0,1}, PlotRange->{0,10}, Filling->Axis]

f[x_] = int4 = Assuming[ 0 <x<1, Integrate[ 1/Sqrt[ Sqrt[x]*(1 - Sqrt[x])], x] ]

Limit[ int4, x->1, Direction->1] - Limit[ int4, x->0, Direction->-1]

Experimentation

Clear[g]

g[x_] = Integrate[ 1/Sqrt[ Sqrt[x]*(1 - Sqrt[x])], x]

f[x]

Table[ {x, f[x], g[x],f[x]-g[x]}, {x,.05,.95,.05}]  //TableForm

________________________________________________________________________________________________________

{Integrate[ x^(-3/2), {x, 1, Infinity} ], Integrate[ 1/Sqrt[E^x - 1], x], Integrate[ Tan[x], {x, 0, Pi/2} ], Integrate[1/Sqrt[ Sqrt[x]*(1- Sqrt[x]) ], {x,0,1}] }

Emphasis for the Quiz on Chapter 4 (the week following  Feb 10-14) will emphasize 
pages 119-120, Exercises 11, 12, 13, 14, 15, 16, 17, 18, 19; and 
page 134, Exercises 1, 2, 3, 4, 5, 6.