Mathematical and Statistical Application - Mathematica - Tutorial
What is Mathematica?
According to Wolfram Research, the vendor of this software, Mathematica is the world's only fully integrated environment for technical computing. It handles a wide variety of numerical, algebraic, graphical, and other tasks, which previously could be done only by using many separate software packages. It can be used as everything from a simple calculator to a high-level programming language.
But enough of the marketing jargon... In simple terms, Mathematica is just a really versatile math software package.
For more information on Mathematica, please visit Wolfram Research.
The Mathematica Windows
The window in which you type is called a notebook, part of the Mathematica front end which handles interaction with the user; the Mathematica kernel actually performs the calculations.
Most Mathematica input must be entered as text, but the palettes in the palette window can be used for some common functions and constants.
To execute input, press shift-return. To interrupt the execution of input, go to Kernel -> Quit Kernel. This is located on the Mathematica bar in the Microsoft Windows version of Mathematica, or the Notebook window in the UNIX versions of Mathematica. ALT-, will also halt execution.
When Mathematica executes input, it adds the label In[n]:= and gives the output. For example, when you type 1+2, Mathematica gives: If Mathematica finds an error in your input, it will produce a warning message. Each message has a name, and you can switch off particular error messages using If you don't want to see them. To turn messages back on, use For example, if you enter Sqrt[4,5], Mathematica responds: Typing Off[Sqrt::argx] followed by Sqrt[4,5] produces: Type On[Sqrt::argx] to turn the warning message back on.
Descriptions of all warning messages can be found in the Mathematica Help. Help -> Help Browser in the Windows command bar when using Mathematica for Microsoft Windows, and " Help Browser " on the UNIX startup palette contain extensive help on all Mathematica input commands. Additionally, the Mathematica manual is located online.
Numerical Calculation Notes
- The arguments of all Mathematica functions are enclosed in square brackets.
- The names of all built-in Mathematica functions and symbols begin with capital letters.
- Arithmetic is the same as on a regular calculator. Note that multiplication can be represented by a space, *, or parentheses, and a power is represented by ^. The following is the Mathematica output from simple examples
- Scientific notation is entered, for example, as 3.1*^5 or 3.1 10^5
- By default, Mathematica produces exact results. To get an approximate numerical value, use expr //N . Also, when using a decimal point, Mathematica assumes the number is accurate only to a fixed number of decimal places. By placing a decimal after an integer, you can force Mathematica to give you approximate results. To specify a particular number of significant digits, say n, use N[expr,n] . Note, however, that unless the numbers in expr are exact or of sufficiently high precision, N[expr,n] may not be able to give results with n-digit precision. For example:
-

Some useful mathematical functions and constants: square root of x exponential natural logarithm of x base b logarithm of x trigonometric functions (arguments in radians) absolute value of x closest integer to x pseudo random number between 0 and 1 maximum and minimum of x, y, ... prime factors of n integer part of x fractional part of x k modulo n (remainder from dividing k by n) factorial n(n-1)(n-2)....1: binomial coefficient, = Euler gamma function, i = : degrees to radians conversion factor You can get descriptions of all functions directly from the Mathematica kernel by typing ?<Function> , where <Function> is the name of a function. For example: - Mathematica contains an extensive list of packages of extra functions that can be loaded. Descriptions of these can be found in the Help Browser from the Add-ons & Links tab under Standard Packages. You can also find the help documentations following this webpage. To load an add-on package, type name where name is the name of the package. For example the program listing illustrates the breadth of Mathematica. This add-on provides abbreviations, atomic weights, and atomic numbers of the elements in the periodic table!
- Building Up Calculations
- To refer to a previous result, Mathematica uses %. Use %% for the next-to-last result, and %%...% (k times) for the kth previous result. For example,
- Variable naming conventions
- If you define a variable as having a specific value, say x=7, it retains that value throughout your entire session. Problems could arise if you forget this. Thus, it is generally a good idea to remove values of a variable after you use them. To do this, use x = . or Clear[x].
- Mathematica differentiates between upper- and lower-case letters. Hence, for example, the variable X is different from the variable x. Predefined Mathematica variables always start with capital letter, so to avoid duplicating these, it is generally a good idea to name your own variables with lower-case letters.
- Variable names can be of any length but cannot start with a number. For example, x5 could be a variable, but 5x is not an allowed variable name but is interpreted as 5 times x by Mathematica
- To define a list of objects, use curly brackets as in the example: You can then perform functions on the whole list at once instead of having to repeat the calculation multiple times as demonstrated by the code: To extract a particular value of a list, use Part[list,i] or list[[i]]. For example: Likewise, to extract a list of elements from a list, use Part[list, {i,j,...}] or list[[{i,j,...}]]. Mathematica contains an extensive collection of additional functions pertaining to working with lists; these can be found in the Mathematica Help.
- To perform repetitive operations in another way, use the Do function, which works like an iterative do loop in many programming languages. The right side of the table below gives the Mathematica commands to perform the operations described on the left side. Evaluate expr with i running from 1 to imax Evaluate expr with i running from imin to imax in steps of di Print expr Make a list of the values of expr with i running from 1 to imax
- So far, we've used four types of bracketing: parentheses used for grouping, (whatever), square brackets used for functions, f[x], curly brackets used for lists, {a,b,c}, and double square brackets used for indexing, a[[i]].
- When doing calculations, often you'll go through multiple steps. You can do each step on a separate line or combine them into one using semicolons. Mathematica won't print output from any command with a semi-colon after it. For example,
Algebraic Calculations
- Mathematica can do symbolic computations using the same operators as in numerical calculations. For example:
- The function Expand multiplies out products and powers. The function Factor does the reverse and factors a long expression. The function Simplify attempts to find the simplest form of an expression by using standard algebraic transformations. The function FullSimplify tries to find the simplest form of an expression by using a larger range of algebraic transformations than included in the Simplify function.
- Some more useful algebraic functions: Apply everywhere (not just to products and powers) Put all terms over a common denominator Separate into terms with separate denominators Cancel common factors between numerators and denominators Group together powers of x Pull out factors that do not depend on x Expand trig expressions into a sum of terms Factor trig expressions into products of terms convert trig functions to exponentials Convert exponentials to trig functions Coefficient of form in expr Maximum power of form in expr nth term of expr Numerator of expr Denominator of expr Show an n-line outline of expr (used for particularly long expressions)
Calculus
-

