You are on page 1of 218

MATHEMATICA

2004

Copyright . , . , 2004


,
.

.
,


,

.

,
. .. ,

,

. ,
,

.


.

. ,
,

4
,
.
.


,
, , .

Mathematica.
, S. Wolfram,
,
.
.

Mathematica.
,
,
,
Mathematica.

, ,
, ,
, .

,
.
,
,
.

Mathematica -
.

5

Mathematica.
, ,

.

,
Mathematica.
.

.
Fourier.

,
Mathematica.
, 2004

1 ......................................................................................11
..............................................................................11
1.1 ............................................................................................11
1.2 ..................................................................................13
1.3 .........................................15
1.4 .......................................................................17
1.5 ....................................................................18
1.6 .................................................20
1.7 .......................................................22
1.8 ................................................................................23
1.9 ................................................24
1.10 - ...............................................................................27
1.11 Table ....................................................................................28
1.12 ..............................................................31
1.13 ..........................................................................31

2 ......................................................................................35
............................................................................35
2.1 .................................................35
2.1.1 ....................................................................37
2.1.2 .............................39
2.1.3 ..........................................................40
2.2 ...............................................41

7
2.2.1 (ContourPlot).............................................42
2.2.2 (DensityPlot) ............................................44
2.3 ...........................................................................45

3 ......................................................................................55
MATHEMATICA ............................................................55
3.1 ........................................55
3.2 ................................................................................57
3.3 .........................................................................................59
3.4 .........................................................................60
3.4.1 ........................................................................60
3.4.2 ................................................................62
3.5 .......................................................................62
3.5.1 ........................................................................63
3.5.2 ................................................................64
3.5.3 ............................................................65
3.6 ...........................................................................66
3.7 .........................................................67
3.8 ....................................70
3.9 ...................................................72
3.9.1 .........................................................................72
3.9.2 ......................................................................74
3.10 ...........................................................................76

4 ......................................................................................77
A MATHEMATICA .....................................77
4.1 .............................................................................77
4.2 .........................................................80
4.3 ................................................................81
4.3.1 .....................................................81
4.3.2 .............................................82
3.4.3 ........................82
3.4.4 - .......................................83

5 ......................................................................................85
..............................................85
5.1 ..........................................................................85
5.2 () .....................................................90
5.3 ............................................................91
5.4 ...............................93
5.5 ...................................................................94
5.6 ...................................................................95
5.6.1 .............................................................95
5.6.2 .............................................96
5.6.3 ...................................................98
5.7 ..................................................................99
5.8 .............................................................101

6 ....................................................................................107
..............................................107
6.1 .......................................................................108
6.1.1 If ....................................................................................108
6.1.2 Which .............................................................................110
6.2 - ......................................................111
6.2.1 Do ...................................................................................111
6.2.2 For ..................................................................................114
6.2.3 While ..............................................................................117
6.3 ...............................................................................118
6.3.1 Continue .........................................................................118
6.3.2 Break ..............................................................................119
6.3.3 Label...............................................................................119
6.3.4 Goto................................................................................119
6.4 ..............................................................................120
6.4.1 And ( && ) ....................................................120
6.4.2 Or ( || ) ...........................................................121
6.4.3 Not ( ! ) ..........................................................121
6.5 ..........................................................................122

9
6.6 ..........................................................................................124

7 ....................................................................................129
MATHEMATICA ...........................................................129
7.1 .......................................................129
7.2 ............................................131
7.3 ....................................................................132
7.4 ..............................................................................132
7.5 ...........................................................................132
7.6 .....................................................133
7.7 ........................................................134
7.8 ................................................134

8 ....................................................................................137
...................................................................................137
8.1 .........................................................................138
8.2 ....................................................................141
8.3 .......................................................142
8.3.1 Map.................................................................................143
8.3.2 Apply..............................................................................145
8.4 ............................................146
8.5 ............................................................................149

9 ....................................................................................153
..........................................................................................................153
9.1 .............153
9.2 .....................155
9.3 ...................................................................................157
9.4 ........................159
9.5 ........................................................................161
9.6 .............................163
9.7 .......................164
9.8 ..........165
9.9 ..........................................................................167

10

10 .................................................................................169
.........................................169
10.1 ....................................................................170
10.2 ..................................................173
10.2.1 .................................................................................174
10.2.2 ...............................................................................175
10.2.3 .......................................................................175
10.3 (Fitting) ..................................................176
10.4 (Interpolation) .............................................................182
10.5 Fourier ..............................................186

11 .................................................................................191
........................................................191
11.1 .............................................................................192
11.2 ............................................................................197
11.3 .......................................................................................199

12 .................................................................................203
................................................203
12.1 .....................................................................203
12.2 ....................................................................205
12.2.1 Begin....................................................205
12.2.2 BeginPackage ......................................207
12.2.3 .........................................................209
12.3 ...................................................210
12.4 ....................................................................................212

...................................................................................213
.............................................................................217

1.1

11

1.1

,
.

.
. ,
,
, ,
,
.
,
,

,
. Mathematica.
Mathematica
.
,
. , Mathematica

12

, .
Mathematica ,
,
,
, . ,
Mathematica
.
Mathematica .

Mathematica,
.
WINDOWS.
Mathematica
.

: Mathematica
.
,
(editor) Mathematica.
, ,
. Mathematica,
, (kernel),
.
, .

: ,
, File
Exit ,
AltF4 .
,
.

1.2

13

(Yes),
.

1.2
Mathematica, ()
, :

+
*
/
^
=
!

()

==
<
<=
>
>=
!=

()


,
. (,
, ==.
). ,
, .
, ,
ShiftEnter, ..
2+7 ShiftEnter
Enter
.

In[1]:= 2+7

14
Out[1]=9

Mathematica ,
.
, ..
In[2]:=8-3
Out[2]=5
In[3]:=4*6
Out[3]=24
In[4]:=24/6
Out[4]=4
In[5]:=2^3
Out[5]=8
,
True () False ().
:
In[6]:= 2>7
Out[6]=False
In[7]:= 2<=7
Out[7]= True
In[8]:=2^3==8
Out[8]=True
In[9]:=2^3!=8
Out[9]= False
;,
,
2+7; ShiftEnter
In[10]:= 2+7;

1.3

15


, ..
.
Mathematica ,
, . ,

1 , .
, ( ), ,
, ..
In[11]:= 3 + 5 + (2/3) + (2 2)
38
.
Out[11]=
3
In[12]:=(5+7*I)/(2+3*I)
31 I
Out[12]=
13 13
In[11] In[12] ,
, .

1.3

, . ,
, ,
, Mathematica
. ,
Mathematica
, .

, ..
In[1]:=3+ 5 + (2.0 / 3) + (2^2)
Out[1]=12.6667

16
In[2]:=(5.0+7*I)/(2+3*I)
Out[2]= 2.38462 - 0.076923 I

: ,
, .
,
[expression], ..
In[3]:= N[3 + 5 + (2/3) + (2 2) ]
Out[3]= 12.6667.
, Numeric,
. Mathematica,
.
,
Function[argument]
Function argument.

argument //Function
argument //
Function.
In[3]
In[4]:= 3 + 5 + (2/3) + (2 2) //N
Out[4]= 12.6667.
Mathematica 16 (

).

[expression, n]

1.4

17

n , n
16. ..
In[5]:=N[1/3, 30]
Out[5]=0.333333333333333333333333333333
30 . n 16,
16 .
, Mathematica
In[n] Out[n] , n
. , , ,
, .. In[n], n
, ..
In[6]:= In[4] + 1.7
Out[6]=14.3667.

%n, Out[n], n ,
In[7]:=%6 - 1.7
Out[7]=12.6667.
,
%, -
%%. ,
, [%n] %n // .

1.4
Mathematica
, ..

18

Pi,

3.14159.....

() , e 2.718281828459...

, I = 1
/180,
.

Degree,
Infinity,

,
,
In[1]:= Pi^2 + I*E^3 // N
Out[1]= 9.8696 + 20.0855 I.
In[2]:=Exp[2]-E^2
Out[2]=0
In[3]:=N[Pi, 40]
Out[3]=3.141592653589793238462643383279502884197

1.5
Mathematica
,
. ,
:
Sqrt[x]

Log[x]

(
x)

Exp[x]

, e x

Cos[x], Sin[x], Tan[x]


ArcCos[x], ArcSin[x], ArcTan[x]
Cosh[x], Sinh[x], Tanh[x]
ArcCosh[x], ArcSinh[x], ArcTanh[x]

1.5

Re[z], Im[z]
Arg[z]
Abs[z]
Conjugate[z.]
Max[x, y, ]
Min[x, y, ]
Mod[x, y]

19


, z




{x, y, }

{x, y, }

x y.

,
.
In[1]:=Sqrt[4]*Log[7]/Exp[3]
Out[1]=(2 Log[7]) / [e^3)
In[2]:=Sqrt[4]*Log[7]/Exp[3]//N
Out[2]=0.193762
In[3]:= Sqrt[2.1]*Log[7]*Sin[2.8]/(Exp[2.0]*Re[2.3 + I*5])
Out[3]= 0.0555834
.
,
, Degree, , ..
In[4]:=Cos[/4]
1
Out[4]=
2
In[5]:=Cos[45 Degree]
1
Out[5]=
2

20

: Mathematica ,
(
) Mathematica
, .. log[12.5] Log[12.5],
.
1.1

) cos(3/2)
) sinh(8)
) arcsin(2/3)
) log( 2 )
) 3100

) 100

) tan(/5)

50 + sin(45 )

1.2

) cos(3/2)
) cos(/10)
) tan(/5)
) cos(75 D )
) 3100

) tan(75 D )

1.3
100
) cos(3/2)
) cos(/10)
) tan(/5)
) cos(75 D )
) 3100

) tan(75 D )

) 100

50 + sin(45 )

1.6
Mathematica, ,
().

1.6

21

,
$.
,

(+, -, *, /, ...) . , ..
height

width height*width,

heightwidth ,
heightwidth, .
In[1]:=name*Sin[2]*Sqrt[2]/Cos[4]
Out[1]=

2 name Sec[4] Sin[2]

In[2]:=name*Sin[2]*Sqrt[2]/Cos[4] // N
Out[2]=-1.96734 name

,
, ,
, =,
name = value
value name.
In[3]:= a = 3.1
Out[3]= 3.1
name

,
In[4]:= a^2 - 1.
Out[4]= 8.61
, ,
, .

22

1.7


name =.

Clear [name]

name
. ,
, . ..
In[1]:= a =.

In[2]:= Clear[a]


ClearAll[Global`*]
: `
~).

,
.
,
Remove[name]
..
In[3]:= Remove[a]

