# University of Southern California

## ITS Information Technology Services

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

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

- For the X front end to mathematica, type:

`mathematica`

`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:

Create the input file containing mathematica commands using your favorite editor.
At the UNIX Shell prompt, give the command:
`math -batchinput -batchoutput < infile > outfile`
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 :

```In[1]:= ?Compile Compile[{x1, x2, ...}, expr] creates a compiled function which evaluates expr assuming numerical values of the xi. Compile[{{x1, t1}, .. .}, expr] assumes that xi is of a type which matches ti. Compile[vars, expr, {{p1, pt1},...}] assumes that subexpressions in expr which match pi are of types whichmatch pti.```

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

```In[2]:= f = Function[{x}, x Sin[x]] Out[2]= Function[{x}, x Sin[x]] ```

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

``` In[3]:= fc = Compile[{x}, x Sin[x]] Out[3]= CompiledFunction[{x}, x Sin[x], -CompiledCode-] ```

The following demonstrates the use of f and fc:

``` In[4]:= {f[2.5], fc[2.5]} ```
``` 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. 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). 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 ```
``` Search the ITS Website Mathematica Documentation In This Section Mathematica Overview Mathematica Software Software for Faculty and Staff Software for Campus Use Software for Students Mathematica for UNIX Starting Mathematica For UNIX Using Mathematica for UNIX Mathematica Commands Related Links Mathematica FAQs The use of all USC computing resources is governed by the USC Computing Policies. ```
``` CONTACT US For any questions regarding your USC account, including password change requests, contact our Customer Support Center at 213-740-5555 or consult@usc.edu. Site Design: ITS Web Services A-Z Index IT at USC For information about computer security, including malware, phishes, and social networking scams, visit the ITS security blog. _uacct = "UA-584548-1"; urchinTracker(); ```