• Tidak ada hasil yang ditemukan

Riemann Boxes

Dalam dokumen BOOK College mathematics and statistics (Halaman 137-143)

Riemann Boxes 121

0 1 2 3 4

0510152025

x

f(x)=5+x2

For our first example will use the functionf(x) = 5+x2and four left boxes from 0 to 4. The first line of code defines the function. The par command sets the margins to 4, 5, 2, and 2 lines for the bottom, left, top, and right, respectively. (The default doesn’t leave enough space on the left to label it with f(x) = 5 +x2.) We create a graph of the function fromx= 0 tox= 4 with curve. We also set they−axis range fromy= 0, toy= 25 with ylim=c(0,25).

By default Graphs in R leaves some space between the axis and beginning of the graph. Visually, this is particularly unsightly with Riemann boxes and so the space is removed with yaxs=“i” and xaxs=“i”. Thex−axis is labelled with x by default, but the y−axis is labelled with f(x) = 5 +x2 by using expression. Theexpressionfunction allows for mathematical typesetting with a few quirks, one of which is that == typesets an = sign. The next 12 lines create the boxes. The segments function adds a line segment to the graph from the first point to the second point. In each case, lwd=2 draws a line that is twice the default thickness. All of the usual graphing options will work in

segmentssuch as col, if you want to color the segments, or lty if you want a dashed line type.

The next example will use the same function, but we will begin to gen- eralize the code as well as use 11 mid boxes that are shaded. We first define the function, and then the values for the left endpoint a=0, right endpoint b=4, and number of boxes n=11. By defining these variables first we can use a, b, and n in the rest of the code, and if we want to change a value then it only needs to be changed once. In fact, if you want to change the function, the interval, the number of subsets, or all of theses, nothing beyond the first four lines of code needs to be modified. As in the intro example, we set dx to be the width of a box and define mid to be the list of midpoint values for the boxes. As in the previous example, par sets the number of lines for the margins and curve graphs the function. Note that we use a and b in curve instead of 0 and 4. We changed the line width to 2 for this example.

R Code

> f=function(x){5+x^2}

> a=0

> b=4

> n=11

> dx= (b-a)/n

> mid=seq(a+dx/2, b, dx)

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

> curve(f,a,b,ylim=c(0,25),lwd=2,yaxs="i",xaxs="i", ylab=expression(f(x)==5+x^2))

> for(i in 1:n){

+ mid.box.x=c(mid[i]-dx/2,mid[i]-dx/2,mid[i]+dx/2, mid[i]+dx/2)

+ mid.box.y=c(0, f(mid[i]), f(mid[i]), 0) + polygon(mid.box.x,mid.box.y,col="gray85", border="black") }

> curve(f,a,b,lty=3,lwd=2,add=TRUE)

> points(mid,0*mid,col="red",pch=16)

> p=par("usr")

> text(p[1], p[4],paste("Sum=",sum(f(mid)*dx)),adj=c(0,1))

Riemann Boxes 123

0 1 2 3 4

0510152025

x

f(x)=5+x2

Sum= 41.2892561983471

To create the shaded boxes we use a for loop, (i in 1:n), with the code to be executed for each i beginning with a { and ending with a }. For each value of i from 1 to n, we will create a shaded box using thepolygonfunction.

Thepolygon function needs a set of xand y values to define the corners of the box, which we define to be mid.box.x and mid.box.y. Each of these vari- ables has four points to define the four x and four y values for the corners of each box. Recall that mid is the set of midpoint values for each box, so mid[i] is the x-value of the middle of box i. Hence, subtracting and adding dx/2 yields the left and right x-values of the box. For mid.box.y two of the y−values are 0 and the other two are obtained by evaluating the function at mid[i]. In the end, our four points for the ith polygon are (mid[i]-dx/2,0), (mid[i]-dx/2,f(mid[i]),(mid[i]+dx/2,f(mid[i]), and (mid[i]+dx/2,0). The poly- gonfunction has the list ofx-values as mid.box.x andy-values as mid.box.y.

The boxes are shaded with gray85 and given a black border.

After the for loop is completed we redraw the curve using lty=3, which

is a dotted line. R creates the graph by drawing a new layer on top of the previous layer. The result here is the boxes cover up parts of the curve. By redrawing the curve with dotted lines we have dotted lines for the curve when the original drawing is covered by a box. We next add points to represent the midpoint of each box with thepointsfunction, which needs a list ofx-values andy-values. We already have ourx-values of midpoints defined by mid and the correspondingy-values are 0, which is accomplished by 0*mid. Note that we can’t just use one 0 for oury-values since the number ofy-values must be the same as the number ofx-values. The syntax 0*mid, multiplies each value of mid by 0.

Our last two lines of code places the value of the sum of the boxes in the top left of the graph. We usepar(“usr”)for the values of the bottom left and top right corners of the graph frame. The first and third values define the point of the lower left corner, (p[1],p[3])=(0,0), and the second and fourth values define the point for the top right corner, (p[2],p[4])=(4,25). In this case, we know these values but if we use a different function and/or interval then we do not have to change this. We usetextto place text on the graph. The first two values are the location of the text where p[1] and p[4] give the location for the top left of the frame. Thepastefunction concatenates the text Sum=, as it is in quotes, with the value of the sum which is calculated bysum(f(mid)*dx).

Lastly adj=c(0,1) sets the alignment of the text where 0 is left or bottom and 1 is right or top alignment. In this case, we aligned the text to the left and top of the location.

We now generalize the process of graphing a function with n midpoint boxes froma tob, by creating a function. We begin by naming our function MidBox, a function of four variables,athe left endpoint,bthe right endpoint, nthe number of boxes, andf the function. The code for the MidBox function begins with{and ends with}. As before, we define dx, the width of the boxes, and mid, the list of midpoints usingseq. The par function sets our margins, although the default margins would have worked. There are two changes to curve. We leave out ylim and go with the default range for they-axis. We also label they-axis as simply “function,” since it is an unknown function.

R Code

> MidBox=function(a,b,n,f){

+ dx=(b-a)/n

+ mid=seq(a+dx/2, b, dx) + par(mar=c(4,4,2,2))

+ curve(f,a,b,lwd=2,ylab="function") + for(i in 1:n){

+ polygon(c(mid[i]-dx/2,mid[i]-dx/2,mid[i]+dx/2, mid[i]+dx/2),c(0,f(mid[i]),f(mid[i]),0),col="gray85", border="black") }

+ curve(f,a,b,lty=3,lwd=2,add=TRUE)

Riemann Boxes 125 + p=par("usr")

+ text(p[1]+.1, p[4]-.2,paste("Sum=",sum(f(mid)*dx)) ,adj=c(0,1)) }

> h=function(x){(5+exp(x)+(2.5)^x*sin(2*pi*x))/3000}

> MidBox(5,11,20,h)

5 6 7 8 9 10 11

05101520

x

function

Sum= 18.4045706250885

We simplify the for loop by not defining mid.box.x and mid.box.y and simply imbedding those terms inside the polygon function. We redraw the curve with lty=3, dotted line, so we see the function in places where the boxes covered the original drawing. We useparandtextas before to place the sum of the boxes on the graph, but adjust the location a bit so that there is some space between the text and the frame. Note that when you type multiple lines in the editor and then send them to the console, you get the + signs. The line within MidBox without a + signs was wrapped to fit the page but is one line in the editor. Finally, we let h be our function from the introduction and run MidBox, which produces the graphical output.

Dalam dokumen BOOK College mathematics and statistics (Halaman 137-143)