Remove[Global`*]

1.8

23

Mathematica,

KernelQuit_KernelLocal.
.

.

1.8

( ).
, Enter
. ,
(ShiftEnter),
In[1]:=a = Sin[1.7] Enter
b = Log[27.1] Enter
c = Sqrt[7.73] Enter
d = Im[17.2 + I 82.3] ShiftEnter
Out[1]=0.991665
Out[2]=3.29953
Out[3]=2.78029
Out[4]=82.3
,
;, ..
In[5]:= a = Sin[1.7]; b = Log[27.1]; c = Sqrt[7.73];
d = Im[17.2 + I*82.3] ShiftEnter
Out[6]=82.3

, d=82.3, ;
.

24

1.9
Mathematica,
.

.
, ,
,
.
FilePalettes, .
,
.
AlgebraicManipulation BasicInput.
,
.
AlgebraicManipulation
.
, ,

. :

Expand:
, ..
In[1]:= Expand[(x+y)^2]
( (x+y)^2//Expand )
Out[1]= x 2 + 2 xy + y 2

(a+x)^2
.
,

Expand .

Factor: , ..

1.9

25

In[2]:= Factor[%]
Out[2]= ( x + y ) 2
Out[1]
Factor .

Together:
.
In[3]:= Together[1/(x + 1) + x/(x^2 - 1)]
1 + 2x
Out[3]=
(1 + x)(1 + x)

Apart:
.
In[4]:= Apart[%]
1
3
Out[4]=
+
2(1 + x) 2(1 + x)

Cancel:
.
In[5]:= Cancel[(x^2 + 4x)/(x^2 - x) + (x^2 + 3x - 4)/(x^2 -1)]
4+ x 4+ x
Out[5]=
+
1+ x 1+ x

Simplify:

In[6]:=Simplify[18 + 39 x + 29 x^2 + 9 x^3 + x^4]
Out[6]=(3 + x)^2 (2 + 3 x + x^2).

FullSimplify: Simplify
.
In[7]:=FullSimplify[18 + 39 x + 29 x^2 + 9 x^3 + x^4]
Out[7]= (1 + x) (2 + x) (3 + x)^2.

26

TrigExpand:
.
In[8]:=TrigExpand[Sin[2ArcTan[t]]]
2t
Out[8]=
1+ t2

TrigFactor: .
In[9]:=TrigFactor[Cos[x] Cos[y] Sin[x] - Sin[x]^2 Sin[y]]
Out[9]= Cos[x + y] Sin[x]

TrigReduce: .
In[10]:=TrigReduce[Cos[x] Cos[y] - Sin[x] Sin[y]]
Out[10]= Cos[x+y]

ExpToTrig:
.
In[11]:=ExpToTrig[I (Exp[-I z]- Exp[I z])/2]
Out[11]= Sin[z]

TrigToExp:
.
In[12]:= TrigToExp[ArcTanh[x]]
1
1
Out[12]= Log[1 x] + Log[1 + x]
2
2

1.4
Mathematica
:
) sin( x y )
) cot( x y )
) cos( x y )

) sin(4 x)

) tan( x y )

) cos(4 x)

1.10 -

27

1.5
Mathematica :
) cos 2 ( x) + sin 2 ( x) = 1
) 2 cos 4 ( x) + 2 sin 4 ( x) + sin 2 (2 x) = 2
) cos 4 (x) - 6 cos 2 (x) sin 2 (x) + sin 4 (x) - sin(4 x) = 0

1.10 -
Mathematica,

. Mathematica,
. , { },
, .
In[1]:= lista = {a1, 2.1, {x, y}, Sqrt[2], I, Sin[x]}
Out[1]= {a1, 2.1, {x, y},

2 , i, Sin[x]}

, lista , ,
... .
,
, .
In[2]:= a = {{x, y, I}, {2.1, Sqrt[3], Cos[1.27]}}
Out[2]={{x, y, i}, {2.1,

3 , 0.296281}}

2 3 .
, ,
. Mathematica (i, j)
, .. a[[i, j]] (i, j)
. , .. lista[[i]].
(i, j) a

28

a[[i, j]].
:
a[[i, j]] =
a

In[3]:=TableForm[a] a//TableForm

In[4]:=MatrixForm[a] a//MatrixForm

, Input Create
Table/Matrix/Palette. ,
, ..
. ,
ControlEnter, Control, (Control
). ,
Delete.
1.6
3 4 , 4 4 , 1 10 , 5 3 ,
.

1.11 Table
,
.
,
Table.
, ,

1.11 Table

29

a[[i, j]] f[i, j],



a = Table[ f[i, j],{i, 1, imax}, {j, 1, jmax} ]
{i, 1, imax}, Table, i
1 imax. {i, imin,
imax, i}, i imin imax i.
{{f[1, 1], f[1, 2], }, {f[2, 1], f[2, 2],
}, }. Table .
:
, (i + j ) / 2 ,
:
In[1]:=ta = Table[(i + j)/2.0, {i, 1, 3}, {j, 1, 5}]
Out[1]={{1., 1.5, 2., 2.5, 3.}, {1.5, 2., 2.5, 3., 3.5}, {2., 2.5, 3., 3.5, 4.}}
1.7
3 4 , 4 4 , 1 10 , 5 3
() sin(i j ) , ()

i j .


.

a = Table[{ f1[i], f2[i], ... fn[i]}, {i, 1, imax} ]
{{f1[1], f2[1], }, {f1[2], f2[2], }, }
..
In[2]:=ta1 = Table[{i, Sqrt[i] // N, Sin[0.3 i]}, {i, 1, 3}]
Out[2]= {{1, 1., 0.29552}, {2, 1.41421, 0.564642}, {3, 1.73205, 0.783327}}

30

1.8
3 2 , 10 2 ,
i / 10 ,

sin(2 i ) , ()

()

2i , i .

:
.
.
Mathematica ( )
, .
, , , ..
In[3]:=[[1]] = 2.1

From In[3]:=Set::"noval": "Symbol A in part assignment does not have
an immediate value."
Out[3]= 2.1
.
A
. ,
n m ,
Table[Null, {n}, {m}]
33
In[4]:=A = Table[Null, {3}, {3}]
Out[4]={{Null, Null, Null}, {Null, Null, Null}, {Null, Null, Null}}.
.

1.12

31

1.12
, x = x0
x0 x
x.
x,
.. f[x] x = x0,
x .
, /.
f[x] /. x x0

, a={x x0, y y0, ...},

f[x, y, ...] /. {x x0, y y0, ...}
f[x, y, ] /. a
:
In[1]:= Sin[0.1x] /. x -> Pi
Out[1]= 0.309017
In[2]:= x^2 y /. {x -> 1, y -> 2}
Out[2]= 2

1.13
:
, ?name
??name, name
( ??name, ,
).
In[1]:=?Log
"Log[z] gives the natural logarithm of z (logarithm to base e).

32
Log[b, z] gives the logarithm to base b."

Mathematica
. ,
F1.
.
,
.
Mathematica.
: .

: (* *), Mathematica .
, (* *),
.
In[2]:=2^3 (* 2 *)
Out[2]=8
FormatStyleText
.

:
Alt . ,
.
, ,
Mathematica,
Mathematica KernelQuit_KernelLocal.
.
,
,
,
.

1.13

33

:

KernelEvaluationEvaluate Notebook.
:
, ,
KernelDelete All Output.
.
,

.
:
File
Save as

. .nb
(notebook).

34

2.1

35


,
. Mathematica
,
, .
,
9.

2.1
Mathematica
.
f ( x)
Plot[f[x], {x, xmin, xmax}]
(xmin,
xmax).
:

x sin( x) , (0, 4),

36
In[1]:=g1 = Plot[Sqrt[x]*Sin[x], {x, 0, 4Pi}]

2
1

10

12

-1
-2
-3


,
Plot[{f1[x], f2[x], ...}, {x, xmin, xmax}]
(f1(x), f2(x), ...) ,
.
:
In[2]:=g2 = Plot[{Sin[x], Cos[x]}, {x, 0, 4Pi}]

2.1

37

0.5

10

12

-0.5

-1

2.1.1
, .. g1, g2, ...,

Show[g1, g2, ...]
:
g1 g2 ,

In[3]:=Show[g1, g2]

38

2
1

10

12

-1
-2
-3

, GraphicsArray,
,
Show[GraphicsArray[{g1, g2,...}]]
..
In[4]:=Show[GraphicsArray[{g1, g2}]]

1
2
0.5

1
2

10

12

-1
-2

-0.5

-3

-1

10

12

2.1

39

2.1
, (-5, 5),
, () , () :
) x3 cos(x),
4

) tan(x),

) x -3x ,
) x+2 sin(2 x),

) sin(x)/x,
) x2+3x+5.

2.1.2
,
x y ,
t, .. (x(t), y(t)).
ParametricPlot[{x[t], y[t]}, {t, tmin, tmax}]
:
(cos(t), sin(t)), (0,
2), ,
In[5]:=ParametricPlot[{Cos[t], Sin[t]}, {t, 0, 2Pi}]
1

0.5

-1

-0.5

0.5
-0.5

-1

40

2.2
, {-5, 5},
:
) {t, t3 cos(t)},

) {t, t2+3t+5},

) {t, t4-3t2},

) {t-0.5 sin(t), 1-0.5 cos(t)}.


) .

) {t, t+2 sin(2 t)},

2.1.3
{xi, yi}
, a={{x1, y1}, {x2, y2}, {xn, yn}},
, xi , , yi ,

ListPlot[a]
:
Table, , ..
In[6]:=a = Table[{i, Sin[0.1 i]}, {i, 0, 10, 0.2}];
sin(0.1x),
(0, 10).
In[7]:=ListPlot[a]

2.2

41

0.8

0.6

0.4

0.2

10

: {yi},
, a={y1, y2, yn}, ListPlot[a]
, a={{1, y1}, {2, y2}, {n, yn}},
yi xi=i.
2.3
,
(0, 15), 0.2, ,
:
) x3 cos(x),
4

) x -3x ,
) x+2 sin(2 x),

) tan(x),
) sin(x)/(x+1),
) x2+3x+5 .

2.2
Mathematica
.

.

42

Plot3D[f[x, y], {x, xmin, xmax}, {y, ymin, ymax}]


f(x,y)
( x min, x max) ( y min, y max) .
:
cos(x y) , :
In[1]:= Plot3D[Cos[x y], {x, 0, 2Pi}, {y, 0, Pi}]

1
0.5
3

0
-0.5
-1
0

1
4
6

2.2.1 (ContourPlot)
,
,
. xy
, (ContourPlot).
:

2.2

43

ContourPlot[f[x, y], {x, xmin, xmax}, {y, ymin, ymax}]


:
cos(x y) ,
In[2]:=ContourPlot[Cos[x y], {x,0, 2Pi}, {y, 0, Pi}, PlotPoints80]

2.5

1.5

0.5

0
0

,
,
.
ContourShading
False, .. ,
In[3]:=ContourPlot[Cos[x y], {x,0, 2Pi}, {y, 0, Pi}, PlotPoints80,
ContourShadingFalse]

44

2.5

1.5

0.5

0
0

2.2.2 (DensityPlot)
,
,
,
.
:
DensityPlot[f[x, y], {x, xmin, xmax}, {y, ymin, ymax}]
In[4]:=DensityPlot[Cos[x y], {x,0, 2Pi}, {y, 0, Pi}, PlotPoints80,
MeshFalse]

2.3

45

2.5

1.5

0.5

0
0

2.4
,
(CountorPlots) (DensityPlots),

) sin(x+y), {0, }, {-, }
) x sin(y)+y cos(x), {0, 2}, {-, },
) sin(x) cos(y), {0, 2}, {-, }.

2.3
, Plot,
.
.
Options[Plot], :

46

Options@PlotD

:AspectRatio

1
, Axes Automatic,
GoldenRatio
AxesLabel None, AxesOrigin Automatic,
AxesStyle Automatic, Background Automatic,
ColorOutput Automatic, Compiled True,
DefaultColor Automatic, Epilog 8<, Frame False,
FrameLabel None, FrameStyle Automatic,
FrameTicks Automatic, GridLines None,
ImageSize Automatic, MaxBend 10.,
PlotDivision 30., PlotLabel None, PlotPoints 25,
PlotRange Automatic, PlotRegion Automatic,
PlotStyle Automatic, Prolog 8<, RotateLabel True,
Ticks Automatic, DefaultFont $DefaultFont,
DisplayFunction $DisplayFunction,

FormatType

$FormatType, TextStyle

$TextStyle>

,
Plot:
Plot[f[x], {x, xmin, xmax}, Option->{}]
Option
, .

.
Help Mathematica .

. y
PlotRange{ymin, ymax}, ..
In[1]:=fg1=Plot[Tan[x], {x, 0, 4Pi}, PlotRange -> {-2, 2}]

2.3

47

2
1.5
1
0.5
2

10

12

-0.5
-1
-1.5
-2

y
, PlotRangeAll.
In[2]:=fg2=Plot[x^2 Sin[x], {x, 0, 4Pi}, PlotRange -> All]
x-
y-
PlotRange{{xmin, xmax}, {ymin, ymax}}.

. Mathematica
.
PlotPoints/.Options[Plot].

,
PlotPointsnumber.
2.2, In[1],
50 ,
In[3]:=fg3=Plot3D[Cos[x y], {x, 0, 2Pi}, {y, 0, Pi}, PlotPoints -> 50]

48

1
0.5
0
-0.5
-1
0

3
2

1
4
6

:
AspectRation/m, n/m
y/x, ..
In[4]:=fg4=Plot[x^2 Sin[x], {x, 0, 4Pi}, AspectRatio-> 1/2.5]

50
25
-25

10

12

-50
-75
-100
-125

:
AxesFalse, ..
In[5]:=fg5=Plot[x^2 Sin[x], {x, 0, 4Pi}, Axes -> False]

2.3

49

:
FrameTrue, ..
In[6]:=fg6=Plot[x^2 Sin[x], {x, 0, 4Pi}, Frame -> True]

50
25
0
-25
-50
-75
-100
-125
0

10

12

:
AxesLabel{x-label, y-label}, ..
In[7]:=fg7=Plot[10 t, {t, 0, 10}, AxesLabel -> {"time", "velocity"}]

50
velocity
100
80
60
40
20

time
2

10

:
PlotLabelTitle of the Figure, ..
In[8]:=fg8=Plot[10 t, {t, 0, 10}, PlotLabel -> "Velocity vs time"]
Velocity

vs time

100
80
60
40
20

10

:
. AxesOrigin{x0, y0}, ..
In[9]:=fg9=Plot[10 t, {t, 0, 10}, AxesOrigin -> {0, 10}]

2.3

51

100
80
60
40
20
0

10

:
PlotStyleRGBColor[n1,n2,n3], {n1, n2, n3}

(R=Read), (G=Green) (B=Blue) ,
..
In[10]:=fg10=Plot[10 t, {t, 0, 10}, PlotStyle -> RGBColor[1, 0, 0]]

:
PlotStyleThickness[n], 0<n<1,
, ..
In[11]:=fg11=Plot[Sin[x], {x, 0, 4Pi}, PlotStyle -> Thickness[0.01]]
1

0.5

2
-0.5

-1

10

12

52

: ListPlot,
d PlotStylePointSize[d], ..
In[12]:=fg12=ListPlot[Out[6], PlotStyle -> PointSize[0.02]]
0.8

0.6

0.4

0.2

10

: ListPlot,
, PlotJoinedTrue, ..
In[13]:=fg13=ListPlot[Out[6], PlotJoined -> True]
0.8

0.6

0.4

0.2

10

2.3

53

2.5

3.1-3.3, .
2.6
(Help) Mathematica
Ticks .

, 5 , 10 , 20 x.

x sin( x) ,

54

3.1

55

3
MATHEMATICA

,
Mathematica, , .. ,
, , ,
, ...
, Mathematica
.

3.1
Mathematica
i2

i2

i1

f [i ]

f [i] .
i1

:
Sum[f[i], {i, i1, i2}]

Product[f[i], {i, i1, i2}]

:
In[1]:=Sum[i^10, {i,1,10}]
Out[1]=14914341925
In[2]:=Sum[x+i,{i,0,3}]
Out[2]=6+4x
In[3]:=Product[i^10, {i,1,6}]
Out[3]=37439062426244874240000000000

MATHEMATICA

56
In[4]:=Product[x+i, {i,0,3}]
Out[4]=x(1+x)(2+x)(3+x)
In[5]:=Sum[Sqrt[n],{n,1,5}]
Out[5]= 3 + 2 + 3 + 5
In[6]:=Product[Sqrt[n], {n, 1, 15}]
Out[6]= 30240 1430

,
Mathematica ,
NSum NProduct.
In[7]:=NSum[Sqrt[n], {n,1,5}]
Out[7]=8.38233
In[8]:=NProduct[Sqrt[n],{n,1,15}]
Out[8]= 1.14354 10 6
:
N
, . ..

NSum[c*Sqrt[n], {n, 1, 15}] NSum[Sqrt[n], {n, 1, c}]


c .
3.1
,

1
,

n =1 n( n + 1)

n
n =1

10

n =1

10

1
,

6
n =1 n

n =1

n(n + 1) ,
10

1
,
n
1

n
n =1

3.2

57
10

n =1

n =1

e
10

1
,

n =1 n( n + 1)( n + 2)

n(n + 1)(n + 2) .
n =1

3.2
f(x), x0, :
Limit[ f[x], x-> x0 ]
:
In[1]:= Limit[x*Sin[1/x], x -> 0]
Out[1]= 0
In[2]:=Limit[((Exp[x]-1)/x)^(1/x), x0]
Out[2]= e
In[3]:=Limit[x^(1/x), xInfinity]
Out[3]=1

x0.
cos( x) / x x 0 .
,
In[4]:=Plot[Cos[x]/x,{x,-Pi/2,Pi/2}];

MATHEMATICA

58
60
40
20

-1

-0.5

0.5

-20
-40
-60


+ x,
.
x0
Limit[ f[x], x-> x0, Directionx1 ]

x1. :
In[5]:=Limit[Cos[x]/x, x0, Direction-1]
Out[5]=
In[6]:=Limit[Cos[x]/x, x0, Direction1]
Out[6]=
3.2
,

sin( x)
,
x 0
x

) lim

1
1
,
) lim

x 1 x 1
ln( x)

) lim
x 0

x+9 3
,
x

ln( x) ln(a)
,
x >a
xa
x 1
) lim
,
x 1 ln( x )
) lim

) lim
x

tan(8 x)
,
tan(5 x)

3.3

59

) lim(e x 1)1 / ln( x ) ,

a
) lim1 + ,
x
x

x 0

1 cos( x)
x 0
x

) lim( x 3 exp(2 x)) .

) lim

3.3
() f[x] x
Mathematica :
D[f[x], x]
In[1]:= D[Sin[3x^2 + Pi/4], x]
Out[1]= 6x Cos[/4 + 3 x^2]
In[2]:=D[x^4 Sin[x]/(1+x^2) , x]
Out[2]=(x^4 Cos[x])/(1 + x^2) -(2 x^5 Sin[x])/(1 + x^2)^2 +
(4 x^3 Sin[x])/(1 + x^2)
n- x :
D[ f[x], {x, n}]
In[3]:= D[Sin[3x^2 + Pi/4], {x, 2}]
Out[3]= 6Cos[/4 + 3 x^2] - 36x^2 Sin[/4 + 3 x^2]

,


... f [ x, y,...] , :
x y
D[f[x,y,...], x, y,...]

In[4]:= D[x^2 y^3, x, y]


Out[4]= 6x y^2

MATHEMATICA

60
3.3
.
)

d ln(1 / x)
,
dx

d 1/ x
x ,
dx

d
( x 2 + 1)ln x ,
dx

d2 3
x x ln x ,
dx 2

d ln x
,

dx x 2
d

sin 2 3x 2 + ,
dx
4

d
1 x
(1 + ) ,
dx
x

d x ln( x)

+ ln(1 x) .

dx 1 x

3.4

, .

Mathematica.

3.4.1
,

f [ x]dx ,


Integrate[f[x], x]

.
In[1]:=Integrate[1/(1+x^2), x]
Out[1]=ArcTan[x]
,

In[2]:=D[%, x]

3.4

61

Out[2]=1/(1 + x^2)

In[3]:=Integrate[x^2/(1+x^2), x]
Out[3]=x-ArcTan[x]
In[4]:=Integrate[Sqrt[x^2+1], x]

Out[4]= (1 / 2) x 1 + x 2 + ArcSinh[ x] / 2

. ,
, NIntegrate
.

3.4

,
)

(x

tan

1+ e

3x + 2)dx ,
2

) sin( x) exp( x)dx ,

xdx
,
2
( x)

( x )dx ,

cos

sin

cosh

dx
x

1 + ln x
dx ,
x2

dx
exp( x) 1

sin( x)dx
1 + cos( x)

dx
,
4
( x)
dx
4

( x)

MATHEMATICA

62

3.4.2
,

dx dy... f [ x, y,...] , :
Integrate[f[x, y,...], x, y, ...]

.

, ,
.
In[5]:=Integrate[x^2 y^3, x, y]
Out[5]=(x^3 y^4)/12
In[6]:=D[%, x, y]
Out[6]=(x^2 y^3)

3.5

,

dx dy x
) dx dy x

y2 ,

sin( x + y ) ,

dx dy x sin( x + y) ,
) dy dx x sin( x + y ) .

3.5
, ,
.

3.5

63

3.5.1

x2

f [ x]dx

Integrate, ,

x1

, x
:
Integrate[f[x], {x, x1, x2}]
,
,
, NIntegrate, :
NIntegrate[f[x], {x, x1, x2}]

, (. , 3.1).
:
In[1]:= Integrate[1/(1 + x^2), {x, 0, }]
Out[1]= /2
In[2]:=%//N
Out[2]=1.5708
In[3]:=NIntegrate[1/(1+x^2), {x, 0, 100}]
Out[3]=1.5608

3.6
,
2

(
1

2x 1 2
) dx ,
x

x ( 2 x 1)dx ,
2

x3
0 exp( x) 1 dx ,

x 4 exp( x)
0 (exp( x) 1) 2 dx ,

MATHEMATICA

64

2 x
x e dx ,
2

10

x
e dx ,
2

exp(t )t

x 1

dt ,

x4/3
0 exp( x) 1 dx ,

10

1
0 1 + x 2 dx ,

x3
0 exp( x) 1 dx .

3.5.2
,
x2

y2

x1

y1

dx dy... f [ x, y,...] , :
Integrate[f[x, y, ], {x, x1, x2}, {y, y1, y2}, ]

. , ,

, ,
.
:
In[4]:= Integrate[x^2 y^3, {x, 0, 1}, {y, 0, 1}]
Out[4]= 1/12
In[5]:=%//N
Out[5]=0.0833333
In[6]:=NIntegrate[x^2 y^3, {x, 0, 1},{y, 0, 1}]
Out[6]=0.0833333
In[7]:=Integrate[x^2 y^3, {x,0,1} ,{y, 0, x}]
Out[7]=1/28
In[8]:=NIntegrate[x^2 y^3, {x,0,1}, {y, 0, x}]
Out[8]=0.0357143

3.5

65

3.7
, ,
,
1

)
)
)

)
)

dx dy x
0

y ,

dx dy sin( x y) ,
0

dx dy sin( x y) ,
0

dx dy sin
0

dx dy sin

2 2
dx dy x y ,
0

( x) sin 2 ( y ) ,

)
)

( x) sin 2 ( y ) ,

dx dy x exp( x y) ,
dx dy x exp( x y) ,

dx dy sin( x + y) exp( x + y) ,
0

dx dy sin( x + y) exp( x + y) .

3.5.3
,
, ,
.

.

exp[a x] dx ,
0


a > 0 .
, , :
Integrate[f[x], {x, x1, x2}, Assumptions{conditions}]

MATHEMATICA

66

, conditions, ,
, .

In[9]:=Integrate[Exp[-a*x^4],{x, 0, Infinity}, Assumptions{a>0}]


Out[9]= (Gamma[5/4]/a^(1/4))
In[10]:=Integrate[(t^(a - 1))((1 - t)^(b - 1)), {t, 0, 1},
Assumptions {Re[a] > 0, Re[b] > 0}]
Out[10]=(Gamma[a] Gamma[b]) /Gamma[a + b]
3.8
,

exp(a x)dx , Re[a]>0,

xa
0 exp( x) 1 dx , Re[a]>0,

1
0 x a dx , Re[a]>1,

exp(a x) sin(b x) dx ,

Re[a]<0,

Im[b]=0,

exp(b x 2 )dx , Re[a]>-1,

exp(a x

) sin(b x) dx , Re[a]<0,

Re[b]>0,

Im[b]=0,

a
x exp(b x)dx , Re[a]>-1,
0

x
a

1
dx ,
x 1
2

Re[b]>0,

a>1,
Re[c]>-1.

3.6
f[x]
Series [f[x], {x, x0, n}]

3.7

67

x0 n
.
In[1]:= =Series[x Exp[x^2 + 1], {x, 0, 6}]
Out[1]=e x+e x3+e x5/2+O[x]7
O[x]7
. O[x]7
,
.
O[x]7, , Normal
In[2]:= Normal[a]
Out[2]=e x+e x3+e x5/2
In[3]:=Series[Sin[x]/x, {x,0,10}]//Normal
Out[3]=(1 - x^2/6 + x^4/120 - x^6/5040 + x^8/362880 - x^10/39916800)
3.9
, , ,
:
) sin(x),
1
)
,
1 + arctan( x)
) arcsin(x),

) exp(arccos( x)) ,

) tan(x),

x2/3 + 2 ,

) arctan(x),

1 + sin 2 ( x ) ,

1
,
1 + sin x

1+ x
) ln(
).
1+ x2

3.7

, f(x), x=x0.

68

MATHEMATICA
FindRoot[f[x] == 0, {x, x0}]
In[1]:= r=FindRoot[(3 - x) Exp[x] -3== 0, {x, 3}]
Out[1]= {x -> 2.82144}
:
1. , ==,
( ),
(=)
( ).
2. , r,
, {x ->
2.82144}, x=2.82144.
,

.

. , .. a
:
a=x/.r
3. FindRoot, , ,
Newton-Raphson, .


,
x ,

FindRoot[f[x] == 0, {x, xmin, xmax}]


:
In[2]:=FindRoot[-x^3+2x+3==0, {x, 0}]
FindRoot::cvnwt:

3.7

69

Newton's method failed to converge to the prescribed accuracy after 15


iterations.
Out[2]={x -0.441029}
(0, 2),
,
In[3]:=FindRoot[-x^3+2x+3==0, {x, 0, 2}]
Out[3]={x 1.89329}
: , FindRoot,
,
,
.
:

f(x) = -x 3 + 2x + 3 ,

, ,
In[4]:=Plot[-x^3+2x+3, {x,-2,2}]

-2

-1


x=1.9. x=1.9 ,
,

70

MATHEMATICA
In[5]:=FindRoot[-x^3+2x+3==0, {x, 1.9}]
Out[5]={x1.89329}

3.10
:
) x4-3xex+5,
) x5-x4sin(x)+4x2-4,
) x12+cos(x)-x2-12,
) x6+5x5-27.

3.8
f[x],
x=x0 {xmin, xmax},
(
,
).
FindMinimum[f[x], {x, x0}]

FindMinimum[f[x], {x, xmin, xmax}]


In[1]:=FindMinimum[x+5*Sin[x], {x, 2Pi}]
Out[1]={-0.387948, {x4.51103}}
In[2]:=FindMinimum[x+5*Sin[x], {x, 4, 5}]
Out[2]={-0.387948, {x4.51103}}
, ,
( ),
( ).
,
,
, ,
x=x0
.

3.8

71

:
x + 5 sin( x) ,
In[3]:=Plot[x+5Sin[x],{x, 0, 8Pi}];
25
20
15
10
5

10

15

20

25

,
x=(2n+1), n=0, 1, 2, . Table FindMinimum
,

In[4]:=Table[FindMinimum[x+5*Sin[x],{x, Pi*(2i+1)}],{i,1,4}]
Out[4]={{-0.387948, x4.51103}}, {5.89524, {x10.7942}},
{12.1784, {x17.0774}}, {18.4616,{x23.3606}}}
FindMinimum
, -f[x], ..
In[5]:=FindMinimum[-(x+5*Sin[x]), {x, 2Pi}]
Out[5]={-12.9543, {x8.05534}}
.
5.0, Mathematica
,

FindMaximum[f[x], {x, x0}]

MATHEMATICA

72

FindMaximum [f[x], {x, xmin, xmax}]


In[6]:=FindMaximum[x+5*Sin[x], {x, 2Pi}]
Out[6]={12.9543, {x8.05534}}
3.11
,
(0, 2).
,
.
) x 2 + x 4 ,

sin( x)
,
x

) x + x 3 ,

) cos( x)e x ,

1
,
2
x +1

) cos( x) x 2 .

3.9
3.9.1
3.7 FindRoot
, ,
, , .
,
.
f[x] :
NSolve[f[x] = = 0, x]
In[1]:= sol=NSolve[x^5 - 3x^2 - 17x + 51 == 0, x]

3.9

73

Out[1]=sol= {{x -> -2.36785}, {x -> -0.561908 - 2.35185 i}, {x -> 0.561908 + 2.35185 i}, {x -> 1.74583- 0.797366 i}, {x ->1.74583 +
0.797366 i}.
f[x]== 0 ,
, ..
equation = f[x] == 0;

NSolve[equation, x]

In[2]:= equation = -3x^2 - 17x + 51 == 0


Out[2]= 51 - 17 x - 3 x^2 == 0
In[3]:=solution = NSolve[equation, x]
Out[3]= {{x -> -7.83611}, {x -> 2.16944}}
: equation,
,
.
NSolve
, .
, . ..
solution[[2]] , ,
,
In[4]:=solution[[2]]
Out[4]={x -> 2.16944}

, ..
In[5]:= a = x /. solution[[1]]
Out[5]= -7.83611
a .

MATHEMATICA

74

NSolve .
,
Solve,
Solve[equation, x]
In[6]:= Solve[-3x^2 - 17x + 51 == 0, x]
1
1
Out[6]= {{x (17 901)}, {x (17 + 901)}}
6
6
3.12
, ,
:
) x4-3x+5,
) x15-3x6+7x2-7,
) x5-x4+4x2-4,
) x3-4x2-5x+7,
) x4-6x3+17x2-23x-12,
) x12+x7-x2+12,
) x6+5x5-3x+27,
) x3+5.
) x7+6x4-3x3+2x-13,
) x 6 x 4 x + 1

3.9.2
, Solve NSolve
.

NSolve[{eq1, eq2,..., eqn}, {x1, x2,..., xn}]
{eq1, eq2,..., eqn} {x1, x2,..., xn}
.
:
eq1 = f1[{xi}] == 0; eq2 = f2[{xi}] == 0; ...; eqn = fn[{xi}] == 0
.

3.9

75

:
In[7]:= eq1 = -2x + y == 4; eq2 = y == 2x^2;
In[8]:= NSolve[{eq1, eq2}, {x, y}]
Out[8]= {{y -> 8., x -> 2.}, {y -> 2., x -> -1.}}
,

Solve[{eq1, eq2,..., eqn}, {x1, x2,..., xn}]




In[9]:= Solve[{eq1, eq2}, {x, y}]
Out[9]= {{y -> 2, x -> -1}, {y -> 8, x -> 2}}
,
, .. equations, :
equations= {f1(x) == 0, f2(x) == 0,..., fn(x) == 0}
:
NSolve[equations, {x1, x2,..., xn}]
In[10]:= equations = {-2x + y == 4, y == 2x^2}
Out[10]= {-2 x + y == 4, y == 2 x^2}
In[11]:=solutions = NSolve[equations, {x, y}]
Out[11]= {{y -> 8., x -> 2.}, {y -> 2., x -> -1.}}
3.13
, , :
) {2x-3y=40, 4x+6y=60},
) {(m+1)x+8y=4m, m x+(m+3)y=3m-1},
) {x2+y2=25, y-x=1},
) {x2+y2+x y=3, x+y=1},

MATHEMATICA

76

) {x2+13y2=5, x y=2},
) {+x+y+z=3, 2+4x+3y+5z=8, -x-y+z=5, +3x-7y-z=12}.

3.10

equations, solutions,

equations /. solutions
:

,
In[1]:= equations /. solutions
Out[1]= {{True, True}, {True, True}},
.
Mathematica
,
. ,

FullSimplification. ,
,
.

4.1

77

4
A MATHEMATICA

,
Mathematica, (Dot
product Mathematica), (Transpose),
(Det), (Inverse), (Trace), (Eigenvalues)
(Eigenvectors) , ...

4.1
.
,
Random[]
. 3x3 , A B, 1
In[1]:=A=Table[Random[Integer, {0, 9}], {3}, {3}]
Out[1]={{7, 7, 6}, {1, 9, 4}, {5, 4, 0}}
In[2]:=B=Table[Random[Integer, {0, 9}], {3}, {3}]
Out[2]={{0 ,6, 0}, {7, 5, 9}, {8, 6, 2}}

: n m , ,
(0, 9),
a=Table[Random[Integer, {0, 9} ], {n}, {m}].
1

A MATHEMATICA

78

: c*A A
c, c.
In[3]:=3*A
Out[3]={{21, 21, 18}, {3, 27, 12}, {15, 12, 0}}
: , ,
A+B
.
In[4]:=A+B
Out[4]={{7, 13, 6}, {8, 14, 13}, {13, 10, 2}}
: ,
nxm mxk,
: A.B
In[5]:=A.B
Out[5]={{97, 113, 75}, {95, 75, 89}, {28, 50, 36}}

,
, , *. ,
,
, ..
In[6]:=A*B
Out[6]={{0, 42, 0}, {7, 45, 36}, {40, 24, 0}}
: A,
Transpose[A]
In[7]:=Transpose[A]
Out[7]={{7, 1, 5}, {7, 9, 4}, {6, 4, 0}}
:
: Det[A].

4.1

79

In[8]:=Det[A]
Out[8]=-218
:
: Inverse[A].
.
In[9]:=Inverse[A]
Out[9]={{8/109, -12/109, 13/109}, {-10/109, 15/109, 11/109},
{41/218, -7/218, -28/109}}
An: Mathematica, An
n- A, ..
In[10]:=A^3
Out[10]={{343, 343, 216}, {1, 729, 64}, {125, 64, 0}},

.A.A...A, n-.
Mathematica
.
A.A.A....A: A n
MatrixPower[A, n].
In[11]:=MatrixPower[A, 3]
Out[11]={{1088, 2106, 1060}, {566, 1356, 632}, {574, 1096, 518}}
MatrixExp[A]: , A,

/ n! .

n =0

In[12]:=MatrixExp[A]//
, .
: (trace) A Tr[A].
In[13]:=Tr[A]
Out[13]=16

80

A MATHEMATICA

4.1

4 2 x

x
, B = 2 x 1 .
x
1 4 x

, () () .
x (i) , (ii)
, .
C=. D=..

1
A =
x

4.2
4.1 ., ...
MatrixExp[]. , C, D
.

4.2
nxn x b
n, A.x=b.
A b , Det[A]0, x
,
x=LinearSolve[A, b]
:
4.1, b
3,
In[1]:=b=Table[Random[Integer, {0, 9}],{3}]
Out[1]={6,3,1},
A.x=b
In[2]:=LinearSolve[A, b]
Out[2]={25/109, -4/109, 169/218}

4.3

81


In[3]:=Inverse[A].b
Out[3]= {25/109, -4/109, 169/218}
4.3
A x = u x,

1 1
u = (3,1) ,
() A =
3 2
4 2 1

() A = 2 4 1 u = (0, 0,1) .
1 4 2

4.3
4.3.1
:
Eigenvalues[A].
:
4.1
In[1]:=a1=Eigenvalues[A]//N//Chop
Out[1]={14.2183, -3.12487, 4.90654}
,
. a1[[i]] i .
//N, ,
, Chop
, .

A MATHEMATICA

82

4.3.2
:
Eigenvectors[A].
:
4.1
In[2]:=a2=Eigenvectors[A]//N//Chop
Out[2]={{1.93396, 1.13714, 1.}, {-0.38656, -0.298019, 1.},
{2.1913, -1.51248, 1.}}
a2 ,
. a2[[i]] i , a2[[i, j]] j-
, ..
In[3]:=a2[[2]]
Out[3]={-0.38656, -0.298019, 1.}
In[4]:=a2[[2,1]]
Out[4]=-0.38656

3.4.3

: Eigensystem[A].
:
4.1
In[5]:=a3=Eigensystem[A]//N//Chop
Out[5]={{14.2183, -3.12487, 4.90654}, {{1.93396, 1.13714, 1.},
{-0.38656, -0.298019, 1.}, {2.1913, -1.51248, 1.}}}
a3
. a3[[1, i]] i- a[[2, i]] i .

4.3

83

In[6]:=a3[[1,2]]
Out[6]=-3.12487
In[7]:=a3[[2,1]]
Out[7]={1.93396, 1.13714, 1.}

3.4.4 -
-, ..
,
In[8]:=A.a3[[2,2]]==a3[[1,2]]a3[[2,2]]//Simplify//Chop
Out[8]=True
Mathematica
,
. ,
FullSimplification. ,
,

.
4.4
4 4 , A, B, C, ()
,
(), A, B, C
. ,
.
.

84

A MATHEMATICA

5.1

85


Mathematica. ,
()
.
,
.

5.1
,
, ,

. Mathematica,
:
name[x_]: = f[x]
name ,
, _, ,
, ,
,

86

.
f[x].
: name[x]: = f[x], x,
, , _,
Mathematica, name[x]
x ,
name[x].
:
x 3 x 2 sin( x) , cube
f1 ,
In[1]:=cube[x_]:=x^3
In[2]:=f1[x_]:=x^2*Sin[x]
:
name:=Function[{x}, f[x] ]
cube f1
:
In[3]:=cube:=Function[{x}, x^3]
In[4]:=f1:=Function[{x}, x^2*Sin[x]]
:
name:= f[#]&
# &,
. cube f1
:
In[5]:=cube:=#^3&
In[6]:=ff1:=#^2*Sin[#]&

5.1

87

,
,
, ..
In[7]:=cube[2.1]
Out[7]=9.261
In[8]:=f1[4.2]
Out[8]=-15.3746

, .. ,
In[9]:=Plot[cube[x],{x,0,10}]

600
500
400
300
200
100

In[10]:=Plot[f1[x],{x,0,4Pi}]

10

88

50
25
2

10

12

-25
-50
-75
-100
-125




. ..
Sin[x],
In[11]:=Sin[x_]:=x^3/(x^2+1)
SetDelayed :: write : Tag Sin in Sin[x_] is Protected.
Out[11]=$Failed
, Sin Sin[x_]
.
,
,
,
, .

,
:
name[x_, y_, ...]: = f[x, y, ...]
:

5.1

89

name:=Function[{x, y, ..}, f[x, y, ...] ]


:
name := f[#1, #2, ... ]&
#1 , #2 .
:
x2+y2 :
In[12]:= f2[x_, y_] := x^2 + y^2

In[13]:= f2 := Function[ {x, y}, x^2 + y^2 ]

In[14]:= f2 := #1^2 + #2^2 &


f2(x)
In[15]:=D[f2[x, y], x]
Out[15]=2 x
5.1
x 3
, fwrong[ x] = x ^3 ,
, fwrong[x], fwrong[1], fwrong[a].
.
.
5.2

)

sin( x)
,
x

1
1
,
)

x
x

1
ln(
)

x 1
,
ln( x)

tan(8 x)
,
tan(5 x)

90

x+9 3
,
x

) (e x 1) ln( x ) ,
)

1 cos( x)
x

ln( x) ln(2)
,
x2

1
) 1 + ,
x

) ( x 3 exp(2 x)) ,

e x 1

)
x

1/ x

) x1 / x .


(1, 5).
5.3

) sin(x+y)+3sin(x)+5cos(y),
) x sin(y)+y cos(x),
) sin(x) cos(y)+cos(x y).
,
(0, 2 ) ( , ) .

5.2 ()

,
, .
:
Function[{x}, f[x] ][y]

F[#]&[y]

f(x), x=y. ..

5.3

91

In[1]:=Function[x, x^3][2]
Out[1]=8
In[2]:=#^3&[2]
Out[2]=8
x 3 x = 2 ,
. ,
, #^3& Function[x,x^3], ,
, [2] ,

x = 2 .
, ..
In[3]:=Function[{x, y}, x^2+y^2][2, 2]
Out[3]=8
In[4]:=#1^2+#2^2&[2, 2]
Out[4]=8
(anonymous pure).

5.3

:= ( , ). ,
:
,
.


. f[x_]:=a Sin[x]
a
, .
a
.

92

,
(=)
(:=), ..
f[x_]=a Sin[x]

(=),

.
ff1[x_]:=D[Sin[x], x]

ff2[x_]=D[Sin[x], x]
ff1[],
In[1]:=ff1[x_]:=D[Sin[x], x]
In[2]:=ff1[Pi]
General :: ivar : is not a valid variable.
Out[2]= 0

( )
x, x=,
Mathematica.

,
In[3]:=ff2[x_]=D[Sin[x], x]
Out[3]=Cos[x]
In[4]:=ff2[Pi]
Out[4]=-1
.

5.4

93

5.4
:
) f 1( x) = x 2 sin( x) ,

) f 3( x) = If [ x < 1, 1,1]

) f 2( x) = If [ x < a, 1,1] , a=1

) f 4( x) = 2 x 2 + x 4 ,

:= =. ,
(-, ).
( )
, .
5.5
, {x}, {x, x2}.

{x, x2}, x , 0.1.
.
5.6

,
) (#^3)&
) (#, - # ^2+#^4)&
) (# ^ #)&
) If[#>0, #, - #]&

.

5.4
,
(:).
1.12.
f[x] /. x x0
f[x] x=x0.
,

94

x0 x f[x] x
, f[x] x
, x0.
,
1.12,
,
f[x] /. x :> x0
, ,

In[1]:=Table[x,{4}]/.xRandom[]
Out[1]={0.204709, 0.204709, 0.204709, 0.204709}
In[2]:=Table[x,{4}]/.x:>Random[]
Out[2]={0.379264, 0.839952, 0.0745168, 0.859175}
x
Random[] ,

Random[],
.

5.5
, x
f(x) D[f[x], x].


, , Mathematica.
D[f[x],x] f[x]
D[f[x], {x, 2}] f[x], ...

,

5.6

95

f '[ x ^ 2 + y ] x 2 + y , x y.
, x
D[f[x^2+y], x],
In[1]:=Sin'[x^2+y]
Out[1]=Cos[x^2 + y]
In[2]:=D[Sin[x^2+y], x]
Out[2]=2 x Cos[x^2 + y]

x
, Mathematica Dt.
In[3]:=Dt[Sin[x^2+y],x]
Out[3]=Cos[x^2 + y](2 x + Dt[y, x])

5.6

.
Mathematica .
:

5.6.1

, .. x,
:
name[x_type]: = f[x]
(type)
Integer, Real, Rational, Complex, Symbol, String, List.
:

96
In[1]:=fun1[n_Integer] := n!

x.
In[2]:=fun1[3]
Out[2]=6,
x ,
In[3]:=fun1[3.1]
Out[3]=fun1[3.1]
fun1[n], .. n=110, A :
In[4]:=A = Table[ fun1[n], {n, 1, 10}]
Out[4]={1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}
5.7
n, :
) f 3(n) = ln(n) ,
) f 1(n) = n 2 ,
) f 2(n) = n + 1 ,

) f 4(n) = exp(n) .


n=1-10,
.

5.6.2

x.
/;
.
,
In[5]:=step[x_ /; x >= 0] := 1
In[6]:=step[x_ /; x < 0] := -1

5.6

97



In[7]:=step[x_] := 1 /; x >= 0
In[8]:=step[x_] := -1 /; x < 0

x ,
x
.
5.8
1-|x| |x|<1, .
.
5.9
x x0,
x=0. .
5.10
x>0 :
() 1,
() sin(x) ,
() x ,
x<0 :
(i) cos(x) ,

(ii) x 2 .
(-, ).

5.11
:
) x 2 + y 2

) x 2 sin( y ) + y 2 sin( x)

) y 2 x 2 .

(-, ).

98

5.6.3

.
,

, ,
.

: ,

, , ..
In[9]:=step1[x_, x0_:0] := 1 /; x >= x0
In[10]:=step1[x_, x0_:0] := 0 /; x < x0
x0 .
,
x0, ..
In[11]:=step1[2]
Out[11]=1,
,
x0.
In[12]:=step1[2, 3]
Out[12]=0
5.12
x>x0 :
() 1,
() sin(x) ,
() x ,
x<0 :
(i) cos(x) ,

(ii) x 2 .

x0, .
x0.

5.7

99

5.7

, n-
, .
,
.
,
, n!:
In[1]:=f[n_Integer]:=n f[n-1]
In[2]:=f[0]=1
Out[2]=1
In[1] , In[2]
. , f[4] 4!
In[3]:=f[4]
Out[3]=24
, n-
n-1, n-1, n-2 ...
,
, .
,
,
In[4]:=g[n_Integer]:=g[n-1]-g[n-2]
In[5]:=g[1]=1; g[2]=2;
, g[10]
In[6]:=g[10]
Out[6]=-1
, .. f[1.5]=1
In[2],
, Mathematica ,

100

.
$RecursionLimit.
256.
$RecursionLimit=newvalue.
5.13

:
() a n =
()

3a n 1
n

() a n = 3.2 a n 1 (1 a n 1 ) a1 = 0.5

a1 = 2

a n = (a n 1 ) 2 a n 2

a1 = 2 , () a n = a n 1 + a n 2 a1 = a 2 = 1 ,

a2 = 1
() a n =

n
a n 1

a1 = 2

() a n = a n 1

1
sin( 2 * a n 1 )
4

a1 = 0.25 .
, ,
,
.
: f(x),
x0,
Newton-Raphson, ,

x n = x n 1

f ( x n 1 )
,
f ' ( x n 1 )

x=x0. ,
3,
(3 x) exp( x) 3 .
FindRoot.

5.8

101

: A

In[7]:=fa1[x_]:=(3-x)*Exp[x]-3
In[8]:=fa2[x_]=D[f1[x], x]
Out[8]= - e x + e x (3 - x)
In[9]:=xroot[n_Integer]:=xroot[n-1]-fa1[xroot[n-1]] / fa2[xroot[n-1]]
In[10]:=xroot[1]=3.
Out[10]=3.
In[11]:==Table[xroot[n], {n, 1, 8}]
Out[11]={3.,2.85064,2.82235,2.82144,2.82144,2.82144,2.82144,2.82144}
In[12]:=FindRoot[fa1[x], {x, 3}]
Out[12]={x 2.82144}
5.14
,
Newton-Raphson,
,
:
) x4-3xex+5,
) x12+cos(x)-x2-12,
) x6+5x5-27.
) x5-x4sin(x)+4x2-4,
FindRoot.

5.8
,
, Table, Sum,
Product, .
Table, ,
Sum Product
.
, 8.3,
.

102

1:
f[n]= n!, 5.7,
10

n=1, 10,

n!
0

,
In[1]:=Table[f[n], {n, 1, 10}]
Out[1]={1,2,6,24,120,720,5040,40320,362880,3628800}
In[2]:=Sum[f[n], {n, 1, 10}]
Out[2]=4037913
2:
b

f ( x)dx

[a, b] t
h=(b-a)/Nt,
b

Nt

k =1

f ( x)dx = h ( f (a + h k ) + f (a + h (k 1))) / 2. ,
, t=50,
1

dx

In[3]:=Remove[x, s1]
In[4]:=fun1[x_]:=x^2
In[5]:=a=0.;b=1.;
In[6]:=Nt=50;h=(b-a)/Nt;
In[7]:=s1[fun_]:=h*Sum[(fun[a+i*h]+fun[a+(i-1)*h])/2.,{i,1,Nt}]
In[8]:=s1[fun1]
Out[8]=0.3334
1/3,
In[9]:=1/3.
Out[9]=0.333333

5.8

103

3:

.
(standard
deviation).
,
.

1 n
xi ,
n i =1

x=

1
n
( xi x ) 2 ,

i =1
n 1
n .
10.3, Mathematica
, , ,
.

,
.

y :
s = s2 =

y=2.0+ Random[]
50
.

In[10]:=m=50;
In[11]:=A=Table[2+ Random[],{m}];
In[12]:=meanvalue[A_, m_]:=Sum[A[[i]], {i,1,m}]/m
In[13]:=mv=meanvalue[A, m]
Out[13]=2.48473

104

In[14]:=S2[A_, m_]:=Sum[(A[[i]]-mv)^2, {i,1,m}]/(m-1)//Sqrt


In[15]:=sd=S2[A, m]
Out[15]=0.276239

,
Randon[] .
5.15
,
x+5*Sin[x], x=*
=1,,10, .
5.16
b

f ( x)dx

[a, b]
,
, k,
b

S1 = f ( x)dx =
a

ba
b a N 1
k) ,
f (a +

N
N 0

S 2 = f ( x)dx =
a

ba
ba N
k) .
f (a +

N
N 1

S = ( S1 + S 2 ) / 2 .
,
, =5, 10, 50, 200,
:

5.8

105

xdx ,

exp( x)dx ,
0

1
dx ,
+1

sin( x)dx .
0

106

5.8

107

,
Mathematica
.

, Fortran, C, C++, .
, Mathematica
,
.
,
.
,
Mathematica
. C, :

x++ x 1, x.
x += dx dx x x.
x x 1, x.
x -= dx dx x x.
++x x 1, x.
--x x 1, x.

108

:
In[1]:=x=2
Out[1]=2
In[2]:=x++
Out[2]=2
In[3]:=x
Out[3]=3
In[4]:=x+=2
Out[4]=5
In[5]:=x-Out[5]=5
In[6]:=x
Out[6]=4
In[7]:=x-=2
Out[7]=2
In[8]:=++x
Out[8]=3
In[9]:=--x
Out[9]=2

6.1

.
If Which. ,
, .

6.1.1 If

If[ test, f1, f2]
If, test,
True () False (),

6.1

109

, (True)
f1 f2, (False)
f2 f1.
f1 f2.
:
If
x x sin(x) , :
In[1]:=fun[x_]:=If[x<0, x, Sin[x]]
In[2]:=Plot[fun[x], {x, -Pi, 2Pi}];

-2

-1

-2

-3

: , f1 f2, If, ,
.
;,
In[3]:=x=4; If[x > 6, a = 2; b = 3, a = 5; b = 6]
Out[3]=6
b,
a, ;

110

.
.

6.1.2 Which
Which ,
. :
Which[ test1, f1, test2, f2, test3, f3, ]
fi
.
x<0 x, 0 x <1 , 1
x x-1, ,
In[4]:=Plot[Which[x < 0, x, x<1, 0, 1<=x, x-1], {x, -Pi, Pi]}];

2
1

-3

-2

-1

1
-1
-2
-3


In[5]:=Plot[If[x<0, x, If[x<1, 0, x-1]],{x,-Pi,Pi}]

6.2 -

111

2
1

-3

-2

-1

-1
-2
-3

, If.

6.2 -

.
, ,
.
D, For
While.

6.2.1 Do
:
Do[ f[i], { i, i1, i2, i }]
f[i] ,
i i1 i2, i. ,

i i2.

112

.
, , ,
Do[
Do[ f[i, j], { i, i1, i2, i }],
{j, j1, j2, j}]
1:
In[1]:=s=0; Do[s=s+x^i; Print[s], {i,1,5}]; s
x
x + x^2
x + x^2 + x^3
x + x^2 + x^3 + x^4
x + x^2 + x^3 + x^4 + x^5
Out[1]=x + x^2 + x^3 + x^4 + x^5
s .
s xi,
i . Print[s]
. Do
.
2:
,
In[2]:=
DoA

DoAPrintA"i=", i, ", j=", j, ",


8j, 1, 3<E

Out[2]=
i=1, j=1,
i=2, j=1,
i=3, j=1,
i=1, j=2,

i=2, j=2,

!!!!!!! !!
i j =1.
!!!!!!!!!
i j =1.41421
!!!!!!!!!
i j =1.73205
!!!!!!!!!
i j =1.41421
!!!!!!!!!
i j =2.

!!!!!!!!!
i j =", Sqrt@1. i jDE, 8i, 1, 3<E,

6.2 -
i=3, j=2,
i=1, j=3,
i=2, j=3,

i=3, j=3,

113

!!!!!!!!!
i j =2.44949
!!!!!!!!!
i j =1.73205
!!!!!!!!!
i j =2.44949
!!!!!!!!!
i j =3.

3:
.
:
Random[].
{1,20}.
In[3]:=A=Table[Random[Integer,{1,20}], {20}]
Out[3]={1,12,17,19,9,16,10,9,1,6,18,6,18,17,13,7,8,18,2,11}

.
. ,
,
.
.
In[4]:=amax=A[[1]]
Out[4]=1
In[5]:=Do[ If[A[[i]]>amax, amax=A[[i]]], {i,1,20}]
In[6]:=amax
Out[6]=19
4:

y :
y=2.0+0.2 Random[]
20
. :
In[7]:=A1=Table[2+0.2 Random[],{20}]
Out[7]={2.12682,2.03318,2.10096,2.17778,2.14763,2.01572,

114

2.17989,2.11257,2.10967,2.15143,2.08852,2.06829,2.00778,
2.06092,2.029,2.04481,2.08911,2.01691,2.15197,2.07032}

In[8]:=m=Length[A1]
Out[8]=20

, :
In[9]:=S=0; Do[S=S+A1[[i]], {i,1,m}]; S
Out[9]=41.7833
In[10]:=meanvalue=S/m
Out[10]=2.08916

6.2.2 For
:

For[i= i1, test, incr, body]


body ( ) ,
i i1
incr, test
.
Do,
In[11]:= s1=0; For[i=0, i<5, i+=1, s1=s1+x^i]; s1
Out[11]=1+x+x^2+x^3+x^4
For
:
( i= i1) ,
,
(test), (body),
(incr).
.

6.2 -

115

For
,
body, , ..
In[12]:=s1=0;For[i=0, i<5, s1=s1+x^i, i+=1]; s1
Out[12]= x+x^2+x^3+x^4+x^5
1:

, .
: Random[].

{1, 20}.
In[13]:=A=Table[Random[Integer,{1, 20}], {20}]
Out[13]={1,12,17,19,9,16,10,9,1,6,18,6,18,17,13,7,8,18,2,11}

.
.
,
,
. ,
.
nindex.
In[14]:=A2=Table[Random[Integer,{1,20}],{20}]
Out[14]={3,12,1,9,11,5,3,11,14,7,8,18,11,5,20,5,3,12,6,8}
In[15]:=m1=Length[A2]
Out[15]=20
In[16]:=amax=A2[[1]]
nindex=1
Out[16]=3
Out[17]=1
In[18]:=For[i=1, i<=m1, i++,

116


If[A2[[i]]>amax, amax=A2[[i]]; nindex=i]]
In[19]:=amax
nindex
Out[19]=20
Out[20]=15

2:

y :
y=2.0+0.2 Random[]
20
(standard deviation).
:
In[21]:=A3=Table[2+0.2 Random[],{20}]
Out[21]={2.14259,2.1251,2.1675,2.17643,2.08517,2.06816,2.10991,
2.1052, 2.16618,2.14631, 2.06025,2.17897,2.05879, 2.12639,
2.11757,2.01996,2.01538,2.19266,2.10196,2.11561}
In[22]:=m3=Length[A2]
Out[22]=20
In[23]:=S=0
For[i=1, i<=m3, i++,
S=S+A3[[i]]];S
Out[23]=0
Out[24]=42.2801
In[25]:=meanvalue=S/m
Out[25]=2.114
In[26]:=S2=0
For[i=1, i<=m3, i++,
S2=S2+(A3[[i]]-meanvalue)^2];S2
Out[26]=0
Out[27]=0.0497223

6.2 -

117

In[28]:=sdeviation=Sqrt[S2/(m3-1)]
Out[28]=0.0511562

6.2.3 While
:
i=i1; While[test, body]
, body,
test. ,

, body, test.
While ,
In[29]:= s2=0; i=0; While[i<5, i+=1; s2=s2+x^i]; s2
Out[29]=x+x^2+x^3+x^4+x^5
In[30]:= s2=0; i=0; While[i<5, s2=s2+x^i; i+=1]; s2
Out[30]=1+x+x^2+x^3+x^4

In[29] i,
s2, In[30] .
:
b

f ( x)dx

[a, b] t
h=(b-a)/Nt,
b

Nt 1

f ( x)dx = h ( f (a + h k ) + f (a + h (k + 1))) /2.,


k =0

, While t=200,
1

x
0

dx

118

In[31]:=fun1[x_]=x^2
Out[31]=x^2
In[32]:=i=0; s=0; Nt=200; h=1./Nt;
While[i<Nt,
s=s+h*(fun1[i*h]+fun1[(i+1)*h])/2.; i=i+1]; s
Out[32]=0.333338
1/3
In[33]:=1/3.
Out[33]=0.333333

6.3
,
Do, For While,
.
, ,
.
Continue Break. Label
Goto.

6.3.1 Continue
,
,
. Continue[] ,
,
, ,
.
In[1]:=
DoAPrint@"i=", i, " j=", jD
!!!!!!!!!

IfAi j, PrintA" i j = ", Sqrt@1. i jDE, Continue@DE,


8i, 1, 2<, 8j, 1, 2<E

Out[1]=i= 1 j= 1

6.3

119

!!!!!!!!!
i j = 1.41421

i= 1 j= 2

!!!!!!!!!
i j = 1.41421

i= 2 j= 1

i= 2 j= 2

6.3.2 Break
,
Do, For While. Break[ ],
.
In[2]:=t=2; Do[t=t*k; Print[t]; If[t>10,Break[]], {k, 1, 20}]
2
4
12

6.3.3 Label
:
Label[pointer]
,
pointer, Goto.

6.3.4 Goto
:
Goto[pointer]

pointer, .
In[3]:=q=2;Label[point1];Print[q];q=q+1; If[q<6, Goto[point1]]
2
3

120
4
5

: .

6.4

, .
And ( &&), Or ( || ) Not
(!).

6.4.1 And ( && )



, ..
In[1]:=x=3; x>2 && x>5
Out[1]=False
In[2]:=x=Pi/2. ; x>1 && x<5 && Sin[x]>0
Out[2]=True
In[3]:=Plot[If[x>0 && Sin[x]>0, Sin[x], 0], {x,-10,16}]

1
0.8
0.6
0.4
0.2

-10

-5

10

15

6.4

121

: .

6.4.2 Or ( || )

, ..
In[4]:=x=3; x>2 || x>5
Out[4]=True
In[5]:=x=Pi/2.; x>2 || x>5 || Sin[x]<0
Out[5]=False
In[6]:=Plot[If[x>0 || Sin[x]>0, Sin[x], 0],{x,-4Pi,4Pi}]

0.5

-10

-5

10

-0.5

-1

: .

6.4.3 Not ( ! )
( False True
True False) , ..
In[7]:=x=3; !(x>2)
Out[7]=False
In[8]:=x=Pi/2.; !( x>1) && x<5 && Sin[x]>0
Out[8]=False
In[9}:=Plot[If[!(x>0) || Sin[x]>0 ,Sin[x], 0],{x,-4Pi,4Pi}]

122

0.5

-10

-5

10

-0.5

-1

: .

6.5
, x, x = x0,
x .
,
x, ,
.
.
x,
.
Module,


.

6.5

123

Module, 12
.
Module
, .

Module. ,
Module
,
.
:
result=Module[{x1, x2, }, expressions[x1, x2, , x, y, ]]
, , {x1, x2, }
Module ,
. Module
:
In[1]:=Module[{z}, z=1; z=z*2; z=z*2; z=z*2]
Out[1]=8
, z ,
. ,
,
Module, , :
In[2]:=factorialNum[y_]:=Module[{x=0, z=1},
If[y==0, 1, While[!(x==y), x=x+1;
z=z x]];
z]
, 4! ,
In[3]:=factorialNum[4]
Out[3]=24

124

:
factorialNum[y].

6.6
6.1
If, ,
, x0 :
() sin(x) , () x , x<0 : (i) x cos(x) , (ii) x 2 .
Which.
6.2
If Which
, x<0
x, 0 x <1 , 1 x <2 , 2 x x.
6.3
, () Do ()
For () While, :
10

1
1 n(n + 1) ,
10

10

n
1

n
1

10

10

)
)

n(n + 1)(n + 2) .
1

2n
1 n!
10

6.4
,
x+5*sin(x), x=*, =1,,
10, () Do () For () While.

6.6

125

6.5
5.8 .
6.6
Fibonacci : ,

. ()
100 , ()
2000.
6.7
5000,
5 %.
20000.
6.8
10000 4%.
,
10 .
6.9
e, ,
n

1
lim1 + . () 100
n
n
n

1
a n = 1 + , () n |e-an|<0.001.
n
6.10
Random[]
20 1-100.

, .

126

6.11

log(1 + x) = (1) n +1 x n / n .
1


log(2)
1%.
6.12

2
6

=
1

1
.
n2

1
an =
2
6
1 m

2
6

2
6

m=10-100. .
6.13

( p) =
1

1
.
np

p=2, 3, ... 10,


.
6.14
,
x e, y 20,
If [ x > 3 & & x <= 8 & & !(e!= 1), y = 20, y = 20] .

6.6

127

6.15

a x 2 + x + = 0 , .
6.16
, Module,
,

Sn = 1 +

1
S n 1

, S1 = 1.

128

7.1

129

7
MATHEMATICA


Mathematica.
Help Mathematica .

7.1
,
Mathematica
.

.

, Mathematica
16 ,
.
, ,
,
16 .
.
Mathematica :
Ceiling[a], .

MATHEMATICA

130
In[1]:=Ceiling[2.4]
Out[1]=3

Floor[a], .
In[2]:=Floor[2.4]
Out[2]=2
Round[a], .
In[3]:=Round[2.6]
Out[3]=3
IntegerPart[a], .
In[4]:=IntegerPart[2.4]
Out[4]=2
Chop[a], .
10-10.
In[5]:=Chop[1.*10^(-11)]
Out[5]=0

n , 16
, :
a1 = Round[10n a]/10n // N
, 10n, 10n.
Round[10n a] / 10n ,
//.

In[6]:=a=Table[{Sqrt[n], Sin[n]}//N, {n, 2, 5}]

7.2

131

Out[6]={{1.41421,0.909297},{1.73205,0.14112},{2.,-0.756802},{2.23607,0.958924}}
In[7]:=a1=Round[10^2 a]/10^2//N
Out[7]={{1.41, 0.91}, {1.73, 0.14}, {2., -0.76}, {2.24, -0.96}}

7.2
a, b, ...

Print[a, b, ]
:
In[1]:=a1=2;a2=3;a3=4;
In[2]:=Print[a1]
From In[2]:=2
In[3]:=Print[a1,a2,a3]
From In[3]:=234

Print, Mathematica
, . ,
,
,
.
, , ..
In[4]:=Print["a1= ", a1,", a2= ", a2,", a3= ", a3]
From In[4]:=a1= 2, a2= 3, a3= 4
,
.

MATHEMATICA

132

7.3
a, ,
:
Export[filename, a, Format]
filename
. Format filename, .. Table
. filename dat,
Format,
:
Export[filename.dat , a]
In[1]:=Export["c:\dat.dat", a1, "Table"]
Out[1]=c:\dat.dat
In[2]:=Export["c:\dat1.dat", a1]
Out[2]=c:\dat1.dat

7.4
Mathematica
, .. Windows
Metafile Format (*.wmf), word,
Encapsulated PostScript Format (*.eps), Tex,
:
Edit Save Selection As Windows Metafile (WMF) EPS,
.

7.5
Mathematica

a = Import[filename, Format]

7.6

133

Filename a
. Format , ..
Table, .
filename dat, Format.
In[1]:=ain=Import["c:\dat.dat"]

7.6
Mathematica
,
,
.
.

, .
:
In[1]:=x=3
Out[1]=3
In[2]:=D[x^2, x]
From In[2]:=General::ivar: 3 is not a valid variable.
Out[2]= 3 9
In[2] .
,
.
, x , ,
3.
,
In[3]:=x=.
In[4]:=D[x^2, x]
Out[4]=2 x

134

MATHEMATICA

,
,
, ,
.

, Remove["Global`*"] (
Kernel),
.
Mathematica
.

