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?
- How can I change the background color in Mathematica?
- How can I run Mathematica In Batch Mode?
- How can I compile Functions In Mathematica?
- How do I create multiple plots on the same postscript file?
- How can I print from Mathematica?
- Why do I fix Mathematica postscript code with 'psfix'?
- How do you save a variable(s) from Mathematica into a file and reload it later when needed, like MATLAB?
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:
-
Create the input file containing mathematica commands using your favorite editor.
At the UNIX Shell prompt, give the command:
math -batchinput -batchoutput < infile > outfileHow 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.psThis will create a standard PostScript file (in this case "tested.ps" which you can then print with:
lpr -Pps_sal125 tested.psWhy 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]stmp = OpenRead["-file name-"]l = ReadList[stmp, Number]4 5 8
0 3 5
2 5 0Writing data to a file.
-
The command "OpenWrite" or "AppendWrite" could be used to open the file for reading.
stmp = OpenWrite["temp"]v = {1, 2, 3} Write[stmp, v]{1, 2, 3}1 2 3kkk = ToString[v]StringReplace[kkk, { "," -> " ", "{" -> " ", "}" -> " "}]
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