Beruflich Dokumente
Kultur Dokumente
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
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
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)
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
Expansion
ExpandAHx + aL2 E
a2 + 2 a x + x2
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
-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
Mathematica Tutorial.nb
y
x
2 x y
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
2 x y
x
MPMAF@%, MPDSolve, 8All, y, x, RCN c1 <D
y
x
2 x y
x
y 2 c1
Trigonometric Functions
1 - Cos@xD2 Sec@xD
Mathematica Tutorial.nb
MPTrigToDoubleACos@xD2 , CosE
1
H1 + Cos@2 xDL
Complex Variables
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
ArcTanA x E
x2 + y2
a3
+c-
27
a2 x
3
+x
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
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
:x
5
2
,y-
3
2
,z-
3
2
>
Mathematica Tutorial.nb
Differentiation
The total derivative operator Dt assumes all symbols are dependent variables.
Dt@a f@xD, xD
Dt@a, xD f@xD + a f @xD
MPExpandD is encountered.
y
3
x2 y
Ha f + b gL
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
fg-g
f
x
g
x
+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
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
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
F f@xD x
Mathematica Tutorial.nb
F HF f x
F f x
f@xD g@x D x x
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<,
F@kD G@kD
1
2p
kx
1
2p
f@x - x D g@x D x
x2 + a2
MPAFEBHF
- x2
1
+
a2
p
a
13
14
Mathematica Tutorial.nb
MPAFEBHF
- x2
+ a2
x, MPTransInt,
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
2p
f r2 Sin@qD f q r
Ha f + b g + c hL x
f@tD t
0
Integration by parts
x
Summation
Mathematica Tutorial.nb
n4
an + an
n=n3
n=1
n=1
fn gm
n=1 m=1
k=0
HA@kD + B@kDL
k=0
MPMergeSumInterval B an + an F
n=0
n=k
an
n=0
e E + H eL.E
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
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
`
`
`
The result is z = r cosq -q sinq.
Transform a vector from spherical to Cartesian coordinates
Mathematica Tutorial.nb
17
`
`
`
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
x+y
2
,h
x-y
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
18
Mathematica Tutorial.nb
MPInsideFunc@f@a xD + a A, CurlH, aD
Ha AL + f@a xD
Operator Analysis
a\ + Xa
+ Xb L A H
b\ + Xb
Q
l0
a\ + Xb
l0
MPCommutatorB
a\ +
b\L, AD
b\
Q
l0
l0
P,
PF
2 l0
PQ -
P l0
Q
2 l0
P l0
2
Q
2 l0
P l0
2
Q
2 l0
P l0
2
QP
Examples
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
m v g m - b v2
0 g m - b v20
gm
v0
m v m g - b v2
v
m v g m - b v2
m v
g m - b v2
v
g-
b v2
m
g-
bg
CoshB
gm
MPEli, :At@2D, v0
gm
b
MPEli, :At@2D,
v0
g
v
g-
b v2
m
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
>
t
T
v0 Ivi + v0 TanhA EM
t
T
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
gm
,T
v0
v0 Iv20 - v2i M
- v2 + v20
v
T v0
b v2
v gm
Mathematica Tutorial.nb
:v0
gm
v0
,T
>
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>
21
22
Mathematica Tutorial.nb
gm
gm
vi +
gm
bg
TanhB
bg
+ vi TanhB
tF
tF
Meter
Second
m 70 Kilo Gram, vi 60
Mile
, b 700
Kilo Gram
,
Meter
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
Hour
gm
vi +
gm
b
Hour v
gm
b
TanhB
+ vi TanhB
bg
m
bg
m
tF
tF
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
We apply the method of series soution to the linear (classical) oscillator equation.
(9.84)
2 y
x2
+ w2 y 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
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
w2 xk+l al + x-2+k+l H- 1 + k + lL Hk + lL al 0
l=0
l=0
H- 1 + kL k 0
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
Mathematica Tutorial.nb
a2+j -
w2 aj
H1 + j + kL H2 + j + kL
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
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 !
y al xk+l
l=0
H- 1Ln w2 n a0
H2 nL !
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
Mathematica Tutorial.nb
27
Mathematica Language
Language Overview (Mathematica Guide)
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)
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
*
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
*
*
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
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)
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
Number of data
Length@dataD
100
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<
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
#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
Install
mlelink = Install@"I:MathematicaMathLinkMathLinkExamplesaddtwoaddtwo.exe"D;
AddTwo@1.0, 2.0D
AddTwo@1., 2.D
Uninstall.
Uninstall@mlelinkD
"I:\Mathematica\MathLink\MathLinkExamples\addtwo\addtwo.exe"
34
Mathematica Tutorial.nb
Unix
mlelink = Install@LinkConnect@"aaa"DD;
Uninstall
Uninstall@mlelinkD
mle.exe
Directory@D
D:\Temp
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
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
-4
-5
-6
1.452
1.454
1.456
1.458
1.460
neff
37
38
Mathematica Tutorial.nb
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<<<
1.4572
1.4570
1.4568
1.4566
1.4564
1.4562
lHmmL
1.3
1.4
1.5
1.6
Mathematica Tutorial.nb
1.46143
1.46142
1.46141
1.46140
1.46139
1.46138
lHmmL
1.3
1.4
1.5
1.6
lHmmL
1.3
-1
1.4
1.5
1.6
39
40
Mathematica Tutorial.nb
0.015
0.010
0.005
-10
-5
rHmmL
5
10
Mathematica Tutorial.nb
mlelink = Install@LinkConnect@"aaa"DD;
Uninstall@mlelinkD
mle.exe
41
42
Mathematica Tutorial.nb
GPIBIfClear@"GPIB0"D
Interface cleared: board "GPIB0".
Directory@D
D:\Temp
Communication Examples
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".
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
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;
44
Mathematica Tutorial.nb
f=
1
Length@tD Ht@@2DD - t@@1DDL
HRange@Length@tDD - 1L;
"=E
F@yD2
100
80
60
40
20
f@kHzD
2
10
Graphics
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
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<
45
46
Mathematica Tutorial.nb
0.4
0.2
t@secD
0.001
0.002
0.003
0.004
0.005
-0.2
-0.4
Graphics
0.4
0.2
sec
0.001
-0.2
-0.4
Graphics
0.002
0.003
0.004
0.005
Mathematica Tutorial.nb
47