Mathematica does a variety of the plethora of calculations we know and love as calculus. Some useful calculus functions: (note that many of these can be entered using the palettes in the palette window.) The partial derivative, Multiple derivative The indefinite integral The multiple integral The definite integral The multiple integral The sum The sum with i increasing in steps of di The nested sum The product Solution to an equation for x Solve a set of simultaneous equations for x, y, ... A power series expansion of f about the point x = x0 The limit limx->x0f Eliminate x, ... in a set of simultaneous equations Give a set of simplified equations, including all possible solutions Truncate a power series to give an ordinary expression Statistics - Mathematica contains the following add-on statistical packages. Refer to the Mathematica Help for detailed information about these. These packages are nice if you want to know something about a common distribution such as the normal or binomial distribution (especially expected values), but fall far behind programs like SAS or Stata for data manipulation functions and any functions requiring advanced statistical theory. Statistics`ConfidenceIntervals` Confidence intervals for data which are univariate normal Statistics`ContinuousDistributions` Properties of common continuous statistical distributions Statistics`DataManipulation` Utilities for data manipulation Statistics`DataSmoothing` smoothing of data Statistics`DescriptiveStatistics` Common descriptive statistics for data Statistics`DiscreteDistributions` Properties of common discrete statistical distributions Statistics`HypothesisTests` Hypothesis tests based on the normal distribution (for small sample sizes) Statistics`LinearRegression` Linear regression analysis using the method of least-squares Statistics`MultiDescriptiveStatistics` Common descriptive statistics for multivariate data Statistics`MultiDiscreteDistributions` Properties of the multinomial, negative multinomial, and multiple Poisson distributions Statistics`MultinormalDistribution` Properties of the multinormal, multivariate Student-t, Wishart, Hotelling T2, and quadratic form distributions Statistics`NonlinearFit` Nonlinear fitting of data Statistics`NormalDistribution` Subset of Statistics`ContinuousDistributions` containing properties of only the normal, Student-t, chi-square, and F-ratio distributions
Matrices
-

Matrices in Mathematica are entered as lists of lists. For example, entering {{a,b},{c,d}} results in the matrix Matrix multiplication is represented by ., and matrix division is, of course, not defined. * represents scalar or element wise multiplication and + and - operators represent matrix addition and subtraction. - Some useful functions for matrices are: Build an m by n matrix by evaluating the function f with i ranging from 1 to m and j ranging from 1 to n generate an n by n identity matrix generate a square matrix with the elements in list on the diagonal or Returns the ith row in the matrix list or Returns the i,j th element in the matrix list Returns the dimensions of a matrix represented by list Display list in matrix form nth power of a matrix matrix inverse determinant transpose trace eigenvalues eigenvectors
Graphics
- Some useful graphics functions: Plot f as a function of x from xmin to xmax Plot several functions together Plots several data points Make a contour plot of f as a function of x and y Make a density plot of f as a function of x and y Make a three-dimensional plot of f as a function of the variables x and y Make a parametric plot Plot several parametric curves together Make a parametric plot of a three-dimensional curve Make a parametric plot of a three-dimensional surface Plot several objects together
-

A few examples of the above functions: For attractive examples of some more complicated uses of these graphics functions, have a look at the Graphics Gallery in the Help Browser by choosing the Demos tab and looking under Graphics Gallery.
End Comments
I hope it has become apparent that Mathematica is a really easy program to use. Important things you should take away from this are:
- Functions begin with a CAPITAL letter.
- Arguments of functions use SQUARE brackets.
- Statements end with a semi-colon ONLY if you don't want output.
- You press SHIFT-return to execute input
- Once you've got these down, you can just refer to the Mathematica Help for the appropriate function name and arguments. Get friendly with the Help! It is definitely worth your time to spend a few moments browsing though it, especially through all the add-on packages. Every time you do, you'll learn about additional Mathematica functions.
- Contact information for Wolfram Research and Mathematica:


