You can define the value of a symbol to be any expression, not just a number. Once defined, a definition will continue to be used whenever the symbol appears, until you explicitly change or remove the definition. The function Clear will clear the value of the symbol while the function Remove will actually remove the symbol completely from Mathematica. See Mathematica Tips, Tricks and Traps: Equal signs, assignments.
| name = value or expression | ||
| Example: | In[n]:= | x=3 |
| Out[n]= | 3 |
|
| In[n+1]:= | x^3-6x |
|
| Out[n+1]= | 9 |
|
| Example (expression): | In[n]:= | y = (x+1)^3 + 3 x - Abs[x] |
| Clear assignment: | In[n]:= | y= . |
You may want to add some commentary text to your programs to make it easier to understand. You can add such text at any point in your code simply by enclosing it in matching(*and*).
| Example: | In[n]:= | (* Enclose like this *) |
|
|
E I Pi EulerGamma Infinity |
|
| Example:
|
In[n]:= | E^(Pi I) |
Different text-based interfaces use slightly different schemes for letting Mathematica know when you have finished typing your input. With the notebook feature you press Shift-Return, while from command tool you press Meta-Return. In interfaces that use Shirt-Return, you can continue your input for several lines by typing Return at the end of each line. In interfaces where Meta-Return is used to signify the end of your input, expressions in a line which Mathematica detects as imcomplete will automatically continue reading successive lines until it has received a complete expression. The continuation parentheses are not required in the above example. Otherwise, the following will allow you the continuation of input for several lines.
| In[n]:= | ( start with left parenthesis |
|
and end with right ) |
||
| Example: | In[n]:= | ( Plot3D[Exp[-(x^2 + y^2)],{x,-2,2}, |
{y,-2,2}, HiddenSurface->False] ) |
functionname[x_]:= expression with x in it |
||
| Example: | In[n]:= | f[x_]:= Exp[x] + Cos[x] Sin[x] |
| Show the definition of f alone: | In[n]:= | ?f |
| (cont. from the example above) | Out[n]= | f[x_]:=Exp[x]+Cos[x]*Sin[x] |
| Clear definition: | In[n]:= | Clear[f] |
| Remove variable completely: | In[n]:= | Remove[f] |
| Partial derivative
|
D[ expression, variable (s) ] | |
| Simple derivative: | In[n]:= | D[x^n, x] |
| Out[n]= | n x-1+n |
|
| Second derivative: | In[n]:= | D[Exp[I x], {x, 2}] |
| Out[n]= | -EI x |
|
| Mixed partial (here x & y): | In[n]:= | D[Sin[x y] + y (Pi/4), x, y] |
| Out[n]= | Cos[x y]-x y Sin[x y] |
The function D[x^n, x] gives a partial derivative in which n is assumed not to depend on x. Mathematica has another function, called Dt, which finds total derivatives in which all variables are assumed to be related.
| Total derivative
|
Dt[ expression, variable] | |
| Example: | In[n]:= | Dt[ x^n ] |
| Out[n]= | n x-1+n Dt[x] +xn Dt[n] Log[x] |
| Do[expr, {i, imax}] |
DO loop will evaluate expr repetitively, with i varying from 1 to imax in steps of 1. When a sequence of expressions are to be evaluated, the expressions may be separated by semicolons.
| Example: | In[n]:= | Do[Print[i], {i, 3}] |
| Out[n]= | 1 |
|
2 |
||
3 |
||
| i varies from imin to imax in steps of di | Do[expr, {i, imin,imax, di}] | |
| Example: | In[n]:= | Do[Print[i/2], {i,2,6,2}] |
| Out[n]= | 1 |
|
2 |
||
3 |
||
| Evaluate n times | Do[expr, {n}] | |
| Example: | In[n]:= | t=x; Do[t=1+(2 t), {3}]; t |
| Out[n]= | 1+2(1+2(1+2x)) |
| Expand[ expression] | ||
| Example: | In[n]:= | Expand[(x + 1)^4] |
| Out[n]= | 1 + 4x + 6x2 + 4x3 +x4 |
|
| To multiply out: | In[n]:= | Expand[(x+2)(x-2)(x^2+4)] |
| Out[n]= | -16 + x4 |
|
| Apply Expand everywhere: | In[n]:= | ExpandAll[(x+2)(x-2)/(x^2+4)(x-3)] |
| Out[n]= |
2 3
12 4x 3x x
------ - ------ - ------ + ------
2 2 2 2
4 + x 4 + x 4 + x 4 + x
|
|
To six decimal digits by default.
| N[ expression ] | ||
| Example: | In[n]:= | N[E] |
| Out[n]= | 2.71828 |
|
| To 10 decimal digits: | In[n]:= | N[Pi^2/6,10] |
| Out[n]= | 1.644934067 |
| Factor[ expression] | ||
| Example: | In[n]:= | Factor[x^2 -1] |
| Out[n]= | (-1+x)(1+x) |
Floor[ expression ] |
||
| Example: | In[n]:= | Floor[3.1415] |
| Out[n]= | 3 |
For function will evaluate start, then repetitively evaluate body and increment, until the specified condition test fails.
| Example, print first 4 numbers: | ||
| In[n]:= | For[n=1, n<=4, n++, Print[n]] |
|
| Out[n]= | 1 |
|
2 |
||
3 |
||
4 |
| Denominator of ratio: | Denominator[ rational expression ] | |
| Example: | In[n]:= | Denominator[a/b] |
| Out[n]= | b |
|
| Numerator of ratio: | In[n]:= | Numerator[a/b] |
| Out[n]= | a |
|
| Combine fractions: | In[n]:= | Together[a/b + c/d] |
| Out[n]= | bc+ad |
|
b d |
||
| Separate rational: | In[n]:= | Apart[(b c + a d)/(bd)] |
| Rationalize a real: | Rationalize[ real number, accuracy (error) ] | |
| Example: | In[n]:= | Rationalize[3.14159265, .00001] |
| Out[n]= | 355 |
|
113 |
||
| accuracy=0 gives the best possible approximation. | ||
| Example: | In[n]:= | Rationalize[3.14159265,0] |
| Out[n]= | 392699 |
|
125000 |
| In[n]:= | ? | |
| Example, about command Range: | In[n]:= | ?Range |
| with pattern matching: | In[n]:= | ?Ran* |
| Info about a function: | In[n]:= | ?f |
| More info about a function: | In[n]:= | ??f |
From a Mathematica notebook window, one may right click on the Help Option to study a wide variety of help options and examples.
IF function will test the condition and evaluate then if the condition is true, else if it is false
| Example: | ||
![]() |
In[n]:= | f[x_]:= If[x<=0, 0,Log[x]] |
| Integrate[ expression, variable] | ||
| Example: | In[n]:= | Integrate[f[x],x] |
| Definite integral: | Integrate[
expr, { variable, min, max}] |
|
| Example, from 0 to
|
In[n]:= | Integrate[Sin[x], {x, 0, Pi/4}] |
| Double integral: | Integrate[ Integrate[ expr,
{ variable, min, max}],{ variable, min, max}] |
|
| Example: | In[n]:= | Integrate[ Integrate[Cos[x],{x,0,Pi}],{x,0,Pi}] |
| Out[n]= | Pi |
|
| Numerical integration | NIntegrate[ expr, { variable, min, max}] |
|
| Example: | In[n]:= | NIntegrate[Sin[x], {x, -Infinity, Infinity}] |
| INTERRUPT computation: | CTRL-C (if in cmd tool) | |
| CTRL-C CTRL-C or F2 F2 (if in EPOCH) | ||
| for menu after interruption: | h | |
| Abort current calculation: | abort | |
| Continue: | continue | |
| Enter an interactive dialog: | inspect | |
| Show current operation: | show | |
| Show all operations: | trade |
Limit[ expression,
variable -> value ] |
||
| Example, at 0: | In[n]:= | Limit[Sin[x]/x, x -> 0] |
| Example, at |
In[n]:= | Limit[ArcTan[x], x -> Infinity] |
| Create a list: | Table[ expr, { variable, min, max, step}] |
|
| stepsize is 1 by default | ||
| Example: | In[n]:= | Table[1 + 2^n, {n, 0, 7}] |
| Out[n]= | {2,3,5,9,17,33,65,129} |
|
| Applying function to the list: | In[n+1]:= | PrimeQ[%] |
| Out[n+1]= | {True,True,True,False,True,False,False,False} |
|
| Constant array: E.g., ai = 1, i=1,..9 | In[n]:= | a = Table[1,{9}] |
| Out[n]= | {1,1,1,1,1,1,1,1,1} |
|
| Function array: E.g.,
|
In[n]:= | b = Array[Abs, 9] |
| Out[n]= | {1,2,3,4,5,6,7,8,9} |
|
| Dot product: | In[n]:= | a . b (* a dot b *) |
| Out[n]= | 45 |
|
| two vectors must have the same dimention | ||
Create an m x n matrix a[i,j]: |
Array[ expr, { rowsize, columnsize}] |
|
| Two dimensional array: | In[n]:= | arr=Table[i*j, {i,1,3},{j,2,4}] |
| Out[n]= | {{2,3,4},{4,6,8},{6,9,12}} |
|
| The nth term in array: | array [[n]] | |
| Example, Third element of arr: | In[n+1]:= | arr[[3]] |
| Out[n+1]= | {6,9,12} |
|
| Example, 3rd row, 2nd column of arr: | In[n+2]:= | arr[[3,2]] |
| Out[n+2]= | 9 |
|
| Inverse of matrix: | In[n]:= | inv = Inverse[arr] |
| Display row by column: | In[n]:= | MatrixForm[arr] |
The execution of a Mathematica program involves the evaluation of a sequence of expressions. You may need some kind of "loop" to evaluate expressions several times.
The most common way that modules are used is to set up temporary or intermediate variables inside functions you define. It is important to make sure that such variables are kept local. If they are not, then you will run into a problem whenever their names happen to coincide with the names of other variables.
| Specify the symbols x,y.. in expr are local | In[n]:= | Module[{x,y,..},
expr] |
| Define the global x | In[n]:= | x=Pi |
| Specify the x inside the Module is local | In[n+1]:= | Module[{x}, x=3; x] |
| Out[n+1]= | 3 |
|
| The global x is unchanged | In[n+2]:= | t |
| Out[n+2]= | Pi |
Plot is the function used to plot one or more functions of a single variable on the same axis. Plot[expr, returns the plot of the expr as a function of variable on the interval from min to max. Specific examples follow.
Plot[function, {variable, min, max}] |
||
| Example, from 2 to |
In[n]:= | Plot[x^3+Sin[x]-1, {x,2, Pi}] |
| Two functions: | In[n]:= | Plot[{Gamma[x], Zeta[x]}, {x,2, 3}] |
| Three dimensions: | In[n]:= | ( Plot3D[Sin[x y],{x,-2,2},{y,-2,2}] ) |
| Contour plot: | In[n]:= | ( ContourPlot[(x y)/(x^2 + y^2), |
{x, -1, 1}, {y, -1, 1}] ) |
||
| Density plot: | In[n]:= | ( DensityPlot[Cos[Sin[x y]], |
{x,-1,1}, {y,-1,1}] ) |
Type ??Plot to view the numerous plot options. A few commonly used options are listed below. For more detail on the options, you may have to check the current manual.
Plot3D usually colors surfaces using a simulated lighting model. LightingFalse switches off the simulated lighting, and instead shades surfaces with gray levels determined by height.
| Example: | In[n]:= | ( Plot3D[Sin[x y],{x,-2,2},{y,-2,2}, |
Lighting->False] ) |
MeshFalse shows the surface without the mesh drawn.
| Example: | In[n]:= | ( Plot3D[Sin[x y],{x,-2,2},{y,-2,2}, |
Mesh->False] ) |
HiddenSurfaceFalse will show the surface as solid.
| Example: | In[n]:= | ( Plot3D[Sin[x y],{x,-2,2},{y,-2,2}, |
HiddenSurface->False] ) |
PlotPoints defines the number of points in each direction at which to sample the function (15 by default). You need to do this to get good pictures of functions that wiggle a lot.
| Example: | In[n]:= | ( Plot3D[Sin[x y],{x,-2,2},{y,-2,2}, |
PlotPoints -> {40, 40}] ) |
ViewPoint option allows you to specify the point x,y,z in space from which you view a surface.
| Example, directly in front: | In[n]:= | ( Plot3D[Sin[x y],{x,-2,2},{y,-2,2}, |
Viewpoint -> {0,-2,0}] ) |
ContourPlot is a topographic map of a function. The contours join points on the surface that have the same height, shading in such a way that regions with higher z values are lighter. ContourShading switches off shading in contour plots.
| Example: | In[n]:= | ( ContourPlot[(x y)/(x^2 + y^2), |
{x, -1, 1}, {y, -1, 1}, ContourShading-> False] ) |
DensityPlot shows the values of a function at a regular array of points. Lighter regions are higher. MeshFalse will remove the mesh.
| Example: | In[n]:= | ( DensityPlot[Cos[Sin[x y]], |
{x,-1,1}, {y,-1,1}, Mesh->False] ) |
| kth PREVIOUS results: | %%..% k times |
|
Example: |
In[n]:= | Expand[(x+1)^4] |
| The last result: | In[n+1]:= | Factor[%] |
| The next-to-last result: | In[n+2]:= | ExpandAll[%%] |
| By line (eg., output of In[4]): | In[n]:= | %4 |
With text-based Mathematica interfaces, the command PSPrint is typically set up to print a piece of graphics (hard copy).
| Create graphics object: | In[n]:= | Plot[Cosh[x],{x,-3,3}] |
| Print graphics object to the default printer | In[n+1]:= | PSPrint[%] |
| Print output n to the default printer | In[n+2]:= | PSPrint[% n] |
| PRODUCT:
|
Product[ f, { var, min, max, step}] |
|
| Example:
|
In[n]:= | Product[n^2/(n^2 - 1), {n, 2, 1000}] |
| Numerical approx:
|
In[n]:= | NProduct[(n^2 + 1)/n^2, {n,Infinity}] |
| Out[n]= | 3.67608 |
You may find that you often need to use certain functions that are not built into Mathematica. You may find a Mathematica package that contains the functions you need. Mathematica packages consist of collections of Mathematica definitions which teach Mathematica about particular application areas. You must read the package into Mathematica in order to use functions from a particular package.
<< filename |
||
| Example: | In[n]:= | <<DataAnalysis/DescriptiveStatistics.m |
You may save definitions for variables and functions using Save.
| in file filename: | Save[" filename", symbol ] |
|
| Example:(define function f) | In[n]:= | f[x_] :=Sin[a x] |
| (gives a some value) | In[n+1]:= | a=Pi/4 |
| (save the definition of f in the file Functions.m") | In[n+2]:= | Save["Functions.m",f] |
| Save several symbols: | In[n]:= | Save["Functions.m",s,h] |
| Simplify[ expression ] | ||
| Example: | In[n]:= | ArcSinh[(Exp[2] - 1)/(2 E)] |
| In[n+1]:= | Simplify[%] |
|
| Simplest form | In[n+2]:= | FullSimplify[%] |
| Find the simplest form by applying transformations |
| SOLVE equation: | Solve[ lhs==rhs, variable ] | |
| Example: | In[n]:= | Solve[ArcSinh[x] == 1, x] |
| Example: | In[n]:= | Solve[x^2-6 x == -8, x] |
| Out[n]= | {{x->2},{x->4}} |
|
| Treat the output as 1 x 2 matrix | ||
| Get the roots | In[n+1]:= | x/. % |
| Out[n+1]= | {2,4} |
|
| Get n-th root (n=2 here) | In[n+2]:= | x/. %%[[2]] |
| Out[n+2]= | 4 |
|
| Linear system: | In[n]:= | Solve[{x - y == 0, x + y == 1}, {x, y}] |
| Numerically: | FindRoot[ equation,{ var, initial values}] |
|
| Example: | In[n]:= | FindRoot[x^2 == Sin[x], {x, .8}] |
| For non-differentiables: | In[n]:= | FindRoot[Gamma[x]==Zeta[x],{x,{2,3}}] |
| In[n]:= | Quit |
Replace variable by value in the expression
expression /. variable -> value |
||
| Example: x=3, y=1-a | In[n]:= | (x+y)(x-y)^2 /. {x->3, y->1-a} |
| Example:
|
In[n]:= | Sqrt[x] /. x -> -1 |
Sum[ expr
,{ var, min, max, step}] |
||
| min, step =1 by default | ||
| Example:
|
In[n]:= | Sum[i^3, {i,10}] |
| Example:
|
In[n]:= | Sum[i, {i,2, 20, 2}] |
| Numerical approximation:
|
In[n]:= | NSum[1/(i^2), {i, Infinity}] |
| Out[n]= | 1.664493 |
Series[ expr, { var, center, order}] |
||
| Example, at 0, degree 5: | In[n]:= | Series[Sin[x], {x,0, 5}] |
| Several variables: | In[n]:= | Series[Sin[x + y], {x, 0, 5}, {y, 0, 4}] |
| Convert to polynomial: | In[n+1]:= | Normal[%] |
Timing[ command ] |
||
| Example:
|
In[n]:= | Timing[NSum[1/i^2, {i,Infinity}]] |
| Out[n]= | {1.17 Second, 1.64493} |
WHILE function will evaluate body so long as test is TRUE.
| Example, while
|
In[n]:= | n=1 (* supply starting value *) |
| print n!, increment n by 1: | In[n]:= | While[n!<=10^10,Print[n!];n++]
|