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. 1. Syntax For simple operations, the form of input is pretty straightforward. For example, enter the following expression 20+17 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: 5^2 Pi Cos[Pi/3] Exp[3] Log[Exp[1]] 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: 21/2 N[21/2] Pi N[Pi] Exp[3] N[Exp[3]] 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: D[x^2+2*x+1] 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: D[x^2+2*x+1,x] 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): D[Exp[2*x],x] D[Log[x],x] D[Cos[x],x] D[Tan[x],x] 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: Integrate[x+Sin[x],x] 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: Integrate[x+Sin[x],{x,0,Pi}] Note that the interval was indicated with curly braces. If we want a decimal approximation, we need to use the “N” operation: N[Integrate[x+Sin[x],{x,0,Pi}]] 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: Plot[x+Sin[x],{x,0,Pi}] You should see a graph of the function x+sin(x) on the interval [0,π]. Try graphing some other functions: Plot[Sin[x]/x,{x,0,50}] Plot[40*(1-Exp[-t]),{t,0,10}] Plot[Sin[Exp[1]*x]+Cos[x],{x,0,10}] In the case of more difficult functions, we can still approximate the area under the curve. Try these: Plot[Exp[-x^2],{x,-5,5}] Integrate[Exp[-x^2],{x,-1,1}] N[Integrate[Exp[-x^2],{x,-1,1}]] 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: f[x_]:=x^2+4*x+4 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: Integrate[f[x],{x,0,4}] 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: Sum[f[i/2]*1/2,{i,1,8}] For left-hand endpoints: Sum[f[(i-1)/2]*1/2,{i,1,8}] Or for midpoints (here, the midpoint works out as i/2-1/4): Sum[f[i/2-1/4]*1/2,{i,1,8}] (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: AvgValue[g_,a_,b_]:=1/(b-a)*Integrate[g,{x,a,b}] This operation will compute the average value of g over the interval [a,b]. For example: AvgValue[Sin[x],0,2*Pi] AvgValue[-x^2+1,-1,1] 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).