• Tidak ada hasil yang ditemukan

Interactive: Rotations around the x-axis

Dalam dokumen BOOK College mathematics and statistics (Halaman 158-164)

Interactive: Rotations around the x-axis 141

are labelled with xlab, ylab, and zlab. We add axis lines with theabclines3d function. The first three values are a point and the fourth is a set of vectors defining the direction to add lines. Herediag(3)is the identity matrix, which provides directions for each axis. To generate the curve to be rotated we need a set of xandy points. The values for xare returned from seq: fifty points from 0 to 4. We define y by squaring and dividing by 4 every value in x. The turn3dfunction rotates the values around the x-axis, where n is the number of polygons used to approximate the rotation and smooth set is to TRUE. To display the image we usewire3d and color the object blue. We can also use shade3d, which will create solid walls.

R Code

> persp3d(c(98,99),c(98,99),diag(2),xlim=c(0,5), ylim=c(-5,5),zlim=c(-5,5),aspect=c(3,1,1), xlab="x axis",ylab="y axis",zlab="z axis")

> abclines3d(0,0,0,diag(3),col="gray40",lwd=2)

> x=seq(0,4,length=50)

> y=x^2/4

> wire3d(turn3d(x,y,n=60,smooth=TRUE),col="blue")

We now add four cylinders based on left-hand endpoints of unit width to the graph. To do this, each cylinder will need four points to define the region to be rotated. We begin by defining f to be x2/4. This was not done in the

Interactive: Rotations around the x-axis 143 first part as it was unnecessary, but here it will help clarify the code. Since these are “left cylinders” we can start with the one from 1 to 2. We define the four points 1,1,2, and 2, and y1 to be 0, f(1), f(1), and 0, which define the corners of the box to be rotated. We useturnd3dfor the rotation and display it withshade3d, coloring the cylinder red. The other two cylinders are created in the same manner. Note thatpoints3d, lines3d, and segments3d exist to add points, lines, and line segments to plots.

R Code

> f=function(x){x^2/4}

> x1=c(1,1,2,2)

> y1=c(0,f(1),f(1),0)

> shade3d(turn3d(x1,y1,n=60),col="red")

> x2=c(2,2,3,3)

> y2=c(0,f(2),f(2),0)

> shade3d(turn3d(x2,y2,n=60),col="red")

> x3=c(3,3,4,4)

> y3=c(0,f(3),f(3),0)

> shade3d(turn3d(x3,y3,n=60),col="red")

We now move to rotating the area bounded by two curves around the x-axis. We consider the two functions f(x) = x3 −7x2 + 12x+ 10 and g(x) = (x−3)4+ 5, defined in the first two lines. The third and fourth

lines graphf from 0 to 6 withy ranging from 0 to 40 and addsg to the plot with add=TRUE. We need to find the intersection points of the two function, so we load the rootSolve package. The function his defined as the difference of fandg, and roots is set to the roots of hon the interval from 0 to 10 with the functionuniroot.all. We now create a set of points to define the bound- ary of the intersected region. The variable x.int is a sequences of 50 points of x-values from the first root to the second root. The variables y.f and y.g are the correspondingy-values. The points alongf andgare plotted withpoints and colored red and blue, respectively. In order to shade a region we need a set of points that follow the region clockwise or counterclockwise. We define x.rot to be the set of points of x.int and the same set in reverse. Similarly y.rot is the y-values of y.f followed by the values along g in reverse,rev(y.g). The functionpolygon shades the region defined by the values ofxandy given in the first two arguments and colored gray. This provides a nice visual of the region that will be rotated.

R Code

> f=function(x){x^3-7*x^2+12*x+10}

> g=function(x){(x-3)^4+5}

> curve(f,0,6,ylim=c(0,40))

> curve(g,0,6,add=TRUE)

> library(rootSolve})

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

> roots=uniroot.all(h,c(0,10))

> x.int=seq(roots[1],roots[2],length=50)

> y.f=f(x.int)

> y.g=g(x.int)

> points(x.int,y.f,col="red")

> points(x.int,y.g,col="blue")

> x.rot=c(x.int,rev(x.int))

> y.rot=c(y.f,rev(y.g))

> polygon(x.rot,y.rot,col="gray")

Interactive: Rotations around the x-axis 145

0 1 2 3 4 5 6

010203040

x

f(x)

Our goal is to rotate the shaded region around thex-axis. This visual will proceed in four parts. We will graph a “dummy” function because persp3d creates a nice set of axes; then we rotatefandgseparately. The last part will rotate the shaded region as defined by x.rot and y.rot. We start by setting the aspect to 15, 1, and 1 forx, y, andz, respectively withapsect3d. The next four lines create the “dummy” function. We set x1 and y1 both to be the two points 0 and 1. We define the constant function as 0x+ 0y−99 and useouter to create the matrix z1, which will be a two-by-two matrix with the values -99. We plot x1,y1,z1 withpersp3d, set the ranges for the axes, include the axes with axes=TRUE, and set aspect to FALSE since it has already been set. We add axis lines with the abclines3d function. The first three values are the coordinates of a point and the fourth is a set of vectors defining the directions to add lines. At this point, we have a box with labelled axes and lines representing the center of the axes.

To addfto the graph we let x2 be a sequence of 50 points from from 0 to 6.

Set y2 to the function values of x2, orf(x2). The functionturn3drotates the points andwire3dgraphs the object as a wire mesh and colored blue. Simi- larly, we define y3 to beg(x2)and graph the rotated object as a wire mesh, but colored green. Our last line rotates the points defined by x.rot and y.rot and this time we display the object with shade3d colored red. The graph here was rotated and zoomed before saving with rgl.snapshot(“filename.png”, fmt=“png”,top=TRUE).

R Code

> aspect3d(15,1,1)

> x1=y1=c(0,1)

> constant=function(x,y){0*x+0*y-99}

> z1=outer(x1,y1,constant)

> persp3d(x1,y1,z1,xlim=c(0,6),ylim=c(-20,20), zlim=c(-20,20),axes=TRUE,aspect=FALSE)

> abclines3d(0,0,0,a=diag(3),col="gray40",lwd=2)

> x2=seq(0,6,length=50)

> y2=f(x2)

> wire3d(turn3d(x2,y2,n=60,smooth=TRUE),col="blue")

> y3=g(x2)

> wire3d(turn3d(x2,y3,n=60,smooth=TRUE),col="green")

> shade3d(turn3d(x.rot,y.rot,n=60,smooth=TRUE), col="red")

Interactive: Geometric Solids 147

Dalam dokumen BOOK College mathematics and statistics (Halaman 158-164)