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: ∫ x2 dx . x3 + 1 Method 1: Type in “Integrate[x^2/(x^3 + 1), x]” and press SE. The output Mathematica gives is Log [x3 + 1] . You must add C. 3 Log is base e. 24 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. Log [x3 + 1] Press SE. Again, the output Mathematica gives is . You 3 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 press SE. 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 means Mathematica could not evaluate it. Example 3. An example of an integral evaluated to a special function: x ∫ e dx . 2 Type “Integrate[E^x^2, x]” or construct it, and press SE. The output is 1 π Erfi [x] So what does Erfi mean? 2 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. 25 II. Definite Integrals Example 1. A simple definite integral: ∫ π sin2 x dx . 0 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: ∫ 2π sin x 2 dx . 0 Type “Integrate[Sin[x^2], {x, 0, 2*Pi}]” or construct it and press SE. [ ] π FresnelS 2 2π . This is a solution which is given in 2 terms of the special function FresnelS. Probably not very helpful. See below to get a numerical value for this quantity. The output is ***** 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. π Example 1. ∫ sin(sin(si n x))dx . 0 26 Type “Integrate[Sin[Sin[Sin[x]]], {x, 0, Pi}]” and press SE. The output, after Mathematica loads some special integration packages, is ∫ Sin[Sin[Sin[x]] ] dl x . Since this is the same as your input, it means that Mathematica 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 construct ∫ π N[ Sin[Sin[Si n[x]]] dl x, 50] . 0 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. ∫ π 2 0 x sin x dx . sin x + cos x 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: ∫ 5π 6 0 x sin x dx . sin x + cos x 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. ∫ 5π 6 x sin x dx , 8] will yield an answer of 4, but changing the 8 to 0 sin x + cos x 25 will result in an answer of 0. × 101, both after giving that convergence warning. Using N[ 27 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. 1 x x2 Example 1. 2 dydx . 0 0 ( y − 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. 8 The answer is − + 4 Log[2]. Enter N[%] to simplify the answer to 0.105922. 3 Example 2. 1 ∫∫∫ ( x + 4) ( y + 3) ( z + 2) 4 3 2 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 + y )2 ( 2 + z ) 3 If you want to see the denominator multiplied out, enter ExpandAll[%]. Example 3. 1 1 − x2 −1 − 1− x 2 ∫ ∫ ∫ 1 −x 2 − y 2 − 1− x 2− y2 ( x + 2)( y + 3)( z + 4) dzdydx . Type “Integrate[(x + 2) * (y + 3) * (z + 4), {x, -1, 1}, {y, -Sqrt[1 - x^2], Sqrt[1 - x^2]}, {z, -Sqrt[1 - x^2 - y^2], Sqrt[1 - x^2 - y^2]}]” and press SE. Of course, it can all be on the same line, but it might be easier to use several lines. The output should be 32 Pi (approximately100.531). 28 IV. Practice Problems Use Mathematica to evaluate the following: 1. 2. 3. 4. ∫ ∫ ∫ ∫ x sin− 1 x dx x dx 1+ x x dx 1 + sin x π x dx 1 + sin x 0 5. 6. ∫ ∫ x dx x + sin x π 0 x dx x + sin x Solutions: 1. x 1 − x 2 sin −1 x x 2 sin−1 x − + +C; 4 4 2 3. 2 log(cos 2x + sin 2x ) + 2. − x cos 2x + x sin x2 +C; cos 2x + sin 2x 5. Mathematica can’t evaluate it +C ; 4. π; 6. 2.06981 29 − 4 + 2x 1 + x + C ; 3 3 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 where R is the dark region in the graph 2 R below. 30 Setting up the integral so as to integrate first with respect to x requires determining the following limits of integration: ∫ top value bottom value ∫ right curve sinh x 2 dxdy . left curve A simple examination of the graph of R reveals the desired limits of integration: y =ln 2 ∫ ∫ y=0 x = ln 2 sinh x 2 dxdy . x=y 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: ∫ ∫ rightmost value leftmost value top curve sinh x 2 dydx . bottom curve This will result in the following integral: x = ln 2 ∫ ∫ x=0 y=x sinh x 2 dydx . y =0 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. 31