7.7
Mathematica

, Mathematica
, .
Style Format
, , , (text).

, , Altn n
1-9 .
, (Font), (Face),
(Size), (Text Color),
(Background Color), (Text Alignment) ...,
Format .

7.8
Timing[expression]: expression
.
.

7.8

135

TimeUsed[ ]: , ,

Mathematica.
,
startingtime=TimeUsed[ ],
, totaltime=TimeUsed[ ]-startingtime.
MaxMemoryUsed[ ]: bytes
Mathematica .
MemoryInUse[ ]: bytes Mathematica
.
Trace[expression]:
expression.
FullForm[expression]:
Mathematica expression.
TreeForm[expression]: FullForm
.

136

MATHEMATICA

7.8

137

Mathematica.
, ,

, .
.
,
.

Range,
Range[a1, a2, a3]
a1 a2 a3.
a3 . a1=a3=1
Range[a2]
1 a2.
:
In[1]:=Range[5]
Out[1]={1, 2, 3, 4, 5}
In[2]:=Range[4.3]
Out[2]={1, 2, 3, 4}
In[3]:=Range[1.2, 7]
Out[3]={1.2, 2.2, 3.2, 4.2, 5.2, 6.2}

138

8.1
.

, .
,
Mathematica

.

In[1]:=A = {12, 3, 6, 3, 8, 9, 10}.


