Using Mathematica (A Beginner's Guide)



Mathematica is a powerful mathmatics calculation tool. Mathematica actually consists of two different modules: the Mathematica Front End and the Mathematica Kernel. The Mathematica Front end is what runs when you double-click on the Mathematica icon. When you perform a calcuation in Mathematica, the front end passes the calculation to the Mathematica Kernel which performs the calculation and returns the result to the front end.


Quick Mathematica Demo

You can use Mathematica to plot functions. For example, if we wanted to compare the behavior of the functions:

f(x) = x
f(x) = x^2
f(x) = x^3

To plot these functions, type Plot[x, {x,0,10}] and press the ENTER key (the key on the number pad on the lower right portion of the keyboard - not the RETURN key). This will plot f(x) = x. Next, click in a new area and type Plot[x^2, {x,0,10}]. This will plot f(x) = x^2. Finally, type Plot[x^3, {x,0,10}]. This will plot f(x) = x^3. Notice that the numbers in the braces represent the domain of the plot.

We assign each of these plots to a name for further display. For example, if we typed: plot1 = Plot[x,{x,0,10}], the plot of f(x) = x between x=0 and x=10 would be assigned to the variable plot1. So if we typed plot1 at a later point in our notebook, it would display the graph of f(x) = x. If we wanted to plot all three of the functions f(x) = x, f(x) = x^2, and f(x) = x^3 on the same plots, we could set plot1 = Plot[x,{x,0,10}], plot2 = Plot[x,{x,0,10}], plot3 = Plot[x,{x,0,10}] and then tell Mathematica to display them all together. The way to do this is to use the Show[] command by typing: Show[plot1,plot2,plot3]. This will display all of these plots on the same set of axes.


Mathematica Notebooks

Mathematica for the Mactintosh uses "Notebooks" to keep track of your calculations. The Mathematica notebooks can hold calculations, plots, programs, and other things that you can do in Mathematica. Each notebook consists of a series of "Cells" which contains a set of related calculations. For example, you can perform calculations in a cell and then plot those calculations in a related cell.

Some things that you can do in cells

1) You can type formatted text within a cell. You can treat the cell much like a word processor and type titles, information, etc. and change the font size, color, etc. The example given in class of formatted text cells was the class list that was used to determine the order of presentations in this class.

Evaulating Cells

To evaluate cells (perform the computation or operation defined in the cell), press the ENTER key on the keyboard. This key is located on the lower right corner of the number keypad. This is not the same key as the RETURN key.


Lists in Mathematica


Mathematica uses lists to store information. A list is a set of numbers, words, or virtually any type of data that is contained within braces with each member of the list separated by columns. For example, if you wanted to put the numbers 1 through 10 in a list, this would be {1,2,3,4,5,6,7,8,9,10} in list format. You can assign a list to a variable (just like the example with the plots above). Let's call the above list joe. To set joe equal to the above list of numbers, we can type joe = {1,2,3,4,5,6,7,8,9,10}.

In class, we used Mathematica to randomize our class presentation schedule. Here is a link to the notebook - download it and study it to see how it works.

Determining the Length of a List

We can determine the number of people in a list by using the Length function in Mathematica. For example, if we typed Length[joe], Mathematica would return a 10, because there are 10 numbers in the above list.


Supressing Output

After we evaluate a function in Mathematica, it usually will spit the output out directly. If you do not want to have Mathematica give you the output of every cell, you can tell it not to do this by putting a semicolon after each evaluated function for which you do not want to see the output.


The Table[] function in Mathematica

The table function takes a list, performs a specified function on the list, and outputs a list that represents the results of the calculation. For example, if we wanted to square all of the elements of the list joe that we created above, we could use the Table[joe^2,{joe,0,Length[joe]}]. This will create a list with the values of Joe^2 in it. The bracketed information is the range that you wish the command to operate on in the list. For example, if we only wanted to square the values of joe between 2 and 7, we could use the command Table[joe, {joe,2,7}] to do this.


Some of the things that Mathematica can do

Mathematica can perform both symbolic and numerical calculations. A numerical calculation could be something like 1+1 or any complex operation that you can usually do on a calculator. In addition, Mathematica can manipulate algebra and solve sets of algebraic equations. For example, it will solve a set of equations (e.g. 10 equations, 10 unknowns) automaticallly and return the algebraic solutions for these equations. In addition, Mathematica can integrate equations, differentiate equations, solve differential equations, etc. Basically, anything that you learned how to do in Calculus, Differential Equations, and Linear Algebra, Mathematica can do it in a matter of nanoseconds and plot up all of the results instantaneously. It is an extremely empowering piece of software because it lets you skirt around the mechanics of the math (which should be relagated to mathematicians) and let you investigate problems with some pretty serious equations.


Graphics in Mathematica

Here is a graphic created in Mathematica:

Check out the graphics demo called
"GraphicsDemo.nb" to see some of the cool things that you can do in Mathematica.

In class, we played with the Table[] function in the notebook in order to animate the graphics and change how the animation was viewed.


Clearing variables in Mathematica


If you have inadvertently assigned a value to a variable that you do not wish to have a value, you can clear the variable's value by "zeroing" the value out. To do this, type the name of the variable followed by= .; For example, if we wanted to clear the value of joe, we could type joe = .;


The Pallettes in Mathematica

There are some tool pallettes in Mathematica that allow you to symbolically integrate, differentiate, sum, raise numbers to a power, etc. To use these tools, click on the desired function (for example, the integration symbol), type your expression, and then evaluate the cell. These tools shortcut many of the intricate Mathematica expressions of past versions of the program.


Using variables to perform calculations

We can assign values to variables and then use the variables to perform calculations. For example, if we have a box, we can represent the dimensions of the box as variables and then use the variables to calculate the volume of the box. If we measured the length, width, and height of the volume, we could type length = 20; width = 10; height = 5; . If we wanted to calculate the volume of the box, we could type volume = length width height; This will set the variable volume equal to the length*width*height (the volume of the box). This is a very useful feature for repeated calculations.


Other tidbits about Mathematica

Make sure to review the intro.nb file in order to get a grasp on the general syntax of many Mathematica commands. It is probably a good idea to read and play around with this notebook before attempting the assignment.


Extensibility of Mathematica

Many people have written all sorts of graphics and computational functions for Mathematica. These functions are not standard with Mathematica - they do not automatically load up when you run the Front End and Kernal. In order to use some of these functions, you need to tell Mathematica that you wish to use the functions and exactly where the functions are located. To do this, use the Needs["NAME OF PACKAGE"] command, where NAME OF PACKAGE is the name of the file containing the functions you wish to use with Mathematica is located. These are functions that you generally do not use very often.



Pages maintained by
Prof. Ramón Arrowsmith

Pages last modified on Wed Nov 26 1997.