Learning Mathematica Basics—Calculus Operations II

Solve each of the following problems using Mathematica exclusively.

1.  In the last notebook we considered differential operations in Mathematica. We now move on to look at integral operations and series generation.  First we'll consider the most basic integration command:  ask Mathematica for the syntax of Integrate.

2.   Looks like it comes in several flavors!  First the most basic...the indefinite integral.  To integrate x^4 the command would be simply Integrate[x^4,x].  Instead, go ahead and integrate the function x sin x.  (Don't forget to change it into Mathematica format first.)

3.   The previous integral required integration by parts, so it would appear that Mathematica can handle at least one step beyond basic integration.  Now you're probably wondering why it is necessary to tell Mathematica what variable we intend to integrate with respect to.  After all, shouldn't it be obvious?  Actually, no!  The integrand you are working with may involve other variables (or parameters.)  Say, for example, you wanted to integrate the generalized quadratic function a x^2 + b x + c.  It would be possible for the integral to be taken with respect to any of a, b, c, or x.  In fact go ahead and do the integral all four ways.

4.   Notice how many ways the same function can be integrated.  Clearly it is important to be able to instruct Mathematica which variable to integrate with respect to.  (By the way, you will often notice similar syntax requirements for other commands in Mathematica, i.e. the need to specify which part of an expression to treat as the variable.  If Mathematica is to retain the ability to handle generalized expressions as in the previous example then it must provide a place for the variable to be specified.)

Let's now try a definite integral.  Check back for the syntax in problem 1 if necessary, and integrate x^3 e^x from 0 to 3.

5.   Observe that the answer we got to the previous problem was exact.  i.e. Mathematica didn't "cheat" and use some numerical technique to produce a decimal approximation to this integral—rather, it actually calculated an antiderivative of the function and substituted in the limits.

OK, so far we've teased Mathematica with a couple of problems requiring integration by parts.  Let's hit it with a few more sophisticated indefinite integrals.

a)  Integrate 1/ (x^2 + 6x + 13)  (which requires completing the square and trigonometric substitution when done "by hand".)

b)  Integrate 1/ (3x^4 - 2x^3 + 18x - 12x)  (which requires partial fractions and substitution when done "by hand".)

c)  Integrate (x^2 + 4x + 3)^(1/2)  (which requires some pretty fancy substitutions when done "by hand".)

d)  Integrate sin(1/(x^2+1)) (which requires goodness knows what when done "by hand".)

6.   That last one did not finish successfully!  It got part of the integral, but part of it was left unevaluated...a valiant effort, maybe.  You should be aware by this point in your mathematical career that given an integral at random, it will almost certainly be impossible to do by hand (symbolically).  Mathematica is no better at these than we are.  Such "impossible" integrals are usually treated numerically, or through the use of infinite series depending on whether the integral is definite or indefinite.

Let's consider the previous integral again, this time giving it some limits—a lower limit of 0 and an upper limit of 2.  Now don't dive straight in and use Integrate.  That would result in the same problem we just had in part d) above when Mathematica tried for an antiderivative.  i.e. it would never get as far as substituting in the limits.  Instead the command we need is NIntegrate.  Apart from it's name, the syntax of this command is identical to Integrate on definite integrals.  Go ahead and use it on the previous problem with the given limits.

7.   You should have gotten about 1.01313.  And fairly rapidly too.  Do not confuse NIntegrate[...] with N[Integrate[...]].  The first of these does the problem numerically from the start using an adaptive version of Simpson's rule, whereas the second attempts to do the problem symbolically, then give the decimal of the answer.  Because of its speed and accuracy NIntegrate is often used even when Integrate would work.

Let's move on to the construction of the series expansion of a function.  Ask Mathematica for the syntax of Series.

8.   So to generate a series expansion of sin x, say, centered at 0, (i.e. a Maclaurin series) going out as far as the 9th degree term, the command would be:  Series[Sin[x],{x,0,9}].  Go ahead and issue this command to see how it works, and read the result into a variable called sinseries.

9.   Look carefully at the result.  Observe that the last term has a somewhat strange appearance.  This is referred to as "big Oh" notation.  What it means is that the actual series goes on forever, but that all of the remaining terms are of order x^10 or greater.  Unfortunately this term, despite being strictly mathematically correct, can be an inconvenience when it comes to actually using the series in further calculations.  Thankfully Mathematica provides a way of stripping away this term—the Normal command.  Go ahead and wrap a Normal command around sinseries to see the "big Oh" term disappear.  Oh, and read the answer back into sinseries again.

10.   To get a feel for how well the series approximates the actual function, let's plot them both on the same graph using the Plot command.  We'll also give them different colors so that we can tell the two apart.  The command would be:

                          Plot[{sinseries,Sin[x]},{x,-2Pi,2Pi},
	           PlotStyle->{RGBColor[1,0,0],RGBColor[0,1,0]}]
	           
Go ahead and issue it.

11.   Now it's your turn.  Create a similar graph for the function e^x.  Center it at 0—a Maclaurin series, and use 6 terms.  Plot your graph on the interval -5≤x≤7.  You can use eseries as the name for your series approximation.

12.  To finish, let's consider the problem of finding the antiderivative of a "nasty" function.  Consider sin(x^2) for example.  You were probably told in your calculus course that such an integral is considered impossible symbolically.  In fact go ahead and ask Mathematica to integrate this function directly.

13.   Surprisingly we got an answer.  Well, maybe not so surprisingly if you've studied any optics in your physics courses.  This particular integral actually shows up often enough in this field to have been given a name—the Fresnel Sine Function.  The other "junk" in our output are purely scaling constants.  The Fresnel Sine Function is just a fancy name, however, for a function which cannot be expressed in terms of the elementary functions we are used to.  Mathematica even has the values of this function "memorized" as it does with the logarithmic functions, etc.  But that's beside the point.  Let's consider a "sneaky" approach to symbolically integrating this function.

Generate the series expansion of sin(x^2) centered at 0 out to the 20th order term, and read the result into a variable called fresnelseries.

14.   Now you can apply the basic Integrate command to fresnelseries.

15.  You should have produced a series with a 22nd order "big Oh" term on the end.  This series represents the series expansion of the antiderivative we originally set out to find.  Apparently, then, we can get a series version of the antiderivative even when we can't evaluate the antiderivative directly.

For the sake of comparison, ask for the series expansion of the answer to problem 12.  (Yes, it will take a bit of typing—or you can cut and paste the entire output cell.)  Center it at 0 and go out to the 21st degree term.  You should get the same answer as you got in problem 14.  (Which is what you expected I hope.)