Introductory Tutorial To Mathematica 8: Getting Started With Mathematica
Introductory Tutorial To Mathematica 8: Getting Started With Mathematica
to Mathematica 8
Author: Harald Höller
version: English version v1.0
last modified: 12.01.2012
Licence: Creative Commons Licence by-nc-sa 3.0 at
This introductory tutorial to Mathematica 8 is designed as interactive course material and should ideally be worked
through together with students at PCs. Each student should progress sequentially through all Input/Output. Dura-
tion: approx. 90min.
Mathematica Basics
Some Basic Remarks
Mathematica allows analytical, numerical and grafical calculations. Mathematica offers a relatively
simple and instructional logical syntax. Most of the known functions do have a syntactically similar
correspondent in Mathematica. The argument of a function always is to set inside square brackets
(e.g. Sin[x]). If you operate on some mathematical expression, also the operation is executed onto
brackets and the arguments are separated by commas (e.g. Integrate[f[x],x]).
Data types
The standard data type, the Notebook (.nb) provides an interactive interface in which calculations are
possible. Under File -> Save As there are some further data formats to which the notebooks can be
exported (.tex, .html, .txt, .pdf). Also single expressions can be copied as LATEX code and be pasted
into a .tex-file. Notebooks can be opened by Mathematica (interactive) or the MathReader (read
only) respectively the Wolfram CDF Player which is available as free internet browser plugin.
The forward- and backward-compatibility is given partially, which means that notebooks created in
Mathematica 7 will usually run in Mathematica 8, the opposite direction will rather cause problems.
exported (.tex, .html, .txt, .pdf). Also single expressions can be copied as LATEX code and be pasted
into a .tex-file. Notebooks can be opened by Mathematica (interactive) or the MathReader (read
2only) respectively the Wolfram CDF Player which is available as free internet browser plugin.
introductory_mathematica_8_tutorial_expand.nb
The forward- and backward-compatibility is given partially, which means that notebooks created in
Mathematica 7 will usually run in Mathematica 8, the opposite direction will rather cause problems.
Wolfram|Alpha
Wolfram|Alpha is a net-based “frontend” for Mathematica usage with a huge database of knowledge.
Describing as “scientific Google” does not really cover its whole power. Either you access it via the
website www.wolframalpha.com or there is even an integration to Mathematica. Beginning an Input
with the “=” sign is interpreted as Wolfram|Alpha query. E.g: “= cosmological redshift z=3”
cosmological redshift z = 3 »
Result
Also include: dark energy density, matter density and radiation density
È universe model È Hubble parameter
Input information:
redshift-wavelength formula
redshift 3
Equation:
1+z
Λo
introductory_mathematica_8_tutorial_expand.nb 3
1+z Λe
Λe emitted wavelength
z redshift
Λo observed wavelength
Timeline:
Input/Output I
The input of commands in Mathematica is done via the Enter key (not Return), which is either
located at the numeric keypad or it can be typed in as Shift + Return. Each Output and Input is num-
bered by Mathematica.
4 introductory_mathematica_8_tutorial_expand.nb
The input of commands in Mathematica is done via the Enter key (not Return), which is either
located at the numeric keypad or it can be typed in as Shift + Return. Each Output and Input is num-
bered by Mathematica.
In@2D + Out@2D
? Integrate
Integrate@ f , 8x, xmin , xmax <, 8y, ymin , ymax <, …D gives the multiple integral à d xà
xmax ymax
dy … f .
xmin ymin
? Integ*
System`
IntegerExpon-
Integer ent IntegerPart IntegerQ IntegerString Integrate
IntegerPartiti-
IntegerDigits IntegerLength ons Integers Integral
The Kernel
Mathematica "remembers" all definitions, In- and Outputs inside a Notebook. As described some
lines before, one can refer to previous terms easily. As long as the Kernel is running, all these defini-
tions are available.
? Random
a = Random@Integer, 100D
b = Random@Integer, 100D
c = Random@Integer, 100D
22
53
a+b+c
78
If you want to delete entires in this memory of definitions, the Clear[Arguments] command can be
used. If you want to clear all kernel memory, i.e. kill and restart the kernel, then go to Evaluation ->
Quit Kernel -> Local. This also terminates running calculations which comes in handy, when you
realize that a calculation seems to have got stuck and you don’t want to wait any longer.
6 introductory_mathematica_8_tutorial_expand.nb
? Clear
Clear@a, b, cD
a+b+c
a+b+c
If one wishes not to quit ther kernel but delete all previous definitions, (of all currently opened Mathe-
matica-Notebooks) the syntax is as follows.
Clear@"Global`*"D
Comments
Comments (i.e. non to be executed, read-only text parts) inside Input-lines can by typed inside round
brackets and stars.
Sqrt@2D
H* Square root of 2 can also be typed via "Ctrl + 2": *L 2
True
Shortcuts
There is a number of shortcuts and hotkeys in order to simplify typing formulas to the notebook.
Some examples:
Syntax Highlighting
When typing formulae in a Mathematica notebook, syntax highlighting is very convenient. Known
Functions, trailing brackets, arguments etc. are recognoized and highlighted; either as positive feed-
back that everything is alright or - usually even more helpful - as negative feedback that there is some
syntactic problem. In case the syntax is faulty, klick on the plus-sign on the right hand side of the
input line to get more information about what might be wrong.
SolveASinAx_ + 3 + CosAx_E = 1, yE
3.14 ´ 3.14
9.8596
?=
d=1
One can surpress the otput of an assignment by adding a colon. This assigment is evaluated not until
the definition is being used somewhere later in the Notebook. This assignment is especially useful
when the right hand side is somewhat elongate and evaluation costs computation time.
8 introductory_mathematica_8_tutorial_expand.nb
? :=
lhs := rhs assigns rhs to be the delayed value of lhs. rhs is maintained in
an unevaluated form. When lhs appears, it is replaced by rhs, evaluated afresh each time.
f := 1
Functions + Pattern
One main powerfulness of CAS such as Mathematica is symbolic calculus, i.e. working with func-
tions. There is one little syntax peculiarity of Mathematica concerning this topic. Objects with argu-
ments in the form f[x] are static; if you want the argument to be variable, then definition of the func-
tion must add an underscore (see Mathematica documentation center under keyword Pattern) to
each argument that should be a variable.
DONT :
h@xD := Sin@xD
h@1D
h@1D
DO :
gAx_E := Sin@xD
g@1D
Sin@1D
As you can see, Mathematica does not necessarily output numerical values. In this case, the result is
an irrational number and one needs to specify further if and what numerical outoput is wanted.
Input/Output III
In order to force numerical output, one needs to type N[Argument,Digits].
introductory_mathematica_8_tutorial_expand.nb 9
N@g@1D, 100D
0.8414709848078965066525023216302989996225630607983710656727517099
919104043912396689486397435430526959
There is also the possibility to append functions and objetcs that operate on the whole expression by
a double slash.
g@1D N
0.841471
2*2
2´2
22
Nested expressions are typed as “with pen and paper” using round brackets.
H1 + H4 - 3L * 2L H1 + 1L + 1
5
2
10 introductory_mathematica_8_tutorial_expand.nb
Clear@"Global`*"D
H* please enter this input to delete all previous definitions
which would partially conflict with the upcoming section *L
E.g.: Differentiation
Just to get used to the syntax we regard a simple example. The differentiation symbol is the capital D:
?D
When we compute the first derivative of a general function F[x], we get the output F'[x]. As described
in the Mathematica documentation, the arguments (function and variable) of this operation are
separated by a comma.
D@F@xD, xD
F¢ @xD
We have to further specify the function to be derived in order to get a calculated output of course. As
an example we define a function G in and compute ther first partial derivative with respecto to one
variable,
GAx_, y_E := x * y
D@G@x, yD, yD
x
introductory_mathematica_8_tutorial_expand.nb 11
D@G@x, yD, x, yD
? Simplify
Simplify@exprD performs a sequence of algebraic and other transformations on expr, and returns the simplest form it finds.
Simplify@expr, assumD does simplification using assumptions.
? FullSimplify
? Expand
E.g.: Polynom
FullSimplify@%D
Expand@%D
? Plot*
System`
PlotRange-
Plot Plot3Matrix PlotJoined PlotLayout PlotPoints Clipping PlotRegion
PlotDivisio- PlotMarke- PlotRange-
Plot3D n PlotLabel rs PlotRange Padding PlotStyle
introductory_mathematica_8_tutorial_expand.nb 13
Plot Functions
8000
6000
4000
2000
2 4 6 8 10
1.0
0.8
0.6
0.4
0.2
-10 -5 5 10
-0.2
14 introductory_mathematica_8_tutorial_expand.nb
-10 -5 0 5 10
Dynamic Graphics
The two major commands for interactice plotting are:
? Dynamic
Dynamic@exprD represents an object that displays as the dynamically updated current value
of expr. If the displayed form of Dynamic@exprD is interactively changed or edited, an assignment
expr = val is done to give expr the new value val that corresponds to the displayed form.
Dynamic@expr, NoneD does not allow interactive changing or editing.
Dynamic@expr, f D continually evaluates f @val, exprD during interactive changing or editing of val.
Dynamic@expr, 8 f , fend <D also evaluates fend @val, exprD when interactive changing or editing is complete.
Dynamic@expr, 8 fstart , f , fend <D also evaluates fstart @val, exprD when interactive changing or editing begins.
? Manipulate
Manipulate@Plot@a * Sin@a * xD x,
8x, -7, 7<, PlotRange ® 8-15, 15<D, 8a, 0, 10, 1<D
15
10
-6 -4 -2 2 4 6
-5
-10
-15
? Graphics
? Show
2.5
2.0
1.5
1.0
0.5
8a, b, c, d<
18 introductory_mathematica_8_tutorial_expand.nb
? Dimensions
Dimensions@vecD
84<
Of course this concept can be used to arbitrarily interlace lists of lists etc.
Dimensions@tensorD
82, 4, 3<
? IdentityMatrix
M := IdentityMatrix@10D
Dimensions@MD
810, 10<
introductory_mathematica_8_tutorial_expand.nb 19
? Table
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
? Subscript
Needs@"VectorAnalysis`"D
CoordinateSystem
Cartesian
one can inquire the momentarily used coordinate system. As mentioned before, this is cartesian by
default. When we change to a spherical coordinate system (remark: “American” coordinate ranges by
default)
SetCoordinates@Spherical@r, Θ, ΦDD
Spherical@r, Θ, ΦD
and compute the divergence of the unit vector in r-direction, (1,0,0), we get the well known result.
Div@81, 0, 0<D
2
r
introductory_mathematica_8_tutorial_expand.nb 21
JacobianMatrix@SphericalD MatrixForm
Just as further example for some functionalities of that package, we calculate the unit volume of a
cylindre with radius and height one by the differential geometric rule as the integral over the Jaco-
bian determinant.
SetCoordinates@Cylindrical@r, Θ, zDD
Cylindrical@r, Θ, zD
Integrate@JacobianDeterminant@CylindricalD,
8r, 0, 1<, 8Θ, 0, 2 * Π<, 8z, 0, 1<D
Just to check, we compute the divergence of the unit vector in x-direction for cartesian coordinates
SetCoordinates@CartesianD
Div@81, 0, 0<D
8a, b, c<
22 introductory_mathematica_8_tutorial_expand.nb
vec.vec
a2 + b2 + c2
With the star * the entries of the list are multiplied pairwise.
vec * vec
9a2 , b2 , c2 =
The cross profuct (vector product) is set with the command Cross.
Cross@vec, vecD
80, 0, 0<
80, 0, 1<
The cross product between two unit base vectors in three dimensions yields the third unit base vec-
tor.
? LinearSolve
We ragard an arbitrary real vector b (filled with random real numbers between 0 and 100)
831, 84, 7, 64, 98, 93, 11, 84, 30, 94, 51, 62, 75, 11, 9<
and solve the system H.x=b, i.e. we want to determine the solution vector x. The matrix H was
defined above in subsection “Automatic Filling of Lists “.
LinearSolve@H, bD MatrixForm
On some computers this may take quite some time since the determinant of this matrix, also known
as Hilbert-Matrix, gets numerically very small with increasing problem size. From the theory of
linear algebra we know that a system of linear equations is only solvable when the determinant is
non-zero. Computationally, matrix inversion gets more and more expensive, the smaller the determi-
nant.
24 introductory_mathematica_8_tutorial_expand.nb
? Det
Det@HD N
1.05854 ´ 10-124
The determinant of the 15-dim. Hilbert-Matrix is “practically” zero. The higher dimensional H, the
smaller gets its determinant and the more difficult gets its inversion.
? HilbertMatrix
HilbertMatrix@nD gives the n´n Hilbert matrix with elements of the form 1 Hi + j - 1L.
HilbertMatrix@8m, n<D gives the m´n Hilbert matrix.
Det@HilbertMatrix@50DD N
1.392615568935140 ´ 10-1466
Clear@"Global`*"D
? Solve
Solve@expr, varsD attempts to solve the system expr of equations or inequalities for the variables vars.
Solve@expr, vars, domD solves over the domain dom. Common choices of dom are Reals, Integers, and Complexes.
in which documentation entry it states already indicatively that it “attempts to solve” the problem
posed. Mathematica has implemented a variety of algorithms that are tried out.
introductory_mathematica_8_tutorial_expand.nb 25
::a ®
1 3
, b® , c ® 1>>
2 2
26 introductory_mathematica_8_tutorial_expand.nb
::a ® - , c ® 1>, :a ® -
1 ä 3 1 ä 3
, b®- , b® , c ® 1>,
2 234 2 234
:a ® , c ® 1>, :a ®
1 3 1 3
, b®- , b® , c ® 1>,
2 234 2 234
Mathematica will list all found solutions also if they sometimes not usable for our purposes because
they are unphysical or violate some other conditions. In this case we frequently have to come back to
pen and paper in order to pick the “right” results.
introductory_mathematica_8_tutorial_expand.nb 27
Mathematica will list all found solutions also if they sometimes not usable for our purposes because
they are unphysical or violate some other conditions. In this case we frequently have to come back to
pen and paper in order to pick the “right” results.
? RandomReal
? MatrixRank
MatrixRank@AD
? Eigenvalues
Eigenvalues@AD
? Eigenvectors
Eigenvectors@AD
? CharacteristicPolynomial
CPAx_E = CharacteristicPolynomial@A, xD
The eigenvalues of a matrix (resp. a linear map) are the zeros of the characteristical polynomial
which we can probe:
CP@Eigenvalues@ADD
Introductory Remarks
Mathematical Entities in Mathematica
As mentioned before, Mathematica has some syntactic idiosyncrasies. As an example we regard one
of the most beautiful formulas in mathematics:
DONT :
e^Hi * piL -1
ei pi -1
DO :
E^HI * PiL -1
True
?E
E is the exponential constant e Hbase of natural logarithmsL, with numerical value > 2.71828.
?I
? Pi
? Infinity
<< PhysicalConstants`
SpeedOfLight
FineStructureConstant
0.00729735
AgeOfUniverse
MagneticFluxQuantum
? Sin
? Log
? Gamma
? DiracDelta
Series
In order to get used to handling series, we examine a frequent application, namely determining the
limit of a series.
Clear@"Global`*"D
H* please enter this input to delete all previous definitions
which would partially conflict with the upcoming section *L
aAn_E = H1 + 1 nL^HnL
1 n
1+
n
or recursively
? Limit
Limit@a@nD, n ® InfinityD
? Sum
i=1
Sum@ f , 8i, imin , imax <, 8 j, jmin , jmax <, …D evaluates the multiple sum â â … f .
imax jmax
i=imin j= jmin
BesselIB0, 2 xF
? BesselI
BesselI@n, zD gives the modified Bessel function of the first kind In HzL.
introductory_mathematica_8_tutorial_expand.nb 33
ãx Gamma@1 + n, xD
n!
and especially useful is also the Series command which expands a function into its Taylor series.
? Series
Series@ f , 8x, x0 , n<D generates a power series expansion for f about the point x = x0 to order Hx - x0 Ln .
SeriesA f , 8x, x0 , nx <, 9y, y0 , n y =, …E successively finds series expansions with respect to x, then y, etc.
x3 x5 x7 x9
x- + - - +
6 120
5040 362 880
x 11 x13 x15
+ - + O@xD16
39 916 800 6 227 020 800 1 307 674 368 000
Clear@"Global`*"D
H* please enter this input to delete all previous definitions
which would partially conflict with the upcoming section *L
? Integrate
Integrate@ f , 8x, xmin , xmax <, 8y, ymin , ymax <, …D gives the multiple integral à d xà
xmax ymax
dy … f .
xmin ymin
34 introductory_mathematica_8_tutorial_expand.nb
Integrate@Sin@xD, xD
-Cos@xD
Integrate@E^Hx^2L, xD
1
Π Erfi@xD
2
? Erfi
Differential Equations
Solving Ordinary Differential Equations (ODEs)
Solving differential equations is another major strenght of CAS. While solving an ODE can require a
sophisticated ansatz and several analytic techniques, Mathematica is a lot faster in giving results
with help of the command DSolve.
Clear@"Global`*"D
H* please enter this input to delete all previous definitions
which would partially conflict with the upcoming section *L
introductory_mathematica_8_tutorial_expand.nb 35
? DSolve
DSolve@eqn, y, xD solves a differential equation for the function y, with independent variable x.
DSolve@8eqn1 , eqn2 , …<, 8y1 , y2 , …<, xD solves a list of differential equations.
DSolve@eqn, y, 8x1 , x2 , …<D solves a partial differential equation.
Whenever Mathematica fails to find a closed analytic solution of an ODE, such as the following
example shows
there is the possibility to look for solutions numerically. Iterative algorithms implemented in Mathe-
matica are capable of finding numerical solutions of ODEs.
36 introductory_mathematica_8_tutorial_expand.nb
there is the possibility to look for solutions numerically. Iterative algorithms implemented in Mathe-
matica are capable of finding numerical solutions of ODEs.
? NDSolve
NDSolve@eqns, y, 8x, xmin , xmax <D finds a numerical solution to the ordinary differential
equations eqns for the function y with the independent variable x in the range xmin to xmax .
NDSolve@eqns, y, 8x, xmin , xmax <, 8t, tmin , tmax <D finds a numerical solution to the partial differential equations eqns.
NDSolve@eqns, 8y1 , y2 , …<, 8x, xmin , xmax <D finds numerical solutions for the functions yi .
The result is output as “InterpolatingFunction” meaning that the function is only known at those
points where the algorithm has provided a numerical result.
1.5
1.0
0.5
2 4 6 8 10 12 14
-0.5
-1.0
-1.5
CAS: Alternatives
Mathematica is a comprehensive computer algebra system, however it is a proprietary product.
There are several free open source alternatives:
Axiom: http://axiom-wiki.newsynthesis.org/FrontPage
Scilab: http://www.scilab.org/
Octave: http://www.gnu.org/software/octave/
Maxima: http://maxima.sourceforge.net/
Wiris: http://www.wiris.com/
Geogebra: http://www.geogebra.org/cms/
Mathematica is a comprehensive computer algebra system, however it is a proprietary product.
There are several free open source alternatives:
introductory_mathematica_8_tutorial_expand.nb 37
Axiom: http://axiom-wiki.newsynthesis.org/FrontPage
Scilab: http://www.scilab.org/
Octave: http://www.gnu.org/software/octave/
Maxima: http://maxima.sourceforge.net/
Wiris: http://www.wiris.com/
Geogebra: http://www.geogebra.org/cms/