Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (25.81 MB, 585 trang )

4.2 Manipulating Lists: More on Part and Map

249

300

200

100

-4

-2

2

-100

Figure 4-9 Graph of f x

3x4

8x3

30x2

72x

We locate and classify the points by evaluating f x and f x for each

of the numbers in critnums. f[x]/.x->a replaces each occurrence

of x in f x by a, so entering

In[683]:=

Out[683]=

x, f x , f

x

/.critnums

2, 152, 180 , 1, 37, 72 , 3, 27, 120

replaces each x in the list x, f x , f x by each of the x-values in

critnums.

By the Second Derivative Test, we conclude that y f x has relative

minima at the points 2, 152 and 3, 27 while f x has a relative

, 152 is the absolute

maximum at 1, 37 . In fact, because limx

minimum value of f x . These results are conﬁrmed by the graph of

y f x in Figure 4-9.

In[684]:= Plot f x , x, 4, 4

4

250

To determine if f is listable,

enter Attributes[f].

Chapter 4 Introduction to Lists and Tables

Map is a very powerful and useful function: Map[f,list] creates a list consisting

of elements obtained by evaluating f for each element of list, provided that

each member of list is an element of the domain of f. Note that if f is listable,

f[list] produces the same result as Map[f,list].

EXAMPLE 4.2.2: Entering

In[685]:= t1

Out[685]=

Table n, n, 1, 100

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,

43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,

56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,

69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,

82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,

95, 96, 97, 98, 99, 100

computes a list of the ﬁrst 100 integers and names the result t1. We

then deﬁne f x

x2 and use Map to square each number in t1.

In[686]:= f x

xˆ2

Out[686]= x2

In[687]:= Map f, t1

Out[687]=

1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169,

196, 225, 256, 289, 324, 361, 400, 441, 484, 529,

576, 625, 676, 729, 84, 841, 900, 961, 1024, 1089,

1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681,

1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401,

2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249,

3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225,

4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329,

5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561,

6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921,

8100, 8281, 8464, 8649, 8836, 8281, 8464, 8649,

8836, 9025, 9216, 9409, 9604, 9801, 10000

4.2 Manipulating Lists: More on Part and Map

251

The same result is accomplished by the pure function that squares its

argument. Note how # denotes the argument of the pure function; the

& symbol marks the end of the pure function.

In[688]:= Map #ˆ2&, t1

Out[688]=

1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196,

225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625,

676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225,

1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936,

2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809,

2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844,

3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041,

5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400,

6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921,

8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604,

9801, 10000

On the other hand, entering

In[689]:= t1

Out[689]=

Table

1, 1 ,

2, 1 ,

3, 1 ,

4, 1 ,

5, 1 ,

a, b , a , 1 , 5 , b , 1 , 5

1, 2 ,

2, 2 ,

3, 2 ,

4, 2 ,

5, 2 ,

1, 3 ,

2, 3 ,

3, 3 ,

4, 3 ,

5, 3 ,

1, 4 ,

2, 4 ,

3, 4 ,

4, 4 ,

5, 4 ,

1, 5 ,

2, 5 ,

3, 5 ,

4, 5 ,

5, 5

is a list (of length 5) of lists (each of length 5). Use Flatten to obtain a

list of 25 points, which we name t2.

In[690]:= t2

Out[690]=

Flatten t1, 1

1, 1 ,

2, 3 ,

3, 5 ,

5, 2 ,

1, 2 ,

2, 4 ,

4, 1 ,

5, 3 ,

1, 3 ,

2, 5 ,

4, 2 ,

5, 4 ,

1, 4 , 1, 5 , 2, 1 , 2, 2 ,

3, 1 , 3, 2 , 3, 3 , 3, 4 ,

4, 3 , 4, 4 , 4, 5 , 5, 1 ,

5, 5

f is a function of two variables. Given an ordered pair x, y , f x, y

returns the ordered triple x, y, x2 y2 .

In[691]:= f

x ,y

We then use Map to apply f to t2.

x, y , xˆ2

yˆ2

252

Chapter 4 Introduction to Lists and Tables

In[692]:= Map f, t2

Out[692]=

1, 1 , 2 ,

2, 4 , 20

2, 3 , 13

3, 3 , 18

4, 2 , 20

5, 1 , 26

5, 5 , 50

,

,

,

,

,

1, 2 , 5 ,

1, 5 , 26

2, 5 , 29

3, 4 , 25

4, 3 , 25

5, 2 , 29

1, 3 , 10 , 1, 4 , 17 ,