Mathematica
,
Sort: ,
In[2]:=Sort[A]
Out[2]={3, 3, 6, 8, 9, 10, 12}
Reverse:
,
In[3]:=Reverse[A]
Out[3]={10, 9, 8, 3, 6, 3, 12}
Rest: ,
In[4]:=Rest[A]
Out[4]={3, 6, 3, 8, 9, 10}
Drop[, n]: n .
n n ,
In[5]:=Drop[A, 3]
Out[5]={3, 8, 9, 10}
In[6]:=Drop[A, -3]
Out[6]={12, 3, 6, 3}

8.1

139

Take[, n]: n
. n n ,
In[7]:=Take[A, 3]
Out[7]={12, 3, 6}
In[8]:=Take[A, -3]
Out[8]={8, 9, 10}
Append: ,
In[9]:=Append[A, a4]
Out[9]={12, 3, 6, 3, 8, 9, 10, a4}
Prepend: ,
In[10]:=Prepend[A, a4]
Out[10]={a4, 12, 3, 6, 3, 8, 9, 10}
Insert[A, a4, i]:
a4 i A. i ,
|i| .
In[11]:=Insert[A, a4, 3]
Out[11]={ 12, 3, a4, 6, 3, 8, 9, 10}
Count[A, a]: a .
In[12]:=Count[A, 3]
Out[12]=2
Position[A, a]:
a.
In[13]:=Position[A, 3]
Out[13]={{2},{4}}
,
. AppentTo, PrependTo

