Sie sind auf Seite 1von 47

Tutorial

on
Symbolic Computing
with Mathematica

Youngjoo Chung
School of Info. and Comm., GIST
ychung@gist.ac.kr
2011. 7. 7

Mathematica Tutorial.nb

<< InitPackages`

Symbolic Computation

Symbolic computation (algebraic manipulation)


Mathematical computation with variables and constants according to the rules of algebra

Science
Engineering

Graphics

Symbolic
Computing

Concept

Formulation

Visualization

Calculation

Numerical
Computing

Commercial software
Mathematica
Maple
MathCad
Matlab
Reduce
Macsyma
Scratchpad
Derive
Basic Concepts of Mathematica

Mathematica is the world's only fully integrated environment for formulation, calculation
and visualization for technical computing.
The concept of Mathematica is 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.

Mathematica Tutorial.nb

The concept of Mathematica is 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
Mathematica computing environment
User Frontend
Numerical
Analysis

Standard Packages
TCP/IP

Add-On Packages

Mathematica
Kernel

MathLink

mle.exe

User Packages
Other Packages

Windows/Unix
Platform

Windows/Unix
Platform

Control &
Instrumentation

External applications are written in C/C++.


Mathematica is used for both data input and output.
External applications and Mathematica communicate 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

Algebraic Calculation
Algebraic Calculations (Mathematica Tutorial)
Symbolic Calculations (Mathematica Tutorial)
Expand
Factor
Apart
Together
Simplify

Basic Algebra
Polynomial Algebra (Mathematica Guide)
Polynomial Systems (Mathematica Guide)

Elementary Functions
Elementary Functions (Mathematica Guide)

Trigonometric Functions
Trigonometric Functions (Mathematica Guide)
Trigonometric Expressions (Mathematica Tutorial)

Complex Variables
Complex Numbers (Mathematica Guide)
Functions of Complex Variables (Mathematica Guide)

Flow Control
Conditionals (Mathematica Guide)
Loops and Control Structures (Mathematica Tutorial)
Flow Control (Mathematica Guide)

Formula Manipulation

Mathematica Tutorial.nb

Formula Manipulation
Formula Manipulation (Mathematica Guide)
Manipulating Equations (Mathematica Guide)

User Package Functions

Mathematica provides a solid foundation for symbolic computing and a number of supporting 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 expressions
with 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

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 of MPMapApplyFunc
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 MPMAF cannot be used.
Basic Algebra

Expansion
ExpandAHx + aL2 E
a2 + 2 a x + x2

The power exponent must be an integer. Otherwise, use MPExpandBinomial.


MPExpandBinomial@Hx + aLn , kD
MPEvalSum@%D
n

k=0

ak x-k+n n !
k ! H- k + nL !

Ha + xLn

Mathematica Tutorial.nb

MPExpandBinomial@Ha + b + cLn , k, lD
MPEvalSum@%D
PowerExpand@%D
ak bl c-k-l+n n !

n -k+n

k ! l ! H- k - l + nL !

k=0 l=0

b+c

cn

a+b+c
b+c

c
Ha + b + cLn

Factoring
FactorAx2 + Ha + bL x + a bE
Ha + xL Hb + xL

MPFactor
MPFactor@a + b, aD
b

a 1+

Reduction of fractions
x2

x+a
x2

a+x

Hx + aL2 - a2 - 2 a Hx + aL + 2 a2
x+a

- a2 - 2 a x + Ha + xL2
a+x
x2

ApartB

-a + x +

, xF

x+a
a2

a+x

MPApart
MPApartB

s2x ex
n2 e0 - ex

s2y ey

n2 e0 - e y

s2z ez
n2 e0 - ez

, 9ex , ey , ez =F

% . - s2x - s2y - s2z - 1

- s2x - s2y - s2z -

-1 -

n2 s2x e0
- n2 e0 + ex

n2 s2x e0
- n2 e0 + ex

n2 s2y e0
- n2 e0 + ey

n2 s2y e0
- n2 e0 + ey

n2 s2z e0
- n2 e0 + ez

n2 s2z e0
- n2 e0 + ez

Separation of variables for solving differential equations

Mathematica Tutorial.nb

y
x

2 x y

MPMAFB%, MPSepVars, 8All, x, Side Right<,


MPDiffToInt, :All, x, y, Constant

Log@c1 D>,

2
MPEvalInt, All,
MPSolve, 8All, y<F
y
x
Log@yD

2 x y

x +

Log@c1 D

2
y

2 x

c1

Direct solution using MPDSolve


y

2 x y
x
MPMAF@%, MPDSolve, 8All, y, x, RCN c1 <D
y
x

2 x y
x

y 2 c1

Trigonometric Functions

Adding sin and cos functions


MPTrigAddSinCos@a Cos@xD + b Sin@xD, SinD

a2 + b2 Sin@x + ArcTan@b, aDD

Conversion to other functions


MPTrigConvert@Tan@xD, CosD

1 - Cos@xD2 Sec@xD

Conversion to half angle


MPTrigToHalf@Cos@2 xD, SinD
1 - 2 Sin@xD2

Conversion to double angle

Mathematica Tutorial.nb

MPTrigToDoubleACos@xD2 , CosE
1

H1 + Cos@2 xDL

Complex Variables

Asterisk (*) is interpreted as the complex conjugate


MPComplexExpandAHu+v L* , u, TargetFunctions ConjugateE
*

v+u

The built-in function ComplexExpand gives a rather different (even though equivalent)
result.
ComplexExpand@Conjugate@u+v D, u, TargetFunctions ConjugateD
1

v+ 2

Hu+Conjugate@uDL

1
CoshB

Hu - Conjugate@uDLF - v+ 2

Hu+Conjugate@uDL

1
SinhB

Hu - Conjugate@uDLF

The exponential form of a complex number


MPComplexToExp@x + yD
y

ArcTanA x E

x2 + y2

Polynomials and Series

Eliminate the second highest order term


MPMergePolyAx3 + a x2 + c, xE

a3

+c-

27

a2 x
3

+x

Transform a polynomial by making a replacement of the variable


MPTransPolyAx3 + a x2 + c, x, a + xE
c + a2 Ha + xL - 2 a Ha + xL2 + Ha + xL3

A case of two variables


MPTransPolyAx2 Hy + aL, 8x, y<, 8x - 1, y + 1<E
a + H- 2 + 2 aL H- 1 + xL + H- 1 + aL H- 1 + xL2 + y + 2 H- 1 + xL H1 + yL + H- 1 + xL2 H1 + yL

Taylor series

10

Mathematica Tutorial.nb

MPTaylorB

1+

x2

1 + x , x, 3F
x3
16

1 + Hx + DxL2 , Dx, 1F

MPTaylorB

1 + x2 +

x Dx
1 + x2

MPAFE@y@xD, MPTaylor, 8All, x - x0 , 2, Variables x<, CS x0 , Head TildeTildeD

y@xD y@x0 D + Hx - x0 L y @x0 D +

Hx - x0 L2 y @x0 D

Equations

Adding equations
MPAddEqs@8a b, c d, e f<D
a+c+e b+d+f

Subtracting equations
MPSubtEqs@8a b, c d, e f<D
a-c-e b-d-f

Move a subpart of an equation to left- or right-hand side


MPMoveEq@x + y z, y, RightD
-x + z y

Eliminating variables from a set of equations


MPEli@8x + y 1, x - y + 2 z 1, y - 3 z 3<, 8y, z<D Simplify
2x 5

Solve equations and write


MPSolve@8x + y 1, x - y + 2 z 1, y - 3 z 3<, 8x, y, z<D

:x

5
2

,y-

3
2

,z-

3
2

>

Mathematica Tutorial.nb

Differentiation

The built-in derivative operator D, or , evaluates the expression immediately.


D@f@xD, xD
f @xD

The total derivative operator Dt assumes all symbols are dependent variables.
Dt@a f@xD, xD
Dt@a, xD f@xD + a f @xD

The partial differential operator

delays the evaluation until the command MPEvalD or

MPExpandD is encountered.
y

3
x2 y

Ha f + b gL

MPMAF@%, MPExpandD, 8At@2D, 8f, g<<D

3 Ha f + b gL
x2 y

ya

3 f
x2 y

+b

3 g
x2 y

The function MPExpandD is used to expand an expression using the differential operators.
MPExpandDB

f + Sin@gD

+ Cos@gD

g
x

, 8f, g, h<F

f + Sin@gD h
x

h2

Integration by parts using the differential operator


MPIntegrateByPartsD BHF f

fg-g

f
x

g
x

Merging the differential operator


f
MPMergeDBa

+b

g
x

Ha f + b gL
x

Transformation of variables

xF

11

12

Mathematica Tutorial.nb

Transformation of variables
2 f
MPTransDB
w2
l3

2 c2 p2 l

2pc

, VT :w, l, w

l4

>F

2 f

4 c2 p2 l2

Evaluation of vector operators


MPAFEA2 y, MPEvalVecOps, 8All, y y@r, jD, Cylindrical@r, j, zD<, Apply ExpandE
MPMAFB%, MPSimpD, :

1 y

2 y

r r

1 2 y

2 y

r2

j2

2 y
r2
+

1 y
r r

2 y
r2

, Mult r>F

1 2 y

r2 j2

r r

y
r

MPAFEA2 y, MPEvalVecOps, 8All, y y@x, y, zD, Cartesian@x, y, zD<E


MPMAFB%, MPTransD, 8At@2D,
VT 88x, y, z<, 8r, q, j<, 8x r Sin@qD Cos@jD, y r Sin@qD Sin@jD, z r Cos@qD<<,
Apply HExpand PowerExpand Simplify &L<,
MPSimpD, ::

2 y

2 y

2 y

2 y
r r

x2
+

2 y

2 y
r r

2 y
y2

r2

>, :

Cot@qD y
q

r2

1 2 y
r2 q2

>>F

z2

Csc@qD2 2 y
r2

2 y

2 y

Cot@qD y
r2

j2

2 y
r2

1 2 y
r2 q2

1 2 Hr yL
r

r2

Csc@qD2 2 y
r2

j2

Csc@qD
r2

y
Sin@qD

Integration

Wrap the integral with HoldForm (HF) to prevent evaluation.


Without the wrap
F f@xD x

F f@xD x

With the wrap

Mathematica Tutorial.nb

F HF f x

F f x

Combining multiple integrals


KHF f@xD xO KHF g@xD xO
MPCombInt@%, RV 8x, x <D
K f@xD xO g@xD x

f@xD g@x D x x

Convolution in Fourier transform


:f@xD HF F@kD k x k, g@xD HF G@kD k x k>

:f@xD F@kD k x k, g@xD G@kD k x k>

HF F@kD G@kD k x k
1
1
- k x
- k x
x, G@kD
x>>,
f@xD
g@xD
2p
2p
MPCombInt, 8All, RV 8x, x , x <<,
MPFourierToDiracDelta , 8All, k<,

MPMAFB%, RA, :All, :F@kD

MPEvalIntDelta, 8All, x <F

F@kD G@kD
1
2p

kx

K f@x D g@x D d@x - x - x D x O x

1
2p

f@x - x D g@x D x

Transformation of the integration variables


MPTransIntB f x , VT 9x, y, y x2 =F
f

x2 + a2

MPAFEBHF

- x2

1
+

a2

p
a

x, MPEvalInt, Apply PowerExpandF

13

14

Mathematica Tutorial.nb

MPAFEBHF

- x2

+ a2

x, MPTransInt,

8All, VT 8x, q, x a Tan@qD<<, RA Sign@aD 1, PostApply MPEvalIntF

1
x2 + a2

p
a

HF Integrate@f, x, y, zD
MPMAF@%, MPTransInt, 8All,
VT 88x, y, z<, 8r, q, f<, 8x r Sin@qD Cos@fD, y r Sin@qD Sin@fD, z r Cos@qD<<<,
Apply PowerExpandD
f z y x
2
f r Sin@qD f q r

Replacement of the integration variable with others


MPTransIntB f x3 , Weight r2 Sin@qD, RV 9x3 , 8r, 0, <, 8q, 0, p<, 8f, 0, 2 p<=F

2p

f r2 Sin@qD f q r

Merging multiple integrals


MPMergeIntBa f x + b g x + c h xF

Ha f + b g + c hL x

Merging integration intervals


a

MPMergeIntInterval B f@tD t + f@tD tF


0

f@tD t
0

Integration by parts
x

MPIntegrateByParts BHF y @yD y2 y, y @yDF


-

- 2 y y@yD y - Iy2 y@yDMy- + x2 y@xD


-

Summation

Changing the summation intervals

Mathematica Tutorial.nb

MPChSumIntervalBHF an , 8n, 81, 2<, 8n3 , n4 <<F


n=-

n4

an + an
n=n3

n=1

Combining multiple summations. The indexes are replaced to avoid duplication.

MPCombSumB fn gn , RV 8n, m<F


n=1

n=1

fn gm
n=1 m=1

Merging multiple summations

MPMergeSumB A@kD + B@kDF


k=0

k=0

HA@kD + B@kDL
k=0

Merging summation intervals


k-1

MPMergeSumInterval B an + an F
n=0

n=k

an
n=0

Vectors and Matrices

Expansion of vector expressions



MPExpandVecBA Ja B CNF


a A.C B - a A.B C

MPExpandVecA Ie EM, 9e, E=E

e E + H eL.E

Matrix form of equations


MPEqsMatForm@8a x + b y z1 , c x + d y z2 <, 8x, y<D

x
a b
z
OK O J 1 N
y
z2
c d

Expansion of A

15

16

Mathematica Tutorial.nb

Expansion of A
MPExpandVecA A, AE

I AM - 2 A

Deleting a column from a matrix


Table@i j, 8i, 3<, 8j, 3<D MatrixForm
MPDeleteColumns@%, 2D MatrixForm
1 2 3
2 4 6
3 6 9
1 3
2 6
3 9

Inserting a column in a matrix


Table@i j, 8i, 3<, 8j, 3<D MatrixForm
MPInsertColumns@%, 80, 0, 0<, - 1D MatrixForm
1 2 3
2 4 6
3 6 9
1 2 3 0
2 4 6 0
3 6 9 0

Diagonalization of a matrix
1 4 9
4 4 0 ;
9 0 4
MPDiagMatrix@%D MatrixForm
1
2

J5 +

397 N
0

0
1
2

J5 -

0
397 N 0

Transform a vector from Cartesian to spherical coordinates


MPVecFromCart@80, 0, 1<, Spherical@r, q, jDD
8Cos@qD, - Sin@qD, 0<

`
`
`
The result is z = r cosq -q sinq.
Transform a vector from spherical to Cartesian coordinates

