• Tidak ada hasil yang ditemukan

Pythagorean Triples and a Checkerboard Plot

Dalam dokumen BOOK College mathematics and statistics (Halaman 38-45)

Pythagorean Triples and a Checkerboard Plot 21 expression(2*e^(r*x)+10),main="r from 1 to 10")

> for(r in 2:10){curve(E(2,r,10)(x),add=TRUE,lwd=2, col=rainbow(9)[r-1])}

0 1 2 3 4 5

50100150200250300

r from 1 to 10

x 2e(rx)+10

Euclid1(2,3) returns the vector 5, 12, 13. Note that the function as written assumes that the first input is the smaller of the two asEuclid1(3,2)returns

−5, 12, and 13. One way to deal with this is to have the function return

“error” if the first input is larger, and this is done with the functionEuclid2.

R Code

> Euclid1=function(n,m){triple=numeric(3) + triple[1]=m^2-n^2

+ triple[2]=2*m*n + triple[3]=m^2+n^2 + return(triple)}

> Euclid1(2,3) [1] 5 12 13

The key difference betweenEuclid1andEuclid2is that the latter has an if- then-else statement. If (m > n) then we do exactly what was done forEuclid1.

In fact, we could have used the functionEuclid1withinEculid2, but we chose to keep them independent. On the other hand, if m > n is not true, then the function returns “error”. With this functionEuclid2(3,2)returns “error”.

Note that each part of the if and else statements is encased in braces and the last two braces represent closing the else portion and then closing the function definition.

R Code

> Euclid2=function(n,m){

+ if (m > n){

+ triple=numeric(3) + triple[1]=m^2-n^2 + triple[2]=2*m*n + triple[3]=m^2+n^2 + return(triple)}

+ else {return("error")}}

> Euclid2(3,2) [1] "error"

We now proceed to create a checkerboard plot of Pythagorean triples. Each row of the plot darkens the three squares that represent a given triple. To get there we have to create a matrix of 1s, representing triples, and 0s. Our first step is to create a matrix of n and m values to be evaluated byEuclid1. The columns of this matrix will be filled by n and m. We define n to be the list of numbers 1,1,2,1,2,3, . . . ,1,2,3,4,5,6 and m to be 2,3,3, . . . ,7,7,7,7,7,7, where rep(i,j) repeats the value i, j times. We could use for loops to create these, which would be more efficient, but possibly less clear. We use matrix

Pythagorean Triples and a Checkerboard Plot 23 to define A wherec(n,m)concatenates the vectors n and m, and that list of values fills the matrix A with two columns by column, byrow=FALSE. The first five rows of A and both columns are shown withA[1:5,].

R Code

> n=c(1:1,1:2,1:3,1:4,1:5,1:6)

> m=c(rep(2,1),rep(3,2),rep(4,3),rep(5,4),rep(6,5), rep(7,6))

> A=matrix(c(n,m),ncol=2,byrow=FALSE)

> A[1:5,]

[,1] [,2]

[1,] 1 2

[2,] 1 3

[3,] 2 3

[4,] 1 4

[5,] 2 4

The mapply function evaluates Euclid1 with each row of A. Note that A[,1] is column 1 andA[,2]is column 2. The output is a 3 by 21 matrix where each column is a Pythagorean triple. We illustrate the first five columns with Triple[,1:5] where the empty first argument in Triples returns all rows.

R Code

> Triples=mapply(Euclid1, A[,1], A[,2])

> Triples[,1:5]

[,1] [,2] [,3] [,4] [,5]

[1,] 3 8 5 15 12

[2,] 4 6 12 8 16

[3,] 5 10 13 17 20

Our second-to-last construction before producing the plot creates a matrix whose first column is the order of the triple and the second is the associated triple. In this case rep(1:21,each=3) creates the vector 1,1,1,2,2,2, . . . ,21,21,21 andc(Triples)stacks the columns of Triples. These two vectors are put together withcbindor column bind. The first 10 rows and both columns are returned withTwo.Columns[1:10,].

R Code

> Two.Columns=cbind(rep(1:21,each=3),c(Triples))

> Two.Columns[1:10,]

[,1] [,2]

[1,] 1 3

[2,] 1 4

[3,] 1 5

[4,] 2 8

[5,] 2 6

[6,] 2 10

[7,] 3 5

[8,] 3 12

[9,] 3 13

[10,] 4 15

Finally, we create the 21 by 85 matrix B with all 0s as we have 21 triples and the largest value is 85. The second line is key in that each row of Two.Columns is read as an (i,j) location of B and is replaced with a 1. The first two rows and 10 columns are returned by B[1:2,1:10]. Notice, for example that in the first row there is a 1 under 3,4, and 5. Similarly, compare the second row with the output of Two.Columns[1:10,] above.

R Code

> B=matrix(0,ncol=85,nrow=21)

> B[Two.Columns]=1

> B[1:2,1:10]

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

[1,] 0 0 1 1 1 0 0 0 0 0

[2,] 0 0 0 0 0 1 0 1 0 1

