Post on 02-Jun-2018
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
1/47
Tutorial
on
Symbolic Computing
withMathematica
Youngjoo Chung
School of Info. and Comm., GIST
ychung@gist.ac.kr
2011. 7. 7
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
2/47
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
3/47
The concept of Mathematicais to create once and for all a single system that could handle
all the various aspects of technical computing in a coherent and unified way.
Manipulation of the very wide range of objects involved in technical computing using only
a fairly small number of basic primitives (over 3,000 built in the kernel of version 7). The functionality can be easily extended through user-defined functions and external
programs.
Platform-independent interactive documents known as notebooks (.nb files)
System-provided macros and user-defined macros in packages (.m files)
Interactive and non-interactive sessions
Communication with external programs using MathLink
Mathematicacomputing environment
TCP/IP
mle.exeMathLink
Standard Packages
Mathematica
Kernel
Windows/Unix
Platform
Windows/Unix
Platform
Add-On Packages
User Packages
Other Packages
User Frontend
Numerical
Analysis
Control
Instrumenta
External applications are written in C/C++.
Mathematicais used for both data input and output.
External applications andMathematicacommunicate via MathLink.
The functionality can be extended through user-defined functions and external programs.
More information available at http://www.wolfram.com
Mathematica Tutorial.nb 3
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
4/47
Algebraic Calculation
Algebraic Calculations (MathematicaTutorial)
Symbolic Calculations (MathematicaTutorial)
Expand
Factor
Apart
Together
Simplify
Basic Algebra
Polynomial Algebra (MathematicaGuide)
Polynomial Systems (MathematicaGuide)
Elementary Functions
Elementary Functions (MathematicaGuide)
Trigonometric Functions
Trigonometric Functions (MathematicaGuide)
Trigonometric Expressions (MathematicaTutorial)
Complex Variables
Complex Numbers (MathematicaGuide)
Functions of Complex Variables (MathematicaGuide)
Flow Control
Conditionals (MathematicaGuide)
Loops and Control Structures (MathematicaTutorial)
Flow Control (MathematicaGuide)
Formula Manipulation
4 Mathematica Tutorial.nb
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
5/47
Formula Manipulation
Formula Manipulation (MathematicaGuide)
Manipulating Equations (MathematicaGuide)
User Package Functions
Mathematica provides a solid foundation for symbolic computing and a number of support-
ing functions.
Built-in kernel functions
User-defined functions to complement the built-in kernel functions
A mechanism is needed to facilitate the algebraic manipulation of mathematical expressionswith the following properties:
Seamless integration with the computing environment of Mathematica
Selective targeting of the object to apply functions
Improved handling of subscripts (a1), tildes (), hats (), etc.
On-line setting and clearing of attributes
Addition of comments
Improved handling of derivatives, integrals and summations
Minimal use of variables
Algebraic manipulation of formulas using symbolic computing
Mathematica is not a word processor or an equation editor.
Application of functions
Substitution using mathematical identities
Allows focusing on the principles instead of time-consuming and error-prone calculations.
Good readability
Minimization of human errors during calculations
Expressions that closely resemble the traditional mathematical style, e.g., subscripts and
vector notations
A large collection of user-defined functions
Basic Algebra
Differentiation
Integration
Summation
Trigonometric Functions
Complex Variables
Vectors and Matrices Polynomials and Series
Functional Analysis
Equations
Operator Analysis
Plotting
Mathematica Tutorial.nb 5
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
6/47
A large collection of user-defined functions
Basic Algebra
Differentiation
Integration
Summation
Trigonometric Functions
Complex Variables
Vectors and Matrices
Polynomials and Series
Functional Analysis
Equations
Operator Analysis
Plotting
Etc.
MPMAF
An abbreviation ofMPMapApplyFunc
The platform for algebraic manipulation of all or parts of an expression
User-defined functions
The function names start with the prefix MP.
Algebraic manipulation of expressions
Mathematical identities Can be used separately independent of the macro MPMAF, in which case the features
provided by the options of MPMAFcannot be used.
Basic Algebra
Expansion
ExpandAHx+ aL2E
a2 +2 a x+ x2
The power exponent must be an integer. Otherwise, useMPExpandBinomial.
MPExpandBinomial@Hx+aLn, kDMPEvalSum@%D
k=0
n ak x-k+n n !
k !H- k +nL!Ha+ xLn
6 Mathematica Tutorial.nb
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
7/47
MPExpandBinomial@Ha+ b +cLn, k, lDMPEvalSum@%DPowerExpand@%D
k=0n
l=0-k+n ak bl c-k-l+n n !
k !l !H- k -l + nL!
cnb +c
c
na+ b +c
b+ c
n
Ha+ b +cLn
Factoring
FactorAx2 +Ha+ bLx +a bEHa+ xL Hb +xL
MPFactor
MPFactor@a+ b, aD
a 1+b
a
Reduction of fractions
x2
x+ a
Hx +aL2 -a2 - 2 aHx+ aL +2 a2
x+ a
x2
a+ x
- a2 -2 a x +Ha+ xL2a+ x
ApartB x2
x+ a, xF
- a +x +a2
a+ x
MPApart
MPApartB sx2 ex
n2 e0 - ex+
sy2 ey
n2 e0 - ey+
sz2 ez
n2 e0 - ez,9ex, ey, ez=F
%. - sx2 - sy2 -sz2 - 1
- sx2 - sy
2 -sz2 -
n2 sx2 e0
- n2 e0+ ex
-
n2 sy2 e0
- n2 e0+ ey
-n2 sz
2 e0
- n2 e0+ ez
- 1 -n2 sx
2 e0
- n2 e0+ ex
-
n2 sy2 e0
- n2 e0+ ey
-n2 sz
2 e0
- n2 e0+ ez
Separation of variables for solving differential equations
Mathematica Tutorial.nb 7
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
8/47
y
x 2 x y
MPMAFB%, MPSepVars,8All, x, Side Right,
MPEvalInt, All,
MPSolve,8All, y
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
9/47
MPTrigToDoubleACos@xD2, CosE1
2H1+ Cos@2 xDL
Complex Variables
Asterisk (*) is interpreted as the complex conjugate
MPComplexExpandAHu+vL*, u, TargetFunctions ConjugateE
v+u*
The built-in function ComplexExpand gives a rather different (even though equivalent)
result.
ComplexExpand@Conjugate@u+vD, u, TargetFunctions ConjugateD
v+
1
2Hu+Conjugate@uDL
CoshB 12
Hu -Conjugate@uDLF- v+ 12 Hu+Conjugate@uDL SinhB 12
Hu- Conjugate@uDLF
The exponential form of a complex number
MPComplexToExp@x+ yD
ArcTanA y
xE
x2 +y2
Polynomials and Series
Eliminate the second highest order term
MPMergePolyAx3 +a x2 +c, xE
-a3
27+c -
a2 x
3+
a
3+x
3
Transform a polynomial by making a replacement of the variable
MPTransPolyAx3 +a x2 +c, x, a +xE
c+ a2 Ha+ xL- 2 aHa +xL2 +Ha +xL3
A case of two variables
MPTransPolyAx2 Hy +aL,8x, y
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
10/47
MPTaylorB 1 +x , x, 3F
1+x
2-
x2
8+
x3
16
MPTaylorB 1 +Hx+ DxL2 , Dx, 1F
1 +x2 +xDx
1+ x2
MPAFE@y@xD, MPTaylor,8All, x- x0, 2, Variables x
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
11/47
Differentiation
The built-in derivative operator D, or , evaluates the expression immediately.
D@f@xD, xDf@xD
The total derivative operator Dtassumes all symbols are dependent variables.
Dt@a f@xD, xDDt@a, xDf@xD+ a f@xD
The partial differential operator
xdelays the evaluation until the commandMPEvalDor
MPExpandDis encountered.
y 3
x2 yHa f +b gL
MPMAF@%, MPExpandD,8At@2D,8f, g
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
12/47
Transformation of variables
MPTransDB 2 f
w2, VT :w, l, w 2pc
l>F
l3
2 c2 p2
f
l+
l4
4 c2 p2
2 f
l2
Evaluation of vector operators
MPAFEA2 y, MPEvalVecOps,8All, y y@r, jD, Cylindrical@r, j, zDF
2 y 1
r
y
r+
2 y
r2+
1
r2
2 y
j2
2 y 1
r2
2 y
j2+
1
r
rr
y
r
MPAFEA2 y, MPEvalVecOps,8All, y y@x, y, zD, Cartesian@x, y, zD
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
13/47
F HF f x
F f x
Combining multiple integrals
KHF f@xD xO KHF g@xD xOMPCombInt@%, RV 8x, x
:f@xD F@kD k x k, g@xD G@kD k x k>
HF F@kDG@kDk x k
MPMAFB%, RA,:All,:F@kD 12p
f@xD -k x x, G@kD 12p
g@xD-k x x>>,
MPCombInt,8All, RV
8x, x
, x
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
14/47
MPAFEBHF -
1
x2 +a2 x, MPTransInt,
8All, VT 8x, q, x a Tan@qD
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
15/47
MPChSumIntervalBHF n=-
an,8n,81, 2
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
16/47
Expansion of A
MPExpandVecA A, AE
I A
M
- 2 A
Deleting a column from a matrix
Table@i j,8i, 3
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
17/47
MPVecToCart@80, 0, 1F
y@xD+ y@xD
g@sD+g
@s
D2 sA case of two variables
x,x f@x, yD- y,y f@x, yDMPTransEqB%, f@x, yD g@x, hD,
VT :8x, y, Apply SimplifyF
- fH0,2L@x, yD+ fH2,0L@x, yD
gH1,1
L@x, hD Abbreviation of derivatives
MPAbbrevFApH0,1L@x, tD -2 a pH1,0L@x, tDqH1,0L@x, tD, Deriv TrueEpt- 2 a px q x
The subscripts denote the derivatives. The function arguments can be restored using MPRe-
storeFunctions.
MPRestoreF@pt - 2 a pxqx,8p, q
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
18/47
MPInsideFunc@f@a xD+ a A, CurlH, aD Ha AL+ f@a xD
Merging arguments of a linear function
MPMergeFunc@a f@x, zD+ b f@y, zD, fDf@a x+ b y, zD
Operator Analysis
Expansion of operator expressions
MPExpandBraket@HXa + Xb LAH a\ + b\L, AD
Xa A a\+ Xa A b\+ Xb A a\+ Xb A b\
MPCommutatorB Ql0 2
+ l0
2
P,Q
l0 2
- l0
2
PFMPExpandOp@%,8Q, P
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
19/47
The terminal velocity, v0, can be found from the equation of motion as t ; when there is
no acceleration, v
= 0, so
m v
m g- b v 2
MPMAFA%
, RA,9All,9v
0, v
v0==,MPSolve,8All, v0, - 1,MPSepVars,8All, t, Times, Side Right
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
20/47
vi
v 1
g- b v2
m
v 1 t
v g m
bTanhB b g
mt +ArcTanhB b
g mviFF
v g m
b
vi+ g m
bTanhB b g
mtF
g m
b+vi TanhB b g
mtF
v
v0Ivi+ v0 TanhA tTEM
v0+ vi TanhA tTE
where
:v0 g mb
, T v0
g>
Verify that our solution satisfies the originial equation of motion:
MPAFEBv, MPExpandD,:All, v v0Ivi + v0TanhAt
TEM
v0 + viTanhA tTE
, OverDot t>, Apply SimplifyF
MPMAFB%, MPTrigConvert,8At@2D, Tanh, Apply Simplify,
RR,:At@2D,:v0 g mb
, T v0
g>>, Apply ExpandF
v
v0Iv02 - vi2M
TIv0 CoshA tTE +viSinhA t
TEM2
v
- v2 +v0
2
T v0
v
g-b v2
m
that is, Newton's equation of motion.
Substituting the numerical data, v0and Tare
20 Mathematica Tutorial.nb
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
21/47
:v0 g mb
, T v0
g>
MPMAFB%, MPEli,8All, v0, 1, Keep True>, Apply PowerExpand,
Convert,:At@1, 2D, MileHour
>F
:v0 g mb
, T v0
g>
:v0 0.989949 MeterSecond
, T 0.101015 Second>
:v0 2.21445 Mile
Hour, T 0.101015 Second>
Plot vHtL.
Mathematica Tutorial.nb 21
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
22/47
v g m
b
vi + g m
bTanhB b g
mtF
g m
b+viTanhB b g
mtF
MPMAFB%, RA,:At@2D,:g 9.8 MeterSecond2
, b 700Kilo Gram
Meter,
m 70 Kilo Gram, vi 60Mile
Hour, t t Second>>, Apply PowerExpand,
MPDivFrac,:At@2D, MileHour
>,
Convert,:: Hour MeterMile Second
, 1>,:At@2D, MileHour
>>,
MPDivEq,:All, MileHour
>,Plot,:At@2D,8t, 0, 0.1, Take LastF
v g m
b
vi+ g m
bTanhB b g
mtF
g m
b+vi TanhB b g
mtF
Hour v
Mile
2.21445H60 +2.21445 Tanh@9.89949 tDL
2.21445+ 60 Tanh@9.89949 tD
0.00 0.02 0.04 0.06 0.08 0.10tHSecondL
10
20
30
40
50
60
vMile
Hour
22 Mathematica Tutorial.nb
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
23/47
Series Solution of Differential Equations
We apply the method of series soution to the linear (classical) oscillator equation.
(9.84)
2 y
x2+ w2 y 0
with known solutions y = sinHwxL, cosHwxL.
We try the series solution of the form
(9.85)
y
@x
D xk
Ia0 + a1 x +a2x
2 +a3x3 +
M HF
l=0
alxk+l
y@xD xk I +a0+ x a1+ x2 a2+ x3 a3M l=0
al xk+l
with a0 0 and the exponent kand all the coefficients alstill undetermined.
By substituting (9.85) into Eq. (9.84), we have
(9.86)
2 y
x2+ w2 y 0
MPMAFB%, RA,:At@1D, y l=0
alxk+l>, Apply MPEvalDF
yw2 +2 y
x2 0
w2 l=0
xk+l al+l=0
x-2+k+l H- 1 +k + lL Hk + lLal 0
which gives
Mathematica Tutorial.nb 23
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
24/47
w2 l=0
xk+l al+l=0
x-2+k+l H- 1 +k+ lL Hk+ lLal 0MPMAFA%, MPShiftSum,9AtA1, x-2+k+lE,8l, 2
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
25/47
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
26/47
y Sin@xwDa0
w
From (9.91) and (9.94), the general solution can be put
y c1 Cos@xwD +c2Sin@xwD
26 Mathematica Tutorial.nb
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
27/47
MathematicaLanguage
Language Overview (MathematicaGuide)
Structure of MathematicaExpressions
Expressions (MathematicaTutorial)
Constants
Numbers (MathematicaOverview)
Types of Numbers (MathematicaTutorial)
Integer (Built-in MathematicaSymbol)
Rational (Built-in MathematicaSymbol)
Real (Built-in MathematicaSymbol)
Complex (Built-in MathematicaSymbol)
Mathematical Constants (MathematicaGuide)
Variables
Defining Variables (MathematicaTutorial)
Eliminating Variables (MathematicaTutorial)
Patterns
Patterns (MathematicaGuide)
Introduction to Patterns
Verbatim Patterns
Example
In four-wave mixing, two waves of frequencies w1and w2interact and generate additional
frequencies through nonlinear mixing. Suppose the two waves are given by
Mathematica Tutorial.nb 27
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
28/47
Ei 1
2IEi - wi t + Ei* witM
for i = 1, 2, and the nonlinear process is due to the third-order Kerr effect:
PNL e0 c3 E3
Find out what frequencies are present in the output wave near w1 w2.
The field is given by
E HF i=1
2
Ei
MPMAFB%, RA,:At@2D, Ei 12
IEi - wit + Ei* wi tM>,MPEvalSum, At@2DF
E i=1
2
Ei
E 1
2i=1
2
I-t wi Ei+ t wi HEiL*M
E 1
2I-t w1 E1+ -t w2 E2+ t w1 HE1L* + t w2 HE2L*M
and the nonlinear mixing produces
PNL e0 c3 E3
MPMAFB%, RA,:At@2D, E 12
I-t w1 E1 + -t w2 E2 + t w1 HE1L* + t w2 HE2L*M>,Expand, At@2, ED, FactorExp tF
PNL E3 e0 c 3
PNL 1
8e0 c 3I-t w1 E1+ -t w2 E2+ t w1 HE1L* + t w2 HE2L*M3
PNL
1
8 e0 c 3J-3 t w1
E
1
3
+3
t
H-2w1-w2
L E
1
2
E
2+ 3
t
H-w1-2w2
L E
1 E
2
2
+
-3 t w2
E
2
3
+3
-t w1
E
1
2
HE
1L*
+
6-t w2 E
1 E
2HE1L* +3 tHw1-2w2L E22 HE1L* +3 t w1 E1IHE1L*M2 +3 tH2w1 -w2L E2IHE1L*M2 +3 t w1 IHE1L*M3 + 3tH-2w1+w2L E12 HE2L* +6 -t w1 E1 E2HE2L* +3 -t w2 E22 HE2L* +6 t w2 E
1HE1L* HE2L* +6 t w1 E2HE1L* HE2L* +3 tH2w1+w2LIHE1L*M2 HE2L* +
3 tH-w1 +2w2L E1IHE2L*M2 +3 t w2 E2IHE2L*M2 + 3tHw1+2w2LHE1L* IHE2L*M2 + 3 t w2 IHE2L*M3N
The frequencies are
28 Mathematica Tutorial.nb
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
29/47
PNL 1
8e0 c3J-3 t w1 E13 +3 tH-2w1-w2L E12 E2 + 3 tH-w1-2 w2L E1 E22 + -3 t w2 E23 +3 -t w1 E12 HE1L* +
6-t w2 E
1 E
2HE1L* +3 tHw1-2w2L E22 HE1L* +3 t w1 E1IHE1L*M2 + 3tH2w1 -w2L E2IHE1L*M2 +3 t w1 IHE1L*M3 + 3tH-2w1 +w2L E12 HE2L* +6 -t w1 E1 E2HE2L* +3 -t w2 E22 HE2L* +6t w2 E1HE1L* HE2L* +6 t w1 E2HE1L* HE2L* +3 tH2w1+w2LIHE1L*M2 HE2L* +3tH-w1 +2 w2L E1IHE2L*M2 +3 t w2 E2IHE2L*M2 + 3tHw1+2w2LHE1L* IHE2L*M2 + 3 t w2 IHE2L*M3N
MPMAFA%, frequencies Cases@, a_,80,
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
30/47
f@100D93326215443944 152 681 699 238 856 266 700 490 715 968 264 381 621 468 592 963 895 217 599 993 229
915 608 941 463 976 156 518 286 253 697 920 827 223 758 251 185 210 916 864 000 000 000 000 000 000
000000
100 !
93326215443944 152 681 699 238 856 266 700 490 715 968 264 381 621 468 592 963 895 217 599 993 229
915 608 941 463 976 156 518 286 253 697 920 827 223 758 251 185 210 916 864 000 000 000 000 000 000
000000
Clear@fD
Functional Programming (MathematicaGuide)
Pure Functions (MathematicaTutorial)
Functional Operations (MathematicaOverview)
Example: data manipulation
Generate the data
data = RandomReal@8- 10, 10
8/10/2019 Youngjoo Chung - Tutorial on Symbolic Computing With Mathematica [2011] [p47]
31/47
Select@data, NegativeD8- 2.71293, - 7.84716, - 2.51371, - 7.20865, - 0.35902, - 5.73894, - 1.13296, - 9.28741,
- 3.3001, - 6.56867, - 4.45742, - 3.50691, - 5.22019, - 7.31909, - 1.75738, - 1.42342,
- 1.08151, - 8.43983, - 1.63389, - 5.64446, - 6.33726, - 5.05503, - 6.70792, - 1.25488,
- 6.64355, - 4.31986, - 7.33067, - 5.96476, - 9.22198, - 4.24909, - 6.01423, - 0.97207,- 0.425487, - 1.08979, - 4.19288, - 4.14596, - 3.44104, - 5.51746, - 3.39021, - 8.64063,
- 2.70173, - 7.25148, - 9.66792, - 8.06609, - 7.88423, - 6.21864, - 4.81503, - 0.399189