• Tidak ada hasil yang ditemukan

Area Between Two Curves

Dalam dokumen BOOK College mathematics and statistics (Halaman 145-149)

A classic calculus problem is finding the area bounded by two curves. In this example, we find the area bounded by the curvesf(x) = (10x2−1)ex and g(x) = x3 and create a graph with the bounded area shaded and text displaying the area.

We begin by loading the rootSolve package, since we will need to find the intersection of the functions, and defining the functions f and g. We then define the functionhas the difference offandg. We useuniroot.all, from the rootSolve package, to find the roots of h, the intersection of the functions f andg. The second argument inuniroot.all, c(−1,2), sets the bounds so that

Area Between Two Curves 129 all roots will be found between−1 and 2. This is required as bounds must be entered. In this case, previous exploration of the functions allowed us to set fairly tight bounds on the search for roots. The variable roots is our list of three intersection points.

R Code

> library(rootSolve)

> f=function(x){(10*x^2-1)*exp(-x)}

> g=function(x){x^3}

> h=function(x){f(x)-g(x)}

> roots=uniroot.all(h,c(-1,2))

> roots

[1] -0.3126725 0.3235736 1.7238132

We calculate our areas by integrating h from the first root, roots[1], to the second root, roots[2]. Note that you cannot integrate f-g as inte- grate must have one function of one variable, not a combination of func- tions, although it could be done in one line as integrate(function(x)f(x)- g(x),1,2). In order to calculate the total area we use $value for the value from integrate, omitting the absolute error part, and subtracting the first area since the integral was negative. We could have also used abs(integrate(h,roots[1],roots[2])$value)to get the absolute value of the re- sulting integral orintegrate(function(x)abs(h(x)), roots[1],roots[3])for the total area in one line.

R Code

> integrate(h,roots[1],roots[2])

> integrate(h,roots[2],roots[3])

> integrate(h,roots[2],roots[3])$value- integrate(h,roots[1],roots[2])$value

> integrate(h,roots[1],roots[2])

-0.4260403 with absolute error < 4.7e-15

> integrate(h,roots[2],roots[3]) 2.14281 with absolute error < 2.4e-14

> integrate(h,roots[2],roots[3])$value- integrate(h,roots[1],roots[2])$value [1] 2.56885

We now turn to creating a graph to illustrate our results. Our first line sets our margins and then we plot the function f. The x-axis is set with xlim=c(−0.5,2) and we let the y-axis be set by default. The line width, lwd, is set to 2 and thexand y labels are set blank by using empty quotes. The functiong is added to the graph withcurve using the same x-axis and lwd,

but coloring the curve red. We have add=TRUE to add this to the previous plot; otherwise it will create a new plot. A legend is added to the top left of the graph with four parts defining the elements oflegend. The first part provides the two function names using expression for mathematical expressions. The last three are the line types with 1 being a solid line, the line width used, and the color. In each casec(,)is used because there are two elements in the legend, one for each function.

R Code

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

> plot(f,xlim=c(-.5,2),lwd=2,xlab="",ylab="")

> curve(g,-.5,3,lwd=2,col="red",add=TRUE)

> legend("topleft",c(expression(f(x)==(10*x^2-1)*e^-x), expression(g(x)==x^3)),lty=c(1,1),lwd=c(2,2),

col=c("black","red"))

> xlist1=seq(roots[1],roots[2],0.01)

> xlist2=seq(roots[2],roots[3],0.01)

> polygon(c(xlist1,rev(xlist1)),c(g(xlist1), f(rev(xlist1))),col="gray65" )

> polygon(c(xlist2,rev(xlist2)),c(f(xlist2), g(rev(xlist2))),col="gray85")

> curve(g,-.5,2,lwd=3,col="red",add=TRUE)

> curve(f,-.5,2,lwd=3,col="black",add=TRUE)

> text(0,-0.25,paste("Area=",

-round(integrate(h,roots[1],roots[2])$value,2)))

> text(1,2,paste("Area=",

round(integrate(h,roots[2],roots[3])$value,2)))

Area Between Two Curves 131

−0.5 0.0 0.5 1.0 1.5 2.0

−1012345 f(x)=(10x21)e−x

g(x)=x3

Area= 0.43

Area= 2.14

We will use polygon to shade the enclosed area. To do this we need to define the shape of each area with points. We define xlist1 to be a sequence of points from the first root, roots[1], to the second root, roots[2], in steps of 0.01.

Similarly, xlist2 defines a list of points from the second to the third root. The input topolygonis thex−values and then they−values of the points that will start at the first intersection point of the function, follow along the functiong to the second intersection point, and then back to the first intersection point by way of f. We do this by combing xlist1 with its reverse, rev(xlist1), for ourx−values. Oury−values combine the points of g(xlist1), evaluating each value in xlist1 withg, withf(rev(xlist1)), evaluating each value of xlist1 withf but in reverse order. We color the polygon with gray65. We could have defined a border color with border=“some color”, but it is not necessary here. The second use of polygon shades the second intersected area in the same way, but we shade with a lighter gray, gray85.

Next, we redraw both the g and f curves. The uses of polygon leaves some

jagged edges along the curves. The commands are executed and layered in the order they are listed. The redrawing in of the two curves creates a much better looking graph. Our last two commands,text, places text on the graph.

The first two values are the location of the text. Thepastefunction is used to combine text, Area=, with values whereroundrounds the value of integrate to two decimal places. Note that$vlauewas appended tointegrateto get the value without the error term.

Dalam dokumen BOOK College mathematics and statistics (Halaman 145-149)