tutorial on symbolic computing with mathematica - … on symbolic computing with mathematica...
TRANSCRIPT
Tutorialon
Symbolic Computingwith Mathematica
Youngjoo Chung
School of Info. and Comm., [email protected]
2011. 7. 7
<< InitPackages`
Symbolic Computation
• Symbolic computation (algebraic manipulation)
– Mathematical computation with variables and constants according to the rules of algebra
Concept Formulation
CalculationVisualization
•Numerical Computing
•Graphics
•Symbolic Computing
•Science•Engineering
• Commercial software
MathematicaMapleMathCadMatlabReduceMacsymaScratchpadDerive
Basic Concepts of Mathematica
• Mathematica is the world's only fully integrated environment for formulation, calculationand visualization for technical computing.
• The concept of Mathematica is to create once and for all a single system that could handleall the various aspects of technical computing in a coherent and unified way.
2 Mathematica Tutorial.nb
• The concept of Mathematica is to create once and for all a single system that could handleall 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 onlya 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 externalprograms.
• 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
TCP/IP
mle.exeMathLink
Standard Packages
MathematicaKernel
Windows/UnixPlatform
Windows/UnixPlatform
Add-On Packages
User Packages
Other Packages
User Frontend
Numerical Analysis
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 3
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
4 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 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 andvector 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 – Etc.
Mathematica Tutorial.nb 5
• 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 featuresprovided by the options of MPMAF cannot be used.
Basic Algebra
• Expansion
ExpandAHx + aL2Ea2 + 2 a x + x2
The power exponent must be an integer. Otherwise, use MPExpandBinomial.
MPExpandBinomial@Hx + aLn, kDMPEvalSum@%D
âk=0
n ak x-k+n n!k! H-k + nL!Ha + xLn
6 Mathematica Tutorial.nb
MPExpandBinomial@Ha + b + cLn, k, lDMPEvalSum@%DPowerExpand@%D
âk=0
n âl=0
-k+n ak bl c-k-l+n n!k! l! H-k - l + nL!
cnb + cc
n a + b + cb + c
n
Ha + b + cLn• Factoring
FactorAx2 + Ha + bL x + a bEHa + xL Hb + xLMPFactor
MPFactor@a + b, aDa 1 +
ba
• Reduction of fractions
x2x + a ®
Hx + aL2 - a2 - 2 a Hx + aL + 2 a2x + a
x2
a + x®
-a2 - 2 a x + Ha + xL2a + x
ApartB x2x + a , xF
-a + x +a2
a + x
MPApart
MPApartB sx2 exn2 e0 - ex
+sy2 ey
n2 e0 - ey+
sz2 ezn2 e0 - ez
, 9ex, ey, ez=F% �. -sx2 - sy2 - sz2 ® -1
-sx2 - sy2 - sz2 -n2 sx2 e0
-n2 e0 + ex-
n2 sy2 e0-n2 e0 + ey
-n2 sz2 e0
-n2 e0 + ez
-1 -n2 sx2 e0
-n2 e0 + ex-
n2 sy2 e0-n2 e0 + ey
-n2 sz2 e0
-n2 e0 + ez
• Separation of variables for solving differential equations
Mathematica Tutorial.nb 7
âyâx Š 2 ãx y
MPMAFB%, MPSepVars, 8All, x, Side ® Right<,MPDiffToInt, :All, x, y, Constant ®
12 Log@c1D>,
MPEvalInt, All,MPSolve, 8All, y<F
âyâx
Š 2 ãx y
Log@yD2
Š ãx +Log@c1D
2y Š ã2 ã
x c1
Direct solution using MPDSolveâyâx Š 2 ãx yMPMAF@%, MPDSolve, 8All, y, x, RCN ® c1<D
âyâx
Š 2 ãx y
y Š ã2 ãx c1
Trigonometric Functions
• Adding sin and cos functions
MPTrigAddSinCos@a Cos@xD + b Sin@xD, SinDa2 + b2 Sin@x + ArcTan@b, aDD
• Conversion to other functions
MPTrigConvert@Tan@xD, CosD1 - Cos@xD2 Sec@xD
• Conversion to half angle
MPTrigToHalf@Cos@2 xD, SinD1 - 2 Sin@xD2
• Conversion to double angle
8 Mathematica Tutorial.nb
MPTrigToDoubleACos@xD2, CosE12
H1 + Cos@2 xDLComplex Variables
• Asterisk (*) is interpreted as the complex conjugate
MPComplexExpandAHãu+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+
12 Hu+Conjugate@uDL CoshB 1
2Hu - Conjugate@uDLF - ãv+ 1
2 Hu+Conjugate@uDL SinhB 12
Hu - Conjugate@uDLF• The exponential form of a complex number
MPComplexToExp@x + ä yDãä ArcTanA y
x E x2 + y2
Polynomials and Series
• Eliminate the second highest order term
MPMergePolyAx3 + a x2 + c, xE-a3
27+ c -
a2 x3
+a3+ x
3
• Transform a polynomial by making a replacement of the variable
MPTransPolyAx3 + a x2 + c, x, a + xEc + a2 Ha + xL - 2 a Ha + xL2 + Ha + xL3
A case of two variables
MPTransPolyAx2 Hy + aL, 8x, y<, 8x - 1, y + 1<Ea + 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
Mathematica Tutorial.nb 9
MPTaylorB 1 + x , x, 3F1 +
x2-x2
8+x3
16
MPTaylorB 1 + Hx + DxL2 , Dx, 1F1 + x2 +
x Dx
1 + x2
MPAFE@y@xD, MPTaylor, 8All, x - x0, 2, Variables ® x<, CS ® x0, Head ® TildeTildeDy@xD » y@x0D + Hx - x0L y¢@x0D + 1
2Hx - x0L2 y¢¢@x0D
Equations
• Adding equations
MPAddEqs@8a Š b, c Š d, e Š f<Da + c + e Š b + d + f
• Subtracting equations
MPSubtEqs@8a Š b, c Š d, e Š f<Da - 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
2 x Š 5
• Solve equations and write
MPSolve@8x + y Š 1, x - y + 2 z Š 1, y - 3 z Š 3<, 8x, y, z<D:x Š
52, y Š -
32, z Š -
32>
10 Mathematica Tutorial.nb
Differentiation
• The built-in derivative operator D, or ¶, evaluates the expression immediately.
D@f@xD, xDf¢@xD
The total derivative operator Dt assumes all symbols are dependent variables.
Dt@a f@xD, xDDt@a, xD f@xD + a f¢@xD
• The partial differential operator ¶¶x delays the evaluation until the command MPEvalD or
MPExpandD is encountered.
y Š¶3
¶x2 ¶yHa f + b gL
MPMAF@%, MPExpandD, 8At@2D, 8f, g<<Dy Š
¶3Ha f + b gL¶x2 ¶y
y Š a¶3f
¶x2 ¶y+ b
¶3g¶x2 ¶y
The function MPExpandD is used to expand an expression using the differential operators.
MPExpandDB ¶
¶xf + Sin@gD
h , 8f, g, h<F1h
¶f¶x
+ Cos@gD ¶g¶x
-f + Sin@gD
h2¶h¶x
• Integration by parts using the differential operator
MPIntegrateByPartsDBHFžà f ¶g¶x âxF
f g - à g ¶f¶x
âx
• Merging the differential operator
MPMergeDBa ¶f¶x + b ¶g
¶x F¶Ha f + b gL
¶x
• Transformation of variables
Mathematica Tutorial.nb 11
• Transformation of variables
MPTransDB ¶2f¶w2
, VT ® :w, l, w Š2 p cl
>Fl3
2 c2 p2¶f
¶l+
l4
4 c2 p2¶2f
¶l2
• Evaluation of vector operators
MPAFEAÑ2y, MPEvalVecOps, 8All, y Š y@r, jD, Cylindrical@r, j, zD<, Apply ® ExpandEMPMAFB%, MPSimpD, : 1
r
¶y
¶r+¶2y
¶r2, Mult ® r>F
Ñ2y Š1r
¶y
¶r+¶2y
¶r2+
1r2
¶2y
¶j2
Ñ2y Š1r2
¶2y
¶j2+1r
¶
¶rr¶y
¶r
MPAFEAÑ2y, MPEvalVecOps, 8All, y Š y@x, y, zD, Cartesian@x, y, zD<EMPMAFB%, 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, :: 2r ¶y
¶r +¶2y
¶r2>, : Cot@qD
r2¶y
¶q+
1r2
¶2y
¶q2>>F
Ñ2y Š¶2y
¶x2+¶2y
¶y2+¶2y
¶z2
Ñ2y Š2r
¶y
¶r+Cot@qD
r2¶y
¶q+¶2y
¶r2+
1r2
¶2y
¶q2+Csc@qD2
r2¶2y
¶j2
Ñ2y ŠCsc@qD2
r2¶2y
¶j2+1r
¶2Hr yL¶r2
+Csc@qD
r2¶
¶qSin@qD ¶y
¶q
Integration
• Wrap the integral with HoldForm (HF) to prevent evaluation.
– Without the wrap
F Š à f@xD âxF Š à f@xD âx
– With the wrap
12 Mathematica Tutorial.nb
F Š HFžà f âx
F Š à f âx• Combining multiple integrals
KHFžà f@xD âxO KHFžà g@xD âxOMPCombInt@%, 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 âkMPMAFB%, RA, :All, :F@kD Š
12 p à f@xD ã-ä k x âx, G@kD Š
12 p à g@xD ã-ä k x âx>>,
MPCombInt, 8All, RV ® 8x, x¢, x¢¢<<,MPFourierToDiracDelta, 8All, k<,MPEvalIntDelta, 8All, x¢¢<F
à F@kD G@kD ãä k x âk12 p à Kà f@x¢¢D g@x¢D d@x - x¢ - x¢¢D âx¢O âx¢¢
12 p à f@x - x¢D g@x¢D âx¢
• Transformation of the integration variables
MPTransIntBà f âx , VT ® 9x, y, y Š x2=Fà f2 y
ây
MPAFEBHFžà-¥
¥ 1x2 + a2
âx, MPEvalInt, Apply ® PowerExpandFà-¥
¥ 1x2 + a2
âx Šp
a
Mathematica Tutorial.nb 13
MPAFEBHFžà-¥
¥ 1x2 + a2
âx, MPTransInt,8All, VT ® 8x, q, x Š a Tan@qD<<, RA ® Sign@aD Š 1, PostApply ® MPEvalIntFà-¥
¥ 1x2 + a2
âx Šp
a
HFžIntegrate@f, x, y, zDMPMAF@%, 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à à à f r2 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à0
¥à0
pà0
2 pf 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
MPMergeIntIntervalBà0
af@tD ât + à
a
bf@tD âtF
à0
bf@tD ât
• Integration by parts
MPIntegrateByPartsBHFžà-¥
xy¢@yD y2 ây, y¢@yDF
-à-¥
x2 y y@yD ây - Iy2 y@yDMyŠ-¥ + x2 y@xD
Summation
• Changing the summation intervals
14 Mathematica Tutorial.nb
MPChSumIntervalBHFž ân=-¥
¥
an, 8n, 81, 2<, 8n3, n4<<Fân=1
2an + â
n=n3
n4an
• Combining multiple summations. The indexes are replaced to avoid duplication.
MPCombSumBân=1
¥
fn ân=1
¥
gn, RV ® 8n, m<Fân=1
¥ âm=1
¥
fn gm
• Merging multiple summations
MPMergeSumBâk=0
¥
A@kD +âk=0
¥
B@kDFâk=0
¥ HA@kD + B@kDL• Merging summation intervals
MPMergeSumIntervalBân=0
k-1an + â
n=k
NanF
ân=0
Nan
Vectors and Matrices
• Expansion of vector expressions
MPExpandVecBA ‰Ja BÓ‰CÓNFa AÓ.CÓ BÓ - a AÓ.BÓ CÓMPExpandVecAÑ × Ie EÓM, 9e, E
Ó=Ee Ñ × E
Ó+ HÑeL.EÓ
• Matrix form of equations
MPEqsMatForm@8a x + b y Š z1, c x + d y Š z2<, 8x, y<DK a bc d O × K x
y O Š J z1z2 N
• Expansion of Ñ ´Ñ ´ÑA
Mathematica Tutorial.nb 15
• Expansion of Ñ ´Ñ ´ÑA
MPExpandVecAÑ ‰Ñ ‰A, AEÑIÑ × AÓM - Ñ2AÓ
• Deleting a column from a matrix
Table@i j, 8i, 3<, 8j, 3<D �� MatrixFormMPDeleteColumns@%, 2D �� MatrixForm
1 2 32 4 63 6 91 32 63 9
• Inserting a column in a matrix
Table@i j, 8i, 3<, 8j, 3<D �� MatrixFormMPInsertColumns@%, 80, 0, 0<, -1D �� MatrixForm
1 2 32 4 63 6 91 2 3 02 4 6 03 6 9 0
• Diagonalization of a matrix
1 4 94 4 09 0 4
;
MPDiagMatrix@%D �� MatrixForm
12 J5 + 397 N 0 0
0 12 J5 - 397 N 0
0 0 4
• Transform a vector from Cartesian to spherical coordinates
MPVecFromCart@80, 0, 1<, Spherical@r, q, jDD8Cos@qD, -Sin@qD, 0<The result is z= r cosq -q
`sinq.
• Transform a vector from spherical to Cartesian coordinates
16 Mathematica Tutorial.nb
MPVecToCart@80, 0, 1<, Spherical@r, q, jDD8-Sin@jD, Cos@jD, 0<The result is j= - x sinj + y cosj.
Functional Analysis
• Variable transformation
y¢@xD + y@xDMPTransEqB%, y@xD Š g@sD, VT ® :x, s, x Š s2, s Š x >F
y@xD + y¢@xDg@sD + g¢@sD
2 s
A case of two variables
¶x,xf@x, yD - ¶y,yf@x, yDMPTransEqB%, f@x, yD Š g@x, hD,VT ® :8x, y<, 8x, h<, :x Š
x + y2 , h Š
x - y2 >>, Apply ® SimplifyF
-fH0,2L@x, yD + fH2,0L@x, yDgH1,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 MPRe-storeFunctions.
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, aDa Ñ ‰A + f@a xD
• Putting a factor inside a function
Mathematica Tutorial.nb 17
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 LëAëH a\ + b\L, ADXa A a\ + Xa A b\ + Xb A a\ + Xb A b\MPCommutatorB Q
l0 2+ ä
l0
Ñ 2P, Q
l0 2- ä
l0
Ñ 2PF
MPExpandOp@%, 8Q, P<D-
Q
2 l0-ä P l02 Ñ
ëQ
2 l0+ä P l02 Ñ
+Q
2 l0+ä P l02 Ñ
ëQ
2 l0-ä P l02 Ñ
ä
ÑPëQ -
ä
ÑQëP
• Simplify a commutator using a commutation rule @Q, PD º QP - PQ= ä Ñ
MPCommutatorAQ2, P2EMPSimpOp@%, 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 initialvelocity 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 isno acceleration, v = 0, so
18 Mathematica Tutorial.nb
• The terminal velocity, v0, can be found from the equation of motion as t ®¥; when there isno acceleration, v = 0, so
m v Š m g - b v2MPMAFA%, RA, 9All, 9v Š 0, v Š v0==,MPSolve, 8All, v0, -1<, MergeP ® TrueE
m v Š g m - b v2
0 Š g m - b v02
v0 Šg mb
• The variables t and v separate.
m v Š m g - b v2
MPMAFB%, RA, :At@1D, v Šâvât >,
MPSepVars, 8All, t, Times, Side ® Right<,MPDivDenom, 8At@1D, m<F
m v Š g m - b v2
m âvg m - b v2
Š ât
âv
g - b v2m
Š ât
which can be integrated to yield the velocity at time t.
âvg - b v2
m
Š ât
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 m CoshB b g
m tF>, MergeP ® True, ,
MPEli, :At@2D, v0 Šg mb , b>, Apply ® PowerExpand, , ,
MPEli, :At@2D, v0g Š T, g>F
âv
g - b v2m
Š ât
Mathematica Tutorial.nb 19
àvi
v 1
g - b v2m
âv Š à 1 âtv Š
g mb
TanhB b gm
t + ArcTanhB bg m
viFFv Š
g mb
vi + g mb TanhB b g
m tFg mb + vi TanhB b g
m tFv Š
v0 Ivi + v0 TanhA tT EM
v0 + vi TanhA tT E
where
:v0 Š g mb , T Š
v0g >
• Verify that our solution satisfies the originial equation of motion:
MPAFEBv , MPExpandD, :All, v Šv0 Ivi + v0 TanhA t
T EMv0 + vi TanhA t
T E , OverDot ® t>, Apply ® SimplifyFMPMAFB%, MPTrigConvert, 8At@2D, Tanh<,MPEli, :At@2D, v Š
v0 Ivi + v0 TanhA tT EM
v0 + vi TanhA tT E , TanhBtT F>, Apply ® Simplify,
RR, :At@2D, :v0 Š g mb , T Š
v0g >>, Apply ® ExpandF
v Šv0 Iv02 - vi2M
T Iv0 CoshA tT E + vi SinhA t
T EM2v Š
-v2 + v02
T v0
v Š g -b v2
m
that is, Newton's equation of motion.
• Substituting the numerical data, v0 and T are
20 Mathematica Tutorial.nb
:v0 Š g mb , T Š
v0g >
MPMAFB%, MPEli, 8All, v0, 1, Keep ® True<, ExpandMergeP ® True,
RA, :All, :g Š 9.8 MeterSecond2
, b Š 700 Kilo GramMeter , m Š 70 Kilo Gram>>, Apply ® PowerExpand,
Convert, :At@1, 2D, MileHour >F
:v0 Š g mb
, T Šv0g
>:v0 Š 0.989949 Meter
Second, T Š 0.101015 Second>
:v0 Š 2.21445 MileHour
, T Š 0.101015 Second>Plot vHtL.
Mathematica Tutorial.nb 21
v Šg mb
vi + g mb TanhB b g
m tFg mb + vi TanhB b g
m tFMPMAFB%, RA, :At@2D, :g Š 9.8 Meter
Second2, b Š 700 Kilo Gram
Meter ,
m Š 70 Kilo Gram, vi Š 60 MileHour , t Š t Second>>, Apply ® PowerExpand,
MPDivFrac, :At@2D, MileHour >,
Convert, :: Hour MeterMile Second , 1>, :At@2D, Mile
Hour >>,MPDivEq, :All, Mile
Hour >,Plot, :At@2D, 8t, 0, 0.1<, PlotRange ® 80, 60<,AxesLabel ® :t@SecondD, vB MileHour F>, ImageSize ® 500>, Take ® LastF
v Šg mb
vi + g mb TanhB b g
m tFg mb + vi TanhB b g
m tFHour vMile
Š2.21445 H60 + 2.21445 [email protected] tDL
2.21445 + 60 [email protected] 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
Ÿ Series Solution of Differential Equations
• We apply the method of series soution to the linear (classical) oscillator equation.(9.84)
â2yâ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žâl=0
¥
al xk+l
y@xD Š xk Iº + a0 + x a1 + x2 a2 + x3 a3M Š âl=0
¥
al xk+l
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)
â2yâx2
+ w2 y Š 0
MPMAFB%, RA, :At@1D, y Š âl=0
¥
al xk+l>, Apply ® MPEvalDFy w2 +
â2yâx2
Š 0
w2 âl=0
¥
xk+l al + âl=0
¥
x-2+k+l H-1 + k + lL Hk + lL al Š 0
which gives
Mathematica Tutorial.nb 23
w2 âl=0
¥
xk+l al +âl=0
¥
x-2+k+l H-1 + k + lL Hk + lL al Š 0
MPMAFA%, MPShiftSum, 9AtA1, x-2+k+lE, 8l, 2<=,MPChSumLimits, 8At@1D, 8l, 0, ¥<<,MPMergeSum, At@1D,MPFactor, 9At@1D, xk+l=, Base ® _SumE
w2 âl=0
¥
xk+l al + âl=0
¥
x-2+k+l H-1 + k + lL Hk + lL al Š 0
âl=0
¥ Ixk+l w2 al + xk+l H1 + k + lL H2 + k + lL a2+lM + 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+lM + H-1 + kL k x-2+k a0 + k H1 + kL x-1+k a1 Š 0
– From the lowest power of x, xk-2, vanishing of the coefficient yieldsH-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 Š 0MPMAFA%, 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 ajH1 + j + kL H2 + j + kL
• Depending on whether k = 0 or k = 1, we have different recurrence relations. First withk = 0, the recurrence relation (Eq. (9.88)) becomes(9.89)
24 Mathematica Tutorial.nb
a2+j Š -w2 ajH1 + j + kL H2 + j + kL
MPMAF@%, RA, 8At@2D, k Š 0<Da2+j Š -
w2 ajH1 + j + kL H2 + j + kLa2+j Š -
w2 ajH1 + jL H2 + jLwhose solution is(9.90)
a2+j Š -w2 ajH1 + jL H2 + jL
MPMAFA%, MPRSolve, 9All, aj, 8j, 0<=,RA, 8All, j Š 2 n<, SimpOne ® True, Apply ® PowerExpand,MPToFactorial, At@2DE
a2+j Š -w2 ajH1 + jL H2 + jL
a2 n ŠH-1Ln w2 n a0Gamma@1 + 2 nDa2 n Š
H-1Ln w2 n a0H2 nL!and our solution of the differential equation is(9.91)
y Š âl=0
¥
al xk+l
MPMAFB%, RA, 8At@2D, k Š 0<,MPChSumInterval, 8At@2D, 8l, 8n, 0, ¥, l Š 2 n<<<,RA, :At@2D, a2 n Š
H-1Ln w2 n a0H2 nL! >, Apply ® MPFactorSum,
MPEvalSum, At@2DFy Š â
l=0
¥
al xk+l
y Š a0 ân=0
¥ H-1Ln x2 n w2 nH2 nL!y Š Cos@x wD a0
• Similarly, if we choose the indicial equation root k = 1 (Eq. (9.88)), the solution becomes(9.94)
Mathematica Tutorial.nb 25
y Š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
26 Mathematica Tutorial.nb
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 additionalfrequencies through nonlinear mixing. Suppose the two waves are given by
Mathematica Tutorial.nb 27
Ei Š12 IEŽi ã-ä wi t + EŽi* ãä wi tM
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
2Ei
MPMAFB%, RA, :At@2D, Ei Š12 IEŽi ã-ä wi t + EŽi* ãä wi tM>,
MPEvalSum, At@2DFE Š â
i=1
2Ei
E Š12
âi=1
2 Iã-ä t wi EŽi + ãä t wi HEŽiL*ME Š
12
Iã-ä t w1 EŽ1 + ã-ä t w2 EŽ2 + ãä t w1 HEŽ1L* + ãä t w2 HEŽ2L*Mand the nonlinear mixing produces
PNL Š e0 c3 E3
MPMAFB%, RA, :At@2D, E Š12 Iã-ä t w1 EŽ1 + ã-ä t w2 EŽ2 + ãä t w1 HEŽ1L* + ãä t w2 HEŽ2L*M>,
Expand, At@2, ED, FactorExp ® ä tFPNL Š E3 e0 c3
PNL Š18e0 c3 Iã-ä t w1 EŽ1 + ã-ä t w2 EŽ2 + ãä t w1 HEŽ1L* + ãä t w2 HEŽ2L*M3
PNL Š18e0 c3 Jã-3 ä t w1 EŽ13 + 3 ãä t H-2 w1-w2L EŽ12 EŽ2 + 3 ãä t H-w1-2 w2L EŽ1 EŽ22 + ã-3 ä t w2 EŽ23 + 3 ã-ä t w1 EŽ12 HEŽ1L* +
6 ã-ä t w2 EŽ1 EŽ2 HEŽ1L* + 3 ãä t Hw1-2 w2L EŽ22 HEŽ1L* + 3 ãä t w1 EŽ1 IHEŽ1L*M2 + 3 ãä t H2 w1-w2L EŽ2 IHEŽ1L*M2 +ã3 ä t w1 IHEŽ1L*M3 + 3 ãä t H-2 w1+w2L EŽ12 HEŽ2L* + 6 ã-ä t w1 EŽ1 EŽ2 HEŽ2L* + 3 ã-ä t w2 EŽ22 HEŽ2L* +6 ãä t w2 EŽ1 HEŽ1L* HEŽ2L* + 6 ãä t w1 EŽ2 HEŽ1L* HEŽ2L* + 3 ãä t H2 w1+w2L IHEŽ1L*M2 HEŽ2L* +3 ãä t H-w1+2 w2L EŽ1 IHEŽ2L*M2 + 3 ãä t w2 EŽ2 IHEŽ2L*M2 + 3 ãä t Hw1+2 w2L HEŽ1L* IHEŽ2L*M2 + ã3 ä t w2 IHEŽ2L*M3N
The frequencies are
28 Mathematica Tutorial.nb
PNL Š18 e0 c3 Jã-3 ä t w1 EŽ13 + 3 ãä t H-2 w1-w2L EŽ12 EŽ2 + 3 ãä t H-w1-2 w2L EŽ1 EŽ22 + ã-3 ä t w2 EŽ23 + 3 ã-ä t w1 EŽ12 HEŽ1L* +6 ã-ä t w2 EŽ1 EŽ2 HEŽ1L* + 3 ãä t Hw1-2 w2L EŽ22 HEŽ1L* + 3 ãä t w1 EŽ1 IHEŽ1L*M2 + 3 ãä t H2 w1-w2L EŽ2 IHEŽ1L*M2 +ã3 ä t w1 IHEŽ1L*M3 + 3 ãä t H-2 w1+w2L EŽ12 HEŽ2L* + 6 ã-ä t w1 EŽ1 EŽ2 HEŽ2L* + 3 ã-ä t w2 EŽ22 HEŽ2L* +6 ãä t w2 EŽ1 HEŽ1L* HEŽ2L* + 6 ãä t w1 EŽ2 HEŽ1L* HEŽ2L* + 3 ãä t H2 w1+w2L IHEŽ1L*M2 HEŽ2L* +3 ãä t H-w1+2 w2L EŽ1 IHEŽ2L*M2 + 3 ãä t w2 EŽ2 IHEŽ2L*M2 + 3 ãä t Hw1+2 w2L HEŽ1L* IHEŽ2L*M2 + ã3 ä t w2 IHEŽ2L*M3N
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
PNL Š18e0 c3 Jã-3 ä t w1 EŽ13 + 3 ãä t H-2 w1-w2L EŽ12 EŽ2 + 3 ãä t H-w1-2 w2L EŽ1 EŽ22 + ã-3 ä t w2 EŽ23 + 3 ã-ä t w1 EŽ12 HEŽ1L* +
6 ã-ä t w2 EŽ1 EŽ2 HEŽ1L* + 3 ãä t Hw1-2 w2L EŽ22 HEŽ1L* + 3 ãä t w1 EŽ1 IHEŽ1L*M2 + 3 ãä t H2 w1-w2L EŽ2 IHEŽ1L*M2 +ã3 ä t w1 IHEŽ1L*M3 + 3 ãä t H-2 w1+w2L EŽ12 HEŽ2L* + 6 ã-ä t w1 EŽ1 EŽ2 HEŽ2L* + 3 ã-ä t w2 EŽ22 HEŽ2L* +6 ãä t w2 EŽ1 HEŽ1L* HEŽ2L* + 6 ãä t w1 EŽ2 HEŽ1L* HEŽ2L* + 3 ãä t H2 w1+w2L IHEŽ1L*M2 HEŽ2L* +3 ãä t H-w1+2 w2L EŽ1 IHEŽ2L*M2 + 3 ãä t w2 EŽ2 IHEŽ2L*M2 + 3 ãä t Hw1+2 w2L HEŽ1L* IHEŽ2L*M2 + ã3 ä t w2 IHEŽ2L*M3N
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 + w2, -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;
Mathematica Tutorial.nb 29
f@100D93 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<, 100D89.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@dataD100
– Addition of all data
Plus žž data
34.7723
– Select positive numbers
30 Mathematica Tutorial.nb
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<Cases@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<
– Select data larger than 5
Select@data, ð > 5 &D87.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 &LD87.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 &ždataPlus žžž %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
Mathematica Tutorial.nb 31
Packages
Ÿ Mathematica Packages (Mathematica Tutorial)
Ÿ Package Development (Mathematica Guide)
Ÿ A sample package
Sample.m
<< Sample`
Context@AddTwoDSample`
AddTwo@a, bDAddTwo@a, bDRemove@Sample`AddTwoDRemove@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;}
32 Mathematica Tutorial.nb
Ÿ 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\IncludeSET LINK=/NOLOGO /SUBSYSTEM:windows /INCREMENTAL:no kernel32.libuser32.lib gdi32.lib /LIBPATH:\LibMPREP addtwo.tm -o addtwotm.cCL addtwo.c addtwotm.cLINK addtwo.obj addtwotm.obj ml32i2m.lib /OUT:addtwo.exe
Ÿ Install and run
Install
mlelink = Install@"I:�Mathematica�MathLink�MathLinkExamples�addtwo�addtwo.exe"D;The two arguments must be integers.
AddTwo@1, 2D3
[email protected], 2.0DAddTwo@1., 2.D
Uninstall.
Uninstall@mlelinkD"I:\Mathematica\MathLink\MathLinkExamples\addtwo\addtwo.exe"
Mathematica Tutorial.nb 33
Ÿ Example (Modeling of Optical Fiber Modes)
Ÿ Initialization
• Open the MathLink connection.
– Windows
mlelink = Install@"mle.exe"D;– Unix
mlelink = Install@LinkConnect@"aaa"DD;– Uninstall
• Set the default directory.
SetDirectory@"D:�Temp"DD:\Temp
Directory@DD:\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 (%).
34 Mathematica Tutorial.nb
profile = 8Step, 1, 84, 0<<[email protected], profile, Scale ® Delta,AxesLabel ® 8r@mmD, "D@%D"<, ImageSize ® 500D8Step, 1, 84, 0<<
2 4 6 8rHmmL
0.2
0.4
0.6
0.8
1.0
D@%D
• 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}}.
Mathematica Tutorial.nb 35
profile = 8Alpha, 2, 880, 1<, 84, -0.5<, 86, -0.5<, 88, 0<<<[email protected], profile, Scale ® Delta,AxesLabel ® 8r@mmD, "D@%D"<, ImageSize ® 500D8Alpha, 2, 880, 1<, 84, -0.5<, 86, -0.5<, 88, 0<<<
5 10 15rHmmL
-0.4
-0.2
0.2
0.4
0.6
0.8
1.0
D@%D
• 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}}.
36 Mathematica Tutorial.nb
profile = 8General, 880, 1<, 84, -0.5<, 86, -0.5<, 86, 0<<<[email protected], profile, Scale ® Delta,AxesLabel ® 8r@mmD, "D@%D"<, ImageSize ® 500D8General, 880, 1<, 84, -0.5<, 86, -0.5<, 86, 0<<<
2 4 6 8 10 12rHmmL
-0.4
-0.2
0.2
0.4
0.6
0.8
1.0
D@%D
Ÿ 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
1.452 1.454 1.456 1.458 1.460neff
-6
-5
-4
-3
Log10 ÈGÈ
Ÿ Effective Index, Group Index, and Dispersion
Mathematica Tutorial.nb 37
Ÿ
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 ® 1D8Mode ® 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
1.3 1.4 1.5 1.6lHmmL
1.4562
1.4564
1.4566
1.4568
1.4570
1.4572
Neff
• Plot the group indexNg
38 Mathematica Tutorial.nb
ListPlotATranspose@8Lambda �. sols, HNgroup �. solsL@@1DD<D,Joined ® True, AxesLabel ® 9l@mmD, Ng=, ImageSize ® 500E
1.3 1.4 1.5 1.6lHmmL
1.46138
1.46139
1.46140
1.46141
1.46142
1.46143
1.46144
Ng
• Plot the dispersion D
ListPlot@Transpose@8Lambda �. sols, HDspn �. solsL@@1DD<D,Joined ® True, AxesLabel ® 8l@mmD, "D@ps�nm�kmD"<, ImageSize ® 500D
1.3 1.4 1.5 1.6lHmmL
-1
1
2
D@ps�nm�kmD
Ÿ Poynting Vector and Field Profile
• Calculate the Poynting vector.
Mathematica Tutorial.nb 39
profile = 8Step, 1.46, 84, 1.45<<;sols = DspnNeff@1, 1.55, profileDSetCoordinates@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
-10 -5 5 10rHmmL
0.005
0.010
0.015
0.020
Sz
DensityPlot@HPoyntingVector@@3DD �. ssolL@x, yD,8x, -10, 10<, 8y, -10, 10<, PlotPoints ® 50, PlotRange ® AllD
40 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<Dboard = "GPIB0", address = 0, eos = 10, timeout = 10device = "tds360", address = 1, board = "GPIB0", eos = 10, timeout = 11device = "hp3245a", address = 9, board = "GPIB0", eos = 10, timeout = 11
Mathematica Tutorial.nb 41
GPIBIfClear@"GPIB0"DInterface cleared: board "GPIB0".
• Set the default directory.
SetDirectory@"D:�Temp"DD:\Temp
Directory@DD:\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".
42 Mathematica Tutorial.nb
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" is88t1, 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
0.001 0.002 0.003 0.004 0.005sec
-0.75
-0.5
-0.25
0.25
0.5
0.75
V
… 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
Mathematica Tutorial.nb 43
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 2"=E
2 4 6 8 10f@kHzD
20
40
60
80
100
ÈF@yDÈ2
… 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" is88t1, y1<, 8t2, y2<, ...<. The time ti is in the unit of second and the data yi is in the unit of volt.
44 Mathematica Tutorial.nb
• Parse the data and put the t and y data in the variable " pdata". The format of "pdata" is88t1, 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
0.001 0.002 0.003 0.004 0.005sec
-0.4
-0.2
0.2
0.4
V
… 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<, tD8A ® -0.009912, B ® -0.507288, f ® 0.159094<
• Plot the sine curve using the fitting parameters.
Mathematica Tutorial.nb 45
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.001 0.002 0.003 0.004 0.005t@secD
-0.4
-0.2
0.2
0.4
V
… Graphics …
• Comparison of the original data and the fitting curve.
Show@8pl1, pl2<D
0.001 0.002 0.003 0.004 0.005sec
-0.4
-0.2
0.2
0.4
V
… Graphics …
46 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
Mathematica Tutorial.nb 47