Integration: Computing Sums
Integration: Computing Sums
Integration: Computing Sums
This notebook will show you how to use Mathematica to explore topics introduced in Chapters 5 through 7 of the text.
Computing Sums
The Mathematica command Sum#aj , j, 1, n' is used to compute nj 1 a j . For example, the following input
instructs Mathematica to compute nj 1 +5 j2 4/.
In[1]:=
Out[1]=
10
In[2]:=
+5 j2 4/
j 1
Out[2]=
1885
Riemann Sums
Since Mathematica does not contain a built-in function for illustrating and computing Riemann sums (discussed in Section
5.2 of the text), you can execute the following input cell to create a function which will compute Rn . Don't worry about
understanding the code contained here since it involves a couple of advanced concepts which have not been introduced in
this manual.
Integration
52
In[3]:=
ba
cccccccccccc , recs, est, gr, x, c ,
n
x#i_' : a i 'x;
c#i_' : x#i 1 m';
n
est
f#c#i'''x;
i 1
recs ListPlot#
Flatten#Table#
x#i 1', 0, x#i 1', f#c#i'',
x#i', f#c#i'', x#i', 0, i, 1, n', 1',
PlotJoined True, DisplayFunction Identity,
PlotRange All, PlotStyle RGBColor#0, 0, 1'';
gr Plot#f#x', x, a, b, DisplayFunction Identity, PlotRange All';
Show#recs, gr, DisplayFunction $DisplayFunction';
Return#est ss N'(
To compute a Riemann sum Rn , define the function f which you are using first and then enter r#a, b, n, 0'if ci xi1 ,
enter r#a, b, n, 1'if ci xi or enter r#a, b, n, .5' if ci cccc12 +xi1 xi /. For example, R6 is computed below for
2
the integral 1 +x3 1/ x using ci xi , ci xi1 and ci cccc12 +xi1 xi /, respectively.
In[4]:=
f#x_' :
r#1, 2,
r#1, 2,
r#1, 2,
x3 1;
6, 0'
6, 1'
6, .5'
1.2
Out[5]=
4.1875
1.4
1.6
1.8
Integration
Out[6]=
1.2
1.4
1.6
1.8
1.2
1.4
1.6
1.8
5.35417
Out[7]=
4.73958
To compute the Riemann sums without the graphical illustration of the rectangles, first execute the contents of the following input cell.
In[8]:=
Clear#r';
ba
cccccccccccc , recs, est, gr, x, c ,
n
est
f#c#i'''x;
i 1
Return#est ss N'(
2
In the following cell, Rn is computed for the integral 0 #x+x 1/'dx for values of n
ci cccc12 +xi1 xi /.
53
Integration
54
In[10]:=
Clear#f';
f#x_' x+x 1/;
Table#r#0, 2, n, .5', n, 10, 100, 10' ss TableForm
Out[12]//TableForm=
0.66
0.665
0.665926
0.66625
0.6664
0.666481
0.666531
0.666563
0.666584
0.6666
2
Indefinite Integrals
Enter Integrate#f #x', x' to evaluate f #x' x.
In[13]:=
Out[13]=
In the above output, note the Mathematica does not include a general constant term C and therefore you must remember
that +3x2 2x 5/dx x3 x2 5x C.
An indefinite integral can also be evaluated using the button e f found on the palette BasicInput.
In[14]:=
Out[14]=
2
+3x 2x 5/ x
5 x x2 x3
Definite Integrals
b
On the palette BasicInput, click on f e f to enter the integral in a more natural form.
2
In[16]:=
2
+3x 2x 5/ x
1
Out[16]=
Integration
55
Partial Fractions
The Apart command will form the standard decomposition of a rational function.
In[17]:=
Out[17]=
1
cccccccccccccc (
Apart$ cccccccccccccccccccccccccccccccc
+2x 5/+x2 1/x
1
8
2 5 x
cccccccc cccccccccccccccccccccccccccccccc cccccccccccccccccccccccc
ccc
5x
145 +5 2 x/
29 +1 x2 /
Before looking at the following output, can you guess that value of the integral based upon the partial fractions in the output
above?
In[18]:=
Out[18]=
% x
5
2 ArcTan#x'
Log#x'
4
ccccccccccccccccccccccccccccccc cccccccccccccccccc cccccccccc Log#5 2 x' ccccccc Log#1 x2 '
58
29
5
145
Out[19]=
1
cccccccccccccc x
cccccccccccccccccccccccccccccccc
+2x 5/+x2 1/x
2 ArcTan#x'
Log#x'
4
5
ccccccccccccccccccccccccccccccc cccccccccccccccccc cccccccccc Log#5 2 x' ccccccc Log#1 x2 '
29
5
145
58
In[20]:=
Out[20]=
eq
1
cccccccccccc y
1y
Log#1 y'
1
t c
cccccccccccccc
1 t2
c ArcTan#t'
Now t and y are replaced with the values 1 and 2, , respectively, using the replacement operator s. and the Solve
command is used to find the value of c.
In[21]:=
Out[21]=
cvalue
1
c cccc +S 4 Log#3'/
4
So the value of c is cccc14 +S 4ln+3// and this value can be recalled by extracting it from the list with
cvalue##1, 1, 2''.
In[22]:=
cvalue##1, 1, 2''
Out[22]=
1
cccc +S 4 Log#3'/
4
Use the Solve command again to find y after replacing c with the value found in the last output.
Integration
56
In[23]:=
sol
y 1 ccc4c
1
Out[23]=
Numerical Integration
2
1
x s2 dx using Mathematica together with the Trapezoidal rule, Midpoint rule or
Suppose you wanted to estimate 0 cccccccc
rcccc
c e
2
2S
f#x_'
1
x2
cccccccc
cccccc cccc2cccc ;
r
2S
ba
cccccc and we will let the functions x#i',
In this example, a 0, b 2, and for this example, we will let n 100. Then h cccc
n
y #i', and m#i' represent xi , yi , and mi , respectively. Then using the formulas introduced in Section 5.10 of the text, we
can define the variables used in the numerical integration rules.
In[25]:=
Clear#a, b, n, x, y, h';
n 100; a 0; b 2;
ba
h cccccccccccc ;
n
x#i_' 0 i h;
1
m#i_' cccc +x#i 1' x#i'/;
2
y#i_' : f#x#i'';
2
1
h
x s2 dx using the Trapezoidal rule, you need to enter and execute cccc
+y#0' 2 n1
To estimate 0 cccccccc
rcccc
c e
i 1 y#i' y#n'/.
2
2S
2
(The command N #expr, 20' is also used to round the value of expr to 20 digits.)
In[31]:=
Out[31]=
n1
\
h L
]
M
]
M
N$ cccc M
2
y#0'
y#i' y#n']
M
], 20(
2 N
i 1
^
0.47724626867805128894
Midpoint Rule:
n
In[32]:=
Out[32]=
0.47725166772970608614
Simpson's Rule
In[33]:=
Out[33]=
h
N$ cccc +y#0' 4 Sum#y#i', i, 1, n 1, 2'
3
2 Sum#y#i', i, 2, n 2, 2' y#n'/, 20(
0.47724986795579570810
Integration
NIntegrate Command
b
The built-in command, NIntegrate#f #x', x, a, b' is used to numerically integrate a f +x/dx using a built-in
Mathematica algorithm.
In[34]:=
Out[34]=
Graphics`Graphics`
In[36]:=
0.5
-1
-0.5
0.5
-0.5
-1
Out[36]=
hGraphicsh
57
Integration
58
In[37]:=
gr
0.5
-1
-0.5
0.5
-0.5
-1
Out[37]=
hGraphicsh
Computing ArcLength
Suppose you wanted to compute the arclength of the curve below for the given parametric equations.
In[38]:=
x#t_' t Cos#t';
y#t_' t Sin#2t';
ParametricPlot#x#t', y#t', t, 0, 4'
3
2.5
2
1.5
1
0.5
-1
Out[40]=
hGraphicsh
Integration
59
Using formula (5) in Section 6.4 of the text, you can attempt to find the value of the arclength with Mathematica.
4
In[41]:=
x '#t'2 y '#t'2 t
0
4
Out[41]=
+1 2 Cos#2 t'/2 +1 Sin#t'/2 t
Mathematica is unable to find the exact value of the integral, so you can instead use the NIntegrate command instead (or
you could use the numerical integration methods introduced in Section 5.10).
In[42]:=
Out[42]=
NIntegrate$
x '#t'2 y '#t'2 , t, 0, 4(
9.33017
Out[44]=
sin+x/, from x
0 to x
Sin#x';
NIntegrate$ 1 f '#x'2 , x, 0, 2 S(
f#x_'
7.6404
Improper Integrals
1
ccccccc dx
Improper integrals are illustrated in this section using a few simple examples. Suppose you want to compute 1 cccccccc
+1x/2
b
1
ccccccc dx and therefore the Limit command can be
using Mathematica. By definition, this integral represents limb 1 cccccccc
+1x/2
Out[46]=
Clear#b, x';
b
1
Limit$ cccccccccccccccccccc x, b (
2
1 +1 x/
1
cccc
2
Mathematica can also evaluate the improper integral directly if you replace b with .
In[47]:=
Out[47]=
1
cccccccccccccccccccc x
+1 x/2
1
cccc
2
1
Since 1 cccccccc
rcccc
cccc x converges, Mathematica is able to evaluate the integral successfully.
x1
In[48]:=
Out[48]=
2
1
cccccccccc
cccccccc
r
x
1
x1
When you attempt to evaluate the following integral, Mathematica outputs a warning message indicating that the integral
does not converge.
Integration
60
In[49]:=
2
1
x
cccccccccccccccccccccccc
3s2
1 +x 1/
1
Integrate::idiv : Integral of cccccccccccccccccccccccc
ccc does not converge on 1, 2.
+1 x/3s2
2
Out[49]=
1
ccc x
cccccccccccccccccccccccc
3s2
1 +1 x/
2
1
cccccccccc x.
To determine if the integral diverges to or (or neither), use Mathematica to compute lima1 a cccccccc
+x1/3 2
s
In[50]:=
Clear#a';
2
1
Limit$ cccccccccccccccccccccccc x, a 1, Direction 1(
3s2
a +x 1/
Out[51]=
Out[53]=
Clear#f';
t#x_' Normal#Series#Sin#x', x, 0, 11''
x3
x5
x7
x9
x11
x ccccccc cccccccccc ccccccccccccc cccccccccccccccccc cccccccccccccccccccccccc
6
120
5040
362880
39916800
By graphing the function together the corresponding Taylor polynomial, you can compare the two functions.
In[54]:=
-5
-2.5
-0.5
2.5
7.5
-1
-1.5
-2
Out[54]=
hGraphicsh
From the graph above, you can see that the polynomial is a "good" approximation of the sine curve from about 5 to 5. To
see the difference between the two functions, graph sin+x/ T11 +x; 0/ . From the corresponding graph below, it appear
that the difference between sin+x/ and T11 +x; 0/ is less than about 0.175 for 5 x 5.
Integration
In[55]:=
Out[55]=
-2
hGraphicsh
S.
Clear#t';
t#x_' Normal#Series#Sin#x', x, S, 11'';
Plot#Sin#x', t#x', x, 3S, 3S, PlotRange 2, 2, PlotStyle
Thickness#.03', RGBColor#0, 1, 0', Thickness#.01', RGBColor#0, 0, 1''
2
1.5
1
0.5
-7.5
-5
-2.5
-0.5
2.5
7.5
-1
-1.5
-2
Out[58]=
hGraphicsh
The command Series#f #x', x, c, n' will display the beginning terms of a Taylor series up to powers of n. The
last term O#x'12 represents all the terms with powers of 12 and higher which are not explicitly shown.
In[59]:=
Out[59]=
61
Integration
62
In[60]:=
Out[60]=
1
Sin# ccc
c'
n
Table$ ccccccccccccccccccccc ss N, n, 1, 50(
1
ccc
c
n
0.841471,
0.997398,
0.999259,
0.999656,
0.999802,
0.999871,
0.999914,
0.958851,
0.997944,
0.999349,
0.999685,
0.999815,
0.999878,
0.999918,
0.981584,
0.998334,
0.999423,
0.999711,
0.999827,
0.999885,
0.999921,
5
Now, for example, suppose you wish to compute
cccccccc . As indicated in Section 7.4 of the text, the value of
n 1 cccccccc
n+n1/
5
k
5
is
the
limit
of
the
sequence
cccccccc
c
ccc
c
ccc
cccccccc
c
ccc
c
ccc
.
The
output below shows the first 200 terms of this sequence.
n 1 n+n1/
n 1 n+n1/
k 1
In[61]:=
Out[61]=
5
\ ss N, k, 1, 200(
M
Table$ L
M cccccccccccccccccccccc ]
]
n+n
1/
N
^
n 1
2.5, 3.33333, 3.75, 4., 4.16667, 4.28571, 4.375, 4.44444, 4.5, 4.54545,
4.58333, 4.61538, 4.64286, 4.66667, 4.6875, 4.70588, 4.72222, 4.73684,
4.75, 4.7619, 4.77273, 4.78261, 4.79167, 4.8, 4.80769, 4.81481,
4.82143, 4.82759, 4.83333, 4.83871, 4.84375, 4.84848, 4.85294, 4.85714,
4.86111, 4.86486, 4.86842, 4.87179, 4.875, 4.87805, 4.88095, 4.88372,
4.88636, 4.88889, 4.8913, 4.89362, 4.89583, 4.89796, 4.9, 4.90196,
4.90385, 4.90566, 4.90741, 4.90909, 4.91071, 4.91228, 4.91379, 4.91525,
4.91667, 4.91803, 4.91935, 4.92063, 4.92188, 4.92308, 4.92424, 4.92537,
4.92647, 4.92754, 4.92857, 4.92958, 4.93056, 4.93151, 4.93243, 4.93333,
4.93421, 4.93506, 4.9359, 4.93671, 4.9375, 4.93827, 4.93902, 4.93976,
4.94048, 4.94118, 4.94186, 4.94253, 4.94318, 4.94382, 4.94444, 4.94505,
4.94565, 4.94624, 4.94681, 4.94737, 4.94792, 4.94845, 4.94898, 4.94949,
4.95, 4.9505, 4.95098, 4.95146, 4.95192, 4.95238, 4.95283, 4.95327,
4.9537, 4.95413, 4.95455, 4.95495, 4.95536, 4.95575, 4.95614, 4.95652,
4.9569, 4.95726, 4.95763, 4.95798, 4.95833, 4.95868, 4.95902, 4.95935,
4.95968, 4.96, 4.96032, 4.96063, 4.96094, 4.96124, 4.96154, 4.96183,
4.96212, 4.96241, 4.96269, 4.96296, 4.96324, 4.9635, 4.96377, 4.96403,
4.96429, 4.96454, 4.96479, 4.96503, 4.96528, 4.96552, 4.96575, 4.96599,
4.96622, 4.96644, 4.96667, 4.96689, 4.96711, 4.96732, 4.96753, 4.96774,
4.96795, 4.96815, 4.96835, 4.96855, 4.96875, 4.96894, 4.96914,
4.96933, 4.96951, 4.9697, 4.96988, 4.97006, 4.97024, 4.97041, 4.97059,
4.97076, 4.97093, 4.9711, 4.97126, 4.97143, 4.97159, 4.97175, 4.97191,
4.97207, 4.97222, 4.97238, 4.97253, 4.97268, 4.97283, 4.97297,
4.97312, 4.97326, 4.9734, 4.97354, 4.97368, 4.97382, 4.97396, 4.97409,
4.97423, 4.97436, 4.97449, 4.97462, 4.97475, 4.97487, 4.975, 4.97512
Integration
From the output above, the sequence of partial sums appears to converge to 5. To verify this conjecture, the limit is com5
cccccccc 0.
puted to obtain
n 1 , cccccccc
n+n1/
In[62]:=
Out[62]=
k
5
L cccccccccccccccc
\, k ( ss Simplify
cccccc ]
Limit$ M
M
]
n+n
1/
N
^
n 1
Out[63]=
5
L cccccccccccccccc
\
cccccc ]
M
M
]
n+n
1/
N
^
n 1
63