140

, .. A, .

.
AppendTo: ,
In[14]:=AppendTo[A, a4]
Out[14]={12, 3, 6, 3, 8, 9, 10, a4}
PrependTo: ,
In[15]:=PrependTo[A, a4]
Out[15]={a4, 12, 3, 6, 3, 8, 9, 10}
First[A]: .
In[16]:=First[A]
Out[16]=12
Last[A]: .
In[17]:=Last[A]
Out[17]=10

Permutations[A]:
.
In[18]:=Permutations[{3,7,4}]
Out[18]={{3,7,4}, {3,4,7},{7,3,4}, {7,4,3},{4,3,7}, {4,7,3}}
Join[A, B]: , .

, ,
In[19]:=Join[{1,4,3}, {3,5,2}]
Out[19]={1,4,3,3,5,2}

8.2

141

Union[, ]: ,
( ).
,
In[20]:=Union[{1,4,3}, {3,5,2}]
Out[20]={1,2,3,4,5}

, .
,
In[21]:=Union[{1,4,6,4,10,1,6}]
Out[21]={1,4,6,10}
Intersection[A, B]: (
).
In[22]:=Intersection[{1,2,3}, {3,5,2,3}]
Out[22]={2,3}
Flatten: ,
( ),
In[23]:=Flatten[{{1, 2}, {3, 4}}]
Out[23]={1, 2, 3, 4}
Partition[A, n]: n,
In[24]:=Partition[{1, 2, 3, 4, 5, 6}, 3]
Out[24]={{1, 2, 3}, {4, 5, 6}}