Mathematica Tutorial.nb

17

MPVecToCart@80, 0, 1<, Spherical@r, q, jDD


8- Sin@jD, Cos@jD, 0<

`
`
`
The result is j = - x sinj +y cosj.
Functional Analysis

Variable transformation
y @xD + y@xD
MPTransEqB%, y@xD g@sD, VT :x, s, x s2 , s

x >F

y@xD + y @xD
g@sD +

g @sD
2s

A case of two variables


x,x f@x, yD - y,y f@x, yD
MPTransEqB%, f@x, yD g@x, hD,
VT :8x, y<, 8x, h<, :x

x+y
2

,h

x-y

>>, Apply SimplifyF

- fH0,2L @x, yD + fH2,0L @x, yD


gH1,1L @x, hD

Abbreviation of derivatives
MPAbbrevFA pH0,1L @x, tD - 2 a pH1,0L @x, tD qH1,0L @x, tD, Deriv TrueE
pt - 2 a px qx

The subscripts denote the derivatives. The function arguments can be restored using MPRestoreFunctions.
MPRestoreF@ pt - 2 a px qx , 8p, q<, Deriv TrueD
- 2 a pH0,1L @t, xD qH0,1L @t, xD + pH1,0L @t, xD

Taking a factor out of a function


MPFactorFunc@f@a xD + Ha AL, CurlH, aD
a A + f@a xD

Putting a factor inside a function

18

Mathematica Tutorial.nb

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, fD
f@a x + b y, zD

Operator Analysis

Expansion of operator expressions


MPExpandBraket@HXa
Xa

a\ + Xa

+ Xb L A H

b\ + Xb
Q

l0

a\ + Xb

l0

MPCommutatorB

a\ +

b\L, AD
b\

Q
l0

l0

P,

PF

MPExpandOp@%, 8Q, P<D


Q

2 l0

PQ -

P l0

Q
2 l0

P l0
2

Q
2 l0

P l0
2

Q
2 l0

P l0
2

QP

Simplify a commutator using a commutation rule @Q, PD QP - PQ =


MPCommutatorAQ2 , P2 E
MPSimpOp@%, 8Q, P, <D
- P2 Q2 + Q2 P2
2 2 + 4 Q P

Examples

Ref. Chap 9, Arfken, Mathematical Methods for Physicists


Parachutist

The equation of motion is

m v m g - b v2

where -b v2 is the drag force and m g is the force of the gravitational attraction. The initial
velocity when the parachute opens at time t = 0 is vH0L = vi.
The terminal velocity, v0, can be found from the equation of motion as t ; when there is

no acceleration, v = 0, so

Mathematica Tutorial.nb

19

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 v2

MPMAFA%, RA, 9All, 9v 0, v v0 ==,


MPSolve, 8All, v0 , - 1<, MergeP TrueE

m v g m - b v2
0 g m - b v20
gm

v0

The variables t and v separate.

m v m g - b v2
v

MPMAFB%, RA, :At@1D, v


>,
t
MPSepVars, 8All, t, Times, Side Right<,
MPDivDenom, 8At@1D, m<F

m v g m - b v2
m v

g m - b v2

v
g-

b v2
m

which can be integrated to yield the velocity at time t.


v
b v2
m

g-

MPMAFB%, MPDiffToInt, 8All, 8v, vi , v<, t<, ,


MPEvalInt, All, , ,
MPSolve, 8All, v<, Hold vi , MergeP True, ,
TrigExpand, At@2D, Apply Simplify,
MPDivFrac, :At@2D,

bg
CoshB

gm

MPEli, :At@2D, v0

gm

, b>, Apply PowerExpand, , ,

b
MPEli, :At@2D,

v0
g

v
g-

b v2
m

tF>, MergeP True, ,

T, g>F

20

Mathematica Tutorial.nb

vi

g-

v 1 t

b v2
m

gm

bg

gm

gm

vi +

gm

vi FF

gm
bg

TanhB

tF

bg

+ vi TanhB

t + ArcTanhB

TanhB

tF

v0 Ivi + v0 TanhA EM
T

v
v0 +

t
vi TanhA E
T

where
:v0

gm

,T

v0

>

Verify that our solution satisfies the originial equation of motion:

MPAFEBv, MPExpandD, :All, v

t
T

v0 Ivi + v0 TanhA EM
t
T

, OverDot t>, Apply SimplifyF

v0 + vi TanhA E
MPMAFB%, MPTrigConvert, 8At@2D, Tanh<,
t

v0 Ivi + v0 TanhA EM
T

MPEli, :At@2D, v

v0 + vi TanhA E

t
, TanhB F>, Apply Simplify,
T

RR, :At@2D, :v0

gm

,T

v0

>>, Apply ExpandF

v0 Iv20 - v2i M

T Iv0 CoshA E + vi SinhA EM

- v2 + v20

v
T v0
b v2

v gm

that is, Newton's equation of motion.


Substituting the numerical data, v0 and T are

Mathematica Tutorial.nb

:v0

gm

v0

,T

>

MPMAFB%, MPEli, 8All, v0 , 1, Keep True<, ExpandMergeP True,


Meter

RA, :All, :g 9.8

Second
Convert, :At@1, 2D,

Mile

, b 700

Kilo Gram
Meter

>F

Hour

:v0

gm

,T

:v0

v0

>

0.989949 Meter

, T 0.101015 Second>

Second
:v0

2.21445 Mile
Hour

Plot vHtL.

, T 0.101015 Second>

, m 70 Kilo Gram>>, Apply PowerExpand,

21

22

Mathematica Tutorial.nb

gm

gm

vi +

gm

bg

TanhB

bg

+ vi TanhB

tF

tF

MPMAFB%, RA, :At@2D, :g 9.8

Meter
Second

m 70 Kilo Gram, vi 60

Mile

, b 700

Kilo Gram
,
Meter

, t t Second>>, Apply PowerExpand,

Hour
MPDivFrac, :At@2D,

Mile

>,
Hour
Hour Meter
Mile
Convert, ::
, 1>, :At@2D,
>>,
Mile Second
Hour
Mile
MPDivEq, :All,
>,
Hour
Plot, :At@2D, 8t, 0, 0.1<, PlotRange 80, 60<,
AxesLabel :t@SecondD, vB

Mile

F>, ImageSize 500>, Take LastF

Hour

gm

vi +

gm
b

Hour v

gm
b

TanhB

+ vi TanhB

bg
m

bg
m

tF

tF

2.21445 H60 + 2.21445 Tanh@9.89949 tDL

Mile

2.21445 + 60 Tanh@9.89949 tD

Mile
v
Hour
60

50

40

30

20

10

0.00

0.02

0.04

0.06

0.08

0.10

tHSecondL

Mathematica Tutorial.nb

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 = sinHw xL, cosHw xL.


We try the series solution of the form
(9.85)

y@xD xk Ia0 + a1 x + a2 x2 + a3 x3 + M HF al xk+l


l=0

y@xD xk I + a0 + x a1 + x2 a2 + x3 a3 M al xk+l
l=0

with a0 0 and the exponent k and all the coefficients al still undetermined.
By substituting (9.85) into Eq. (9.84), we have
(9.86)

2 y
x2

+ w2 y 0

MPMAFB%, RA, :At@1D, y al xk+l >, Apply MPEvalDF


l=0

y w2 +

2 y
x2

w2 xk+l al + x-2+k+l H- 1 + k + lL Hk + lL al 0
l=0

which gives

l=0

23

24

Mathematica Tutorial.nb

w2 xk+l al + x-2+k+l H- 1 + k + lL Hk + lL al 0
l=0

l=0

MPMAFA%, MPShiftSum, 9AtA1, x-2+k+l E, 8l, 2<=,


MPChSumLimits, 8At@1D, 8l, 0, <<,
MPMergeSum, At@1D,
MPFactor, 9At@1D, xk+l =, Base _SumE

w2 xk+l al + x-2+k+l H- 1 + k + lL Hk + lL al 0
l=0

l=0

Ixk+l w2 al + xk+l H1 + k + lL H2 + k + lL a2+l M + H- 1 + kL k x-2+k a0 + k H1 + kL x-1+k a1 0


l=0

xk+l Iw2 al + H1 + k + lL H2 + k + lL a2+l M + H- 1 + kL k x-2+k a0 + k H1 + kL x-1+k a1 0


l=0

From the lowest power of x, xk-2, vanishing of the coefficient yields


H- 1 + kL k a0 0

Since a0 was chosen to be nonzero, we have


(9.87)

H- 1 + kL k 0

This equation is called the indicial equation and we have k = 0 or k = 1.


Since the coefficient of xk+ jH j 0L vanishes, this results in
(9.88)

w2 al + H1 + k + lL H2 + k + lL a2+l 0
MPMAFA%, RA, 8At@1D, l j<,
MPSolve, 9All, a2+j =E
w2 al + H1 + k + lL H2 + k + lL a2+l 0
w2 aj + H1 + j + kL H2 + j + kL a2+j 0
a2+j -

w2 aj
H1 + j + kL H2 + j + kL

Depending on whether k = 0 or k = 1, we have different recurrence relations. First with


k = 0, the recurrence relation (Eq. (9.88)) becomes
(9.89)

Mathematica Tutorial.nb

a2+j -

w2 aj
H1 + j + kL H2 + j + kL

MPMAF@%, RA, 8At@2D, k 0<D

a2+j -

a2+j -

w2 aj
H1 + j + kL H2 + j + kL
w2 aj
H1 + jL H2 + jL

whose solution is
(9.90)

a2+j -

w2 aj
H1 + jL H2 + jL

MPMAFA%, MPRSolve, 9All, aj , 8j, 0<=,


RA, 8All, j 2 n<, SimpOne True, Apply PowerExpand,
MPToFactorial, At@2DE

a2+j -

w2 aj
H1 + jL H2 + jL

a2 n

H- 1L w2 n a0
Gamma@1 + 2 nD
H- 1Ln w2 n a0

a2 n

H2 nL !

and our solution of the differential equation is


(9.91)

y al xk+l
l=0

MPMAFB%, RA, 8At@2D, k 0<,


MPChSumInterval, 8At@2D, 8l, 8n, 0, , l 2 n<<<,
RA, :At@2D, a2 n

H- 1Ln w2 n a0
H2 nL !

>, Apply MPFactorSum,

MPEvalSum, At@2DF

y al xk+l
l=0

y a0
n=0

H- 1Ln x2 n w2 n
H2 nL !

y Cos@x wD a0

Similarly, if we choose the indicial equation root k = 1 (Eq. (9.88)), the solution becomes
(9.94)

25

26

Mathematica Tutorial.nb

Sin@x wD a0
w

From (9.91) and (9.94), the general solution can be put


y c1 Cos@x wD + c2 Sin@x wD

Mathematica Tutorial.nb

27

Mathematica Language
Language Overview (Mathematica Guide)

Structure of Mathematica Expressions


Expressions (Mathematica Tutorial)

Constants
Numbers (Mathematica Overview)
Types of Numbers (Mathematica Tutorial)
Integer (Built-in Mathematica Symbol)
Rational (Built-in Mathematica Symbol)
Real (Built-in Mathematica Symbol)
Complex (Built-in Mathematica Symbol)

Mathematical Constants (Mathematica Guide)

Variables
Defining Variables (Mathematica Tutorial)
Eliminating Variables (Mathematica Tutorial)

Patterns
Patterns (Mathematica Guide)
Introduction to Patterns
Verbatim Patterns
Example

In four-wave mixing, two waves of frequencies w1 and w2 interact and generate additional
frequencies through nonlinear mixing. Suppose the two waves are given by

28

Mathematica Tutorial.nb

Ei

*
IEi - wi t + Ei wi t M

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
2

E HF Ei
i=1

MPMAFB%, RA, :At@2D, Ei

*
IEi - wi t + Ei wi t M>,

2
MPEvalSum, At@2DF
2

E Ei
i=1

1
2

*
I- t wi Ei + t wi HEi L M
i=1

*
*
I- t w1 E1 + - t w2 E2 + t w1 HE1 L + t w2 HE2 L M

and the nonlinear mixing produces


PNL e0 c3 E3
MPMAFB%, RA, :At@2D, E

*
*
I- t w1 E1 + - t w2 E2 + t w1 HE1 L + t w2 HE2 L M>,

2
Expand, At@2, ED, FactorExp tF
PNL E3 e0 c3
PNL

*
* 3
e0 c3 I- t w1 E1 + - t w2 E2 + t w1 HE1 L + t w2 HE2 L M

8
PNL

1
8

3
2
2
3
2 *
e0 c3 J-3 t w1 E1 + 3 t H-2 w1 -w2 L E1 E2 + 3 t H-w1 -2 w2 L E1 E2 + -3 t w2 E2 + 3 - t w1 E1 HE1 L +

*
2 *

* 2

* 2
6 - t w2 E1 E2 HE1 L + 3 t Hw1 -2 w2 L E2 HE1 L + 3 t w1 E1 IHE1 L M + 3 t H2 w1 -w2 L E2 IHE1 L M +
* 3
2 *
*
2 *
3 t w1 IHE1 L M + 3 t H-2 w1 +w2 L E1 HE2 L + 6 - t w1 E1 E2 HE2 L + 3 - t w2 E2 HE2 L +
* *
* *
* 2 *
6 t w2 E1 HE1 L HE2 L + 6 t w1 E2 HE1 L HE2 L + 3 t H2 w1 +w2 L IHE1 L M HE2 L +

* 2

* 2
*
* 2
* 3
3 t H-w1 +2 w2 L E1 IHE2 L M + 3 t w2 E2 IHE2 L M + 3 t Hw1 +2 w2 L HE1 L IHE2 L M + 3 t w2 IHE2 L M N

The frequencies are

Mathematica Tutorial.nb

PNL

3
2
2
3
2 *
e0 c3 J-3 t w1 E1 + 3 t H-2 w1 -w2 L E1 E2 + 3 t H-w1 -2 w2 L E1 E2 + -3 t w2 E2 + 3 - t w1 E1 HE1 L +

*
2 *

* 2

* 2
6 - t w2 E1 E2 HE1 L + 3 t Hw1 -2 w2 L E2 HE1 L + 3 t w1 E1 IHE1 L M + 3 t H2 w1 -w2 L E2 IHE1 L M +
* 3
2 *
*
2 *
3 t w1 IHE1 L M + 3 t H-2 w1 +w2 L E1 HE2 L + 6 - t w1 E1 E2 HE2 L + 3 - t w2 E2 HE2 L +
* *
* *
* 2 *
6 t w2 E1 HE1 L HE2 L + 6 t w1 E2 HE1 L HE2 L + 3 t H2 w1 +w2 L IHE1 L M HE2 L +

* 2

* 2
*
* 2
* 3
3 t H-w1 +2 w2 L E1 IHE2 L M + 3 t w2 E2 IHE2 L M + 3 t Hw1 +2 w2 L HE1 L IHE2 L M + 3 t w2 IHE2 L M N
MPMAFA%, frequencies Cases@, a_ , 80, <D &, At@2D, Take Last,
Part, 8At@2D, 82<<, Level 1,
MPDiv, 8At@2D, t<,
Cases, 9At@2D, wi_ 2 wi_ - wj_ =, Apply UnionE
1

PNL

3
2
2
3
2 *
e0 c3 J-3 t w1 E1 + 3 t H-2 w1 -w2 L E1 E2 + 3 t H-w1 -2 w2 L E1 E2 + -3 t w2 E2 + 3 - t w1 E1 HE1 L +

8
*
2 *

* 2

* 2
6 - t w2 E1 E2 HE1 L + 3 t Hw1 -2 w2 L E2 HE1 L + 3 t w1 E1 IHE1 L M + 3 t H2 w1 -w2 L E2 IHE1 L M +
* 3
2 *
*
2 *
3 t w1 IHE1 L M + 3 t H-2 w1 +w2 L E1 HE2 L + 6 - t w1 E1 E2 HE2 L + 3 - t w2 E2 HE2 L +
* *
* *
* 2 *
6 t w2 E1 HE1 L HE2 L + 6 t w1 E2 HE1 L HE2 L + 3 t H2 w1 +w2 L IHE1 L M HE2 L +

* 2

* 2
*
* 2
* 3
3 t H-w1 +2 w2 L E1 IHE2 L M + 3 t w2 E2 IHE2 L M + 3 t Hw1 +2 w2 L HE1 L IHE2 L M + 3 t w2 IHE2 L M N
frequencies 8- 3 w1 , - 2 w1 - w2 , - w1 - 2 w2 , - 3 w2 , - w1 , - w2 , w1 - 2 w2 , w1 ,
2 w1 - w2 , 3 w1 , - 2 w1 + w2 , - w1 , - w2 , w2 , w1 , 2 w1 + w 2 , - w1 + 2 w2 , w2 , w1 + 2 w2 , 3 w2 <
frequencies 8w1 , 2 w1 - w2 , w2 , - w1 + 2 w2 <

Four frequencies are present in the output wave and they are in the order
frequencies 82 w1 - w2 , w1 , w2 , - w1 + 2 w2 <

if w1 < w2 .
Functions
Mathematical Functions (Mathematica Guide)

Programming
Procedural Programming (Mathematica Guide)
Defining Functions (Mathematica Tutorial)

Functions can be called recursively.


f@n_D := n f@n - 1D; f@0D = 1;

29

30

Mathematica Tutorial.nb

f@100D
93 326 215 443 944 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
000 000
100 !
93 326 215 443 944 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
000 000
Clear@fD

Functional Programming (Mathematica Guide)


Pure Functions (Mathematica Tutorial)
Functional Operations (Mathematica Overview)
Example: data manipulation

Generate the data


data = RandomReal@8- 10, 10<, 100D
89.2069, 4.38196, 7.36707, 2.44153, - 2.71293, - 7.84716, - 2.51371, - 7.20865, - 0.35902,
0.547298, - 5.73894, 6.72848, 0.891682, 6.47749, 2.32973, - 1.13296, - 9.28741, 2.6097,
- 3.3001, 1.31403, 3.28091, - 6.56867, - 4.45742, - 3.50691, - 5.22019, - 7.31909,
8.72281, 2.20909, - 1.75738, 8.50959, 4.91354, 7.45721, 5.48025, - 1.42342, 9.46615,
4.72104, 7.33425, - 1.08151, 5.55214, 2.43006, 8.78305, - 8.43983, - 1.63389,
- 5.64446, - 6.33726, 6.14862, - 5.05503, 7.83732, 9.89825, - 6.70792, - 1.25488,
5.78948, - 6.64355, - 4.31986, 5.54391, 5.09748, 4.97521, - 7.33067, - 5.96476,
0.273494, 4.95501, - 9.22198, - 4.24909, - 6.01423, 1.07997, - 0.97207, - 0.425487,
1.10524, 4.61603, - 1.08979, - 4.19288, - 4.14596, 0.206045, - 3.44104, - 5.51746,
- 3.39021, 4.92202, - 8.64063, - 2.70173, 2.66111, 7.56427, 3.89614, 4.80096,
- 7.25148, - 9.66792, 2.19267, - 8.06609, 2.25114, 5.52372, 9.20761, - 7.88423,
4.23404, - 6.21864, 6.96702, 5.57343, 7.28515, 9.36244, - 4.81503, 6.72125, - 0.399189<

Number of data
Length@dataD
100

Addition of all data


Plus data
34.7723

Select positive numbers

Mathematica Tutorial.nb

Select@data, NegativeD
8- 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<
Cases@data, _ ? NegativeD
8- 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<

Select data larger than 5


Select@data, > 5 &D
87.97756, 7.06071, 6.25687, 7.77989, 8.95827, 9.383, 6.92007, 6.09351,
9.35384, 8.79465, 6.81701, 9.68147, 5.23082, 6.14421, 8.4387, 6.12878,
7.46329, 6.51098, 9.32175, 8.12827, 9.10668, 9.73478, 8.44398, 6.23945<
Cases@data, _ ? H > 5 &LD
87.97756, 7.06071, 6.25687, 7.77989, 8.95827, 9.383, 6.92007, 6.09351,
9.35384, 8.79465, 6.81701, 9.68147, 5.23082, 6.14421, 8.4387, 6.12878,
7.46329, 6.51098, 9.32175, 8.12827, 9.10668, 9.73478, 8.44398, 6.23945<

Group two numbers and add the numbers in each group.


Partition@, 2D & data
Plus %
889.2069, 4.38196<, 87.36707, 2.44153<, 8- 2.71293, - 7.84716<, 8- 2.51371, - 7.20865<,
8- 0.35902, 0.547298<, 8- 5.73894, 6.72848<, 80.891682, 6.47749<, 82.32973, - 1.13296<,
8- 9.28741, 2.6097<, 8- 3.3001, 1.31403<, 83.28091, - 6.56867<, 8- 4.45742, - 3.50691<,
8- 5.22019, - 7.31909<, 88.72281, 2.20909<, 8- 1.75738, 8.50959<, 84.91354, 7.45721<,
85.48025, - 1.42342<, 89.46615, 4.72104<, 87.33425, - 1.08151<, 85.55214, 2.43006<,
88.78305, - 8.43983<, 8- 1.63389, - 5.64446<, 8- 6.33726, 6.14862<,
8- 5.05503, 7.83732<, 89.89825, - 6.70792<, 8- 1.25488, 5.78948<, 8- 6.64355, - 4.31986<,
85.54391, 5.09748<, 84.97521, - 7.33067<, 8- 5.96476, 0.273494<, 84.95501, - 9.22198<,
8- 4.24909, - 6.01423<, 81.07997, - 0.97207<, 8- 0.425487, 1.10524<, 84.61603, - 1.08979<,
8- 4.19288, - 4.14596<, 80.206045, - 3.44104<, 8- 5.51746, - 3.39021<,
84.92202, - 8.64063<, 8- 2.70173, 2.66111<, 87.56427, 3.89614<, 84.80096, - 7.25148<,
8- 9.66792, 2.19267<, 8- 8.06609, 2.25114<, 85.52372, 9.20761<, 8- 7.88423, 4.23404<,
8- 6.21864, 6.96702<, 85.57343, 7.28515<, 89.36244, - 4.81503<, 86.72125, - 0.399189<<
813.5889, 9.8086, - 10.5601, - 9.72236, 0.188278, 0.989546, 7.36917, 1.19678, - 6.67771,
- 1.98607, - 3.28776, - 7.96432, - 12.5393, 10.9319, 6.75221, 12.3708, 4.05682,
14.1872, 6.25274, 7.9822, 0.343213, - 7.27836, - 0.188635, 2.78229, 3.19033, 4.53461,
- 10.9634, 10.6414, - 2.35546, - 5.69127, - 4.26698, - 10.2633, 0.107901, 0.679749,
3.52624, - 8.33884, - 3.235, - 8.90766, - 3.7186, - 0.040626, 11.4604, - 2.45052,
- 7.47525, - 5.81495, 14.7313, - 3.65019, 0.748378, 12.8586, 4.54741, 6.32206<

Packages

31

32

Mathematica Tutorial.nb

Packages
Mathematica Packages (Mathematica Tutorial)
Package Development (Mathematica Guide)
A sample package

Sample.m
<< Sample`

