Integrating with Mathematica
(Supplement to Making Some Impossible Integrals Possible)
by Dave Morstad
Objectives of Assignment
1. To demonstrate how Mathematica integrates indefinite integrals symbolically.
2. To demonstrate how Mathematica integrates definite integrals symbolically.
3. To demonstrate how Mathematica integrates numerically.
è Mathematica is case sensitive. Upper and lower case letters are not interchangeable.
è You must provide the constant of integration for indefinite integrals.
è To make Mathematica type bigger, click on “Format” up at the top of the screen, then, at the bottom of the pull-down menu, click on magnification and select 200%.
I. Indefinite Integrals
Mathematica 3.0 has two ways in which you can tell it to perform integration. You can
use the “Integrate” command, or you can use the mouse to construct the integral. Both methods are shown in the following example.
Example 1. An integral Mathematica can evaluate: x
x dx 2 3 1 +
∫
.Method 1: Type in “Integrate[x^2/(x^3 + 1), x]” and press
SE
.The output Mathematica gives is Log [x 1] 3
3 +
Method 2: Look over to the right side of the computer screen. Click the mouse on the button containing
∫
dl . That action should put the integral symbol in your Mathematica workspace.Next, right-click your mouse and choose “Expression Input.” Then choose “Fraction.” Now you can type in the numerator and denominator. Right-click, choose “Expression Input” and “Superscript” to put in the exponents. Finally, put an “x” in the little box after dl.
Press
SE
. Again, the output Mathematica gives is Log [x 1] 33 +
. You must add C. Log is base e.
Example 2. An integral Mathematica cannot evaluate:
∫
sin(sin )x dx. Type “Integrate[Sin[Sin[x]], x]” or construct it, and pressSE
.The output is Sin[Sin[x]] dl x
∫
, which is exactly what you put in. Whenever Mathematica gives back the same thing you typed in, it meansMathematica could not evaluate it.
Example 3. An example of an integral evaluated to a special function: e dxx2
∫
. Type “Integrate[E^x^2, x]” or construct it, and pressSE
.The output is 1
2 π Erfi [x] So what does Erfi mean?
For help, type “?? Erfi” and press
SE
. Mathematica will tell you a little about Erfi. Look in a text on complex analysis for more information. This type of an answer means Mathematica couldn’t come up with an elementary solution.II. Definite Integrals
Example 1. A simple definite integral: sin2
0
x dx
π
∫
.Type “Integrate[Sin[x]^2, {x, 0, Pi}]” or construct it by selecting the definite integral button. Notice e and π are available on the palette at the right. Press
SE
.The output is π 2.
Example 2. An integral with a more complicated solution: sin x dx2 0
2π
∫
.Type “Integrate[Sin[x^2], {x, 0, 2*Pi}]” or construct it and press
SE
.The output is π
[
π]
2 FresnelS 2 2 . This is a solution which is given in terms of the special function FresnelS. Probably not very helpful. See below to get a numerical value for this quantity.
***** VALUABLE INFO *****
Type “N[%]” and press
SE
to make Mathematica numerically evaluate its last output. In Example 2, the numerical value of the solution is 0.642138.Type “N[%%]” and press
SE
to make Mathematica evaluate its second to the last output. For example, if you did Example 1 and then Example 2, N[%%] will evaluate π2, the output from Example 1, and give you 1.5708.
N[%%%] will evaluate the third to the last output, and so on.
III. Numerical Approximation of Definite Integrals with “NIntegrate”
When Mathematica cannot evaluate a definite integral using the command Integrate[f(x), {x, a, b}], you can force Mathematica to approximate the integral through numerical methods. NIntegrate[f(x), {x, a, b}] tells Mathematica to find a numerical approximation.
Type “Integrate[Sin[Sin[Sin[x]]], {x, 0, Pi}]” and press
SE
. The output, after Mathematica loads some special integration packages, isx dl ] in[Sin[x]] Sin[S
∫∫
. Since this is the same as your input, it means thatMathematica could not evaluate it.
Instead, type “NIntegrate[Sin[Sin[Sin[x]]], {x, 0, Pi}]” and press
SE
. If you prefer to construct the integral, you can type in and constructN[
∫∫
π 0 50] x, dl n[x]]] Sin[Sin[Si .Now the output is correct to 50 decimal places.
NIntregrate will not allow you to specify 50 place of accuracy, but you can use the following: N[Integrate[Sin[Sin[x]], {x, 0, π }], 50]. Example 2. x x x x dx sin sin +cos
∫
0 2 π .Using Integrate will not result in a solution.
Type “NIntegrate[x*Sin[x]/(Sin[x]+ Cos[x]), {x, 0, Pi/2}]” and press
SE
. The output is 0.802635.Example 3. An integral which might not converge: x x
x x dx sin sin +cos
∫
0 5 6 π .Using NIntegrate for this integral gives –0.850269, but Mathematica also gives a warning that the value is possibly incorrect. If you plot the function, you will see it has a vertical asymptote within the interval of integration.
Using N[ x x x x dx sin sin +cos
∫
0 5 6 π, 8] will yield an answer of 4, but changing the 8 to 25 will result in an answer of 0. × 101, both after giving that convergence warning.
IV. Iterated Integrals
***** WARNING *****
When you type the “Integrate”, Mathematica’s order of integration may seem
backwards compared to standard notation. Mathematica will first integrate with respect to the variable which is listed last. It will integrate last with respect to the variable which is listed first. Example 1.
(
yx)
dydx x 2 2 0 0 1 2 −∫
∫
.Type “Integrate[x^2/(y – 2)^2, {x, 0, 1}, {y, 0, x}]”, or construct it by clicking on the definite integral button, selecting the integrand box after it’s been inserted, and then clicking the definite integral button again, and press
SE
.Notice the order of the limits of integration in the Integrate command: x is given first, y second. This may seem backwards.
The answer is − +8
3 4Log[ ]. Enter N[%] to simplify the answer to 0.105922. 2
Example 2. 1
44 3 3 22
(x+ ) (y+ ) (z+ ) dydzdx
∫∫∫
.Type “Integrate[1/((x+4)^4*(y+3)^3*(z+2)^2), x, z, y]” and press
SE
.The solution is -1
6(4+ x)3
(3+ ) (2 2+ )
y z .
If you want to see the denominator multiplied out, enter ExpandAll[%].
Example 3. (x )(y )(z ) dzdydx x y x y x x + + + − − − − − − − − −
∫
∫
∫
2 3 4 1 1 1 1 1 1 2 2 2 2 2 2 . Type “Integrate[(x + 2) * (y + 3) * (z + 4), {x, -1, 1}, {y, -Sqrt[1 - x^2], Sqrt[1 - x^2]},IV. Practice Problems
Use Mathematica to evaluate the following:
1.
∫
xsin−1x dx 2. x x dx 1+∫
3. x x dx 1+∫
sin 4. x x dx 1 0 +∫
sin π 5. x x+ x dx∫
sin 6. x x+ x dx∫
sin 0 π Solutions: 1. x 1 x x x x C 4 4 2 2 1 2 1 − − − + − + sin sin ; 2. − + + + 4 3 2 3 1 x x C; 3. 2 2 2 2 2 2 2log(cos sin ) cos sin
cos sin x x x x x x x x C + + − + + + ; 4. π;
Making Some Impossible Integrals Possible
Objectives of Assignment
1. To practice integrating using Mathematica.
2. To practice reversing the order of integration and switching integrals to different coordinate systems when the integration seems impossible.
I. Introduction
Mathematica is an integrating wizard. Its extensive collection of integration algorithms
will probably seem very impressive if you’ve been through Calculus II. Unfortunately, even with
Mathematica’s great prowess evaluating integrals, there are still many types of integrals which
are simply impossible unless special functions or series are utilized.
Some integrals which seem impossible to evaluate only need to be restated in an alternate form to render them solvable. For example, reversing the order of integration or switching to a different coordinate system can sometimes change a non–integrable integrand into something more workable.
Working through the preceding examples in the hand–out “Integrating with
Mathematica” is a prerequisite for attempting this part if the assignment. The correct syntax
and several other little tricks are explained there.
II. A Typical Example
Suppose you want to evaluate sinh x dA
R
2
∫∫
where R is the dark region in the graph below.Setting up the integral so as to integrate first with respect to x requires determining the following limits of integration:
sinh x dxdy left curve right curve bottom value top value 2
∫
∫
.A simple examination of the graph of R reveals the desired limits of integration:
sinh ln ln x dxdy x y x y y 2 2 0 2 = = = =
∫
∫
.Unfortunately, if you try to evaluate this double integral by hand, you will get stuck. Feeding it to Mathematica results in some bright red warnings about recursions exceeding 256. After the warnings, Mathematica gives a long messy output with Erf’s and Erfi’s. You can then use N[%] to find out that Mathematica thinks the value is 0.058827, but this is the result of numerical methods not the result of symbolic integration. (Face saving hint: Do not ask the instructor what N[%], Erf, and Erfi mean. It will be a dead give away that you did not even go through the first page of “Integrating with Mathematica”.)
Well, if Mathematica could not symbolically integrate it the way you wrote it above, why not try another way? For example, switch to Polar coordinates or reverse the order of integration. Since nothing in this problem resembles circles or polar relationships, try reversing the order of integration. That is, set up the integral as follows:
sinh x dydx bottom curve top curve leftmost value rightmost value 2
∫
∫
.This will result in the following integral:
sinh ln x dydx y y x x x 2 0 0 2 = = = =
∫
∫
.You should be able to evaluate this by hand and, if you’ve been doing the homework, you should understand why this is possible to evaluate but the other double integral was not.
Having Mathematica evaluate this produces no warnings and yields an answer of 0.0588275. Now it is apparent that the first answer was accurate.