8.2

:
Length: ,
In[1]:=Length[A]
Out[1]=7

142
Dimensions: .
In[2]:=Dimensions[A]
Out[2]={7}

Short:
. Short[A, n]
. n
.

8.3
,
, .
Mathematica
(TreeForm),

. expression

TreeForm[expression]
..
In[1]:=TreeForm[{1,3,2,8}]
Out[1]//TreeForm=List[1,3,2,8]
,
, ,
. ,
List ,
.
, .
,
, ,

8.3

143

. .. ,

In[2]:=TreeForm[{{2,3},{5,8}}]

ListB
,
F
List@2, 3D List@5, 8D ,


( ),
.
. ,
, .
.
Map
Apply.

8.3.1 Map
Cos[x] , ..
{a, b, c, d}, :
In[3]:=A={a, b, c, d}
Out[3]={a, b, c, d}
In[4]:=Cos[A]
Out[4]={Cos[a], Cos[b], Cos[c], Cos[d]}
Mathematica Cos .
, Listable,
Cos[x], Log[x],
.
, Map,
Map[f, {a, b, c}]
Map /@, ..

144
f /@ {a, b, c}

f
, ,
,
In[5]:=Map[f, A]
Out[5]={f[a], f[b], f[c], f[d]}
f ,
k, MapAt,
MapAt[f, list, k]
In[6]:=MapAt[f, A, 2]
Out[6]={a, f[b], c, d}
,
, Map,
,
In[7]:=Map[f, {{a, b},{c, d}}]
Out[7]={f[{a, b}], f[{c,d }]}
, ..
k,
Map[f, expression, {k}]
Map
,
In[8]:=Map[f, {{a, b}, {c, d}}, {2}]
Out[8]={{f[a], f[b]},{f[c], f[d]}}

8.3

145

8.3.2 Apply
Map, Apply.
.
:
Apply[NewHead, expression]
expression,
NewHead. , ,
NewHead@@expression
:
In[9]:=Apply[Plus,{1,2,3,4}]
Out[9]=10
In[10]:=Apply[Plus, {{1,2}, {3,4}}]
Out[10]= {4, 6}

, 1 + 2 + 3 + 4 , , {1, 2} + {3, 4} .
,
() List Plus.
,
.
k
Apply[NewHead, expression, {k}]
,
,
, :
In[11]:=Apply[Plus, {{1,2}, {3,4}} ,{1}]
Out[11]= {3, 7}.
.

146

8.4
,
.
.
.
1:
, ,
Modulo .
: , F,
,
In[1]:=F={0};
Modulo .
F,
In[2]:=Do[If[Mod[i,3]==1, AppendTo[F, i]],{1, 2, 20}]
In[3]:=F
Out[3]={1,4,7,10,13,16,19}
Mod[x,y] x modulo y.
2:
10 ,
. ,
.
: ,
,
In[4]:=F1=Table[Random[Integer, {1,10}],{10}]
Out[4]={10,2,8,1,6,10,6,7,2,9}
Union,
Length ,
In[5]:=F2=Union[F1]
Out[5]={1,2,6,7,8,9,10}

8.4

147

In[6]:=m=Length[F2]
Out[6]=7
F2,
F1, ,
In[7]:=Do[m1=Count[F1,F2[[i]]];
m2=Position[F1,F2[[i]]];
Print["element= ",F2[[i]],", frequency=",m1,", positions= ",m2],{i,1,m}]
element= 1 , frequency= 1 , positions= {{4}}
element= 2 , frequency= 2 , positions= {{2},{9}}
element= 6 , frequency= 2 , positions= {{5},{7}}
element= 7 , frequency= 1 , positions= {{8}}
element= 8 , frequency= 1 , positions= {{3}}
element= 9 , frequency= 1 , positions= {{10}}
element= 10 , frequency= 2 , positions= {{1},{6}}
3:
50 , 1500. Map, Modulo
11,
: ,
Random,
In[8]:=G=Table[Random[Integer, {1,500}], {50}];
Modulo 11
In[9]:=Map[Mod[#,11]&, G]
Out[9]={3,6,1,8,0,1,2,4,6,8,7,10,0,0,0,2,9,3,0,3,3,8,6,2,1,1,4,8,
7,5,9,8,7,1,1,0,8,1,3,0,4,0,8,1,10,6,4,1,7,0}
4:
,
, y,
.

148

,
:
y=2.0+Random[]
50
, av, , sd.
In[10]:=m=50;
In[11]:=G[m_]:=Table[2.+Random[],{m}];
In[12]:=A=G[m];
In[13]:=av=Apply[Plus, A]/m
Out[13]=2.49292
In[14]:=f2:=Apply[Plus,#^2]/(m-1)&
In[15]:=sd=Sqrt[f2[A-av]]
Out[15]=0.318769
,
, 25 ,
.
In[16]:=m1=25
Out[16]=25
In[17]:=1=Partition[A,m1];
In[18]:=av2=Apply[Plus, 1,1]/m1
Out[18]={2.4908,2.49503}
In[19]:=sd2=Apply[Plus,(1-av2)^2,1]/(m1-1)//Sqrt
Out[19]={0.338498, 0.304747}
: 4.
8.1
(i) 15 ,
) Table ) Range.
(ii) 10 ,
) Take ) Drop.

8.5

149

8.2
Table Random ,
15 1
10. ,
() ,
() ,
,
() ,
() .

8.5

,
( ). Mathematica
,
MatrixManipulation.
Help
Mathematica, , ,
.

Mathematica, .

<< LinearAlgebra`MatrixManipulation`
In[1]:=<<LinearAlgebra`MatrixManipulation`
b1
b2 .
MatrixForm,
.

150


In[2]:=b1={{5,9,6,9},{3,6,0,2},{8,0,4,1},{1,5,7,4}}; b1//MatrixForm
Out[2]=
i5 9 6 9z
y
j
j
j
z
3 6 0 2z
j
z
j
z
j
z
j
z
8
0
4
1
j
z
j
z
k1 5 7 4{
In[3]:=b2={{9,7,7,7},{7,2,3,3},{1,6,9,6},{4,3,2,9}}; b2//MatrixForm
Out[3]=
i9 7 7 7z
y
j
j
j
z
7 2 3 3z
j
z
j
z
j
z
j
z
1
6
9
6
j
z
j
z
k4 3 2 9{

MatrixManipulation
:

AppendColumns, (
),
In[4]:=AppendColumns[b1,b2]//MatrixForm
Out[4]=
5 9 6 9y
i
j
z
j
j
z
3 6 0 2z
j
z
j
z
j
z
j
z
8
0
4
1
j
z
j
z
j
z
j
z
1
5
7
4
j
z
j
z
j
j
z
9 7 7 7z
j
z
j
z
j
z
j
z
7
2
3
3
j
z
j
z
j
z
j
z
1
6
9
6
j
z
j
z
4
3
2
9
k
{
AppendRows, ,
In[5]:= AppendRows[b1,b2]//MatrixForm
Out[5]=
5 9 6 9 9 7 7 7y
i
j
z
j
j
z
3 6 0 2 7 2 3 3z
j
z
j
z
j
z
j
z
8
0
4
1
1
6
9
6
j
z
j
z
k1 5 7 4 4 3 2 9{

8.5

151

TakeColumns,
,
In[6]:=TakeColumns[b1, 3]//MatrixForm
Out[6]=
5 9 6y
i
j
z
j
j
z
3 6 0z
j
z
j
z
j
z
j
z
8
0
4
j
z
j
z
k1 5 7{

,
In[7]:=TakeColumns[b1, -3]//MatrixForm
Out[7]=
9 6 9y
i
j
z
j
j
z
6 0 2z
j
z
j
z
j
z
j
z
0
4
1
j
z
j
z
k5 7 4{
TakeRows,
,
In[8]:=TakeRows[b1, 3]//MatrixForm
Out[8]=
5 9 6 9y
i
j
z
j
j
z
3 6 0 2z
j
z
j
z
8
0
4
1
k
{

,
In[9]:=TakeRows[b1, -3]//MatrixForm
Out[9]=
3 6 0 2y
i
j
z
j
z
j
z
j
j8 0 4 1z
z
k1 5 7 4{

152

TakeColumns, ,

,
In[10]:=TakeColumns[b1,{2, 4}]//MatrixForm
Out[10]=
9 6 9y
i
j
z
j
j
z
6 0 2z
j
z
j
z
j
z
j
z
0
4
1
j
z
j
z
k5 7 4{
TakeRows, ,

,
In[11]:=TakeRows[b1,{2,3}]//MatrixForm
Out[11]=
3 6 0 2
J
N
8 0 4 1
TakeMatrix,

In[12]:=TakeMatrix[b1,{2,2},{3,3}]//MatrixForm
Out[12]=

6 0
N
0 4

9.1

153

2 .
Mathematica
,
.

Mathematica, .

9.1

. ,
,
,
.
.
Graphics, .

<<Graphics`Graphics`
, ,
, LinearLogPlot, LogLinearPlot, LogLogPlot
.

154

- ( x y
), -
- .
Linear-Log :
In[1]:=<<Graphics`Graphics`
In[2]:=LinearLogPlot[Exp[x], {x, 1, 10}]

10000
1000
100
10
1
0

10


LinearLogListPlot, LogLinearListPlot, LogLogListPlot .

In[3]:=a=Table[{x, x^(-2)}, {x, 0.1 , 10, 0.1}];
In[4]:=LogLogListPlot[a]

9.2

155

100

10

0.1

0.01
0.1

0.2

0.5

10

Linear,
, .. LinearLogListPlot[a]
LogListPlot[a].
9.1
, (1, 100),
) exp(-2x)
) x4
) ln(x4).
, ,
, , .

9.2

. .. y(t) , t,
y(t),
y(t), t y(t).
,

{t i , y i , y i } ,
, y ,
. ()
{t i , y i }

156

, y i
.

Graphics, .

<<Graphics`Graphics`
,

ErrorListPlot[ datalist }]
datalist .

In[1]:=<< Graphics`Graphics`
In[2]:=dat=Table[{i/10.,Sin[i/5.],.2},{i,1,50}];
In[3]:=errorp = ErrorListPlot[dat];

1
0.5

-0.5
-1

9.2
100 m/sec2.

9.3

157

1
100t 10t 2 ,
2
y =

y .

{t i , y i , y i } , t 20, 0.5 .
.

9.3
,
,
() ,
.
,

.

.
,

. .
,

.
Mathematica .
Graphics,
.

<<Graphics`Graphics`
,

Histogram[ datalist }]

158

datalist .

In[1]:=<<Graphics`Graphics`
In[2]:= In[20]:=n1=10000
Out[2]=10000
In[3]:=a1=a^(1/4);
In[4]:=Histogram[a1];

300

200

100

0.2

0.4

0.6

0.8

10000
(0, 1),
.
. Mathematica,
. .. 1000 ,
Mathematica
. , ,
, .
,
,
In[5]:=Histogram[a1, HistogramScale1];

9.4

159

0.2

0.4

0.6

0.8

,
, .. (0, 0.8),
,
In[6]:=Histogram[a1, HistogramRange{0.,0 .8}, HistogramScale1];
5

0.2

0.4

0.6

0.8

,
, Help
Mathematica.

9.4
,
y=f[x], F[x,
y]=0, y. H Mathematica

160


.
ImplicitPlot, .

<<Graphics`ImplicitPlot`
,
ImplicitPlot :
ImplicitPlot[ F[x, y]==0, {x, xmin, xmax}]

In[1]:=<<Graphics`ImplicitPlot`
In[2]:=ImplicitPlot[x^2+y^2==4, {x, -2, 2}]

-2

-1

-1

-2

9.3
, x (-4, 4),
:
) x 2 / 4 + y 2 = 4 ,

) x 3 + y 3 = 4 xy ,

9.5

161

) y 2 x 2 = 1 ,

) x 2 y 2 = ( y + 1) 2 (4 y 2 ) .

) ( y 2 + 2 x 2 ) 2 = xy ,

) x 4 + x y + y 4 = 1

9.5
, ,
r=f(),
.
Graphics,

<<Graphics`Graphics`
,

PolarPlot[ f[], {, 1, 2}]
r , f()
. f()
,
. ,
r = sin(3 )

r =| sin(3 ) | , :
In[1]:=PolarPlot[Sin[3],{, 0, 2Pi}]
In[2]:=PolarPlot[Abs[Sin[3]],{, 0, 2Pi}]

162
1

0.5
0.25

0.5

-0.75 -0.5 -0.25

0.25

-0.25

0.5 0.75
-0.75 -0.5 -0.25

0.25 0.5 0.75

-0.5
-0.5

-0.75
-1

-1

1.

2.

1,
2.

< <

2
,
3

5
4
,
< < 2 .
3
3
sin(3) .

< <

9.4
, x
(-4, 4), :
) cos 4 ( ) + sin 4 ( ) ,

) sin( ) ,

) sin( 2 ) ,

) sin 4 ( ) cos 2 ( ) .

9.6

163

9.6

,
.
PoltField,

<<Graphics`PlotField`
,

PlotVectorField[{V1[x, y], V2[x, y]}, {x, xmin, xmax}, {y, ymin, ymax}],
{V1[x, y], V2[x, y]} .
:
In[1]:=PlotVectorField[{y,x},{x,-1,1},{y,-1,1}]

164

9.7
,
r = f ( , ) ,
Euler, , .
ParametricPlot3D,