The beginning of creating our graph sets the bottom, left, top and right, margins to 3,3,4, and 2, respectively with par(mar=c(3,3,4,2)). The title of the graph is complicated enough that we define it separately and set it to Title. Here, we use expression, for mathematical objects, and then atop to stack our two lines. The first line is created bypastewhere text is in quotes.

The second part of atopis all text and in quotes. Theimagefunction creates the checkerboard plot where each column of the matrix is plotted along a row, and hence the need to transpose B witht(B). Theimagefunction creates the plot from 0 to 1 and so we remove the axes with axes=FALSE and we will create our own in subsequent lines. The color scheme is set with col and the title is added with the main argument.

We set corners to the four corners of the graph frame withpar(“usr”). To create a grid,ablineis used with the vertical lines set to the sequence generated byseq(corners[1],corners[2],len=86). Note that we need 86 lines for 85 cells.

Similarly, h=seq(corners[3],corners[4],len=22) creates the horizontal lines.

Pythagorean Triples and a Checkerboard Plot 25 We now use axis three times. The first use adds 85 tick marks along the x- axis. Here the first argument 1 is for thex-axis, the second is where to place the tick marks, and the third leaves off any labels with label=FALSE. The next line places the values 5 through 85,seq(5,85,by=5)set to label, at the locations listed by seq(start.axis,1,length=17), where start.axis is the fifth tick mark found byseq(0,1,length=85)[5]. Again, the first argument of 1 is for the x-axis. The last use of axis is for the y-axis, first argument 2, and labels the 21 locations from 0 to 1,seq(0,1,length=21), with the values from the vector m created earlier. The last argument, las=2, orients the labels so that they are horizontal.

R Code

> par(mar=c(3,3,4,2))

> Title=expression(atop(paste("Pythagorean Triples Generated by ",m^2-n^2,", ",2*m*n, ",and",m^2+n^2,"."),

"The y-axis displays m with the values of n taken from 1 to m-1."))

> image(t(B),axes=FALSE, col=c("white","black"), main=Title)

> corners=par("usr")

> abline(v=seq(corners[1],corners[2],len=86), h=seq(corners[3],corners[4],len=22))

> axis(1,at=seq(0,1,length=85),label=FALSE)

> start.axis=seq(0,1,length=85)[5]

> axis(1,at=seq(start.axis,1,length=17), label=seq(5,85,by=5))

> axis(2,at=seq(0,1,length=21),label=m,las=2)

Pythagorean Triples Generated by m2−n2, 2mn, and m2+n2. The y−axis displays m with the values of n taken from 1 to m−1.

5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 2

3 3 4 4 4 5 5 5 5 6 6 6 6 6 7 7 7 7 7 7

Code Review for the Functions and Their Graphs Chapter abline(h=v1,v=v2) adds horizontal lines at the points given by vec-

torv1 and vertical lines at the points given by vector v2. Execute

?abline for optional graphing parameters.

axis(i,at=,label=) adds tick marks to the graph at the location given by at with labels given by labels. The values ofi are 1, 2, 3, and 4, for the bottom, left, top, and right axis, respectively.

curve(f,a,b) graphs the functionf fromato b. When the argument add=TRUE is included the function is added to the current graph.

There are numerous options. Execute ?curve for optional graphing parameters.

function(x1, x2, . . . , xn){expression} defines a function of

Pythagorean Triples and a Checkerboard Plot 27 x1, x2, . . . , xn with expression. The function will return the last line of expression and for clarity use return().

grid(n1,n2) adds a grid to the current graph withn1 andn2 cells in the x and y direction, respectively. The arguments can be set to NULL in which case lines are places at the current tick marks of the graph. Two useful options are col, for the color of the grid lines, and lty, for the type of grid lines.

ifelse(condition,expression1,expression2) returns expression1 if condition is true or else expression2 is returned.

image(A) is used here to create a checkerboard plot where A is a matrix of 0s and 1s. The columns of A are associated with rows on the checkerboard. Use col=c(“color1”,“color2”) for the colors of the grids.

par(mar=c(i,j,k,l)) sets the bottom, left, top, and right margins to i,j, k, andl lines, respectively.

par(“usr”) returns a vector x1, x2, y1, andy2 representing the cor- ners of the current graph.

points(x,y) adds a point at (x, y) to the graph. Vectors can be used for x and y to add multiple points. Execute ?points for optional graphing parameters.

polar(t,f(t)) returns a polar plot off(t), wheretis given in radians.

Uses the pracma package. Allows for most optional graphing pa- rameters, with grcol the grid color and bxcol the box color specific to polar().

rainbow(n) returns a vector ofncolors spaced across the colors of a rainbow.

stepfun(v1,v2) returns a step function with jumps listed by vector v1 and jumps defined by the vectorv2. Note vectorv2 must be one longer than vectorv1.

which.min(v) returns the location of the minimum value of vectorv, with the first location being returned if there are ties.

Dalam dokumen BOOK College mathematics and statistics (Halaman 38-45)