Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
395 views

Mathematica With Examples

This document provides an introduction to Mathematica, covering basic arithmetic, notebook structure, built-in functions, shortcuts, and defining functions. It discusses input and output cells, lists, parentheses and brackets, accessing list elements, and suppressing output. It also covers delayed versus immediate assignment, defining inline or pure functions, and functions of one or more variables.

Uploaded by

GGCTTS Books
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
395 views

Mathematica With Examples

This document provides an introduction to Mathematica, covering basic arithmetic, notebook structure, built-in functions, shortcuts, and defining functions. It discusses input and output cells, lists, parentheses and brackets, accessing list elements, and suppressing output. It also covers delayed versus immediate assignment, defining inline or pure functions, and functions of one or more variables.

Uploaded by

GGCTTS Books
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

WOLFRAM

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

In[]:=  find the roots of x^2-5x-6=0


Results (1 of 2)

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

 Semicolon ; is placed at the end of an input to suppress the output e . g .,

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

However, if we use y then it will be evaluated as well.


In[]:= y
Out[]= 8

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

For another example, consider


In[71]:= r1 = RandomReal[] (*this function gives a pseudorandom real number in the range 0 to 1*)

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

The function RandomReal[{a,b}] gives a pseudorandom real number in the range a to b


 A function of a single variable e.g., f(x) = x^2 is defined as

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 &

and its value at x = 4 is given by


In[]:= Function[x, x ^ 2][4]

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

The value of this function at x = 1 and y = 2 is found as


In[]:= f[1, 2]
Out[]= 4

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

 Sometimes we have to remove an assignment or definition e.g., if we set

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

In[82]:= N[2 / (4 + 5)]


Out[82]= 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

 FullForm shows a full form of an expression with no special syntax e.g.,

In[]:= FullFormx ^ 2 y + 3 y z
Out[]//FullForm=

Plus[Times[Power[x, 2], Power[y, Rational[1, 2]]], Times[3, y, z]]


7 | Mathematica with Examples.nb

1. Multiply x2 + x + 1 and x2 - x + 1 and then factor again

In[]:= Expand[(x ^ 2 + x + 1) (x ^ 2 - x + 1)]


Out[]= 1 + x2 + x4

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}}

== is used to write equations.


3. Find the values of x and y if x 2 + y 2 = 1, x + y = 0

In[]:= Solve[x ^ 2 + y ^ 2  1 && x + y  0, {x, y}]


1 1 1 1
Out[]= x  - ,y , x  ,y- 
2 2 2 2

x^2+y^2==1&&x+y==0 may be replaced by {x^2+y^2==1,x+y==0} in the argument

4. Find the the conjugate of z = 2 + 3i


z = 2 + 3 I;
z (* to insert  use Esc + co + Esc *)

Out[88]= 2-3

or use the Conjugate command


5. Find the union and intersection of {1, 2, 3} and {3, 4, 5, 6}
In[]:= Union[{1, 2, 3 }, {3, 4, 5, 6}]
Intersection[{1, 2, 3 }, {3, 4, 5, 6}]
Out[]= {1, 2, 3, 4, 5, 6}

Out[]= {3}

The symbols ⋃ and ⋂ may also be used


6. Convert the cartesian coordinates (1, 1) into polar coordinates
In[58]:= CoordinateTransform["Cartesian"  "Polar", {1, 1}]
π
Out[58]=  2, 
4

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

8. Find the distance between the points P(1, 1, 1) and Q(1, 2, 3)


In[]:= EuclideanDistance[{1, 1, 1}, {1, 2, 3}]
Out[]= 5

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.

In[]:= BaseForm[2^^1100111 + 7^^251624, 8]


Out[]//BaseForm=

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[]:= Functionx, x2 + 1 /@ {1, 2, 3}

Out[]= {2, 5, 10}

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}}

In[]:= Map[(#〚1〛 + I #〚2〛) &, %]


Out[]= {0, 1, 1 + , }
9 | Mathematica with Examples.nb

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 limx1 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[]:= x1
x-1
Out[]= 2

2. Evaluate limx3- x-13 - 1


x -3
(Left Limit)

1 1
In[14]:= f[x_] := - ;
x-3 Abs[x - 3]
Limit[f[x], x  3, Direction  1]
Out[15]= -∞

or
In[16]:= x3- f[x]
Out[16]= -∞

3. Evaluate limx1+  x-21 (Right Limit)


x -1

x-1
In[17]:= f[x_] := ;
Sqrt[x ^ 2 - 1]
Limit[f[x], x  1, Direction  - 1]
Out[18]= 0

or
In[20]:= x1+ f[x]
Out[20]= 0
Mathematica with Examples.nb | 10

4. Evaluate limx∞ a^x for a >1, a = 1 and 0 < a < 1


In[]:= f[x_] = a ^ x;
Limit[f[x], x  Infinity, Assumptions  a > 1]
Limit[f[x], x  Infinity, Assumptions  a  1]
Limit[f[x], x  Infinity, Assumptions  0 < a < 1]
Out[]= ∞

Out[]= 1

Out[]= 0

5. Find the limit superior and limit inferior of 1 / x as x  0


f[x] := 1 / x
MaxLimit[f[x], x  0] (*or use  for limit superior (by Esc + Mlim + Esc)*)

MinLimit[f[x], x  0] (*or use  for limit inferior (by Esc + mlim + Esc)*)

Out[95]= ∞

Out[96]= -∞

cos x x≤0
6. Evaluate limx0 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 limx0 x 3 ⌊x ⌋
where ⌊x ⌋ is the floor or bracket function also written as [x]

In[28]:= f[x_] := x ^ 3 * Floor[1 / x]


Limit[f[x], x  0]
Out[29]= 0
1
8. Evaluate limx0 x 3 ⌈x ⌉
where ⌈x ⌉ is the ceiling function

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 limx0 limy0 f (x, y ) and limy0 limx0 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

For the limit of a sequence use DiscreteLimit


or use Esc + dlim + Esc to insert ∞ 

11. Find the limit of the sequence n  n+1 - n

In[]:= an = Sqrt[n] (Sqrt[n + 1] - Sqrt[n]);


DiscreteLimit[an , n  Infinity]
1
Out[]=
2

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 ∞  *)

DiscreteMinLimit[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

In[]:= bk = Sum[1 / n ^ 2, {n, 1, k}];


DiscreteLimit[bk , k  ∞]
π2
Out[]=
6

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

You might also like