Mathematica With Examples
Mathematica With Examples
MATHEMATICA
W
MM
Examples
BS
Mathematics/Physics
Ba
Department of Mathematics
Government Graduate College
Toba Tek Singh
www.ggcttsingh.edu.pk
shahzadggctts@gmail.com
CONTENTS
Introduction 1
Limits 9
Derivatives 12
Vectors and Matrices 15
Function Visualization 18
Integrals 24
Numerical Methods 26
Differential Equations 32
Group Theory 37
Books:
1. Mathematica by Example
2. Hands-on Start to Wolfram Mathematica
1 | Mathematica with Examples.nb
Introduction
Basic arithmetic operations are performed in an ordinary way: +, - , * and / respectively are
used for addition, subtraction, multiplication and division. A space between two symbols also
multiplies. For example, to multiply 2 and 3, write 2*3 or 2 3.
Press Shift + Enter to execute a Mathematica input. To abort an evaluation, press Alt + . in
windows and command + . in mac or choose Abort Evaluation from the Evaluation menu.
Mathematica notebook is divided into input, output and group cells shown of the right side of
notebook in the form of brackets ]. The hierarchy of cells serves as a structure for organizing
the information in a notebook.
Enter(return) key inserts a new line in a cell. It is possible to put two or more Mathematica
commands in the same input cell; each command begins on a new line. e.g.,
A single command can be extended over several lines if necessary by using the Enter(return)
key, but this is unnecessary because Mathematica will automatically begin a new line if
required.
To create a new cell, move the pointer in the notebook window until it becomes a horizontal I-
beam then click and start typing.
To divide a cell into two cells, place the cursor anywhere in the cell, right click and choose
Divide Cell. The cell shall be divided at that point.
To merge two or more cells, select the cells to be merged and right click on any of them and
choose Merge Cells.
Built-in functions or commands start with upper case letters, such as Sin, Cos, Integrate,
Plot, etc. It is recommended to write the first letter of a user defined functions in lower case.
Function arguments are written in square brackets [ ], e.g., Sin[x], Cos[π], Log[5], etc.
Lists are enclosed by braces { } e.g., a = { 1, 4, 5}. List elements are accessed via [[ ]], e.g.,
a[[3]] refers to the third element of list a. This means that order in { } is important.
In[]:= a = {1, 4, 5}
Out[]= {1, 4, 5}
In[]:= a〚3〛
Out[]= 5
Parentheses( ) are used for grouping and have the ordinary meaning of brackets, e.g.,
In[]:= (2 + 3) / 4
5
Out[]=
4
Mathematica with Examples.nb | 2
For a list of shortcut keys search Keyboard Shortcut Listing in wolfram documentation. To open
wolfram documentation press F1 or choose Wolfram Documentation from the Help menu.
Some shortcut keys are:
Alt + ] matching [ ]
Alt + Shift + ] matching { }
Alt + Shift + 0 matching ( )
Ctrl + 6 superscript and exponent
Ctrl + - subscript
Ctrl + / quotient
Shift + 6 ^ exponent
Ctrl + 2
Ctrl + 2, Ctrl + 5
Esc + alpha + Esc α
Esc + i + Esc ι(iota) or use I
Esc + pi + Esc π or use Pi
Esc + ee + Esc or use E
Esc + elem + Esc ∈
Esc + mem + Esc ∋
Esc + un + Esc ⋃
Esc + inter+ Esc ⋂
For Alt key use command key in mac.
For an input in symbolic form choose a palette e.g., Basic Math Assistant or Classroom Assis-
tant, etc. from the Palettes menu.
The Wolfram Language has integrated features for accepting input in natural language and in
other forms that require semantic understanding for interpretation. Using = at the beginning of
an input specifies a complete computation using natural language e.g., typing
= find the roots of x^2 -5x - 6=0
and pressing Enter or Shift + Enter will show the following
Reduce[- 6 - 5 * x + x ^ 2 0, x]
Out[]= x - 1 || x 6
More details can be seen by clicking the plus icon at the top right corner of the above box. The
line under Results shows wolfram language input. Clicking this line shall replace the cell with
this input.
= works at the beginning of a cell only. In contrast, pressing ctrl + = works anywhere in a cell.
Another way is to use WolframAlpha which is a computational search engine. Use == at the
beginning of a query or use the command WolframAlpha to send the query to WolframAlpha
and import the output. Usually, a step by step solution is also available this way.
3 | Mathematica with Examples.nb
a = 2; (*semicolon prevents mathematica from printing an output cell with the value 2*)
The operator = (set) which is an Immediate assignment is used to define functions and assign
values to variables. Its right side is immediately evaluated and the value is assigned to the
expression on its left side.
The operator := (set delayed) which is a delayed assignment is also used to define functions
and assign values to variables. Its right side remains unevaluated until the expression on its left
side is used and is evaluated afresh each time when it is used.
For example, if we set a=2 and define x and y as follows and generate the output, we see that
only x has been evaluated here while y remains unevaluated.
In[]:= a = 2; (*use of ; does not print the value of a in the output*)
x = a+5
y := a + 6
Out[]= 7
Now, if we change the value of a and see the values of x and y, the value of x remains the same
as previously evaluated whereas y is being evaluated again according to the new value of a
In[]:= a = 3;
x
y
Out[]= 7
Out[]= 9
Out[71]= 0.75691
r1 is evaluated immediately and keeps this value. Each time we use r1, it will give the same value
In[74]:= r1
Out[74]= 0.75691
However, if we define
In[]:= r2 := RandomReal[]
r2 is unevaluated. Each time we use r2, it is evaluated and can give a different value
In[]:= r2
Out[]= 0.611717
In[]:= r2
Out[]= 0.719797
Mathematica with Examples.nb | 4
f[x_] = x ^ 2
or
In[]:= f[x_] := x ^ 2
Here, f is the function name. x with _ (underscore or blank) is called a pattern which can be
replaced by any value e.g.,
In[]:= f[4]
Out[]= 16
In the above example, we have used the “name” f of the function to specify the function. It is
often inconvenient to have to explicitly name a function for every small operation that we wish
to perform. The Wolfram Language lets declare functions inline (called pure or anonymous
functions) to get around this. Pure functions allow us to give functions which can be applied to
arguments, without having to define explicit names for the functions i.e., we do not need to give
the function a name to use it. The function f(x) = x^2 in the form of a pure function is given by
Function[x, x ^ 2]
or
#^2 &
16
or
In[]:= # ^ 2 &[4]
Out[]= 16
A function of two or more variables can be defined in a similar way e. g., f (x, y) = 2 x y is defined
as
In[]:= f[x_, y_] := 2 x y
or
Function[{x, y}, 2 x y]
or
2 #1 #2 &
although pure functions do not require separate definitions or a names, we can name a pure
function e.g.,
In[]:= g := Function[{x, y}, 2 x y]
or
In[]:= h := 2 #1 #2 &
5 | Mathematica with Examples.nb
or
In[]:= Function[{x, y}, 2 x y][1, 2]
Out[]= 4
or
In[]:= 2 #1 #2 &[1, 2]
Out[]= 4
or
In[]:= g[1, 2]
Out[]= 4
or
In[]:= h[1, 2]
Out[]= 4
In[]:= x = 2;
and then integrate a function e.g., sin x with respect to x, we don’t get the required output
In[]:= Integrate[Sin[x], x]
Integrate : Invalid integration variable or limit (s ) in 2.
Out[]= Sin[2] 2
We can use the Clear command or =.(unset) to remove any variable definitions, Clear[x,y]
clears the values stored by variables x and y, and a=. removes the definition of a.
Now, if we clear the value of x in the above example and then integrate, we get the required
result
Clear[x] (* or x=. *)
Integrate[Sin[x], x]
Out[]= - Cos[x]
Clear clears the definition of a symbol but not necessarily all of the information associated
with it. In almost every case, Clear is sufficient but ClearAll clears all of the information
associated with the symbol. It is better to use ClearAll instead of Clear.
Use the command Clear["Global`*"] or Remove["Global`*"] to clear all definitions(these
two commands are not exactly the same).
Use the Quit command or choose Quit Kernel from the Evaluation menu to quit the kernel,
which clears everything from the kernel.
Mathematica with Examples.nb | 6
Local variables need not to be cleared. To create local variables Module is commonly used.
Module takes two arguments: the first is a list of variables that are local to that statement, and
the second is a calculation or list of operations e.g.,
In[]:= Module[{x = 5, y = 2}, x + y]
Out[]= 7
There is no need to clear the values of x and y to use in future because this assignment of values
does not travel outside of the Module command.
A symbol used for pattern matching does not need to be cleared. For example, if
In[]:= x=5
Out[]= 5
and
In[64]:= f[x_] := x ^ 2
we can evaluate f at any point. The variable x, which is currently defined as having the value 5,
is treated differently than the pattern x_. So there is no need to clear x before evaluating f at
any point.
However, the symbol f is to be cleared if f is used elsewhere in a notebook or Mathematica
session as a global variable.
When Mathematica does not recognize a name, it will be displayed in bright blue color. If the
name is not recognized, it could be because the command name may be misspelled (such as for
a Wolfram Language function) or because the symbol may not yet be defined (for user-defined
functions and variables)
Mathematica gives exact values e.g., (3 + 4)/5 gives 7/5. To get an approximate value, use . with
any of the numbers 3, 4 and 5 or use the N command e.g.,
In[81]:= 2. / (4 + 5)
Out[81]= 0.222222
the Postfix operator, which has the shorthand symbol //, can be used to apply N
In[83]:= 2 / (4 + 5) // N
Out[83]= 0.222222
The N command also accepts an optional second parameter, which specifies the desired
number of digits of precision to be returned by the approximation e.g.,
In[80]:= N[Pi, 50]
Out[80]= 3.1415926535897932384626433832795028841971693993751
In[]:= FullFormx ^ 2 y + 3 y z
Out[]//FullForm=
In[]:= Factor[%]
Out[]= 1 - x + x2 1 + x + x2
% gives the last result generated, %% gives the result before last and so on
2. Solve x 3 - 2 x + 1 = 0
Solve[x ^ 2 - 2 x + 1 0, x] "or use Reduce"
Out[]= {{x 1}, {x 1}}
Out[88]= 2-3
Out[]= {3}
7. Convert the cartesian coordinates (1, 1, 1) into cylindrical and spherical coordinates
In[53]:= CoordinateTransform["Cartesian" "Cylindrical", {1, 1, 1}]
CoordinateTransform["Cartesian" "Spherical", {1, 1, 1}]
π
Out[53]= 2 , , 1
4
π
Out[54]= 3 , ArcTan 2 ,
4
Mathematica with Examples.nb | 8
9. Find the distance between the point P (0, 0) and the line 3 x + 4 y + 10 = 0
In[]:= reg = ImplicitRegion[3 x + 4 y + 10 0, {x, y}];
RegionDistance[reg, {0, 0}]
Out[]= 2
10. Find the distance between the point P (1, 3, 6) and the plane x + 4 y - 4 z + 7 = 0
In[]:= reg = ImplicitRegion[x + 4 y - 4 z + 7 0, {x, y, z}];
RegionDistance[reg, {1, 3, 6}]
4
Out[]=
33
11. Add 110 111 2 and 251 624 7 and show the answer in octal system.
1324518
12. Apply f(x) = x 2 + 1 separately to each of the elements of the list {1, 2 , 3}
f[x_] := x2 + 1;
Map[f, {1, 2, 3}] (*Map[f][{1,2,3}] is also possible*)
Out[]= {2, 5, 10}
or
f[x_] := x2 + 1;
f /@ {1, 2, 3}
Out[]= {2, 5, 10}
or
In[]:= (# ^ 2 + 1) & /@ {1, 2, 3}
Out[]= {2, 5, 10}
or
In[]:= Functionx, x2 + 1 /@ {1, 2, 3}
13. Convert the list of complex numbers {0, 1, 1 + i, i} into the list of ordered pairs and then
convert back again
In[]:= Map[{Re[#], Im[#]} &, {0, 1, 1 + I, I}]
Out[]= {{0, 0}, {1, 0}, {1, 1}, {0, 1}}
Limits
To find the limit of a function f (x ) as x a, use Limit and write x a as its second argument
or press Esc + lim + Esc to insert and write x a as a subscript
2
1. Evaluate limx1 xx--11
x^2 - 1
In[11]:= f[x_] := ;
x-1
Limit[f[x], x 1]
Out[12]= 2
or
x^2 - 1
In[]:= Limit , x 1
x-1
Out[]= 2
or
In[13]:= Limit[f[x], x 1]
Out[13]= 2
or
x^2 - 1
In[]:= x1
x-1
Out[]= 2
1 1
In[14]:= f[x_] := - ;
x-3 Abs[x - 3]
Limit[f[x], x 3, Direction 1]
Out[15]= -∞
or
In[16]:= x3- f[x]
Out[16]= -∞
x-1
In[17]:= f[x_] := ;
Sqrt[x ^ 2 - 1]
Limit[f[x], x 1, Direction - 1]
Out[18]= 0
or
In[20]:= x1+ f[x]
Out[20]= 0
Mathematica with Examples.nb | 10
Out[]= 1
Out[]= 0
MinLimit[f[x], x 0] (*or use for limit inferior (by Esc + mlim + Esc)*)
Out[95]= ∞
Out[96]= -∞
cos x x≤0
6. Evaluate limx0 f (x ) where f (x ) = (Limit of a Piecewise Function)
1-x x>0
f[x_] := Piecewise[{{Cos[x], x ≤ 0}, {1 - x, x > 0}}];
Limit[f[x], x 0]
Out[]= 1
1
7. Evaluate limx0 x 3 ⌊x ⌋
where ⌊x ⌋ is the floor or bracket function also written as [x]
f[x_] := x ^ 3 * Ceiling[1 / x]
Limit[f[x], x 0]
Out[27]= 0
5-x 2
9. Evaluate lim(x,y)(0,0) 4+x +y
(Limit of a Function of Two Variables)
5 - x^2
f[x_, y_] :=
4+x+y
Limit[f[x, y], {x, y} {0, 0}]
5
Out[24]=
4
or
In[23]:= {x,y}{0,0} f[x, y]
5
Out[23]=
4
11 | Mathematica with Examples.nb
x2
10. Find the iterated limits limx0 limy0 f (x, y ) and limy0 limx0 f (x, y ) of f(x, y) = x^2+y^2
( these limits are not defined in the same way as the above limit)
x^2
In[38]:= f[x_, y_] :=
x^2 + y^2
Limit[f[x, y], {x 0, y 0}]
Limit[f[x, y], {y 0, x 0}]
Out[39]= 1
Out[40]= 0
or
In[]:= n∞ an
1
Out[]=
2
12. Find the limit superior and limit inferior of the sequence (-1)n
an = (- 1)n ;
DiscreteMaxLimit[an , n ∞] (* or use Esc + dMlim + Esc to enter the template ∞ *)
Out[109]= 1
Out[110]= -1
Limit of a convergent series may be found as the limit of sequence of partial sums
∞
1
13. Find the limit of ∑n=1
n2
or as an infinite sum
Sum[1 / n ^ 2, {n, 1, Infinity}]
π2
Out[]=
6
SumConvergence tests a series for convergence or divergence & gives condition(s) for convergence