Context@AddTwoD
Sample`

AddTwo@a, bD
AddTwo@a, bD

Remove@Sample`AddTwoD

Remove@Global`AddTwoD

Communication with External Programs


Introduction to MathLink (Mathematica Tutorial)
MathLink C API Developer Guide (Windows)
MathLink API (Mathematica Guide)
Example (addtwo.exe)
addtwo.c

#include "mathlink.h"
extern int addtwo( int i, int j);
int addtwo( int i, int j)
{
return i+j;
}

Mathematica Tutorial.nb

33

addtwo.tm

int addtwo P(( int, int));


:Begin:
:Function:
addtwo
:Pattern:
AddTwo[i_Integer, j_Integer]
:Arguments:
{ i, j }
:ArgumentTypes: { Integer, Integer }
:ReturnType: Integer
:End:
:Evaluate: AddTwo::usage = "AddTwo[x, y] gives the sum of two machine integers x and y."
Compile and link

SET CL=/nologo /c /DWIN32 /D_WINDOWS /W3 /O2 /DNDEBUG /I\Include


SET LINK=/NOLOGO /SUBSYSTEM:windows /INCREMENTAL:no kernel32.lib
user32.lib gdi32.lib /LIBPATH:\Lib
MPREP addtwo.tm -o addtwotm.c
CL addtwo.c addtwotm.c
LINK addtwo.obj addtwotm.obj ml32i2m.lib /OUT:addtwo.exe
Install and run

Install
mlelink = Install@"I:MathematicaMathLinkMathLinkExamplesaddtwoaddtwo.exe"D;

The two arguments must be integers.


AddTwo@1, 2D
3

AddTwo@1.0, 2.0D
AddTwo@1., 2.D

Uninstall.
Uninstall@mlelinkD
"I:\Mathematica\MathLink\MathLinkExamples\addtwo\addtwo.exe"

34

Mathematica Tutorial.nb

Example (Modeling of Optical Fiber Modes)


Initialization

Open the MathLink connection.


Windows
mlelink = Install@"mle.exe"D;

Unix
mlelink = Install@LinkConnect@"aaa"DD;

Uninstall
Uninstall@mlelinkD
mle.exe

Set the default directory.


SetDirectory@"D:Temp"D
D:\Temp

Directory@D
D:\Temp

Turn off Sellmeier formula by default.


DspnSetSellmeier@FalseD;

Refractive Index Profiles

Step index
The data format for the step index profile is
{Step, ncore, {acore-clad , nclad }}
or
{Step, Dcore, {acore-clad , Dclad }}.
D is given in percent (%).

Mathematica Tutorial.nb

profile = 8Step, 1, 84, 0<<


DspnPlotIndex@1.55, profile, Scale Delta,
AxesLabel 8r@mmD, "D@%D"<, ImageSize 500D
8Step, 1, 84, 0<<
D@%D
1.0

0.8

0.6

0.4

0.2

rHmmL
2

a-index
The data format for the a index profile is
{Alpha, a, {{0, ncore}, {acore , n1 }, {r2, n2 }, ... , {rN , nN }, {acore-clad , nclad }}
or
{Alpha, a, {{0, Dcore}, {acore , D1 }, {r2, D2 }, ... , {rN , DN }, {acore-clad , Dclad }}.

35

36

Mathematica Tutorial.nb

profile = 8Alpha, 2, 880, 1<, 84, - 0.5<, 86, - 0.5<, 88, 0<<<
DspnPlotIndex@1.55, profile, Scale Delta,
AxesLabel 8r@mmD, "D@%D"<, ImageSize 500D
8Alpha, 2, 880, 1<, 84, - 0.5<, 86, - 0.5<, 88, 0<<<
D@%D
1.0

0.8

0.6

0.4

0.2

rHmmL
5

10

15

-0.2

-0.4

General index
The data format for the general index profile is
{General, {0, ncore}, {r1 , n1}, {r2, n2 }, ... , {rN , nN }, {acore-clad , nclad }}
or
{General, {0, Dcore}, {r1 , D1}, {r2, D2 }, ... , {rN , DN }, {acore-clad , Dclad }}.

Mathematica Tutorial.nb

profile = 8General, 880, 1<, 84, - 0.5<, 86, - 0.5<, 86, 0<<<
DspnPlotIndex@1.55, profile, Scale Delta,
AxesLabel 8r@mmD, "D@%D"<, ImageSize 500D
8General, 880, 1<, 84, - 0.5<, 86, - 0.5<, 86, 0<<<
D@%D
1.0

0.8

0.6

0.4

0.2

rHmmL
2

10

12

-0.2

-0.4

Characteristic Equation

Plot the characteristic equation.


profile = 8Step, 1.46, 84, 1.45<<;
DspnPlotCharEquation @1, 1.55, profile,
AxesLabel 8"neff ", "Log10 G"<, ImageSize 500D
Log10 G
-3

-4

-5

-6

1.452

1.454

Effective Index, Group Index, and Dispersion

1.456

1.458

1.460

neff

37

38

Mathematica Tutorial.nb

Effective Index, Group Index, and Dispersion

Calculate the effective indexes, group indexes and dispersion of the guided modes.
profile = 8Step, 1.46, 84, 1.45<<;
sols = DspnNeffNgroupDspn @1, 81.2, 1.6, 0.02<, profile, NumModes 1D
8Mode 8HE1,1 <, Lambda 81.2, 1.22, 1.24, 1.26, 1.28, 1.3, 1.32, 1.34,
1.36, 1.38, 1.4, 1.42, 1.44, 1.46, 1.48, 1.5, 1.52, 1.54, 1.56, 1.58, 1.6<,
Neff 881.4573, 1.45723, 1.45716, 1.45709, 1.45702, 1.45696, 1.45689,
1.45682, 1.45675, 1.45668, 1.45661, 1.45654, 1.45647, 1.45641,
1.45634, 1.45627, 1.4562, 1.45613, 1.45606, 1.45599, 1.45592<<,
Ngroup 881.46137, 1.46138, 1.46139, 1.4614, 1.46141, 1.46142, 1.46143,
1.46143, 1.46144, 1.46144, 1.46144, 1.46144, 1.46144, 1.46144,
1.46144, 1.46143, 1.46143, 1.46142, 1.46142, 1.46141, 1.4614<<,
Dspn 882.10049, 1.93054, 1.75769, 1.58207, 1.4038, 1.22301, 1.03981, 0.854336,
0.666699, 0.477018, 0.285407, 0.0919793, - 0.103155, - 0.299888, - 0.498114,
- 0.697731, - 0.898639, - 1.10074, - 1.30395, - 1.50817, - 1.71333<<<

Plot the effective indexNeff


ListPlot@Transpose@8Lambda . sols, HNeff . solsL@@1DD<D,
Joined True, AxesLabel 8l@mmD, Neff <, ImageSize 500D
Neff

1.4572

1.4570

1.4568

1.4566

1.4564

1.4562

lHmmL
1.3

Plot the group indexNg

1.4

1.5

1.6

Mathematica Tutorial.nb

ListPlotATranspose@8Lambda . sols, HNgroup . solsL@@1DD<D,


Joined True, AxesLabel 9l@mmD, Ng =, ImageSize 500E
Ng
1.46144

1.46143

1.46142

1.46141

1.46140

1.46139

1.46138

lHmmL
1.3

1.4

1.5

1.6

Plot the dispersion D


ListPlot@Transpose@8Lambda . sols, HDspn . solsL@@1DD<D,
Joined True, AxesLabel 8l@mmD, "D@psnmkmD"<, ImageSize 500D
D@psnmkmD
2

lHmmL
1.3

-1

Poynting Vector and Field Profile

Calculate the Poynting vector.

1.4

1.5

1.6

39

40

Mathematica Tutorial.nb

profile = 8Step, 1.46, 84, 1.45<<;


sols = DspnNeff@1, 1.55, profileD
SetCoordinates@Cartesian@x, y, zDD;
ssol = DspnPoyntingVector @1, 1.55, 1, profile, Phase p 2, MaxRadius 20D;
8Mode 8HE1,1 <, Lambda 1.55, Neff 81.4561<<

Plot the Poynting vector


Plot@HPoyntingVector@@3DD . ssolL@x, 0D, 8x, - 10, 10<, AxesLabel 8r@mmD, Sz <D
Sz
0.020

0.015

0.010

0.005

-10

-5

rHmmL
5

10

DensityPlot@HPoyntingVector@@3DD . ssolL@x, yD,


8x, - 10, 10<, 8y, - 10, 10<, PlotPoints 50, PlotRange AllD

Mathematica Tutorial.nb

Plot3D@HPoyntingVector@@3DD . ssolL@x, yD, 8x, - 10, 10<, 8y, - 10, 10<,


AxesLabel 8x, y, Sz <, BoxRatios 81, 1, 1<, PlotPoints 50, PlotRange AllD

Example (Hardware Control via GPIB)


Initialization

Open the MathLink connection.


mlelink = Install@"mle.exe"D;

mlelink = Install@LinkConnect@"aaa"DD;

Uninstall@mlelinkD
mle.exe

Initialization of the GPIB interface for data acquisition and control.


GPIBInitialize@8"GPIB0", 0, IsBoard True<,
8"tds360", 1, TimeOut 11<, 8"hp3245a", 9, TimeOut 11<D
board = "GPIB0", address = 0, eos = 10, timeout = 10
device = "tds360", address = 1, board = "GPIB0", eos = 10, timeout = 11
device = "hp3245a", address = 9, board = "GPIB0", eos = 10, timeout = 11

41

42

Mathematica Tutorial.nb

GPIBIfClear@"GPIB0"D
Interface cleared: board "GPIB0".

Set the default directory.


SetDirectory@"D:Temp"D
D:\Temp

Directory@D
D:\Temp

Communication Examples

Identfication of the equipment


GPIBClear@"tds360"D;
GPIBWrite@"tds360", "*IDN?"D;
GPIBRead@"tds360"D
TEKTRONIX,TDS 360,0,CF:91.1CT FV:v1.09

Retrieve the data from the equipment and store the data to a file.
GPIBWrite@"tds360", "CURVe?"D;
GPIBReadFile@"tds360", "data.txt", TimeOut 11, Delete TrueD
Read to file: device "tds360" to file "D:\Temp\data.txt".

Switch the data source to Channel 1.


GPIBWrite@"tds360", "DATa:SOUrce CH1"D;

Verify the data source.


GPIBWrite@"tds360", "DATa:SOUrce?"D;
GPIBRead@"tds360"D
:DAT:SOU CH1

Square Pulses and the Fourier Spectrum

Set the amplitude and frequency of the signal source.


GPIBWrite@"hp3245a", "RST"D;
GPIBWrite@"hp3245a", "APPLY SQV 1.0"D;
GPIBWrite@"hp3245a", "FREQ 1E3"D;

Read the data from the oscilloscope and the store the data in the variable "data".

Mathematica Tutorial.nb

43

GPIBWrite@"tds360", "CURVe?"D;
data = GPIBRead@"tds360", TimeOut 11D;

Parse the data and put the t and y data in the variable " pdata". The format of "pdata" is
88t1, y1 <, 8t2, y2<, ...<. The time ti is in the unit of second and the data yi is in the unit of volt.
GPIBClear@"tds360"D;
GPIBWrite@"tds360", "WFMPre:CH1:XINcr?"D;
xinc = ToExpression@
StringReplace@Part@StringSplit@GPIBRead@"tds360"D, " "D, 2D, "E" "*^"DD;
GPIBWrite@"tds360", "WFMPre:CH1:YMUlt?"D;
ymu = ToExpression@StringReplace@
Part@StringSplit@GPIBRead@"tds360", TimeOut 12D, " "D, 2D, "E" "*^"DD;
adata = StringDrop@data, 6D;
pdata = ToExpression StringSplit@adata, ","D;
pdata = Table@8xinc * i, ymu * pdata@@iDD<, 8i, Length@pdataD<D;
Length@pdataD
1000

Plot the data.


ListPlot@pdata, Joined True, ImageSize 500,
PlotRange 8- 0.8, 0.8<, AxesLabel 8"sec", "V"<D
V
0.75

0.5

0.25

sec
0.001

0.002

0.003

0.004

0.005

-0.25

-0.5

-0.75

Graphics

Fourier transform. First separate the time and signal data. Put the time data in the variable
"t" and the signal data in the variable "y".
8t, y< = Transpose@pdataD;

The frequency data

44

Mathematica Tutorial.nb

f=

1
Length@tD Ht@@2DD - t@@1DDL

HRange@Length@tDD - 1L;

The Fourier transform of the signal data


fy = Fourier@yD;

Plot the spectrum data.


ListPlotATranspose 9f * 0.001, HAbs fyL2 =, PlotRange 880, 10<, All<,
ImageSize 500, AxesLabel 9"f@kHzD", "F@yD

"=E

F@yD2
100

80

60

40

20

f@kHzD
2

10

Graphics

Clear the data.


f =.; t =.;

Sinusoidal Waves and Curve Fitting

Set the amplitude and frequency of the signal source.


GPIBWrite@"hp3245a", "RST"D;
GPIBWrite@"hp3245a", "APPLY ACV 1.0"D;
GPIBWrite@"hp3245a", "FREQ 1E3"D;

Read the data from the oscilloscope and the store the data in the variable "data".
GPIBWrite@"tds360", "CURVe?"D;
data = GPIBRead@"tds360", TimeOut 11D;

Parse the data and put the t and y data in the variable " pdata". The format of "pdata" is
88t1, y1 <, 8t2, y2<, ...<. The time ti is in the unit of second and the data yi is in the unit of volt.

Mathematica Tutorial.nb

Parse the data and put the t and y data in the variable " pdata". The format of "pdata" is
88t1, y1 <, 8t2, y2<, ...<. The time ti is in the unit of second and the data yi is in the unit of volt.
GPIBClear@"tds360"D;
GPIBWrite@"tds360", "WFMPre:CH1:XINcr?"D;
xinc = ToExpression@
StringReplace@Part@StringSplit@GPIBRead@"tds360"D, " "D, 2D, "E" "*^"DD;
GPIBWrite@"tds360", "WFMPre:CH1:YMUlt?"D;
ymu = ToExpression@StringReplace@
Part@StringSplit@GPIBRead@"tds360", TimeOut 12D, " "D, 2D, "E" "*^"DD;
adata = StringDrop@data, 6D;
pdata = ToExpression StringSplit@adata, ","D;
pdata = Table@8xinc * i, ymu * pdata@@iDD<, 8i, Length@pdataD<D;
Length@pdataD
1000

Plot the data.


pl1 = ListPlot@pdata, Joined True, ImageSize 500, AxesLabel 8"sec", "V"<D
V

0.4

0.2

sec
0.001

0.002

0.003

0.004

0.005

-0.2

-0.4

Graphics

Curve fitting. Fit the data to the sine curve and find the fitting parameters.
f = 1000;
fitparams = FindFit@pdata, A + B Sin@2 p f t + fD, 8A, B, f<, tD
8A - 0.009912, B - 0.507288, f 0.159094<

Plot the sine curve using the fitting parameters.

45

46

Mathematica Tutorial.nb

pl2 = Plot@Evaluate@A + B Sin@2 p f t + fD . fitparamsD, 8t, 0, 0.005<,


ImageSize 500, PlotStyle 8RGBColor@1, 0, 0D<, AxesLabel 8"t@secD", "V"<D

0.4

0.2

t@secD
0.001

0.002

0.003

0.004

0.005

-0.2

-0.4

Graphics

Comparison of the original data and the fitting curve.


Show@8pl1, pl2<D

0.4

0.2

sec
0.001

-0.2

-0.4

Graphics

0.002

0.003

0.004

0.005

Mathematica Tutorial.nb

If you want to know more about symbolic computing,


please visit:
http://ontl.gist.ac.kr
Lectures Symbolic Computing Methods and
Applications

47

Das könnte Ihnen auch gefallen