University of Southern California

ITS Information Technology Services

A division of the Office of the Chief Information Officer

Frequently Asked Questions About Mathematica

We would like to answer some of the most frequently asked questions:

General Questions:


How do I start the X-windows version of Mathematica?

- For the X front end to mathematica, type:

mathematica

instead of:

math

Of course you have to type "source /usr/usc/math/default/setup.csh" first.

Type "man mathematica" for more info on the X windows interface version.

How can I change the background color In Mathematica?

- You can change the Background color in your plots with the "Background" option, e.g:

Plot[Sin[x], {x,0,2Pi}, Background->GrayLevel[0.9]]

Mathematica also picks up on the global defaults like the default color that is set by your X server. It will use this color definition by default. The default color is usually defined in your .Xresources file.

How can I run Mathematica in Batch Mode?

- You can use Mathematica in batch mode to perform large calculations with multiple lines of input. Following is the procedure:

where "infile" is the name of the Mathematica input file you have created and "outfile" is the name of file where Mathematica will write the output.

How can I compile Functions In Mathematica?

- You can create compiled functions in Mathematica using Compile. Compile[{x1,x2, ...}expr] takes an expression expr and returns a 'compiled function' which evaluates this expression when given arguments x1,x2,... . Information command (?) on Compile gives :

The following defines a function, f, which evaluates xSin[x]:

The following creates a compiled function, fc, which evaluates xSin[x]:

The following demonstrates the use of f and fc:

You can save this compiled function by using Save["filename", f,g,...] :

    In[5]:= Save["ftmp", fc]

The function definition can later be recalled by reading the file.

    In[7]:= << ftmp
    Out[7]= CompiledFunction[{x}, x Sin[x], -CompiledCode-]
    In[8]:= !!ftmp
    < Out[8]= fc = CompiledFunction[{_Real}, {0, 0, 3, 0}, {{1, 17}, {4, 1, 0}, {49, 0, 1}, {36, 0, 1, 2}, {8, 2}}, Function[{x}, x*Sin[x]]]

How do I create multiple plots on the same postscript file?

- To create multiple plots on one display in mathematica, you can use the "Show" command.

Here's an example:

Create 2 plots:

    ln[1]= y1=Plot[Sin[x],{x,0,2Pi}]
    ln[2]= y2=Plot[Cos[x],{x,0,2Pi}]

Display the plots side by side by using the "Show" command as follows:

    ln[3]=Show[GraphicsArray[{y1,y2}]]

Now use the "Display" command to output the file to a postscript file:

    Display["test.ps", Out[3]]

where Out[3] refers to the figure with the 2 plots.

How can I print from Mathematica?

- If you are using the graphical interface, simply select the graphic and choose Print Selection from the File menu. From the print dialog box, you can then select to print to a printer or print to a postscript file.

If you are running Mathematica in text mode, you can save your plot in postscript format by using "Display" command to do so. This will save the plot in a non-standard format which is not directly usable on our system. To get the correct postscript format you will have to use the command "psfix", e.g:

    In[40]:= Display["test.ps", %]

From inside math this will save the last plot in the file "test.ps". To convert this file into a standard postscript file, you need to exit Mathematica and run the following command at the Unix prompt:

    /usr/usc/math/default/SystemFiles/Graphics/SystemResources/psfix test.ps > tested.ps

This will create a standard PostScript file (in this case "tested.ps" which you can then print with:

    lpr -Pps_sal125 tested.ps

Why do I fix Mathematica Postscript code with 'psfix'?

- The PostScript produced by "Display" does not include initialization or termination commands, nor does it include text font definitions. Hence the need for 'psfix'.

How do you save a variable(s) from Mathematica into a file and reload it later when needed, like MATLAB?

- Since mathematica is more algebric expression oriented, righting and reading lists of data is not that straight forward. That doesn't mean that it's impossible.

Reading lists of data in to mathematica is easier than writing it out.

  1. Reading lists of data in.
      The main commands for reading data are "ReadList", and "Read". If one needs to read a set of data to a variable from a file the best command to use is "ReadList"
        l = ReadList["-name of the file-", Number]
      Here the word Number is a type defined in mathematica. If one needs to manipulate the file more than once it's best to open the file for reading initially.
        stmp = OpenRead["-file name-"]
      then the ReadList command would be
        l = ReadList[stmp, Number]
      The problem occurs when there is more than one set of data in the file. For example, if the tile "temp" has the following data
        4 5 8
        0 3 5
        2 5 0
    It's best not to use the ReadList command, if data in each column should be read into a separate variable. Here one may be forced to use the Read command.(Will need to create a loop).
  2. Writing data to a file.
      The command "OpenWrite" or "AppendWrite" could be used to open the file for reading.
        stmp = OpenWrite["temp"]
      If one needs to write out data it's best to avoid the "Write" command.
        v = {1, 2, 3} Write[stmp, v]
      here if you look at the file temp, you find that it would contain the text
        {1, 2, 3}
      instead of
        1 2 3
      To avoid this first write the data in to a string using the command "ToString"
        kkk = ToString[v]
      Then remove ",", "{", and "}" using the "StringReplace" command.
        StringReplace[kkk, { "," -> " ", "{" -> " ", "}" -> " "}]
      Now write to the file using "WriteString" command. Always remember to include a new line character manually in the command.(WriteString, doesn't automatically append a new line character).

NOTE:

Don't use the the "Write" command to write the string to the file. If one needs to write the data in table format, one may need to write a piece of code, using the above commands.

Last updated:
February 07, 2011

Mathematica Documentation

The use of all USC computing resources is governed by the USC Computing Policies.