<<Graphics`ParametricPlot3D`
,

SphericalPlot3D[ f[, ], {, 1, 2}, {, 1, 2}]
r , f(, )
. f(, )
, .
:
In[1]:=SphericalPlot3D[(Sin[] Cos[])^2, {, 0, Pi}, {, 0, 2Pi},
PlotRangeAll, Ticks->False, PlotPoints50]

px
.

9.8

165

9.5
,
:
) d z 2 =
) d x 2 y 2

1
(3 cos 2 ( ) 1) 2 ,
2
3
= (sin( ) cos( ) cos( ) sin( )) 2 ,
4

) d xy = 3(sin 2 ( ) cos( ) sin( )) 2 ,



.

9.8

ParametricPlot3D,

<<Graphics`ParametricPlot3D`
ParametricPlot3D[ {x[t], y[t], z[t]}, {t, t1, t2}]
{x(t), y(t), z(t)}
{t1, t2} .
:
In[1]:=ParametricPlot3D[{(2+Sin[2t])Cos[t],(2+Sin[t])Cos[t],Cos[2t]},
{t,0,6Pi}, PlotPoints300]

166

1
0.5
0
-0.5
-1

0
-2
0

-2
2

,
ParametricPlot3D[{x[t, u], y[t, u], z[t, u]}, {t, t1, t2}, {u, u1, u2}]
{t1, t2}, {u1, u2}
.
:
In[2]:=ParametricPlot3D[{(5+Cos[u])Cos[t], (5+Cos[u])Sin[t], Sin[u]},
{t, 0, 2Pi}, {u, 0, 2Pi}, PlotRangeAll, TicksFalse]

9.9

167

9.9
Mathematica
. ,
,
. ..
In[1]:=Do[Plot[Sin[x-t], {x, 0, 4Pi}], {t, 0, 2Pi, 2Pi/11}]
11 ,
, .
,


CellAnimate Selected Graphics.

.
( ) ()
, .
9.6
, Do PolarPlot,
r=,
, {, 8, 16,
/3}. .
9.7
, Do,
cos(x*y), x
(0, t/2), y (0, t), t
{t, 1, 5, 3/5}.
.

168

9.9

169

10

,
. ,

,
.
.
.
10.2.
,
,
.
(fitting),
(interpolation).
10.3,

10.4. ,
,
, Fourier,
. Fourier
10.5.
Mathematica
, .

170


.
,
.

10.1
8.5
.
, .

DataManipulation.
Help Mathematica.
.

Mathematica, .

<< Statistics`DataManipulation`
In[1]:=<< Statistics`DataManipulation`
b1 b2
.
MatrixForm,
.
In[2]:=b1={{5,9,6,9},{3,6,0,2},{8,0,4,1},{1,5,7,4}}; b1//MatrixForm
Out[2]=
5 9 6 9y
i
j
z
j
z
j
j3 6 0 2z
z
j
z
j
z
j
z
8
0
4
1
j
z
j
z
k1 5 7 4{
In[3]:=b2={{9,7,7,7},{7,2,3,3},{1,6,9,6},{4,3,2,9}}; b2//MatrixForm

10.1
Out[3]=
9 7 7
i
j
j
j
7 2 3
j
j
j
j
j
j1 6 9
k4 3 2

171

7y
z
z
3z
z
z
z
6z
z
z
9{

DataManipulation
:
Column, ,
In[4]:= Column[b1, 2]//MatrixForm
Out[4]=
9y
i
j
z
j
j
z
6z
j
z
j
z
j
z
j
z
0
j
j z
z
k5{
,
In[5]:= Column[b1, {1,3}]//MatrixForm
Out[5]=
5 6y
i
j
z
j
j
z
3 0z
j
z
j
z
j
z
j
z
8
4
j
z
j
z
1
7
k
{
ColumnTake, ,

,
In[6]:= ColumnTake[b1,{1, 3}]//MatrixForm
Out[6]=
5 9 6y
i
j
z
j
j
z
3 6 0z
j
z
j
z
j
z
j
z
8
0
4
j
z
j
z
k1 5 7{

172

ColumnDrop, ,

,
In[7]:= ColumnDrop[b1, {1,3}]//MatrixForm
Out[7]=
9y
i
j
z
j
j
z
2z
j
z
j
z
j
z
j
1
j z
z
j
z
k4{
ColumnJoin, (
),
In[8]:= ColumnJoin[b1, b2]//MatrixForm
Out[8]=
5 9 6 9y
i
j
z
j
j
z
3 6 0 2z
j
z
j
z
j
j
z
8 0 4 1z
j
z
j
z
j
z
j
z
1
5
7
4
j
z
j
z
j
z
j
z
9
7
7
7
j
z
j
z
j
j
z
7 2 3 3z
j
z
j
z
j
z
j
z
1
6
9
6
j
z
j
z
k4 3 2 9{
BinCounts

dx, {xmin, xmax}.
BinCounts[list, {xmin, xmax, dx}].
In[9]:=b3=Table[2Random[], {100}];
In[10]:=BinCounts[b3, {0, 2, 0.5}]
Out[10]={20, 28, 20, 32}

10.2

173

10.2


. ,
( )
.

.
.
.
Mathematica
.

,
Random[], [0,1],

In[1]:=expdat=Table[1+Random[],{100}];
,
expdat, 100
.
:
In[2]:=ListPlot[expdat, PlotRange{0, 2}]

174
2
1.75
1.5
1.25
1
0.75
0.5
0.25

20

40

60

80

100

,
.

. ()
, ()

, .
, .
,
,
expdat.
Mathematica


.
In[3]:=<<Statistics`DescriptiveStatistics`
,
.

10.2.1
n

10.2

175

1
xi
n
.

data
x=

Mean[data]
expdat,
In[4]:=Mean[expdat]
Out[4]= 1.50919

10.2.2
(variance)
.

1
( xi x ) 2
n 1

1
s 2 = ( xi x ) 2 , n
n
, n
.
Mathematica
s2 =

Variance[data]
expdat,
In[5]:=Variance[expdat]
Out[5]= 0.0831552

10.2.3
(standard
deviation), ,

176

1
( xi x ) 2 ,
n 1

. Mathematica

s = s2 =

StandardDeviation[data]
expdat,

In[6]:=StandardDeviation[expdat]
Out[6]= 0.288366

10.1
100
Random[].
100, 1000 10000. ,
, ,

. .
10.2
: dat1=Table{Random[], {100}]
dat1=Table{Random[], {100}],
dattotal=Join[dat1, dat2].

.

10.3 (Fitting)

.

10.3 (Fitting)

177

,
, ,
.

.

.
,
.

, .. ( f 1 ( x), f 2 ( x), .. f n ( x)) ,

.
, (( x1 , y1 ), ( x 2 , y 2 ), ..., ( x N , y N ))
,

F (c1 , ..., c n , x) = c1 f 1 ( x) + c 2 f 2 ( x) + ... + c n f n ( x) ,


{c1, c2, , cn}

N
i =1

(F ( c 1 ,

..., c n , x i ) y i

)2


N
2
(
)
F
(
c
,
...,
c
,
x
)
y
0
,
j
1
,
2
,
...,
n

=
=
,

1
n
i
i

c j i =1
:
N
N

i =1 (c1 f1 ( xi ) + c 2 f 2 ( xi ) + ... + c n f n ( xi ) ) f j ( xi ) = y i f j ( xi ), j = 1, 2, ..., n .


i =1

{c j }
, {c j = c 0j } .
F (c10 , c 20 , ..., c n0 , x)
.
Mathematica

178

name=Fit[data, { f1 [ x], f 2 [ x], ... f n [ x]} , x]


data , { f1 [ x], f 2 [ x], ... f n [ x]}
,
, x , name
, ,
, (c1 , ..., c n ) .
data ,
, yi ,
i, (i, y i ) .
:

In[1]:=data=Table[{x, Cos[x]+x+1.5Random[]}, {x, 0, 4Pi, Pi/8}]//N;

In[2]:=g1=ListPlot[data, PlotStyle->PointSize[0.02]]

14
12
10
8
6
4
2
2

10

12


{1, x, Cos[x]}

10.3 (Fitting)

179

In[3]:=param=Fit[data, {1, x, Cos[x]}, x]


Out[3]=0.911801+0.989735 x+0.738884 Cos[x]

In[4]:=g2=Plot[param, {x, 0, 4Pi}]

14
12
10
8
6
4
2
2

10

12


,

In[5]:=Show[g1, g2]

,
.

180
14
12
10
8
6
4
2
2

10

12

param, In[3],
. ,
x, .. x=x0, ,
param/.x->x0
In[6]:=param/.x->2.
Out[6]=2.58379

:
fitfun[x_]=Fit[data, {f1[x], f2[x], fn[x]}, x]
fitfun[x]
, ..
In[7]:=fitfun[x_]=Fit[data, {1, x, Cos[x]}, x]
Out[7]=0.911801+0.989735 x+0.738884 Cos[x]
In[8]:=fitfun[2.]
Out[8]=2.58379
In[9]:=g4=Plot[fitfun[x],{x,0, 4Pi}]

10.3 (Fitting)

181

14
12
10
8
6
4
2
2

10

12

In[10]:=NIntegrate[fitfun[x], {x, 0, 2Pi}]


Out[10]=25.2656

10.3
Cosh[x] x
1.5, 0.05.
- , 5, 7, 9. ,
,
. , -
,
.
, x,
Erf[x].
10.4
.

f ( x) = a +

b
.
( x c) 2 + d 2

182

f ( x) = 2 +

1
+ 0.1 Random[] .
( x 4) 2 + 1

,
, x (-8,
18), 0.1.
,
) f 1( x) = a +

b
( x 4) 2 + 1

) f 2( x) = a + b * exp(( x 4) 2 ) .

;
,
.
. .

10.4 (Interpolation)
,
,

.

, ,
.
..
, 11.
n+1 ,
n

( {xi , y i } , i=1,n+1), n- , f ( x) = a k x k .
0

n+1 {a k , k = 0,1, ..., n}


n+1 ,

10.4 (Interpolation)

183

( y i = f ( xi ) , i=1,n+1), , ,

x 1n ...
x1 1 a n
y1
n

x 2 1 a n 1
x 2 ...
2
.
.
= .
.
.
.

.
.
.
.

n
a

y
x
x
n +1
n + 1 ... n + 1 1 0
{ai } .
, ..
Newton Lagrange,
, .
Mathematica,
,
. Mathematica
,
.

name=Interpolation[data]
name
.

name=Interpolation[data, InterpolationOrder->value]
name
.

184

:
data, ,
:
In[1]:=b=Interpolation[data]
Out[1]=InterpolatingFunction[{{0.,12.5664}},<>]
,
In[2]:=b[2.]
Out[2]=2.75707
In[3]:=g3=Plot[b[x], {x, 0, 4Pi}]

14
12
10
8
6
4
2
2

In[4]:=Show[g1, g3]

10

12

10.4 (Interpolation)

185

14
12
10
8
6
4
2
2

10

12

In[5]:=NIntegrate[b[x], {x, 0, 2Pi}]


Out[5]=24.8883
10.5
sin(x), x
(0, 2), 0.1.
.
.
.
. .
10.6
cosh(x) x
1.5, 0.05.
. , ,
.
, 10- ,

.
; ,
x, Erf[x].

186

10.5 Fourier

, ,
Fourier,
.
T, f (t ) , t,

f (t ) = f (t + T ) , Fourier ,
+

f (t ) = a n exp(i (2 / T )n t ) ,

n = 0,1,2,...

, a n , :
T

an =

1
exp(i (2 / T )n t ) f (t )dt .
T 0

, ,
,
, h = T / N .
, h
1/h, , (
5.8, S2):
N

a n = (1 / N ) exp(i n k ) f k ,
1

n = 2 n / N , f k = f (t k ) , t k = h k .
a n + N = a n ,
.
,

.
, = N = 2 ,
t = 1 ,

t = 2 .

10.5 Fourier

187

1/
.
, Fourier
:

an exp(i 2nk / N ) , an =

exp(i 2nk / N ) f k
N 1
N 1
Mathematica.
Help.
Mathematica, { f k }
fk =

,
f={f1, f2, f3, ..., f}.

, a={a1, a2, a3, ..., aN}, :
a=Fourier[f]
- a

= (2 / N ) .
,
, ,
Fourier
ff=InverseFourier[a]

In[1]:=f={ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1}
Out[1]={0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1}
In[2]:=a=Fourier[f]
Out[2]={1.73925+0. I, -0.158114-0.486624 I, -0.158114-0.217625 I,
-0.158114 - 0.114876 I, -0.158114-0.0513743 I, -0.158114+0. I,
-0.158114 +0.0513743 I, -0.158114+0.114876 I, -0.158114+0.217625 I,
-0.158114+0.486624 I}

188

,
In[3]:=ff=InverseFourier[a]
Out[3]={0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.}
.

10.7
, Random[],
6, 7, 8, 9, 10 .
Fourier .

.
10.8
f(x) 2,
(-, ) f(x)=x.
Fourier.

:

(1) k +1
sin(kx) .
k
(:
Fourier).
f ( x ) = 2 1

10.9
f(x) = | sin(x) | .

Fourier.

| sin( x) |=

cos(2nx)
.
4n 2 1

10.5 Fourier

189

(:
Fourier).

190

10.5 Fourier

191

11

,
,

dy d 2 y
,
,....) = 0 ,
dt dt 2

, .. y[t], .
y[t].
,
.
{ f x [t ], f y [t ], f z [t ]} ,
F (t , y,

{fi[t]} ,

d 2 x[t ]
d 2 y[t ]
d 2 z[t ]
=
f
[
t
]
,
m
=
f
[
t
]
,

m
= f z [t ] .
x
y
dt 2
dt 2
dt 2

, x[t], y[t], z[t].
m

: 5.5 , t,
, y[t],
, . ,
D[y[t], t],
.

192

D[y[t], t] y[t]
D[y[t], {t, 2}] y[t], ...
:
,
.
.. x[t0]=x0, x[t0]=0,
x[t1]=x1, x[t2]=x2. Mathematica
, .

11.1

:
DSolve[equation, y[t], t ]
equation , y[t] ,
t .
Mathematica .
:
y+y+2y=0, ,
:
In[1]:=s1=DSolve[y''[t]+y'[t]+2y[t]==0, y[t], t]
1
1
Out[1]={{y[t] Exp[ (1 i 7 )t ]C[1] + Exp[ (1 + i 7 )t ]C[2] }}
2
2
DSolve,
,
solution=DSolve[equation, y, t ]

11.1

193

, , ,

Mathematica.
:
y+y+2y=0
In[2]:=solution=DSolve[y''[t]+y'[t]+2 y[t]==0, y, t]
Out[2]={{yFunction[{t},

1
1
Exp[ (1 i 7 )t ]C[1] + Exp[ (1 + i 7 )t ]C[2]] }}
2
2
,
solution.
, ,
.
,
f[t_]:=y[t]/.solution[[1]]
[[1]] ,
.
In[3]:=f[t_]:=y[t]/.solution[[1]]
, f[t]
, ..
In[4]:=f[t]
Out[4]:=
1
1
1
1
(1 i 7 ) Exp[ (1 i 7 )t ]C[1] + (1 + i 7 ) Exp[ (1 + i 7 )t ]C[2]
2
2
2
2
11.1
,
, :

194
) y-4x=exp(x),
) y+y=cos(3x),
) y+y=x
) y+y=exp(x),
) y-2y=sin(x),

) y-y=x,
) y-y=x2 ,
) y-y=sin(2x),
) y+y=x cosh(x),
) y+2y+y=x2 exp(x).