, 2, 1 , 5 , 2, 2 , 8 ,

, 3, 1 , 10 , 3, 2 , 13

, 3, 5 , 34 , 4, 1 , 17

, 4, 4 , 32 , 4, 5 , 41

, 5, 3 , 34 , 5, 4 , 41

,

,

,

,

We accomplish the same result with a pure function. Observe how

#[[1]] and #[[2]] are used to represent the ﬁrst and second arguments: given a list of length 2, the pure function returns the list of

ordered triples consisting of the ﬁrst element of the list, the second

element of the list, and the sum of the squares of the ﬁrst and second

elements.

In[693]:= Map

Out[693]=

#

1

1, 1 , 2 ,

1, 5 , 26

2, 4 , 20

3, 3 , 18

4, 2 , 20

5, 1 , 26

5, 5 , 50

,#

,

,

,

,

,

2

,#

1

ˆ2

#

2

1, 2 , 5 , 1, 3 , 10 ,

2, 1 , 5 , 2, 2 , 8 ,

2, 5 , 29 , 3, 1 , 10

3, 4 , 25 , 3, 5 , 34

4, 3 , 25 , 4, 4 , 32

5, 2 , 29 , 5, 3 , 34

ˆ2 &, t2

1, 4 , 17 ,

2, 3 , 13 ,

, 3, 2 , 13

, 4, 1 , 17

, 4, 5 , 41

, 5, 4 , 41

,

,

,

,

EXAMPLE 4.2.3: Make a table of the values of the trigonometric functions y sin x, y cos x, and y tan x for the principal angles.

SOLUTION: We ﬁrst construct a list of the principal angles which is

accomplished by deﬁning t1 to be the list consisting of nΠ/ 4 for n 0,

1, . . . , 8 and t2 to be the list consisting of nΠ/ 6 for n

0, 1, . . . , 12.

The principal angles are obtained by taking the union of t1 and t2.

Union[t1,t2] joins the lists t1 and t2, removes repeated elements,

and sorts the results. If we did not wish to remove repeated elements

and sort the result, the command Join[t1,t2] concatenates the lists

t1 and t2.

In[694]:= t1

Table

nΠ

, n, 0, 8

4

t2

Table

nΠ

, n, 0, 12

6

4.2 Manipulating Lists: More on Part and Map

In[695]:= prinangles

Out[695]=

Union t1, t2

Π Π Π Π 2Π 3Π 5Π

, , , ,

,

,

, Π,

6 4 3 2 3

4

6

7 Π 5 Π 4 Π 3 Π 5 Π 7 Π 11 Π

,

,

,

,

,

,

,2 Π

6

4

3

2

3

4

6

0,

We can also use the symbol

, which is obtained by clicking on the

button on the BasicTypesetting palette to represent Union.

In[696]:= prinangles

Out[696]=

253

t1

The BasicTypesetting

palette:

t2

Π Π Π Π 2Π 3Π 5Π

, , , ,

,

,

, Π,

6 4 3 2 3

4

6

7 Π 5 Π 4 Π 3 Π 5 Π 7 Π 11 Π

,

,

,

,

,

,

,2 Π

6

4

3

2

3

4

6

0,

Next, we deﬁne f x to be the function that returns the ordered quadruple x, sin x, cos x, tan x and compute the value of f x for each number

in prinangles with Map naming the resulting table prinvalues.

prinvalues is not displayed because a semi-colon is included at the

end of the command.

In[697]:= Clear f

f x

x, Sin x , Cos x , Tan x

In[698]:= prinvalues

Map f, prinangles

Finally, we use TableForm illustrating the use of the TableHeadings

option to display prinvalues in row-and-column form; the columns

are labeled x, sin x, cos x, and tan x.

In[699]:= TableForm prinvalues,

TableHeadings

None, "x", "sin x ", "cos x ", "tan x "

Remember that the result of

using TableForm is not a

list so cannot be manipulated

like lists.

254

Chapter 4 Introduction to Lists and Tables

x

0

Π

6

Π

4

Π

3

Π

2

2Π

3

3Π

4

5Π

6

Out[699]= Π

7Π

6

5Π

4

4Π

3

3Π

2

5Π

3

7Π

4

11 Π

6

2Π

sin x

0

1

2

1

cos x

1

3

2

1

2

3

2

1

1

2

0

2

3

ComplexInfinity

3

2

1

1

2

1

2

2

3

2

1

3

2

1

1

2

0

1

2

1

2

3

2

1

0

