A Brief Introduction to
Mathematica

by Duane Storti

What is Mathematica?

In "The Mathematica Book" by Stephen Wolfram, Mathematica is described as "the world's only fully integrated environment for technical computing." While there are certainly other software providers who would argue about the "only", there is no doubt that Mathematica provides a unified environment that supports many of the kinds of computation that arise in engineering: symbolic manipulation, numerical computation, and graphic visualization, etc. In addition, Mathematica provides capabilities for editing and formatting (this document was prepared using Mathematica), playing sounds, and programming your own custom features.

Getting Started: How do I use it?

To get started using Mathematica, log on and start up the application on your computer. (If there is a Mathematica icon on the desktop, just double-click on it. Otherwise, click on the START button at the lower left hand corner of the screen then drag up to programs until another menu pops up. Drag to the Mathematica folder, and when it opens, click on Mathematica 5 to launch the program.) You may notice in your taskbar at the bottom of your screen that you have actually started 2 programs: the kernel and the front-end. You don't really need to deal with this now, but in case you want to know, the kernel does the actual computations while the front-end supports the notebook interface and facilitates your interaction with Mathematica.

Mathematica provides for several levels of user interaction. New users often view Mathematica as an interactive super-calculator. While batch mode is available, you are more likely to interact with Mathematica through the "Notebook" interface (You are looking at a notebook right now!) in which items are organized into various types of cells (indicated by the blue brackets along the right edge of the notebook).

Typically when you start the program, an empty notebook will pop open for you to work in. If there is no open notebook, or if you need an extra one to work in, select NEW from the FILE menu at the upper left corner of the screen to create a new notebook.

Once you have an open notebook to work in, a  veritable plethora of built-in functions can be accessed by typing the function name and inputs (with appropriate syntax as discussed below) into an input cell followed by <SHIFT-ENTER>. Mathematica then evaluates the function and prints out the result immediately below in an output cell. The results of previous calculations can be saved and accessed later to build up more complicated calculations as illustrated below.

To illustrate a basic numeric calculation, move the mouse over this paragraph (which appears in a text cell identified by the horizontal tick mark near the top of the blue cell bracket) then move down until the cursor changes to a horizontal I-beam. Click the left mouse button to create a cell boundary (indicated by a solid horizontal line), and type "2 + 2" or some other numerical operation. What you type will appear in an input cell (denoted by the diagonal line at the top of the cell bracket, or check the Format-Style submenu). Evaluate the contents of your input cell by pressing <SHIFT-ENTER> (i.e. the SHIFT and ENTER keys pressed simultaneously), and the results of the evaluation will appear in a output cell (denoted by a diagonal line above the horizontal tick) just below the input cell. Try entering and evaluating a couple numerical expressions. What happens when you double click on the outer cell bracket that groups the output and input? How does the bracket change its appearance? What happens if you double-click it again?

The cells  below illustrate symbolic calculation and graphics. Click in the cell and evaluate (using <SHIFT-ENTER>) to see the corresponding output.

Expand[(x + y)^4]

Factor[%]

N[π,  100]

D[x^3,  x]

Plot[Sin[π t],  {t, 0,  4}]

Plot3D[x^2 + y^2, {x, -3, 3}, {y, -3, 3}]

Note that all built-in functions and constants start with an upper case letter or $ (so when you make up names for things you create, just start with lower case and you will never have a name collision with something built-in).