,
:
solution=DSolve[ {eqn1, eqn2, ...}, y, t ]
,
(eqn1) (eqn2, ),
,
.
n n
{y1[t], y2[t], ..., yn[t]}, :
solution=DSolve[ {eqn1, eqn2, ...}, {y1, y2, ..., yn}, t ]

solution

{eqn1, eqn2, }/.solution[[1]]
:
y+y+2y=0, {y'[0]=0,
y[0]=1}, :
In[5]:=sol2=DSolve[{y''[t]+y'[t]+2 y[t]==0, y'[0]==0 , y[0]==1}, y, t]

Out[5] = {{y Function[{t},


+

1
1
1

7 Exp[ (1 i 7 )t ] + i 7 Exp[ (1 i 7 )t ]
14
2
2

1
1
1

7 Exp[ (1 + i 7 )t ] i 7 Exp[ (1 + i 7 )t ] ]}}


14
2
2

11.1

195

f2[t],
, ,
In[6]:=f2[t_]:=y[t]/.sol2[[1]]
In[7]:=fig1=Plot[{f2[t],
f2'[t]},
{t,
0,
10},
PlotStyle{
{},Dashing[{0.04}]},
PlotRangeAll]
1
0.75
0.5
0.25
2

10

-0.25
-0.5
-0.75

:
1 Kgr, g=10
m/sec2, y[0]=0
y[0]=10 m/sec.

In[8]:=eqn1 = y''[t] == -10



In[9]:=eqn2 = y[0] == 0;
eqn3 = y'[0] == 10
Out[9]=y[0] == 0
Out[10]= y'[0] == 10

In[11]:=sol3=DSolve[{eqn1,eqn2,eqn3}, y, t]

196


Out[11]={{y[t] ->Function[{t}, -5(-2t+ t2]}}


In[12]:={eqn1, eqn2, eqn3}/.sol3[[1]]
Out[12]={True,True,True}
,
,
.
Mathematica . ..
y[x]+y[x]/x=x2
In[13]:=DSolve[{y'[x]+y[x]/x==x^2}, y, x]
Out[13]={{y-> Function[{x}, x^3/4 + C[1]/x]}},
y[0]=1,
In[14]:=DSolve[{y'[x]+y[x]/x==x^2, y[0]==1}, y, x]
Out[14]={}

11.2
,
:
) y+y=x+3, y(0)=1, y(0)=0
) y-y=sin(x) , y(0)=0, y(0)=-1/2,
x
) y+y=2 e , y(0)=0, y(0)=1,
) y+y=x, y(0)=2,
) y-y=1, y(0)=A, y(0)=B
) y+y=exp(-x), y(0)=0.
11.3
,
, :
) x(t)=3x(t)+y(t), y(t)=x(t)+3y(t)
) x(t)+8x(t)+2y(t)=0, y(t)+2x(t)+5y(t)=0
) x(t)=2x(t)+3y(t)+et, y(t)=3x(t)+2y(t)+3et,
) x(t)=-x(t)+y(t)+cos(t), y(t)=-5x(t)+3y(t), x(0)=0, y(0)=0,

11.2

197

) x(t)=x(t)+2y(t), y(t)=-2x(t)-3y(t), x(0)=A, y(0)=B.

11.2
,
.
,
.
, ,

. ,
,
.
{t1, t2},
, :
NDSolve[ {eqn1, eqn2, ...}, y, {t, t1, t2} ]
NDSolve[ {eqn1, eqn2, ...}, {y1, y2, ...}, { t, t1, t2} ]
.
NDSolve
, , ,
. .
,
MaxStepsNumber, ..
NDSolve[ {eqn1, eqn2,...}, y, {t, t1, t2}, MaxSteps5000].
,
, Help Mathematica.
:
y[t]+y[t]+2y[t]=0, {y[0]=0,
y[0]=1}, ,
{0, 15} :

198

In[1]:=sol4 = NDSolve[{y''[t] + y'[t] +2 y[t] == 0, y'[0] == 0, y[0] == 1},


y, {t, 0, 15}]
Out[1]={{y -> InterpolatingFunction[{{0., 5.}}, <>]}}
,
, sol4.

,
In[2]:=f[t_]:=y[t]/.sol4[[1]]
, ..
,
In[3]:=fig2=Plot[{f[t], f'[t]}, {t, 0, 12}, PlotStyle{ {}, Dashing[{0.04}]},
PlotRangeAll]
1
0.75
0.5
0.25
2

10

-0.25
-0.5
-0.75

,
, fig1 fig2,
In[4]:=Show[fig1, fig2]

11.3

199

1
0.75
0.5
0.25
2

10

-0.25
-0.5
-0.75

,
.
11.4
, {0,5}, ,
:
1) y+y=x+3, y(0)=1, y(0)=0
4) y+y=sin(x) , y(0)=0, y(0)=-1/2,
x
2) y+y=2 e , y(0)=0, y(0)=1,
5) y+y=x, y(0)=2,
3) y-y=1, y(0)=0, y(0)=1,
6) y+y=exp(-x), y(0)=0,

11.3
10 gr ,

dr
n
, x[0] = 0 cm, x'[0] = 10
dt
cm/sec, y[0] = 0 cm, y'[0] = 100 cm/sec, x y
.
() n=0 () n=0.1 cgs,
.
: :
In[1]:=m = 10

200


g = 10
Out[1]=10
Out[2]=10
(* {t, 0, 100}
*)
In[3]:=newtonsol[n_]:=NDSolve[{y''[t]==-n y'[t]-g, x''[t]==-n x'[t],
x[0]==0, x'[0]==10, y[0]==0, y'[0]==100}, {x, y}, {t, 0, 100}]
(* *)
In[4]:=boli1=newtonsol[0.]
Out[4]={{x -> InterpolatingFunction[{{0., 100.}}, <>],
y -> InterpolatingFunction[{{0., 100.}}, <>]}}
(* *)
In[5]:=boli2=newtonsol[0.1]
Out[5]={{x -> InterpolatingFunction[{{0., 100.}}, <>],
y -> InterpolatingFunction[{{0., 100.}}, <>]}}

, 2:
(* *)
In[6]:=p1= ParametricPlot[Evaluate[{x[t], y[t]} /. boli1], {t, 0, 25},
PlotRange -> All, PlotStyle -> RGBColor[1, 0, 0],
AxesLabel -> {"x[t]", "y[t]"}]

Evaluate, . , ,
x(t) y(t)
, Plot, {x[t], y[t]}/.boli1.
Plot
.

11.3

201
y@tD

400
200
50

100

150

200

250

x@tD

-200
-400
-600

(* *)
In[7]:=p2=ParametricPlot[Evaluate[{x[t], y[t]} /. boli2], {t, 0, 25},
PlotRange -> All, PlotStyle -> RGBColor[0, 0, 1],
AxesLabel -> {"x[t]", "y[t]"}]
y@tD

200

20

40

60

80

x@tD

-200
-400
-600

In[8]:=Show[p1, p2] (* *)

202
y@tD
400
200
50

100

150

200

250

x@tD

-200
-400
-600


:
In[9]:=fg1=Plot[Evaluate[{x[t], y[t]} /.boli1], {t, 0, 25}, PlotRange -> All,
PlotStyle -> RGBColor[1, 0, 1], AxesLabel -> {"time", "x[t], y[t]"}]
In[10]:=fg2=Plot[Evaluate[{x[t], y[t]} /.boli2], {t, 0, 25}, PlotRange ->
All, PlotStyle -> RGBColor[1, 0, 1], AxesLabel -> {"time", "x[t], y[t]"}]
In[11]:=Show[fg1, fg2] (*
*)

12.1

203

12

, Mathematica

.
.
, ,
. 5 ,
.
Begin BeginPackage,
Mathematica .
12.1,
12.2, 12.3 .
12.2 ,
.

12.1
Mathematica
() , (Global),
(Directories)
Windows.
Mathematica.

204

Mathematica
$Context
In[1]:=$Context
Out[1]=Global`
, , yy yy=2,
In[2]:=yy=2
Out[2]=2
, , Global.
?Global`*
,
In[3]:=?Global`*
Global`yy
yy=2.
Global, System
.
$ContextPath
,
In[4]:=$ContextPath
Out[4]={Global`, System`}
, ,
,
.
,
.
,

12.2 *

205
Begin["name`"]
.
.
.
End[],

BeginPackage["name`"]
.
.
.
EndPackage[].
Begin BeginPackage ,
End EndPackage, , .
,
.
, .

12.2 *
12.1,
Begin
BeginPackage.
.

12.2.1 Begin
, ,
Global, Begin["name`"],

,
.

206

End[].
Global,
.
,
.
, ,

. ,
. ,

,
. ,
In[5]:=Begin["Topiko1`"]
Out[5]=Topiko1`
In[6]:=$Context
Out[6]=Topiko1`
In[7]:=$ContextPath
Out[7]={Global`, System`}
In[8]:=xx=3
Out[8]=3
In[9]:=Print[xx]
From In[9]:=3
Topiko1
Global, .. yy
In[2],
In[10]:=Print[yy]
From In[10]:=2

Global, .. yy,
.

12.2 *

207

Global
Global, .. xx,
In[11]:=yy=3
Out[11]=3
In[12]:=End[]
Out[12]=Topiko1`
In[13]:=$Context
Out[13]=Global`
In[14]:=$ContextPath
Out[14]={Global`,System`}
In[15]:=Print[xx]
From In[15]:=xx
In[16]:=Print[yy]
From In[16]:=3

xx, , In[8],
In[17]:=Begin["Topiko1`"]
Out[17]=Topiko1`
In[18]:=xx
Out[18]=3
In[19]:=End[]
Out[19]=Topiko1`

12.2.2 BeginPackage

BeginPackage["name`"] ( ) EndPackage[] (
).
,
,
Begin. ,
Global
,

208


.
,
,

.

.
,
In[20]:=ClearAll["Global`*"]
In[21]:=yy=4
Out[21]=4
In[22]:=BeginPackage["Topiko2`"]
Out[22]=Topiko2`
In[23]:=$Context
Out[23]=Topiko2`
In[24]:=$ContextPath
Out[24]={Topiko2`,System`}
In[25]:=Print[yy]
From In[25]:=yy
In[26]:=yy=5
Out[26]=5
In[27]:=yz=6
Out[27]=6
,
yz, yy. yy=5
yy=4 Global.
In[28]:=EndPackage[]
In[29]:=$Context
Out[29]=Global`
In[30]:=$ContextPath
Out[30]={Topiko2`,Global`,System`}

12.2 *

209

In[31]:=Print[yy]
From In[31]:=4
In[32]:=Print[yz]
From In[32]:=6

12.2.3
,
Begin BeginPackage.

,
BeginPackage[SupportDir`]
x1::usage=Comments for variable x1
.
.
Begin[`Private`]
x1=.
.
.
End[]
EndPackage[]
, SupportDir
Global, .. x1
Global.


Private Global
, , SupportDir.

Private Global,
SupportDir. ,
SupportDir,
variablename::usage=Comments for the variablename

210

variablename .
Global ?variablename.

12.3
ASCII
, .. Notepad Windows.
.m, .. PackageStructure.m.
, , :
(* : PackageStructure.m *)
(* : G. Theodorou *)
(* *)
(* ,
BeginPackage.
*)
BeginPackage["PackageDir`"]
(*
Global.
. *)
userFunction::usage="userFunction[x] is accessible by user"
(* , `Private`,
Begin *)
Begin["`Private`"]
(* *)
userFunction[x_]:=Sin[x]+Cos[x]
(* `Private` *)
End[]
(* *)
Protect[userFunction]
(* PackageStuctureDir` *)

12.3

211

EndPackage[]
(* *)
12.2.3,
Global
. BeginPackage,
Begin, ,
, Global.
COMMON
FORTRAN.

.
<<Direction\filename`
,
, Mathematica,

<< filename`
..
<<PackageStructure`
Mathematica
$Path,
.

Program Files,
Mathematica ,
Documents and Settings.

212

12.4

.
6.5, .
(* : Paragontiko.m *)
(* : G. Theodorou *)
(* *)
BeginPackage["paragontiko`"]
paragontiko::usage="paragontiko[n] calculates factorial of an integer"
Begin["`Private`"]
x=0
z=1
paragontiko[y_]:=If[y==0, 1, While[!(x==y), x=x+1; z=z*x]; z]
End[]
EndPackage[]
12.1

.

12.4

213

,
.
Assumptions: , .. Integrate,

.
Integrate[Sin[x] BesselJ[0, a x]/x, {x, 0, Infinity}, Assumptions {Im[a] == 0,
a^2<1}]
, Assumption,
.
DiracDelta[x]: Dirac, (x).
Dt[f[x, y, , t], t]:
f(x, y, , t) t.
Evaluate: .
, Plot[D[x^2,x], {x, 0, 10}]
.
Table.
Evaluate .
:
Plot[Evaluate[D[x^2, x], {x, 0, 5}]
Table[Evaluate[D[x^2 Sin[x], x], {x, 0, 5}]

214

FixedPoint: FixedPoint[f, x] f
f[x],
, ..
,
FixedPoint[(#+2/#)/2&, N[1, 40]]

2 ,

KroneckerDelta[n1, n2, ]: Kroneker,


{ni} .
IdentityMatrix[n]: n n .
Nest: Nest[f, x, n] , n , f
f[x], , ..

2 ,
,
Nest [ (# + 2/#)/2&, N[1,40], 7]
NestList: List,
, ..
NestList [ (# + 2/#)/2&, N[1,40], 7]
Options: Options[Expression]
Expression, ..
Options[NDSolve]
Random[ ]:
(0,1). ,
Random[type, range]
, .
Roots: Roots[f[x]==0, x] f[x],
.

12.4

215

Sign[x]: x, +1 x>0, 0 x=0, 1 x<0.


UnitStep[x]: , 0 x<0 1
x0.

216

12.4

217

1. N. Blachman and C. Williams, Mathematica a Practical Approach, Prentice


Hall (1999).
2. J. Clynn and T. Gray, The Beginners Guide to Mathematica, Cambridge
University Press (1997).
3. E. Don, Schaums Outline: Mathematica, McGraw-Hill (2001).
4. R. Gass, Mathematica for Scientists and Engineers: Using Mathematica to do
Science, Prentice Hall (1998).
5. J. W. Gray, Mastering Mathematica: Programming Methods and Applications,
Second Edition. Academic Press (1998).
6. R. Meader, Programming in Mathematica, Addison-Wesley (1997).
7. . , Mathematica, ,
(2000).
8. P.T. Tam, A Physicists Guide to Mathematica, Academic Press.
9. . , Mathematica ,
(2001).
10. S. Wolfram, The MATHEMATICA book, fourth edition, Cambridge University
Press (1999).
11. M. L. Abbel and J. P. Braselton, Differential Equations with Mathematica,
Academic Press, New York (1993).
12. G. Baumann, Mathematica in Theoretical Physics, TELOS / Springer-Verlag,
New York (1996).
13. K.R. Coombes, R.L. Lipsman, and J.M. Rosenberg, Multivariable Calculus and
Mathematica, Springer-Verlag, New York, (1998).
14. H. Varian, Economic and Financial Modeling with Mathematica, TELOS /
Springer-Verlag, Berlin, (1993).
15. I. Vardi, Computational Recreations in Mathematica, Addison-Wesley, New
York (1991).

218

16. . . , ,
(1999).
17. R.H. Enns and G.C, McGuire, Nonlinear Physics with Mathematica for
Scientists and Engineers, Birkhuser, Berlin (2001).
18. . . , ,
(1988).

You might also like