3

1

1

3

0

1

3

1

2

1

2

0

3

2

1

1

2

1

2

2

3

2

1

1

2

tan x

0

1

3

1

3

ComplexInfinity

3

1

1

3

0

In the table, note that functions like y

tan x are undeﬁned at certain values of

x. y

tan x is undeﬁned at odd multiples of Π/ 2 and Mathematica appropriately

returns ComplexInfinity at those values of x for which y tan x is undeﬁned.

object=name assigns the

object object the name

name.

We can use Map on any list,

including lists of functions

and/or other lists.

Remark. The result of using TableForm is not a list (or table) and calculations on it

using commands like Map cannot be performed. TableForm helps you see results

in a more readable format. To avoid confusion, do not assign the results of using

TableForm any name: adopting this convention avoids any possible manipulation of TableForm objects.

Lists of functions are graphed with Plot:

Plot[Evaluate[listoffunctions],{x,a,b}]

graphs the list of functions of x, listoffunctions, for a

x

b.

4.2 Manipulating Lists: More on Part and Map

255

1

0.8

0.6

0.4

0.2

5

10

15

20

25

-0.2

-0.4

Figure 4-10

Graphs of Jn x for n

0, 1, 2, . . . , 8

EXAMPLE 4.2.4 (Bessel Functions): The Bessel functions of the ﬁrst

xy

x2 n2 y

0.

kind, Jn x , are nonsingular solutions of x2 y

BesselJ[n,x] returns Jn x . Graph Jn x for n 0, 1, 2, . . . , 8.

SOLUTION: In t1, we use Table and BesselJ to create a list of Jn x

for n 0, 1, 2, . . . , 8.

In[700]:= t1

Table BesselJ n, x , n, 0, 8

Next, we deﬁne a list, named grays, consisting of GrayLevel[i] for

8 equally spaced values of i between 0 and 0.8. We then use Plot to

graph each function in t1; the graphs in Figure 4-10 are shaded according to grays. In the plot, the graph of J0 x is black. Subsequent plots

are lighter; the lightest gray is the graph of J8 x .

In[701]:= grays

Table GrayLevel i , i, 0, 0.8, 0.8/7

Plot Evaluate t1 , x, 0, 25 , PlotStyle

grays

A different effect is achieved by graphing each function separately. To

do so, we deﬁne the function pfunc. Given a function of x, f, pfunc[f]

plots the function for 0 x 100. The resulting graphic is not displayed

because the option DisplayFunction->Identity is included in the

Plot command. We then use Map to apply pfunc to each element of

t1. The result is a list of 9 graphics objects, which we name t2. A nice

way to display 9 graphics is as a 3 3 array so we use Partition

to convert t2 from a list of length 9 to a list of lists, each with length

256

Chapter 4 Introduction to Lists and Tables

0.4

0.2

20 40 60 80 100

0.6

0.4

0.2

-0.2

-0.4

-0.2

0.4

0.3

0.2

0.1

0.4

0.3

0.2

0.1

-0.1

-0.2

-0.3

20 40 60 80 100

20 40 60 80 100

20 40 60 80 100

-0.1

-0.2

20 40 60 80 100

-0.2

0.3

0.2

0.1

20 40 60 80 100

-0.1

-0.2

20 40 60 80 100

0.3

0.2

0.1

0.3

0.2

0.1

0.3

0.2

0.1

-0.1

-0.2

-0.1

-0.2

0.4

0.2

20 40 60 80 100

-0.1

-0.2

20 40 60 80 100

Figure 4-11 In the ﬁrst row, from left to right, graphs of J0 x , J1 x , and J2 x ; in the second

row, from left to right, graphs of J3 x , J4 x , and J5 x ; in the third row, from left to right,

graphs of J6 x , J7 x , and J8 x

Think of Flatten and

Partition as inverse

functions.

3 – a 3 3 array. Partition[list,n] returns a list of lists obtained

by partitioning list into n-element subsets.

In[702]:= pfunc f

Plot f, x, 0, 100 ,

DisplayFunction Identity

t2

Map pfunc, t1

t3

Partition t2, 3

Instead of deﬁning pfunc, you can use a pure function instead. The

following accomplishes the same result. We display t3 using Show

together with GraphicsArray in Figure 4-11.

In[703]:= t2

t3

Map Plot #, x, 0, 100 ,

DisplayFunction Identity &, t1

Partition t2, 3

Show GraphicsArray t3

4.2 Manipulating Lists: More on Part and Map

