# Ask SIPB

Today, we’ll discuss two popular mathematics software packages, Matlab and Mathematica. In addition, we’ll take a look at gnuplot, a tool designed to produce high-quality data plots.

### How do I get started with Matlab?

Matlab is a popular language for working numerically with lists and matrices. You can bring up the full interface with:

athena% add matlab

athena% matlab -desktop

(Without the -desktop flag, Matlab will start its text-mode interface.)

“Matlab” is short for “matrix laboratory.” You can enter a matrix with square brackets, with spaces between numbers on the same row and semicolons to break rows:

>> a = [1 2; 3 4]

a =

1 2

3 4

You can also use an iteration syntax for long lists. For example, [1:1000] is the first thousand positive integers, and [0:.01:1 2:.01:3] represents all hundredths from 0 to 1 and from 2 to 3, inclusive.

Standard matrix operations such as multiplication (*), transpose, etc. are supported. You can call a function with parentheses:

>> transpose([1; 2; 3])

ans =

1 2 3

Since Matlab will try matrix operations first, if you want to do a “scalar operation” on each element, you’ll need to add a period before the operator. Using the answer from the previous problem:

>> ans * [4 5 6]

??? Error using ==> mtimes

Inner matrix dimensions must agree.

>> ans .* [4 5 6]

ans =

4 10 18

To plot a function via Matlab, first create the data points, then use the plot command:

>> x=[0:.1:pi]; y=sin(a);

>> plot(x, y)

You can find more information about options to the plot command, and in fact about most Matlab commands, by typing help followed by the name of the command.

A good, detailed reference about using Matlab is SIPB’s document “Inessential Matlab,” available in hard copy from our office. An online version is also linked from the Web copy of this article at *http://www.mit.edu/~asksipb/*.

### What’s a good tool for doing algebra and other symbolic math?

Mathematica is an interactive computer algebra system as well as a mathematical programming language. You can launch the standard notebook interface by typing

athena% add math

athena% mathematica

You’ll be able to start typing expressions in standard mathematical notation. To evaluate an expression, hit Shift-Enter.

In[1] := 16! ^ (1/256)

Out[1] = 215/256 33/128 53/256 71/128 1431/256

Standard Mathematica functions are capitalized and use square brackets for their arguments, for example:

In[2] := Simplify[Integrate[ 2 Sin[Log[x]], x]]

Out[2] = x (-Cos[Log[x]] + Sin[Log[x]])

In[3] := Integrate[Sin[x]/x, {x, 0, Infinity}]

Out[3] = �/2

Mathematica is a powerful yet lightweight programming language as well; the classic example of the factorial can be re-implemented as:

fact[x_] := If[x==0, 1, x fact[x-1]]

In a more rule-based style, you can write

fact[x_] := x fact[x-1]

fact[0] := 1

(The underscore indicates a variable argument, rather than a literal “x.”) You can even use higher-order functions, just like in Scheme:

In[4] := Map[(#^2+1) &, {1, 2, 3, 10}]

Out[4] = {2, 5, 10, 101}

Mathematica’s online help, available from the Help menu, is extremely detailed and has several tutorials as well as a complete function reference. You can also type a question mark followed by a function name for a quick overview of the function’s syntax:

In[5]:= ?Sin

Sin[z] gives the sine of z.

### How can I plot my data?

Although advanced math packages such as Mathematica and Maple incorporate some graphics capabilities, their focus is on data analysis and not presentation To produce more professional figures, it is best to use an external program such as the gnuplot utility.

Gnuplot runs in a terminal, but it opens a separate window to display graphs. To start a gnuplot> prompt, add the gnuplot locker (enter add gnuplot at an Athena prompt), then type gnuplot. Once running, gnuplot has an extensive on-line help system, accessible by typing help at the prompt.

Making plots on-screen is easy; for example, try:

gnuplot> plot x**2*sin(x)

It’s also easy to plot data files, such as the output from this Mathematica command: Export[“trig.dat”, Table[{x,Sin[x],Cos[x],Tan[x]}, {x,-3,3,0.1}]]. Assuming that trig.dat is in the directory where you started gnuplot, you can plot the tangent function with plot “trig.dat” using 1:4.

You can also get gnuplot to write plots to a file. Try:

gnuplot> set terminal jpeg

gnuplot> set output “tangent.jpg”

gnuplot> plot “trig.dat” using 1:4 smooth unique title “tan(x)”

gnuplot> set terminal x11

Gnuplot can produce an extensive variety of plotting styles — try using it for your next scientific paper!

To ask us a question, send e-mail to *sipb@mit.edu*. We’ll try to answer you quickly, and we can address your question in our next column. You can also stop by our office in W20-557 or call us at x3-7788 if you need help. Copies of each column and pointers to additional information are posted on our Web site:* http://www.mit.edu/~asksipb/*