Arguments to functions appear in square brackets following the function. (We'll discuss later that EVERYTHING in Mathematica has an internal representation of this form.)

% is a special symbol which refers to the output of the previous computation.

Note that putting a semi-colon at the end of the line supresses the output. (Try it!)

Saving your work

You can save your work during your session by simply saving your notebook. However, the machines in the ME department computing labs are not designed for saving personal files. Use FTP or Pine e-mail to transfer your files or send them as e-mail attachments.

Getting Help

There are so many built-in functions in Mathematica that it can be mind-boggling. The listing of the built-in functions requires 350 pages and the index by itself is 80 pages long. How can you possibly expect to find the functions you need for your intended purpose? Luckily, there are extensive on-line help facilities (Learning how to use the help facilities is one of the most important things you can learn in your first Mathematica session.) including introductory tours, an alphabetical listing of functions, a function browser organized by topic with some search capabilities.

To learn about the functions that are available, access the Help Browser by clicking on HELP in the HELP menu at the top of the screen (or use the keyboard shortcut <SHIFT-F1>). At the top of the Help Browser you will see a Go To button, with a field for entering a function name to search for, and six radio buttons entitled: "Built-in Functions", "Add-ons", "The Mathematica Book", "Front End", "Getting Started", "Tour", "Demos", and "Master Index". You should spend some time exploring each of these, but for your first session "Getting Started/Your First Mathematica Calculatio" is probably the most appropriate. Before we go on to that, let's just list what the others provide.

Built-in Functions - Gives information on all of the built-in functions including examples of their use and links to related functions.

Add-ons - Gives information about using the additional functions provided in packages which are not included in the basic package.

The Mathematica Book - Amazingly enough, it is just what it says. The entire thing available on-line for your perusal.

Front End - Deals with menu layouts, keyboard shortcuts, etc.

Master Index - An alphabetical listing of built-in functions, so if you are almost sure of a function name you should be able to find it (and its relatives) here.

Getting Started - Just what the name says: the basics of setting up and starting to use Mathematica.

Tour - The tour of Mathematica is a nice collection of sample computations. They are "live" so that you can edit them and press <SHIFT-RETURN> to have your input evaluated.

Take the Tour and see what things are of interest to you. Select some topics to follow up in the help browser to get more information. See how amazing a graphic you can construct. Explore the selection of palettes which give you access to a family of functions by simplying clicking buttons.

While touring Mathematica, be sure to check out some of the items that we know will be of use (since we are beginning with a study of differential equations). Start by looking at the help entries for DSolve, NDSolve and Plot. Test out these functions and follow the links to look at related functions. For example, in class we solved the  1st order initial value problem (dy/dx) =  (y^(1/2)),  y(0)  =  1using DSolve as follows:

soln = DSolve[{y'[x]  == Sqrt[y[x]],  
          y[0]  == 1},  y[x],  x]

Execute this command and then use the trick of editing the output cell to create an input version of the solution. Incorporate this into a Plot command and plot the solution. Experiment with Options to control the range of the plot, add labels to the axes and whatever other embellishments you desire (plot in color or with a dashed line, add a title, ...)

Overcoming Murphy: 
The Mathematica Programming Language

No matter how familiar you get with Mathematica's built-in function, add-on packages, and notebooks or packages you find via the internet or other sources, eventually Murphy's law will prevail: At some point, the function you need will not be there.

Mathematica deals with this in the most effective way possible by including a full-blown, multi-faceted programming environment. If you are accustomed to prodedural programming in a language like C, such procedures are supported. If you are used to manipulating lists in a language like Lisp, list operations are supported. As you will see later, Mathematica is especially useful as a pattern matching language. See the "Mathematica as a Programming Language" part of the tour for some examples.

Some simple programming background.

Before you start programming, there is one thing to know. You can easily write code that unintentionally requests a very difficult or impossible evaluation. If you get no response to your input, use <ALT-.> (Alt and Period depressed together) to abort a calculation. You can also try the interrupt and abort selections in the Kernel menu.

Internal representation: Everything is an expression.

Clear[x, y, z]

x^2

FullForm[%]

Internally, everything is an expression and is represented in the form Head[...].

Assignments: Immediate (for defining parameter values) and delayed (for defining functions)

x

Immediate assignment:

x = 2;

x

x^2

y  =  x ;

y

x = 3

y

Delayed assignment (useful for defining functions)

Clear[y]

y

\(\( (*\ Double\ check\  - \ y\ is\ a\ global\ variable, \ 
      but\ has\ no\ definition\ *) 
\(?y\)\)\)

\(\( (*\ 
      Note\ that\ x\ does\ have\ a\ definition\ since\ it\ was\ assigned\ a\ 
        value\ *) 
\(?x\)\)\)

y :=  x

y

x  =  8;  y

Defining functions and rules

To do some mathematical operation once, it is usually easiest to just type in the desired expression. For example, we typed x^2 to square x. However, to perform the operation repeatedly, there are other useful approaches. We could write a function to do the operation. (Remember that internal operations start with upper case, so if you name your functions starting with lower case, you will avoid "collisions".) Let's write a function to compute squares:

\(\(\(\ \)\( (*\ 
      This\ line\ defines\ how\ the\ function\ operates\ on\ a\ generic\ argument\ *) \)\(
\)\(square[x_]\  := \ x^2\)\)\)

\(\( (*\ Check\ that\ the\ definition\ was\ created\ *) 
	
    \(?square\)\)\)

square[x]   square[I ρ]

You can also operate on expressions using rules that seek pattern matches and replace the matched patterns in specified ways. The line below says subject the expression x to a rule that replaces an expression by its square. 
a_ is the generic argument. "/." is shorthand for Replace and everything after "/." is the rule that replaces a generic argument with its square.

x  /.  a_  ->  a^2

The result is 64, as expected, but what is the current value of x?

x

It has not changed. No assignment took place. Alternatively we could assign a new value to x based on the result of the rule.

\(\( (*\ Assign\ new\ value\ based\ on\ result\ of\ replacement\ rule\ *) 
      
	x\  = \ \((x\  /. \ a_ -> \ a^2)\)\ \)\)

x

Linear Algebra

In the not too distant future, we will be studying linear algebra, and a tool like Mathematica can be very useful there as well. A couple examples are presented below, but you are encouraged to explore other capabilities through the Help facilities. (Explore the Palettes available under the File menu. Palettes such as Basic Input or Basic Typesetting provide tools for formatting vectors, matrices, etc.)

a = (({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}))

MatrixForm[a]

b = (({{10}, {11}, {12}}))

MatrixForm[b]

x  =  LinearSolve[a, b]

a . x