lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1...

31
計算與視覺化軟體 Mathematica 簡易手冊 入門篇 Mathematica 是一具數值和符號運算與視覺化的高階語 言,由美國 Wolfram Research 公司於 1988 年推出,是從 事科學研究工作不可或缺的工具之一。 Mathematica 軟體 語法簡單,能輕鬆上手,並能幫助個人學習及輔助教學。 此講義中將介紹如何使用它來做代數運算、解方程組、 函數之微分及積分、序列及級數、基本統計、平面及空 間中曲線及空間中曲面的描繪等等。 東海大學數學系 胡馨云、林大鈞 編著 (第一版 2010 年 5 月 26 日

Transcript of lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1...

Page 1: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

計算與視覺化軟體

Mathematica 簡易手冊 入門篇

Mathematica 是一具數值和符號運算與視覺化的高階語

言,由美國 Wolfram Research 公司於 1988 年推出,是從

事科學研究工作不可或缺的工具之一。Mathematica 軟體

語法簡單,能輕鬆上手,並能幫助個人學習及輔助教學。

此講義中將介紹如何使用它來做代數運算、解方程組、

函數之微分及積分、序列及級數、基本統計、平面及空

間中曲線及空間中曲面的描繪等等。

東海大學數學系

胡馨云、林大鈞 編著 (第一版 2010 年 5 月 26 日)

Page 2: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

Site created 1995. © 2010 Stephen Wolfram, LLC

Page 3: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

Stephen Wolfram is a distinguished scientist, inventor, author, and business leader. He is the creator of Mathematica, the author of A New Kind of Science, the creator of Wolfram|Alpha, and the founder and CEO of Wolfram Research. His career has been characterized by a sequence of original and significant achievements.

Born in London in 1959, Wolfram was educated at Eton, Oxford, and Caltech. He published his first scientific paper at the age of 15, and had received his PhD in theoretical physics from Caltech by the age of 20. Wolfram's early scientific work was mainly in high-energy physics, quantum field theory, and cosmology, and included several now-classic results. Having started to use computers in 1973,

Wolfram rapidly became a leader in the emerging field of scientific computing, and in 1979 he began the construction of SMP—the first modern computer algebra system—which he released commercially in 1981.

In recognition of his early work in physics and computing, Wolfram became in 1981 the youngest recipient of a MacArthur Prize Fellowship. Late in 1981 Wolfram then set out on an ambitious new direction in science aimed at understanding the origins of complexity in nature. Wolfram's first key idea was to use computer experiments to study the behavior of simple computer programs known as cellular automata. And starting in 1982 this allowed him to make a series of startling discoveries about the origins of complexity. The papers Wolfram published quickly had a major impact, and laid the groundwork for the emerging field that Wolfram called "complex systems research."

Through the mid-1980s, Wolfram continued his work on complexity, discovering a number of fundamental connections between computation and nature, and inventing such concepts as computational irreducibility. Wolfram's work led to a wide range of applications—and provided the main scientific foundations for such initiatives as complexity theory and artificial life. Wolfram himself used his ideas to develop a new randomness generation system and a new approach to computational fluid dynamics—both of which are

now in widespread use.

Following his scientific work on complex systems research, in 1986 Wolfram founded the first research center and the first journal in the field, Complex Systems. Then, after a highly successful career in academia—first at Caltech, then at the Institute for Advanced Study in Princeton, and finally as Professor of Physics, Mathematics, and Computer Science at the University of Illinois—Wolfram launched Wolfram Research, Inc.

Wolfram began the development of Mathematica in late 1986. The first version of Mathematica was released on June 23, 1988, and was immediately hailed as a major advance in computing. In the years that followed, the popularity of Mathematica grew rapidly, and Wolfram Research became established as a world leader in the software industry, widely recognized for excellence in both technology and business.

Page 4: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

Contact | © 2010 Stephen Wolfram, LLC

Following the release of Mathematica Version 2 in 1991, Wolfram began to divide his time between Mathematica development and scientific research. Building on his work from the mid-1980s, and now with Mathematica as a tool, Wolfram made a rapid succession of major new discoveries. By the mid-1990s his discoveries led him to develop a fundamentally new conceptual framework, which he then spent the

remainder of the 1990s applying not only to new kinds of questions, but also to many existing foundational problems in physics, biology, computer science, mathematics, and several other fields.

After more than ten years of highly concentrated work, Wolfram finally described his achievements in his 1200-page book A New Kind of Science. Released on May 14, 2002, the book was widely acclaimed and immediately became a bestseller. Its publication has been seen as initiating a paradigm shift of historic importance in science, with new implications emerging at an increasing rate every year.

Wolfram has been president and CEO of Wolfram Research since its founding in 1987. In addition to his business leadership, Wolfram is deeply involved in the development of the company's technology, and continues to be personally responsible for overseeing all aspects of the functional design of the core Mathematica system.

Wolfram has a lifelong commitment to research and education. In addition to providing software for a generation of scientists and students, Wolfram's company maintains some of the web's most visited sites for technical information. Wolfram is also increasingly active in defining new directions for education, especially in the science he has created.

Building on Mathematica, A New Kind of Science, and the success of Wolfram Research, Wolfram in May 2009 launched Wolfram|Alpha—an ambitious, long-term project to make as much of the world's knowledge as possible computable, and accessible to everyone.

Related Links

Interviews and Publications about Stephen Wolfram »

Scrapbook and Timeline »

Page 5: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

Part I Mathematica 簡介

Basic Operations

ü Do arithmetic

2.3 + 5.36 ∗ 2 − 8.9^2 + 4.5ê 1.2

−62.44

ü Handle numbers of any size

100!

N@Pi, 100D

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

ü Find the prime factors of an integer

FactorInteger@45DFactorInteger@242D

883, 2<, 85, 1<<

882, 1<, 811, 2<<

ü Greatest common divisor & least common multiple

GCD@12, 16DLCM@12, 16D

4

48

lecture note 20100526.nb 1

Page 6: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

ü Factor the polynomials

Factor@x2 − y2DFactor@x9 − y9D

Hx − yL Hx + yL

Hx − yL Hx2 + x y + y2L Hx6 + x3 y3 + y6L

ü Multiply out products and power

Expand@H1 + xL5DExpandAHx2 − y2 + 10L3E

1 + 5 x + 10 x2 + 10 x3 + 5 x4 + x5

1000 + 300 x2 + 30 x4 + x6 − 300 y2 − 60 x2 y2 − 3 x4 y2 + 30 y4 + 3 x2 y4 − y6

ü Simplify factors the polynomials

Simplify@x2 + 2 x + 1DSimplify@−108 + 108 x + 45 x2 − 40 x3 − 10 x4 + 4 x5 + x6D

H1 + xL2

H−2 + xL2 H−1 + xL H3 + xL3

ü Put all terms over a common denominator

TogetherA 5

x − 3−

2

x + 1E

11 + 3 xH−3 + xL H1 + xL

ü Separate into terms with simple denominators

ApartA x3 − 3 x + 2

Hx + 1L Hx − 3L3E

5H−3 + xL3 +

194 H−3 + xL2 +

1716 H−3 + xL −

116 H1 + xL

lecture note 20100526.nb 2

Page 7: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

ü Use some mathematical functions

a = 10.0;Sqrt@aDExp@aDLog@aDSin@aDClear@aD

3.16228

22026.5

2.30259

−0.544021

ü Advances: limit, derivatives and anti-derivatives

Clear@nDLimit@xn, x → 2DD@xn, xDD@Log@xD, xD

‡ xn x

‡ Log@xD x

‡ x ∗ Exp@−xD x

D@Log@x2 ∗ yD, xDD@Log@x2 ∗ yD, yD

2n

n x−1+n

1x

x1+n

1 + n

−x + x Log@xD

−x H−1 − xL

2x

1y

lecture note 20100526.nb 3

Page 8: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

Solving Equations

ü Solve a polynomial equation in one variable

Solve@a x2 + b x + c == 0, xD

99x →−b −

è!!!!!!!!!!!!!!!!!!!!b2 − 4 a c2 a

=, 9x →−b +

è!!!!!!!!!!!!!!!!!!!!b2 − 4 a c2 a

==

Solve@x2 + 5 x + 3 == 0, xD

99x →12I−5 −è!!!!!!13 M=, 9x →

12I−5 +è!!!!!!13 M==

Solve@x2 + x + 3 == 0, xD

99x →12I−1 − è!!!!!!11 M=, 9x →

12I−1 + è!!!!!!11 M==

ü Solve a simple equation of higher degree

Solve@3 x3 + 2 x2 − 2 x − 1 0, xDSolve@x6 1, xD

98x → −1<, 9x →16I1 −è!!!!!!13 M=, 9x →

16I1 +è!!!!!!13 M==

88x → −1<, 8x → 1<, 8x → −H−1L1ê3<, 8x → H−1L1ê3<, 8x → −H−1L2ê3<, 8x → H−1L2ê3<<

ü Solve two simultaneous algebraic equations

sys1 = 8y 1 + 2 x, x 5 − y<;Solve@sys1, 8x, y<D

99x →43

, y →113==

sys2 = 9 1 + x

−1 + y== 0, 2 − 3 y + y2 == 0=;

Solve@sys2, 8x, y<D

88x → −1, y → 2<<

lecture note 20100526.nb 4

Page 9: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

ü Solve an equations involving exponentials, logarithms and trigonometric functions

eqn1 = x−2 8;Solve@eqn1, xDeqn2 = Log@x + 2D 5;Solve@eqn2, xDeqn3 = Sin@xD 1ê 2;Solve@eqn3, xD

88x → 2 + Log@8D<<

88x → −2 + 5<<

99x →π6==

Sequences & Series

ü List the terms of a sequence

Table@i2, 8i, 10<DTable@j3, 8j, −10, 10<D

TableA 1

k, 8k, 10<E

TableA H−1Lk−1

k2, 8k, 10<E

81, 4, 9, 16, 25, 36, 49, 64, 81, 100<

8−1000, −729, −512, −343, −216, −125, −64,−27, −8, −1, 0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000<

91, 12

, 13

, 14

, 15

, 16

, 17

, 18

, 19

, 110

=

91, −14

, 19

, −1

16, 1

25, −

136

, 149

, −1

64, 1

81, −

1100

=

ü The sum of the sequence

a = 2; d = 2;Table@a + Hn − 1L∗ d, 8n, 1, 10<DSum@a + Hn − 1L∗ d, 8n, 1, 10<D

82, 4, 6, 8, 10, 12, 14, 16, 18, 20<

110

By formula

lecture note 20100526.nb 5

Page 10: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

n = 10; dSum =Ha + a + Hn − 1L∗ dL

2∗ n

110

ü The sum of the geometric sequence

b = 1; r = 1 ê3;Table@b ∗ rn−1, 8n, 1, 10<DSum@b ∗ rn−1, 8n, 1, 10<D

91,13

,19

,1

27,

181

,1

243,

1729

,1

2187,

16561

,1

19683=

2952419683

By formula

n = 10; rSum =b ∗H1 − rnL

1 − r

2952419683

ü The sum of the telescoping sequences

TableA 1

2 n − 1−

1

2 n, 8n, 1, 10<E

SumA 1

2 n − 1−

1

2 n, 8n, 1, 10<E

9 12

,1

12,

130

,1

56,

190

,1

132,

1182

,1

240,

1306

,1

380=

155685007232792560

lecture note 20100526.nb 6

Page 11: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

Statistics

ü Descriptive statistics

data = 84.3, 7.2, 8.4, 5.8, 9.2, 3.9, 8.8<;Mean@dataDMedian@dataDVariance@dataDStandardDeviation@dataDTotal@dataDFit@data, 81, x, x2<, xD

6.8

7.2

4.69

2.16564

47.6

4.2 + 1.275 x − 0.125 x2

Basic Plotting

ü The two-dimensional functions plots

Plot@x2 − 2, 8x, −4, 4<, Frame → TrueD;Plot@Exp@xD, 8x, −4, 4<, Frame → TrueD;Plot@Log@xD, 8x, 0, 4<, Frame → TrueD;Plot@Sin@xD + Sin@2 ∗ xD + Sin@3 ∗ xD, 8x, 0, 25<, Frame → TrueD;

-4 -2 0 2 4

0

2.5

5

7.5

10

12.5

lecture note 20100526.nb 7

Page 12: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

-4 -2 0 2 40

5

10

15

20

25

30

35

0 1 2 3 4-10

-8

-6

-4

-2

0

0 5 10 15 20 25

-2

-1

0

1

2

ü Combining several plots

Plot@82x, 3x, 5x<, 8x, −5, 5<, Frame → True,PlotStyle → 8RGBColor@1, 0, 0D, 8RGBColor@0, 1, 0D<, 8RGBColor@0, 0, 1D<<D;

-4 -2 0 2 40

20

40

60

80

100

120

lecture note 20100526.nb 8

Page 13: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

g1 = Plot@Sin@xD, 8x, −2 π, 4 π<,PlotStyle → 8RGBColor@1, 0, 0D<, PlotRange → 8−2, 2<, Frame → TrueD;

g2 = Plot@Cos@xD, 8x, −2 π, 4 π<, PlotStyle → 8RGBColor@0, 1, 0D<,PlotRange → 8−2, 2<, Frame → TrueD;

Show@g1, g2D;g3 = Plot@Sin@x − 1D, 8x, −2 π, 4 π<,

PlotStyle → 8RGBColor@1, 0, 1D<, PlotRange → 8−2, 2<, Frame → TrueD;g4 = Plot@Cos@x + 0.5D, 8x, −2 π, 4 π<, PlotStyle → 8RGBColor@0, 1, 1D<,

PlotRange → 8−2, 2<, Frame → TrueD;Show@GraphicsArray@88g1, g2<, 8g3, g4<<DD;

-5 -2.5 0 2.5 5 7.5 10 12.5

-1.5

-1

-0.5

0

0.5

1

1.5

2

-5 -2.5 0 2.5 5 7.5 10 12.5

-1.5

-1

-0.5

0

0.5

1

1.5

2

-5 -2.5 0 2.5 5 7.5 10 12.5

-1.5

-1

-0.5

0

0.5

1

1.5

2

lecture note 20100526.nb 9

Page 14: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

-5 -2.5 0 2.5 5 7.5 10 12.5

-1.5

-1

-0.5

0

0.5

1

1.5

2

-5 -2.5 0 2.5 5 7.5 10 12.5

-1.5

-1

-0.5

0

0.5

1

1.5

2

-5-2.502.557.51012.5

-1.5-1

-0.50

0.51

1.52

-5-2.502.557.51012.5

-1.5-1

-0.50

0.51

1.52

-5-2.502.557.51012.5

-1.5-1

-0.50

0.51

1.52

-5-2.502.557.51012.5

-1.5-1

-0.50

0.51

1.52

ü The three-dimensional surfaces plots

Plot3D@Sin@x yD, 8x, 0, 3<, 8y, 0, 3<D;g = Plot3D@Exp@−Hx2 + y2LD, 8x, −3, 3<, 8y, −3, 3<, PlotRange → AllD;Show@g, Mesh → FalseD;Show@g, Shading → FalseD;Show@ContourGraphics@gD, ColorFunction −> HueD;

lecture note 20100526.nb 10

Page 15: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

0

1

2

3 0

1

2

3

-1-0.5

00.5

1

0

1

2

-2

0

2-2

0

2

00.250.5

0.751

-2

0

2

-2

0

2-2

0

2

00.250.5

0.751

-2

0

2

-2

0

2-2

0

2

00.250.5

0.751

-2

0

2

lecture note 20100526.nb 11

Page 16: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

-3 -2 -1 0 1 2 3-3

-2

-1

0

1

2

3

Visualization

ü Curves in plane

a = 1.5; b = 3;ParametricPlot@8a ∗ Cos@tD − aê2 ∗ Sin@2 ∗ tD, b ∗ Sin@tD<,8t, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-2 -1 1 2

-3

-2

-1

1

2

3

a = 4; b = 2;ParametricPlot@8a ∗ Cos@tD, b ∗ Sin@tD<, 8t, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-4 -2 2 4

-2

-1

1

2

lecture note 20100526.nb 12

Page 17: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

a = 32; b = 4;

ParametricPlotA9Ha + bL∗ Cos@tD − b ∗ CosA a + b

b∗ tE, Ha + bL∗ Sin@tD − b ∗ SinA a + b

b∗ tE=,

8t, 0, 2 ∗ Pi<, AspectRatio → AutomaticE;

-30 -20 -10 10 20 30

-30

-20

-10

10

20

30

ParametricPlot@8Sin@θD − θ ∗ Cos@θD, Cos@θD + θ ∗ Sin@θD<,8θ, 0, 6 ∗ Pi<, AspectRatio → AutomaticD;

-15 -10 -5 5 10 15

-15

-10

-5

5

10

r = 2 ∗ Cos@5 ∗ θD;ParametricPlot@8r ∗ Cos@θD, r ∗ Sin@θD<, 8θ, 0, Pi<, AspectRatio → AutomaticD;

-1.5 -1 -0.5 0.5 1 1.5 2

-2

-1

1

2

lecture note 20100526.nb 13

Page 18: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

r = 2 ∗H1 − Cos@θDL;ParametricPlot@8r ∗ Cos@θD, r ∗ Sin@θD<, 8θ, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-4 -3 -2 -1

-2

-1

1

2

r = 50 ∗H1 + 0.2 ∗ Cos@6 ∗ θDL;ParametricPlot@8r ∗ Cos@θD, r ∗ Sin@θD<, 8θ, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-60 -40 -20 20 40 60

-40

-20

20

40

ü Curves in space

ParametricPlot3D@83 ∗ Cos@3 tD, 3 ∗ Sin@3 tD, t<, 8t, 0, 4 ∗ Pi<, AspectRatio → AutomaticD;

-20

2

-20

2

0

5

10

-20

2

lecture note 20100526.nb 14

Page 19: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

ParametricPlot3D@82 ∗ t, Sin@5 ∗ tD + Sin@tD, t<, 8t, 0, 4 ∗ Pi<, AspectRatio → AutomaticD;

ü Surfaces in space

ParametricPlot3D@8u ∗ Cos@vD, u ∗ Sin@vD, 2 ∗ v<,8u, 0, 5<, 8v, 0, 4 ∗ Pi<, AspectRatio → AutomaticD;

-5-2.5 0 2.5 5

-5-2.5

02.5

5

0

10

20

.502.5

5

ρ = 1;ParametricPlot3D@8ρ ∗ Sin@uD∗ Cos@vD, ρ ∗ Sin@uD∗ Sin@vD, ρ ∗ Cos@uD<,8u, 0, Pi<, 8v, 0, 12 ∗ Piê 6<, AspectRatio → AutomaticD;

-1-0.5

00.5

1

-1-0.5

00.5

1

-1

-0.5

0

0.5

1

-1-0.5

00.5

1-0.5

00.5

lecture note 20100526.nb 15

Page 20: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

a = 5; b = 5; c = 2;ParametricPlot3D@8a ∗ Cos@vD∗ Cos@uD, b ∗ Cos@vD∗ Sin@uD, c ∗ Sin@vD<, 8u, 0, 2 ∗ Pi<,8v, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-5-2.5

02.5

5 -5

-2.5

0

2.5

5

-2-1012

-5-2.5

02.5

ParametricPlot3D@82 ∗ u ∗ Cos@vD, 2 ∗ u ∗ Sin@vD, u2<,8u, 0, 4<, 8v, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-50

5

-5

05

0

5

10

15

-50

5

-5

05

a = 2;ParametricPlot3D@8H4 + a ∗ Cos@vDL∗ Cos@uD, H4 + a ∗ Cos@vDL∗ Sin@uD, a ∗ Sin@vD<,8u, 0, 2 ∗ Pi<, 8v, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-5

0

5-5

0

5

00.511.52

-5

0

5

lecture note 20100526.nb 16

Page 21: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

ParametricPlot3D@8u ∗ Cos@uD ∗H4 + Cos@u + vDL, u ∗ Sin@uD∗H4 + Cos@u + vDL, u ∗ Sin@u + vD<,8u, 0, 3 ∗ Pi<, 8v, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-40-20

020 -20

0

20

40-505

-40-20

020

ρ = 1 + 0.2 ∗ Sin@uD∗ Sin@5 ∗ vD;ParametricPlot3D@8ρ ∗ Sin@uD∗ Cos@vD, ρ ∗ Sin@uD∗ Sin@vD, ρ ∗ Cos@uD<,8u, 0, Pi<, 8v, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-1-0.5

00.5

1

-1-0.5

00.5

1

-1

-0.5

0

0.5

1

-1-0.5

00.5

1

-1-0.5

00.5

ρ = 1 + 0.2 ∗ Sin@8 ∗ uD ∗ Sin@8 ∗ vD;ParametricPlot3D@8ρ ∗ Sin@uD∗ Cos@vD, ρ ∗ Sin@uD∗ Sin@vD, ρ ∗ Cos@uD<,8u, 0, Pi<, 8v, 0, 2 ∗ Pi<, AspectRatio → AutomaticD;

-1-0.5

00.5

1

-1-0.5

00.5

1

-1

-0.5

0

0.5

1

-1-0.5

00.5

1

-1-0.5

00.5

lecture note 20100526.nb 17

Page 22: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

ParametricPlot3D@8H4 − v ∗ Sin@uDL∗ Cos@2 ∗ uD, H4 − v ∗ Sin@uDL∗ Sin@2 ∗ uD, v ∗ Cos@uD<,8u, 0, Pi<, 8v, −2, 2<, ViewPoint → 81.2, −2.5, 1.2<, AspectRatio → AutomaticD;

-5-2.5

02.5

-5-2.5

02.5

5

-2-1012

-5-2.5

02 5

-5-2.5

02.5

lecture note 20100526.nb 18

Page 23: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

Part II Mathematica 語法

基本運算

a+b+c 加法a-b 減法a*b*c 乘法a/b 除法-a 負號a^b 次方

※ 先乘除後加減且冪次優先於乘除

數學常數

Pi 圓周率 p=3.141592654ΩE 尤拉常數 e=2.71828182ΩI 虛數Infinity 無限大Degree 角度轉換弳度的常數 即 Pi/180

數字的形式

256 整數2.56 實數11/35 分數2+6I 複數

指定之前計算結果的方法

% 前一個運算結果%% 前二個運算結果%%Ω%(n個%) 前n個運算結果%n 或 Out[n] 前n個運算結果

lecture note 20100526.nb 19

Page 24: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

複數的運算指令

a+b I 複數Re[z]/ Im[z] 複數z的實部/虛部Abs[z] 複數z的大小或模數Arg[z] 複數z的幅角Conjugate[a+b I] 共軛複數

常用數學函數

Sin[x],Cos[x],Tan[x],... 三角函數 其引數需為弳度Sinh[x],Cosh[x],Tanh[x],... 雙曲函數ArcSin[x],ArcCos[x],ArcTan[x],... 反三角函數ArcSinh[x],ArcCosh[x],ArcTanh[x],... 反雙曲函數Sqrt[x] 根號Exp[x] 自然指數Log[x] 自然對數Log[a,x] 以a為底的對數Abs[x] 絕對值Round[x] 最接近x的整數Floor[x] 小於或等於x的最大整數Ceiling[x] 大於或等於x的最小整數Mod[a,b] 數a 除 b 所得的餘數n! 階乘Random[ ] 0至1之間的亂數Max[a,b,c,...] 極大值Min[a,b,c,...] 極小值

輸出的控制指令

expr; 做一個運算,但不顯示出結果expr1;expr2;expr3 做數個運算,但只顯示出最後一個運算的結果expr1;expr2;expr3; 做數個運算,全都不顯示出結果

括號的使用

( term ) 圓括號,括號內的先作運算f[x] 方括號,函數專用 x,y,z 大括號或串列括號,內放串列的元素

lecture note 20100526.nb 20

Page 25: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

p[[i]] 或 Part[p,i] 雙方括號,p的第i項元素p[[i,j]] 或 Part[p,i,j] 雙方括號, p的第i項第j個元素

數之設定

x=a 將變數x的值設為ax=y=b 將變數x和y的值均設為bx=. 或 Clear[x] 清除變數x 所儲存的值

變數使用的法則

xy 中間沒有空格,視為變數xyx y 中間有空格,視為變數 x 乘上變數 y3 x 中間有空格,視為3乘上xx3 中間沒有空格,視為變數x3

處理代數的指令

Expand[expr] 將 expr展開Factor[expr] 將 expr因式分解Simplify[expr] 將 expr化簡成精簡的式子FullSimplify[expr] 將 expr化成更精簡的式子

多項式/分式轉換的函數

ExpandAll[expr] 把算是全部展開Together[expr] 將 expr各項通分合併Apart[expr] 把分式拆開成數項分式Apart[expr,var] 視var以外的變數為常數,將 expr拆成數項Cancel[expr] 消去分子和分母共同的因子

分母/分子的運算

Denominator[expr] 取出expr的分母Numerator[expr] 取出expr的分子ExpandDenominator[expr] 展開expr的分母ExpandNumerator[expr] 展開expr的分子

lecture note 20100526.nb 21

Page 26: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

多項式的轉換函數

Collect[expr,x] 將 expr表示成x的多項式,Collect[expr,x,y,Ω] 將 expr分別表示成 x,y,Ω的多項式FactorTerms[expr] 將 expr的數值因子提出,FactorTerms[expr,x] 將 expr中把所有不包含x項的因子提出FactorTerms[expr,x,y,Ω] 將 expr中把所有不包含x,y,...項的因子提出

三角函數、雙曲函數和指數的運算

TrigExpand[expr] 將三角函數展開TrigFactor[expr] 將三角函數式因式分解TrigReduce[expr] 將相乘或次方的三角函數化成一次方的基本三角函數之組合ExpToTrig[expr] 將指數函數化成三角函數或雙曲函數TrigToExp[expr] 將三角函數或雙曲函數化成指數函數

複數、次方乘積之展開

ComplexExpand[expr] 假設所有的變數都是實數來對 expr展開ComplexExpand[expr,x,y,Ω] 假設x,y,..等變數均為複數來對 expr展開PowerExpand[expr] 將次方乘積展開

多項式項次、係數與最高次方之取得

Coefficient[expr,form] 於 expr中form的係數Exponent[expr,form] 於 expr中form的最高次方Part[expr,n] 或 expr[[n]] 在 expr項中第n個項

代換運算子

expr/.xØvalue 將 expr裡所有的x均代換成valueexpr/.xØvalue1,yØvalue2,Ω 執行數個不同變數的代換expr/.xØvalue1,xØvalue2,Ω 將 expr代入不同的x值expr//.xØvalue1,yØvalue2,Ω 重複代換到 expr不再改變為止

求方程式的根

lecture note 20100526.nb 22

Page 27: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

Solve[lhsãrhs,x] 求方程式lhsãrhs 的代數解Nsolve[lhsãrhs,x] 求方程式lhsãrhs 的數值解Solve[lhs1ãrhs1,lhs2ãrhs2,Ω,x,y,Ω] 求聯立方程式的代數解NSolve[lhs1ãrhs1,lhs2ãrhs2,Ω,x,y,Ω] 求聯立方程式的數值解FindRoot[lhsãrhs,x,x0] 初始點x0 給定 求 lhsãrhs 的根

縮短輸出的指令

expr//Short 顯示 一行的計算結果Short[expr,n] 顯示 n行的計算結果Command; 執行command,但不顯示出結果

函數的定義、查詢與清除

f[x_]=expr 立即定義函數f[x]f[x_]:=expr 延遲定義函數f[x]f[x_,y_,...] 函數f有兩個以上的引數?f 查詢函數f的定義Clear[f] 或 f=. 清除f的定義Remove[f] 將f自系統中清除掉

條件式的自訂函數

lhs:=rhs/;condition 當condition成立時,lhs才會定義成 rhs

If 指令

If[test,then,else] 若test為真,則回應then,否則回應elseIf[test,then,else,unknow] 同上,若test無法判定真或假時,則回應unknow

極限

Limit[expr,xØc] 當x趨近c時,求expr的極限Limit[expr,xØc,DirectionØ1] 單邊極限,由右邊Limit[expr,xØc,DirectionØ-1] 單邊極限,由左邊

lecture note 20100526.nb 23

Page 28: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

微分

D[f,x] 函數f對x作微分D[f,x1,x2,...] 函數f對x1,x2,...作微分D[f,x,n] 函數f對x微分n次D[f,x,NonConstantsØy,z,...] 函數f對x作微分,將y,z,...視為x的函數

全微分

Dt[f] 全微分dfDt[f,x] 全微分Dt[f,x1,x2,...] 全微分Dt[f,x,ConstantsØc1,c2,...] 全微分,視c1,c2,... 為常數

不定積分與定積分

Integrate[f,x] 不定積分 Integrate[f,x,xmin,xmax] 定積分Integrate[f,x,xmin,xmax,y,ymin,ymax] 定積分

數列之和與積

Sum[f,i,imin,imax] 求數列和Sum[f,i,imin,imax,di] 求數列和,引數i以di遞增Sum[f,i,imin,imax,j,jmin,jmax]Product[f,i,imin,imax] 求數列積Product[f,i,imin,imax,di] 求數列之積,引數i以di遞增Product[f,i,imin,imax,j,jmin,jmax] 求數列之積

函數之泰勒展開式

Series[expr,x,x0,n] 將 expr 對 x0點作泰勒級數展開至n項Series[expr,x,x0,m,y,y0,n] 將 expr 對 x0 和 y0 展開

關係運算子

aãb 等於

lecture note 20100526.nb 24

Page 29: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

a>b 大於a¥b 大於等於a<b 小於a§b 小於等於a∫b 不等於

邏輯運算子

!p 否p && q 和X or [p,q,...] 或

二維基本繪圖

Plot[f,x,xmin,xmax] 畫出f在xmin到xmax之間的圖形Plot[f1,f2,Ω,x,xmin,xmax] 同時畫出數個函數圖形Plot[f,x,xmin,xmax,optionØvalue] 指定特殊的繪圖選項,畫出函數f的圖形

選項預設值說明

AspectRatio /GoldenRatio 圖形高和寬之比例Axes 是否將坐標軸畫出AxesLabel 是否為坐標軸貼上標記AxesOrigin 坐標軸的相交的點Frame 是否將圖形加上外框FrameLabel 從x軸下方依順時針方向加上圖形外框的標記FrameTicks 是否為外框加上刻度GridLines 是否將主要刻度上加上網格線PlotLabel 是否為整張圖之圖名PlotRange 指定y方向畫圖的範圍Ticks 坐標軸之刻度

※「Automatic、None、True、False」為Mathmatica常用的選項設定其代表意義分別為「使用內部設定、不包含此項、作此項目、不作此項目」

串列繪圖

ListPlot[y1,y2,...] 畫出1,y1,2,y2,... 的點ListPlot[x1,y1,x2,y2,...] 畫出x1,y1,x2,y2,... 的點

lecture note 20100526.nb 25

Page 30: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

ListPlot[x1,y1,x2,y2,...,PlotJoinedØTrue] 把畫出來的點用線段連接

繪圖顏色的指定

Plot[f1,f2,Ω,x,xmin,xmax,PlotStyleØRGBColor[r1,g1,b1],RGBColor[r2,g2,b2],Ω]彩色繪圖Plot[f1,f2,Ω,x,xmin,xmax,PlotStyleØGrayLevel[i],GrayLevel[j],Ω]灰階繪圖

圖形的處理

Show[plot] 重畫一個圖Show[plot1,plot2,...] 將數張圖併成一張Show[plot,optionØopt] 加入選項

圖形的排列

Show[GraphicsArray[plot1,plot2,...]] 將圖形橫向排列Show[GraphicsArray[plot1,plot2,...]] 將圖形垂直排列Show[GraphicsArray[plot1,plot2,...,...]] 將圖形成二維矩陣式排列

二維參數圖

ParametricPlot[f1,f2,t,tmin,tmax]參數繪圖ParametricPlot[f1,f2,g1,g2,Ω,t,tmin,tmax] 同時繪數個參數圖ParametricPlot[f1,f2,t,tmin,tmax,AspectRatioØAutomatic]保持曲線的真正形狀,即x,y坐標比為1:1

等高線圖

ContourPlot[f,x,xmin,xmax,y,ymin,ymax]於指定範圍之內畫出f的等高線圖

lecture note 20100526.nb 26

Page 31: lecture note 20100526web.thu.edu.tw/billshen/www/Lecture_Note_20100526_V.pdf2010/05/26  · 2n−1 − 1 cccccccc 2n, 8n, 1, 10

等高線圖的選項預設值 說明

ColorFunction 選Automatic 為灰階,選Hue則為系列色彩Contours 10 等高線的數目,設ContoursØz1,z2,...則指定等高值為z1,z2,... ContourShading 選True為 Contour的上色,選False則不上色PlotRange 選Automatic 高度z值的範圍,也可指定zmin,zmax

三維基本繪圖

Plot3D[f,x,xmin,xmax,y,ymin,ymax ] 畫出f在 xmin 到 xmax 及 ymin 到 ymax 之間的圖形

三維參數圖

ParametricPlot3D[f1,f2,f3,s,smin,smax,t,tmin,tmax] 參數繪圖

lecture note 20100526.nb 27