Doing math in Mathematica

advertisement
Center for Teaching, Research and Learning
Research Support Group
American University, Washington, D.C.
Hurst Hall 203
rsg@american.edu
(202) 885-3862
Intro to Mathematica
This tutorial outlines the basic uses of Mathematica to solve problems, similar to what that the students
may encounter in the classroom. The tutorial is designed to continue for approximately 75 minutes. The
material will refer to several Mathematica files (notebooks) that are located in:
J:\CLASSES\ Workshops
Learning Objectives





Understanding Mathematica’s notebooks, cells and the classroom assistant
Doing simple math in Mathematica
Using Mathematica’s basic functions
Working with Mathematica expressions and user written functions
Using Solve and NSolve
Notebooks, cells and the classroom assistant
The basic file type in Mathematica is called a notebook and it has an extension .nb. Students are
expected to do most of their work in notebooks and submit the final file to the TA for evaluation. A
Mathematica notebook is a very versatile and dynamic file type that allows both textual and math
content. Users can design ornate typeset presentations using the rich editing abilities of the notebook
which include titles, subtitles, bullets, sections, multiple font families etc.
Each notebook file is composed of multiple cells. A cell is designed to be a self-contained unit. There are
two basic types of cells – math cells and text cells. Users are encouraged to type all text like titles (e.g.
Homework 10), authors, dates and paragraphs of explanations in text cells. All of the mathematical
content should be in mathematica input cells (math cells).
Each cell is delineated by a horizontal line which is visible if you hover with the mouse over it. At the leftmost end of the line there is a “+” sign where the user can select the type of the cell (Mathematica input
or a plain text cell for example). Some of the rich properties of text cells can be explored in the Format
menu. For example, all the different types of text cells can be found in Format -> Style. In addition, the
Format menu has quite a few features that will make your notebooks look professional (explore the
Stylesheet options).
Another useful feature of Mathematica is the Classroom Assistant and you can find it at Palettes ->
Classroom Assistant. The Classroom Assistant is a graphical user interface to the most popular features
of Mathematica including mathematical operations, navigations within the document, formatting,
typesetting and others. It is a helpful tool for beginners so we recommend that you keep it open in your
first experiments with Mathematica, but one should not rely only on it at the expense of learning the
language itself. Everything that can be done through the Classroom Assistant can be done with
commands in the notebook itself!
Doing math in Mathematica
Mathematical operations are done in Mathematica input cells or math cells. The easiest way to evaluate
an expression is to type it in math input cell and press Shift+Enter at the end. This will result in the
creation of a new output cell immediately after the original input cell with the answer to the problem.
You should keep in mind that simply pressing enter doesn’t evaluate an expression nor does it move the
cursor to another cell. Instead, pressing enter moves the cursor to a new line within the same cell. This
means that several expressions can reside in the same cell and Shift+Enter will evaluate all of them. This
feature is sometimes useful for more complex operations.
We have prepared a few simple examples in the 1_Begin.nb file.
Basic Mathematica functions
Functions are the backbone of Mathematica. Almost everything you type in a math cell is a function.
Although not explicitly, you already worked with functions in the Begin.nb file from the previous section.
For example, you may have used the square root symbol from the Classroom Assistant to calculate the
square root of the integer 4. However, Mathematica automatically translates this symbol to the function
Sqrt[] and gives it an input of 4. Another example is simple subtraction. Mathematica translates the
expression 5-2 into the function Subtract[5,2] and returns the result of that function.
As with any language there are certain syntax rules that must be followed. The input of all functions are
enclosed in square brackets ([]). If a function has multiple inputs (like the Subtract example above) they
must be separated by commas. All of the pre-written (as opposed to user-written) functions will start
with a capital letter. If you would like to view the help file for a given function you should type in a
question mark (?) before the function name (e.g. ?Subtract) and then press Shift+Enter.
Some of the functions are computational in nature (e.g. trigonometric functions) but some control the
way the output is displayed (e.g. N[], TraditionalForm[] etc. )
We demonstrate the use of a few basic algebraic functions in the notebook 2_Algebra.nb.
Definitions, expressions and user-written functions
When doing math you often need to refer over and over to the same number. For example, this could
be some constant that is repeatedly used in your computations. The way this is handled in most
programming languages, including Mathematica is through variable definition and value assignment. For
example you can assign the value 2 to the variable a in the beginning of your notebook and reuse that
variable in the following cells. The assignment is done with the equal sign (=) like this: a=2. Any
subsequent use of the letter a will be immediately substituted with the value that was assigned to it.
The variable name need not be a single letter, it could be one or more words (e.g. gdp).
You were already introduced to some assignment in the Matrices.nb file where we assigned vectors and
matrices to variables which simplifies subsequent operations and saves re-typing of matrices.
In a similar fashion you can define a whole expression with a mix of known and unknown quantities. For
example you can define the following expression:
expr1 = 5*x + 1
and refer to it throughout your notebook. A very useful feature is the replace option with expression.
For example you can replace the value of x above with some quantity and evaluate the expression by
typing:
expr1 /.x->3
which will substitute x with the value 3. This is very helpful if you need to refer to the same expression
over and over again but possibly use different values for the unknowns.
Closely related to expressions are user-written functions. You were already introduced to some of the
basic functions in Mathematica. However, the software also gives you the option to define your own
functions. Function assignment is often done with the := operator where you need to specify the name
of the function and the inputs that it takes. Here is a very quick example of this:
myfunc[x_] := x+3
this defines a user-written function called myfunc and you can call it like you would call any function in
mathematica (e.g. myfunc[3] will return 6 as a result)
Finally, you can clear any prior definitions with the help of the Clear[] function (e.g. Clear[a, expr1,
myfunc])
A richer set of examples can be found in the file 4_Definition.nb
Solve and NSolve
One of the most impressive features of Mathematica is the Solve (and NSolve) command. Mathematica
can do the work for you. Suppose that you want to solve for x in x+5=3. You can simply type in
Mathematica input cell:
Solve[x+5==3]
and you will immediately get the right result.
Even more impressive is the ability of Mathematica to do symbolic computations. For example:
Solve[x+a==b, x]
Will give you as a result for b-a. You read the above command as solve the expression x+a equals b in
terms of x. On a more technical level Solve is a function which takes as its first input the expression that
needs to be evaluated and as its second input the element for which it is solving (for a more in-depth
look at the Solve function see the help by typing ?Solve).
Notice the use of the double equals (==)! This is required because you are not assigning a value to the
left hand side as you would in variable definition. Instead you are checking equality and this is why you
need a different operator. Solve is an extremely powerful command that can be used for solving
difficult expressions, systems of equations and can save a great deal of time. However, it won’t always
do the work for you. There might be cases where you know that analytical solutions exist but
Mathematica won’t be able to return the result. There you will have to figure out clever ways to get to
the result. NSolve stands for numeric Solve and it simply solves the expression by using numerical
methods.
We have prepared several examples with Solve and NSolve in the file6_Solve.nb.
Download