EXAMPLE 4.2.5 (Dynamical Systems): Let fc x

0 and xn 1

the dynamical system given by x0

bifurcation diagram of fc .

257

x2 c and consider

fc xn . Generate a

SOLUTION: First, recall that Nest[f,x,n] computes the repeated

composition f n x . Then, in terms of a composition,

xn

1

fc n 0 .

fc xn

We will compute fc n 0 for various values of c and “large” values of n

so we begin by deﬁning cvals to be a list of 300 equally spaced values

of c between 2.5 and 1.

In[704]:= cvals

Table c, c, 2.5, 1., 3.5/299

We then deﬁne fc x

x2 c. For a given value of c, f[c] is a function

of one variable, x, while the form f[c_,x_]:=... results in a function

of two variables.

In[705]:= Clear f

f c

x

xˆ2

c

To iterate fc for various values of c, we deﬁne h. For a given value of c,

h c returns the list of points c, fc 100 0 , c, fc 101 0 , . . . , c, fc 200 0 .

In[706]:= h c

Table

n, 100, 200

c, Nest f c , 0, n

,

We then use Map to apply h to the list cvals. Observe that Mathematica

generates several error messages when numerical precision is exceeded.

We choose to disregard the error messages.

In[707]:= t1 Map h, cvals

General

ovfl Overflow occurred in computation.

General

ovfl Overflow occurred in computation.

General

ovfl Overflow occurred in computation.

General

stop Further output of General

ovfl will be suppressed during this calculation.

t1 is a list (of length 300) of lists (each of length 101). To obtain a list of

points (or, lists of length 2), we use Flatten. The resulting set of points

is plotted with ListPlot in Figure 4-12. Observe that Mathematica

again displays several error messages, which are not displayed here

Compare the approach used

here with the approach used

in Example 4.1.7.

258

Chapter 4 Introduction to Lists and Tables

xc n , n 100..200

2

1

-2.5 -2 -1.5 -1 -0.5

0.5

1

c

-1

-2

Figure 4-12 Bifurcation diagram of fc

for length considerations, that we ignore: Mathematica only plots the

points with real coordinates and ignores those containing Overflow[].

In[708]:= t2

Flatten t1, 2

ListPlot t2, AxesLabel

"c", xc n , n

100.0.2

4.2.1 More on Graphing Lists; Graphing Lists of Points

Using Graphics Primitives

Include the PlotJoined->True option in a ListPlot command to connect successive points with line segments.

Using graphics primitives like Point and Line gives you even more ﬂexibility.

Point[{x,y}] represents a point at x, y .

Line[{{x1,y1},{x2,y2},...,{xn,yn}}]

represents a sequence of points x1 , y1 , x2 , y2 , . . . , xn , yn connected with line segments. A graphics primitive is declared to be a graphics object with Graphics:

Show[Graphics[Point[{x,y}]] displays the point x, y . The advantage of

using primitives is that each primitive is affected by the options that directly precede it.

4.2 Manipulating Lists: More on Part and Map

Table 4-1

Year

1930

1935

1940

1945

1950

1955

1960

1965

1970

1975

1980

1985

1990

Union membership as a percentage of the labor force

Union Membership as a Percentage of the Labor Force

11.6

13.2

26.9

35.5

31.5

33.2

31.4

28.4

27.3

25.5

21.9

18.0

16.1

EXAMPLE 4.2.6: Table 4-1 shows the percentage of the United States

labor force that belonged to unions during certain years. Graph the data

represented in the table.

SOLUTION: We begin by entering the data represented in the table as

dataunion:

In[709]:= dataunion

30, 11.6 , 35, 13.2 , 40, 26.9 ,

45, 35.5 , 50, 31.5 , 55, 33.2 , 60, 31.4 ,

65, 28.4 , 70, 27.3 , 75, 25.5 , 80, 21.9 ,

85, 18. , 90, 16.1

the x-coordinate of each point corresponds to the year, where x is the

number of years past 1900, and the y-coordinate of each point corresponds to the percentage of the United States labor force that belonged

to unions in the given year. We then use ListPlot to graph the set of

points represented in dataunion in lp1, lp2 (illustrating the

PlotStyle option), and lp3 (illustrating the PlotJoined option).

All three plots are displayed side-by-side in Figure 4-13 using Show

together with GraphicsArray.

In[710]:= lp1

ListPlot dataunion

In[711]:= lp2

ListPlot dataunion,

PlotStyle PointSize 0.03

259

Tải bản đầy đủ (.pdf) (585 trang)