Using Mathematica for Calculus

(If you are reading this document interactively within Mathematica, then we need to do some housekeeping to make it work right. We need to make sure that all the variables we will use start out with no prior values. Put the cursor in the word "Remove" and press shift-enter. Please excuse the interruption.)

Remove[a, b, c, d, e, f, g, h, x, y]

Introduction

The following section is just for reading. It will show you what Mathematica can do. 
Mathematica  is a software system for doing mathematical calculations. It is the Mark McGwire of calculators, much more powerful than anything you can carry in your pocket. Mathematica will perform calculations:

Sin[3.4]

simplify algebraic expressions:

Apart[((3  x - 4)/((x^3) + 2  (x^2) - x - 2))]

graph functions:

Plot[Sin[(x^2)], {x, -1, 3}]

integrate and differentiate:

∫(x^2) sin(2 x) cos(
            3 x) \[DifferentialD]x

and solve equations.

FindRoot[Cos[x] == 2*x, {x, 1}]

Mathematica will do many other mathematical operations too, most of which you have not yet learned about. As the famous entertainer, Dr. Science, says: "Mathematica knows more than you do." But that is not really true. Although Mathematica knows a lot of functions, it cannot do word problems. It cannot prove theorems or build models. It cannot create equations; it can only solve them. The hard parts of mathematics still have to be done by people, but Mathematica will help you by taking over some of the routine.

The document you are looking at, either on paper or on a screen, is a Mathematica notebook, a collection of text paragraphs, mathematical calculations, and graphs. The text paragraphs can include mathematical expressions, like this:

(d/dt) sin t = cos t

Whenever you use Mathematica, you will create a Mathematica notebook. Notebooks can be saved and reloaded, printed, or passed around by email. Once you leave Mathematica, your notebook is just a file like a spreadsheet or word-processing file. You could do an entire homework assignment in a Mathematica notebook, print it, and turn it in. 
The purpose of this lesson is to teach you to use Mathematica as a calculator. You are not going to learn how to create text paragraphs. You are just going to learn to do calculations.

How to Use This Worksheet

Read the text while sitting before a computer running Mathematica, and do all of the "Try it Yourself" sections. The expected time to complete this worksheet is 1-2 hours. Your teacher may ask you to work on selected subsections.

Getting Started

Try It Yourself

If you are reading this on a computer, place the cursor in the cell labelled (* empty cell *) just below. Put the cursor right after the right parenthesis.  Otherwise start your computer and get Mathematica running. Type

a = 2+2

and press ⇧↵ (Shift-Enter). Your formula should appear as input, and the result should appear as output. Make sure this works before proceeding. You have to press ⇧↵ after every entry to execute it.

Every bit of text and formula in Mathematica is contained in a cell.  The cells are outlined on the right. If you press the up- and down-arrow keys, you will see the cursor change from a vertical line to a horizontal line, and then back to a vertical line as you pass from one cell to another.  If you start typing while the cursor is a long horizontal line, you will open a new cell. 
Before going to the next section, let's put your name on the worksheet. Move the cursor to a location between two cells, and type your name as a comment between the symbols (* ... *) , like this:
 (* Your Name *)

If there are more than one of your working on this computer, everyone should put their names on the worksheet

Calculating with Numbers

Arithmetic

Your first goal should be to make Mathematica do what your calculator can do. You can enter numbers into Mathematica with or without decimal points. Numbers entered without decimal points are considered "exact", numbers with decimal points are approximate. Calculations with exact numbers have exact results; calculations with decimal numbers have decimal results. Addition and subtraction are denoted with the usual symbols  +  and  - , and division is  /  .  Here are some examples:

b = 3/4 + 7/9

c = 3.0/4.0 + 7.0/9.0

Multiplication is denoted by a space between the factors:

d  =  3 4 5

Powers are expressed with the caret  ^  :

e = 3^100

This last calculation shows that calculations with exact numbers have exact results. 
Two points.
1. It is a very good idea to assign each calculation to a variable. Then you can use the variable to recall the result of a calculation.
2. If a calculation has an exact results, you can get the decimal equivalent with the function N[..] . Note the square brackets. For example:

f = N[e]

You can use parentheses to organize your calculations. For example, to calculate ((2 + 3)/7), you would enter:

g = (2 + 3)/7

Try It Yourself

Calculate the following results exactly:
1. a  =  (2/3) - (1/4)
2. b  =  a((3/4) - (2/7))
3. c  =  (b^3)
4. Find the decimal approximation of c.

Functions

Mathematica knows all the standard functions. Here they are.

a = Sin[3.4]        
b = Cos[3.4]
c = Tan[3.4]
d = Sqrt[3.4]         
e  =  Log[3.4]       (* the natural log ln is spelled Log *)
f  = E ^ 3.4

Two things to notice:
1. Every function name starts with a capital letter. Mathematica is VERY fussy about that.
2. The arguments are surrounded by SQUARE BRACKETS, not parentheses. Parentheses are used to prioritize mathematical operations.
The value π can be entered as an exact value into Mathematica with the symbol Pi. For example:

a  =  Sin[Pi/3]

If you use exact numbers with functions, you get exact output, which sometimes cannot be simplified. Consider these examples:

b  =  Cos[3]
c  =  Cos[3.0]

Since Cos[3] cannot be simplified; it is retained as written. But Cos[3.0] returns an approximate value for the cosine. Neither is more correct than the other. You have to use whichever is appropriate for your problem.

Try It Yourself

Verify that the following formulas all evaluate to 0:
1. 2  sin (π/6) - 1
2. ln (e^2)  - 2
3. (Sqrt[225]) - 15
4. (e^(ln 3)) - 3

Plotting Graphs

To graph an expression in Mathematica, you have to specify the expression, the graphing variable, and the domain for the variable. The variable and its domain are all specified in a list, a sequence of expressions contained between curly braces {...}. For example, to plot x (e^(-x)) on the domain -1 ≤ x ≤ 2, enter the formula:

Plot[x E^(-x),  {x,  -1,  2}]

Notice the space between x and E^(-x), indicating that these factors are to be multiplied. Without the space, you would have raised the unknown symbol xE to the power -x .

Try It Yourself

1. Plot the function (x^x) for 0 ≤ x ≤ 2 and estimate the minimum point. 
2. In the next cell write a comment, text placed between the symbols (*...*), in which you record the x- and y-coordinates of the minimum. Your comment should look like

(* I think the minimum is at x = ???? and y = ???? *)

You replace ???? with the correct values.

Calculus--Integrals and Derivatives

Derivatives

To take the derivative of an expression, use the D[..] operator. You have to specify the variable for differentiation, because you may differentiate a formula that has more than one variable.

D[x E^(a x), x]

Oops. You see in the above formula one of the frequently encountered difficulties using Mathematica. We had already defined a to have a value, and that value was used in the last calculation. But we want to start with an undefined variable a in this example, so we have to erase the previous value. That is done with the command Remove[..].

Remove[a]

D[x E^(a x), x]

You can take higher derivatives of an expression by adding an order of differentiation to the expression, combining the variable and the order in a list. Parameters for commands are often combined into lists.

D[x E^(a x), {x, 3}]

Integrals

One of the original purposes of Mathematica was to perform integration faster and better than most people. The purpose has been achieved. Integrals, both indefinite and definite, are calculated with the Integrate[..] command. To calculate an anti-derivative (without arbitrary constant), just specify the expression to be integrated and the variable. First we find ∫x (e^(-x)) dx:

Integrate[x E^(-x), x]

You can guess how to compute a definite integral. You add the limits of integration to the variable in a list. Here's how you would calculate (∫_0^2) 
          x (e^(-x)) dx:

Integrate[x E^(-x),  {x, 0, 2}]

Sometimes even Mathematica cannot do an integral exactly. Let's try □ (∫_0^1)(( 1 - 
                  (x^4))^(1/□)) dx

Integrate[Sqrt[1 - x^4], {x, 0, 1}]

The result is not very helpful. You can get a numerical result for any integral with the command NIntegrate[..]

NIntegrate[Sqrt[1 - x^4], {x, 0, 1}]

Try It Yourself

1. Find the derivative of the expression (x^x) . Give the derivative a name by entering something like

y = D[...] .

2. Plot the derivative on the domain 0 ≤ x ≤ 2. Use the command

Plot[y, ...]

Estimate the point where the graph crosses the x-axis enter this value into a comment. This should be the same value as you got for the minimum of (x^x) in the exercise above, because a local minimum of a function usually occurs where the derivative is 0.

3. Find the area below the curve (x^x) for 0 ≤ x ≤ 1. Can you do this exactly, or do you have to approximate?

Solving Equations

Hang on—this is the last section. To find the approximate solution to an equation f(x)  =  g(x) , use the command FindRoot[..] . You have to specify the variable to solve for and an estimate of the solution. To solve the equation cos x  =  2  x with an estimate of x  =  1.

FindRoot[Cos[x] == 2 x,  {x, 1}]

Notice two points:
1. You must use the double-equals sign == between the two sides of the equation.
2. The answer comes out in weird form. To assign the solution to the variable x , copy-and-paste the output to another input cell, and replace the arrow with an ordinary equals sign. Here's what you get after copy-and-paste:

x -> 0.450183621502111197`

After replacing the arrow with "=" and executing you get:

x  =  0.450183621502111197`

Here is a real problem and its solution:
Find the minimum of the function  x ln x .
Begin by plotting the function to get some idea of the solution:

Plot[x Log[x],  {x, 0, 3}]

There appears to be a minimum near x  =  0.5 . To find the minimum, we will find where the derivative of  x ln x  is 0. We have to begin by erasing a previous value from the variable x .

Remove[x] 
y  =  D[x Log[x], x]

FindRoot[y,  {x, 0.5}]

The minimum occurs at  x  =  0.367879 .  The y-value at the minimum is:

\(x\  = \ 0.367879441171048249; \  (*\ 
      semicolon\ suppresses\ output\ of\ already\ known\ x - value\ *) \ 

    y\  = x\ Log[x]\)

Try It Yourself

Let y  =  (x^4) - (x^3) - (x^2) + x + 1 . 
1. Plot y and show that  y  =  0  has no real solutions.
2. Find all maxima and minima on the curve