Mathematica 4 Calc
Mathematica 4 Calc
(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.)
In[1]:=
Remove@a, b, c, d, e, f, g, h, x, yD
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:
In[122]:=
Out[122]=
[email protected]
0.255541
Out[123]=
E
+ 2 x2 x 2
10
1
7
3 H2 + xL
6 H1 + xL
2 H1 + xL
ApartA
3x4
x3
graph functions:
In[124]:=
-1
1
-0.5
-1
Out[124]=
Graphics
Mathematica4Calc.nb
In[125]:=
Out[125]=
2
x sinH2 xL cosH3 xL x
1
H250 Cos@xD + 125 x2 Cos@xD +
250
2 Cos@5 xD 25 x2 Cos@5 xD 250 x Sin@xD + 10 x Sin@5 xDL
8x 0.450184<
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
sin t = cos t
dt
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.
Mathematica4Calc.nb
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.
H empty cell L
Every bit of text and formula in Mathematica is contained in a cell. The cells are outlined on the right. If you press the upand 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
Mathematica4Calc.nb
In[128]:=
Out[128]=
In[129]:=
Out[129]=
b = 34 + 79
55
36
d = 345
H 345 L
60
e = 3 ^ 100
515377520732011331036461129765621272702107522001
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:
In[132]:=
Out[132]=
f = N@eD
5.15378 1047
2+3
You can use parentheses to organize your calculations. For example, to calculate
, you would enter:
7
In[133]:=
Out[133]=
g = H2 + 3L 7
5
7
Try It Yourself
Calculate the following results exactly:
1. a = 23 - 14
2. b = aH 34 - 27 L
3. c = b3
4. Find the decimal approximation of c.
Functions
Mathematica knows all the standard functions. Here they are.
Mathematica4Calc.nb
In[134]:=
a = [email protected]
b = [email protected]
c = [email protected]
d = [email protected]
H square root L
e = [email protected]
H the natural log ln is spelled Log L
f = E ^ 3.4
H E stands for e=2.71828... L
Out[134]=
0.255541
Out[135]=
0.966798
Out[136]=
0.264317
Out[137]=
1.84391
Out[138]=
1.22378
Out[139]=
29.9641
Out[140]=
a = Sin@Pi 3D
!!!
3
2
If you use exact numbers with functions, you get exact output, which sometimes cannot be simplified. Consider these
examples:
In[141]:=
b = Cos@3D
c = [email protected]
Out[141]=
Cos@3D
Out[142]=
0.989992
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 p6 - 1
2. ln e2 - 2
!!!!!!!!
3. 225 - 15
4. eln 3 - 3
Mathematica4Calc.nb
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:
In[143]:=
-0.5
-0.1
0.5
1.5
-0.2
Out[143]=
Graphics
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 xx 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
Mathematica4Calc.nb
D@x E ^ Ha xL, xD
E
!!!!
3 x
2
1 !!! !!!!32 x
x
3 E
2
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[..].
In[145]:=
Remove@aD
In[146]:=
D@x E ^ Ha xL, xD
Out[146]=
Ea x + a Ea 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.
In[147]:=
Out[147]=
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 x:
In[148]:=
Out[148]=
Integrate@x E ^ HxL, xD
Ex H1 xL
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
2
would calculate 0 x e-x x:
In[149]:=
Out[149]=
3
E2
Mathematica4Calc.nb
1 !!!!!!!!
!!!!!
1 - x4
2 EllipticK@1D
3
Out[7]=
The result is not very helpful. You can get a numerical result for any integral with the command NIntegrate[..]
In[8]:=
Out[8]=
0.874019
Try It Yourself
1. Find the derivative of the expression xx . 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 xx 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 xx for 0 x 1. Can you do this exactly, or do you have to approximate?
Solving Equations
Hang onthis is the last section. To find the approximate solution to an equation f HxL = gHxL , 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.
In[150]:=
Out[150]=
Mathematica4Calc.nb
In[151]:=
Out[151]=
x = 0.450183621502111197`
0.450184
Out[152]=
1.5
2.5
Graphics
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 .
In[153]:=
Out[154]=
In[155]:=
Out[155]=
Remove@xD
y = D@x Log@xD, xD
1 + Log@xD
FindRoot@y, 8x, 0.5<D
8x 0.367879<
Out[156]=
x = 0.367879441171048249;
H semicolon suppresses output of already known xvalue L
y = x Log@xD
0.36787944117144232
Try It Yourself
Let y = x4 - x3 - x2 + x + 1 .
1. Plot y and show that y = 0 has no real solutions.
2. Find all maxima and minima on the curve