• Tidak ada hasil yang ditemukan

Sequences and Series

Dalam dokumen BOOK College mathematics and statistics (Halaman 103-109)

The colon operator allows us to investigate sequences because it is easy to define input values, and, as we will see, we can define our inputs from the left and right so that they may be colored differently in graphs. Our first example considers 1/n near 0 and we view 1/n as a function where as in the following example we create the sequence as we did in the introduction example. There are always multiple ways to achieve goals in R.

Our example begins by defining input values from the right of 0, nr, and from the left of 0, nl. The values of nr are from 1 down to 1/100 where 100:1 generates the sequence of integers from 100 down to 1 and then each value is divided by 100. Similarly nl is a sequence of values from -1 up to -1/100.

The functionfis defined to be 1/n in the next line. Theparcommand creates 4,6,4,4 lines around the graphing frame. We set this because they-axis caption needed extra space. We first plot with the positive input values. In this case a scatter plot is being produced with nr as thexvalues andf(nr)as theyvalues.

Briefly, ourplot command colors the point red, sets thexand y range with xlim and ylim, and creates axis captions with xlab and ylab. The expression command for ylab creates the fraction 1/n. The points command adds the negative side of the graph and colors those points blue. For reference,abline adds a versicle line a 0, with dashed line type, and colored by gray40.

Sequences and Series 87 R Code

> nr=100:1/100

> nl=-100:-1/100

> f=function(n){1/n}

> par(mar=c(4,6,4,4))

> plot(nr,f(nr),col="red",xlim=c(-1,1),ylim=c(-100,100), xlab="Value of n",ylab=expression(frac(1,n)))

> points(nl,f(nl),col="blue")

> abline(v=0,lty=2,col="gray40")

−1.0 −0.5 0.0 0.5 1.0

−100−50050100

Value of n

1 n

We now move to exploring 1/nfor “large” values of nwhile comparing it to 1/n2. We begin the code by defining the lower and upper values that will be used by setting them to l and u. If we later decide to use different lower and upper values for our sequence this is the only part of the code that will need to be changed. Next, n1 creates a sequence from 1 to 1000 and then our first sequence, s1, is created as 1/n1. Similarly our second sequence is created

by squaring the values from 1 to 1000 and setting s2=1/n2. Ourx-axis range is set by l and u, but then our y-axis range is set by s1 and s2. Our next two lines will be used to set our ylim in plot. The command min finds the minimum of a sequence and we first find the minimum of s1 and s2 separately and then set miny to be the smallest of these two values. Similarly, we find maxy. We are assuming here that since we are exploring these sequences we don’t know which is smaller. Also, this code allows us to easily change s1 and s2 for other examples.

We now graph s1 by usingplot. Again, we are technically creating a scatter plot so we need to plot n1 and s1. We set the point type to 1. Note how our xlim and ylim take advantage of the variables l, u, miny, and maxy, created earlier in the code. We then add the scatter plot for the second sequence with pointsand note that we use a different point type, pch=17 which is a solid triangle, and color it red to clearly differentiate it from the first sequence. We also added a legend for clarity and a thicker horizontal line aty= 0.

R Code

> l=1

> u=1000

> n1=l:u

> s1=1/n1

> n2=(l:u)^2

> s2=1/n2

> miny=min(min(s1),min(s2))

> maxy=max(max(s1),max(s2))

> plot(n1,s1,pch=1,xlim=c(l,u),ylim=c(miny,maxy), xlab="n",ylab="Sequence Values")

> points(n1,s2,pch=17,col="red")

> legend("topright",c(expression(frac(1,n)),

expression(frac(1,n^2))),pch=c(1,17),pt.cex=c(1.5,1.25), col=c("black","red"),y.intersp=1.25)

> abline(h=0,lwd=2)

Sequences and Series 89

0 200 400 600 800 1000

0.00.20.40.60.81.0

n

Sequence Values

