This is an old revision of the document!
MATHEMATICA - AN INTRODUCTION
Revision: v.0.1 Document Summary : This document provides basic information on using Mathematica installed on Physics Computer Network Author: Tom Nguyen
Introduction
Mathematica(tm) is a scientific software package created by Stephen Wolfram to provide a multi-purpose technical computing environment. It can be used as a programming language, a system for representing mathematical knowledge, or a tool in the standard computing environment. Like MapleTM , Mathematica offers symbolic manipulation of mathematical expressions. Mathematica's structure consists of a frontend and the kernel. Depending on the platform on which Mathematica runs, the frontend (user interface) can be slightly different. This document is written for a Unix-based host (amdahl.physics.purdue.edu) running off a PC via the X-Window interface (NCD's PCXware). Currently, PCN carries a two-user license of Mathematica 5.2.
Conventions used in this document:
- Bold-faced: Command, function names, and screen display to be entered or selected exactly as shown.
- Italics: Special notes to specified commands or specific named entry used in examples or a string appended to a command.
The Softer Side of Mathematica
To start a Mathematica session, you need to log on to Amdahl (amdahl.physics.purdue.edu). You must use ssh (secure shell) for your login session to amdahl. At the shell prompt, enter: Mathematica (make sure you have the capital “M”).
NOTE:
- If you get an error complaining about missing fonts, please click here to get instructions on how to correct the problem.
- If you successfully bring up (envoke) Mathematica and receive no response (no In:=[] or Out:=[] prompts), this usually means that the site license has reached its limit and you will have to wait for other users to free up some space. Currently, only two users can run Mathematica at the same time.
Upon a successful start of a Mathematica session, two windows will appear on the screen. One window contains the mathematical operation palette and the other is the “workspace” (or notebook) where inputs and outputs are displayed. A typical view of these window is shown in Figure 1. Note that each input and output is designated by a numbered block ”[xx]” and is organized by a “cell” structure.
Figure 1 - Typical view of a Mathematica session with tool palette shown on the left of the workspace or notebook area.
Defining and Clearing Variables
As shown in the examples above, a variable can be assigned to a value with an equal sign ”=”. A named variable can be cleared with the operator ”=.” (equal+period) For example, Numvalue =. will clear all values assigned to variable ”Numvalue”,
Alternately, you may use the command Clear[x] to perform an equivalent operation, where x is the variable name, e.g., Clear[Numvalue]
Working with Mathematica
Mathematica can be used to perform a simple calculation or complex mathematical expression manipulation. With the help of the 'toolbar' (left window in Figure 1), you may substitute the common operation command such as square root or intergral with a simple click of the templated operation. For example you may enter the command Sqrt[4] or click on the
button in the toolbar to perform the same operation of taking the square root of a numeric value of 4.
Similarily, you may use the matrix template
to create a matrix. In the example below, the matrix [M] is created by clicking on the matrix template button. The elements of [M] are entered in symbolic format, namely a, b,c, and d. (Tab key is used to jump to the next entry or element of the matrix).
To execute the expression, use combination keystrokes SHIFT + ENTER. Effectively, every time the SHIFT + ENTER keystrokes are made, the input(s) is(are) sent to the kernel to be processed and the result is displayed in the “frontend” (workspace window). ENTER key by itself results in a next-line entry.
In this example, the determinant of [M] can be symbolically obtained by using the simple command Det. Note that operation commands such as Det and Eigenvalues are “reserved words”, signifying a built-in function. The first character of the command must be in uppercase. Other common reserved words are I (imaginary number), E (natural number), Pi, Degree, and Infinity.
In addition, some conventions used by Mathematica are:
^ Operation ^ Convention ^ Examples ^
| Arguments of functions | in square bracket [ ] | Sin[x] |
| Power | ^ | Sin[x]^2+Cos[x]^2=1 |
| Multiplication | * or space | 2 log[y] = 2*log[y] |
| Scientific Notations | *^ | 1*^-3 = 1 10^-3 = 0.001 |
| List | in curly bracket { } | U={a,b,x} |
| Group | parentheses ( ) | x^(2y) = Note: x^2y = |
Mathematica organizes the computation session in cells (groups) signified by symbolized brackets as shown in the right of the screenshot below.
You may also substitute (replace) the symbolic expression with numerical values with a simple replacement operator ”/.” (forward slash+period). To convert the result in output 5 (Out[5]) in the example above to numerical values with a=1, b=0, c=1, d=1, the following line is entered:
Note that there is no space between the forward slash and the period in the replacement operator ”/.” (Numvalue is an arbitary name)
“%” is used to represent results (output, expression) from previous operation. Similarly, ”%%” represents output from next-to-last operation, and ”%n” represents output from previous n th operation.
To obtain a numeric value of a symbolic expression, you may use either N[ ….] or //N. For example, you may want to know the numeric value of Sin(Pi/2),
Some Useful Shortcuts
Mathematica offers some alternate way to entering notations by using a set of keystroke sequences, such as ESC or CTRL keys, to help speed up the entry process for routine operations. Here are a few selected shortcuts.
^ Key strokes ^ Results^
| ESC p ESC | Π |
| ESC inf ESC | ∞ |
| ESC deg ESC | degree sign, ° |
| ESC != ESC | not equal to, ≠ |
| CTRL and ^ (both keys at the same time) | superscript |
| CTRL and _ (underscore) | subscript |
| CTRL and space key | reset to normal from superscript mode |
| CTRL and @ key | set square root group |
| ESC int ESC | intergal sign, ∫ |
| ESC dd ESC | differential sign used in integral |
| ESC pd ESC | partial derivative sign, ∂ |
Use of Pure Function
You can define a function in which the variable is replaced by any argument you provide.
Syntax: Function[ {x,y,…},body ]
Example: :
You may also use the special short form in Mathematica to perform substitution in functions by using the following syntax:
body & , where body contains substitution notation #
Example:
In this example, the first two elements of each list in {} brackets are raised to the power of 2 (squared) as specified in the Take function with # designated the substituttion notation.
The Other Softer Side of Mathematica
Equation solving and calculus applications
Mathematica may be used to solve, simplify, and evaluate rather involved mathematical expression that would otherwise be time consumming. Some basic math applications are presented below. Please refer to Wofram's Mathematica book 3rd. Ed for further information.
Solving an equation for an unknown variable
Syntax: Solve[ lefthand side == righthand side, x ]. Note that double equal sign is used to define the equation
Example: A given function is defined as: h = x^5 + 3x^2 - 81
To obtain the solutions of x for h = 0, we enter in Mathematica as follows:
As it is observed from the above solutions (roots of h = 0), Mathematica provides solutions in “root objects”. This is because for a certain equation, it is impossible for Mathematica to find explicit formulas for the solutions.
To obtain a numeric representation of the above solutions, use the N[ - ] command. e.g.,
where I is imaginary number. Similar commands: FindRoot, NSolve(numerical equation solving). See Wofram's Mathematica Book for more details
Solving a system of equations for specified unknowns
Syntax: Solve[{lhs1 == rhs1, lhs2 == rhs2, lhs3 == rhs3,…},{ x,y,z,…}]
Example: The given set of functions are:
We want to use Mathematica to solve this system of equations f = 0, g = 0, and h = 1 simultaneously. In Mathematica,
Note: ” I ” stands for imaginary notation I = square root of -1
Eliminating variables in a set of simultaneous equations
Syntax: Eliminate[{ lhs1 == rhs1, lhs2 == rhs2, ….},{x,….}]
Example: We want to eliminate the variable x in the set of function f = 0 and g = 0 in the example above. In Mathematica,
Obtaining a set of simplified solutions from a set of simultaneous equations
Syntax: Reduce[{ lhs1 == rhs1, lhs2 == rhs2, …},{x,….}]
Example: We want to obtain a set of simplified solutions of f = 0 and g = 1. The result will be all possible solutions to this set of simultaneous equations. In Mathematica,
The numerical solutions are
Note that the && notation designates the required condition that both y = -1.87846 and x = 1.73205 to exist at the same time for the solutions to be true. The same is applied to the second set of solutions in this example.
Obtaining a simplified form of an expression
Syntax: Simplify[ expr ]
Example: Mathematica can be used to carryout mathematical operations that involve symbolic expansion and simplication:
See also ComplexExpand
Doing Calculus and Differential Equations with Mathematica...
You can obtain a derivative or taking an integral of a complicated or simple function rather quickly using the symbolic mathematics feature of Mathematica. Here are some examples of applications in calculus.
Examples used in the table below are based on the following functions (except for ODE examples):
| What you want to do…. | How to tell Mathematica | Examples |
|---|---|---|
| Partial derivative of g | D[g,x] | |
| Total differentiation of g | Dt[g] | |
| Total derivative of g with respeect to x | Dt[g,x] | |
| Using chain rule on function 5z h(z4) | see input in example | |
| Multiple derivative | D[g,{x,n}] (n: nth derivative) | |
Indefinite integral | Integrate[g,x] | |
Indefinite multiple integral | Integrate[g,x,y] | |
Definite integral | Integrate[g,{xmin,xmax}] | |
Definite multiple integral | Integrate[g,{xmin,xmax,ymin,ymax}] | |
| ODE | ||
| Solve an ordinary differential equation (ODE) for y[x] {see note 1} | DSolve[eqn, y [x], x] | ![]() |
| Solve a differential equation for a function y {see note 2} | DSolve[eqn, y, x ] | ![]() |
- Note 1: Mathematica returns only the solution of y[x] and not y'[x] nor the initital conditiony[0]. This is different from the solution of y.
- Note 2: In this case Mathematica returns the solutions of the function y itself. Note that in cell [33], a test is performed to verify the solution obtained in cell [29]. The returned argument was 'true' signifying the substitution of the solution to the LHS is indeed equal to value “1” on the RHS.
- Note 3: For some common commands such as derivatives and integrals, you may choose to use an alternate method of entry by using the provided tools pallete.
Plotting and Graphics
You may find Mathematica useful in the visualization of a functions. In the following example, an integral is evaluated and the result is plotted with x ranged from 0 to 1. the Plot command is reserved for 2-D plotting while Plot3D is used for 3-D plots.
The axes can be labeled with the option “AxesLabel”,
You may choose to frame the plot and/or assign grid lines to the plot…
A plot can be re-drawn with the Show command. You may also use the PlotRange option to specify the ranges for both x and y axes in the syntax of {{xmin,xmax},{ymin,ymax}}.
To plot a x-y pair of data points with the ListPlot command. In the example below, the point pairs are generated with the Table command. You may choose to show these data points with a solid line (PlotJoined → True option) or by dots (default).
3-D Ploting for the function Sec(x)*Tan(y) in the range of between 0 and Pi for both x and y is shown below:
You may change the view of a 3-D plot with the ViewPoint option. Note that {0,-2,0} is direct front view; {0,0,2} is the top view; {2,-2,0} is a view from the right hand corner, etc.
You may also label axes with AxesLabel command as shown below:
Further information on plotting can be found in Sec 2.9 of Wolffram's The Mathematica Book 3rd Edition.
Files and Import/Export
You may find it's quite convenient to export and import files in Mathematica. A few common file manipulations are shown in Table below
Exporting graphics
You may send a selected graphic from Mathematica directly to a printer or to a file.
To export a graphic to a file, use the command: Display in the following syntax:
Display[“filename.ext ”, graphic to be display, “file format ” ] Example:
The following file formats are supported by Mathematica 3.0:
| Type of Format | Extension callout (File Format) |
|---|---|
| Mathematica Postscript | MPS |
| Encapsulated Postscript | EPS |
| Macintosh PICT | PICT |
| Adobe Illustrator | Illustrator |
| Adobe Acrobat Portable Document Format | |
| Gif | GIF |
| Tagged Image File Format | TIFF |
| Postscript Image format | PSImage |
| X Windows bitmap | XBitmap |
To send a graphic directly to a printer, use the command Display in the following format:
Display[” !lpr “,graphic to be printed, ” file format ” ]
Example: Assume that you wish to send a graphic in a Mathematica's most recent cell to printer babbage. In a Mathematica's cell, enter the following:
Display[”!lpr -P babbage”, % , “EPS”]

































