|Calculus with Mathematica
Mathematica is a general computing environment, with both algorithmic and visualization capabilities. It can perform most of the operations of differential and integral calculus. In this sheet, we’ll work our way towards some of the more sophisticated operations and computations that we’ve been exploring in this class.
For simple operations, the form of input is pretty straightforward. For example, enter the following expression
and then press Shift + Return.
Often, we will want Mathematica to do more sophisticated computations. All of the basic functions from calculus are built-in to the computing platform. Try the following expressions:
again following each by Shift + Return. Notice that Mathematica returns the exact value of the input, not a decimal approximation. We can force Mathematica to do an approximation (to 6 decimal digits) with the “N” operation. Try these:
The “N” function is an example of a built-in operation in Mathematica. Let’s look at some more advanced operations.
2. Built-in Operations
In Mathematica, the “D” operation will compute the derivative of a function. Try the following computation:
What happened here? In this case, we didn’t tell Mathematica the variable with which to perform the operation. (Obvious to us, but Mathematica needs to be told.) Let’s try again:
Okay, this time it worked. This is a good example of the form required when we input expressions into Mathematica. The operation D is applied to an expression x^2+2*x+1, and relevant parameters are listed thereafter. Try some of the following derivatives (and some of your own):
Notice that for all of these functions, we capitalize the initial letter and use square brackets instead of curved brackets.
Mathematica can also perform integration. For example, enter the following:
Here, the output is an antiderivative (notice that there’s no “ + C ” here). If we specify only the variable of integration, Mathematica will assume we wish to do an indefinite integral, not a definite integral. Here’s how we can compute a definite integral:
Note that the interval was indicated with curly braces. If we want a decimal approximation, we need to use the “N” operation:
All of this symbolic manipulation can get a bit tiring; fortunately, there’s a way to bring graphics into the process. One of the most useful visualization tools in Mathematica is the “Plot” operation. As far as syntax goes, it’s very similar to the “Integrate” operation. Let’s try this on the previous function:
You should see a graph of the function x+sin(x) on the interval [0,π]. Try graphing some other functions:
In the case of more difficult functions, we can still approximate the area under the curve. Try these:
3. Defining New Operations
Sometimes, we want to use a function that isn’t built into Mathematica. We can define such a function and then use it for further computations. For example, enter the following:
You won’t see any output; Mathematica has stored this definition. (The “:=” indicates that we’re defining f to be the function on the right. Note also that on the left side, we need “x_” and not “x”.) We may call up the function f at any point hereafter:
We can also compute Riemann sums for this function. Suppose we divide the interval [0,4] into 8 pieces (so that ∆x = 1/2 and xi = i/2). Using the right-hand endpoints, we can compute the Riemann sum in the following way:
For left-hand endpoints:
Or for midpoints (here, the midpoint works out as i/2-1/4):
(Of course, all of these presume that we’ve already defined a function f.) We can also define an operation for Mathematica that allows us to compute the average value of a function:
This operation will compute the average value of g over the interval [a,b]. For example:
4. Further Things of Interest
There’s no need to stop here! Try manipulating some of your own functions; compute derivatives; graph functions; integrate. To learn how to do other things, go to the Mathematica Documentation Center (under the Help menu).