1 n 1 n2

As we can see this graph really doesn’t help us understand the differences, if any, between 1/nand 1/n2. We will next zoom in on our graph to the section fromx= 900 tox= 1000. Our code will be almost identical to the previous except, for example, instead of considering all of s1 we will use only the values from 900 to 1000 by using s1[c(l2:u2)], where the square brackets set the range of the desired sequence. Our code begins by setting two new lower and upper values with l2 and u2, which are used to select the range of the sequences. We again find min and maxyvalues set to miny2 and maxy2. We restrict scientific notation withoptions(scipen=9). Theplotandpointscommands are similar to those used in the previous example. The legend was moved slightly because topright was in the way of the graph so we chose x = 990 and y = 0.0009 coordinates for the location of the legend. We now have a graph that is a portion of the first graph and one that is more useful in understanding the differences between the two sequences.

R Code

> l2=900

> u2=1000

> miny2=min(min(s1[c(l2:u2)]),min(s2[c(l2:u2)]))

> maxy2=max(max(s1[c(l2:u2)]),min(s2[c(l2:u2)]))

> options(scipen=9)

> plot(n1[c(l2:u2)],s1[c(l2:u2)],pch=1,xlim=c(l2,u2), ylim=c(miny2,maxy2),xlab="n",ylab="Sequence Values")

> points(n1[c(l2:u2)],s2[c(l2:u2)],pch=17,col="red")

> legend(990,0.0009,c(expression(frac(1,n)),

expression(frac(1,n^2))),pch=c(1,17),pt.cex=c(1.5,1.25), col=c("black","red"),y.intersp=1.25)

> abline(h=0,lwd=2)

900 920 940 960 980 1000

0.00000.00020.00040.00060.00080.0010

n

Sequence Values

1 n 1 n2

We know that both of these sequences converge to 0 asn gets large, but their convergence is not the same. We now turn our attention to the sum of

Sequences and Series 91 these sequences to investigate the questions of whether or not the difference in the convergence to 0 of the sequences impacts their corresponding series, P

0 1/n and P

0 1/n2. The function cumsum allows us to easily generate the sequence of partial sums. Our next graph will have the two sequences of partial sums graphed from 900 to 1000 since that seemed more useful in our previous example (this choice is questioned in an exercise).

We begin by defining l2 and u2. Recall that s1 was the sequence 1/n, and so ps1 is the sequence of partial sums ofP

0 1/n. For example the first few terms of ps1= (1/1,1/1 + 1/2,1/1 + 1/2 + 1/3, . . .). What is the last term of ps1? The sequence ps2 is the sequence of partial sums of 1/n2. Everything in the code after ps2 is exactly the same as the previous example except for one change in the legend command. What is that change and why was it necessary? We see from the graph that the behaviors of the two sequences of partial sums appear very different. In the endP

0 1/n diverges while the P

0 1/n2converges.

R Code

> l2=900

> u2=1000

> ps1=cumsum(s1)

> ps2=cumsum(s2)

> miny3=min(min(ps1[c(l2:u2)]),min(ps2[c(l2:u2)]))

> maxy3=max(max(ps1[c(l2:u2)]),min(ps2[c(l2:u2)]))

> plot(n1[c(l2:u2)],ps1[c(l2:u2)],pch=1,xlim=c(l2,u2), ylim=c(miny3,maxy3),xlab="n",

ylab="Sequence of Partial Sums Values")

> points(n1[c(l2:u2)],ps2[c(l2:u2)],pch=17,col="red")

> legend(990,7,c(expression(frac(1,n)),

expression(frac(1,n^2))),pch=c(1,17),pt.cex=c(1.5,1.25), col=c("black","red"),y.intersp=1.25)

900 920 940 960 980 1000

234567

n

Sequence of Partial Sums Values

1 n 1 n2

Dalam dokumen BOOK College mathematics and statistics (Halaman 103-109)