Spring 2007 PLOTTING LINE SEGMENTS Math 7 Elementary Linear Algebra PLOTS and ROTATIONS Example 1: Suppose you wish to use MatLab to plot a line segment connecting two points in the xy-plane. Recall that the basic plot command requires two inputs: a vector of x values and a vector of y values. To plot the line segment connecting the point P 1 ( 0,0 ) to the point P 2 (,1) observe that the x values will change from x = 0 to x = and the y values will change from y = 0 to y = 1. To plot the line segment, store the x and y values in row vectors and then plot: >> x = [0 ] >> y = [0 1] Let s plot something slightly more interesting. Example 2: We re going to construct a triangular figure by plotting the line segments that connect the three points ( 0,0 ), (,1 ), and ( 2, ). One way to do this is by constructing the plotting vectors for each line segment and then plotting them all together: >> x 1 = [0 ] >> x 2 = [ 2] >> x = [2 0] >> y 1 = [0 1] >> y 2 = [1 ] >> y = [ 0] >> plot( x1, y1, x2, y2, x, y ) In the plot command above the pairing x1, y 1 constructs the line segment from ( 0, 0 ) to (,1 ); the pairing 2, 2 and the pairing, x y constructs the line segment from ( ) ( ) x y constructs the line segment from ( 2, ) to ( 0,0 ).,1 to 2, ; A more efficient way to plot the triangle, however, is to store the coordinate date in a pair of row vectors. If we think of moving along the sides of the triangle from 2,, and then returning the point ( 0,0 ) to the point (,1 ), from there to the point ( )
Plots and Rotations page 2 to the origin, we see that the x values change from 0 to to 2 to 0. We store that information in a row vector: >> x = [0 2 0] We can store the information about the y values in the same way: >> y = [0 1 0] then plot ROTATION OF PLOTS the angle θ. If the ordered pair ( ) cosθ sinθ The matrix P = sinθ cosθ can be used to rotate a figure in the plane through x, y gives the coordinates of a point in the xy- x cosθ sinθ x plane, then the multiplication P y = sinθ cosθ y will produce the coordinates of the rotated point. Example : To see how this works, we will rotate the triangle plotted above π through the angle. 6 First define the rotation matrix P: >> P= [cos( pi/ 6) sin( pi/ 6) ; sin( pi/ 6) cos( pi/ 6)] Next, multiply P times a matrix whose rows are the x and y row vectors entered previously. >> R = P*[ x; y] Row 1 of the resulting matrix R contains the x -coordinates of the rotated points, while row 2 contains the y -coordinates of the rotated points. To plot the rotated figure, we need to define rotated x and y row vectors. We can use a MatLab command to do this: >> xr = R(1,:) >> yr = R(2,:)
Plots and Rotations page Now, we will plot the original triangle and the rotated triangle (in red): >> plot( xr, yr,' r ') Example 4: In this example we will construct a figure and then rotate it. Here is a plot of the figure 6 5 4 2 1 0-1 -1 0 1 2 4 5 The line segments that make up the figure are defined by the points ( 0,0 ), ( 2,0 ), (,0 ), (, 2 ), ( 2, 2 ), ( 4,0 ), ( 4, ), ( 2,5 ), and ( 0, ) To draw the house, construct line segments by starting at the origin and moving counterclockwise around the frame of the house, with a side-trip around the doorway. Enter the following x and y row vectors and plot command to draw the house. >> x = [0,2,,,2,2,,4,4,2,0,0] >> y = [0,0,0,2,2,0,0,0,,5,,0]
Plots and Rotations page 4 You will notice that MatLab draws the graph so that it fills the plot frame. To change the size of the viewing window, use the axis command. The syntax for this command is Enter the command >> axis([ -1 5-1 6 ]) to obtain the view shown above. axis([xmin xmax ymin ymax]) Example 5: Now, let s rotate the house through the angle 2 π. Using the method shown in Example, define a rotation matrix P and create a matrix of rotated points by multiplying P times a matrix whose rows are the row vectors used to graph the house. From this matrix, extrapolate the rotated x and y row vectors and plot them. >> P = [cos(2*pi/) -sin(2*pi/); sin(2*pi/) cos(2*pi/)] (This defines the rotation matrix) >> R = P*[x;y]; (This defines the matrix of rotated points) >> xr = R(1,:); yr = R(2,:); (This defines the row vectors for plotting) (turns on the grid) (holds the grid plot) >> plot(x,y,xr,yr,'r') (plots the original house and the rotated house) >> axis([-6.5 5-6]) (enlarges the viewing window) Example 6: We are not limited to plotting and rotating figures defined by leg segments. In this example, we will plot and then rotate an ellipse. Recall the general form of the equation of an ellipse centered at ( hk, ) is given by the equation ( x h) ( y k) + = 1 a b where the major axis is determined by the relative sizes of a and b. Since the graph of an ellipse is not the graph of a function, we use a parameterization to graph the ellipse in MatLab. The parameterization makes use of the trigonometric identity cos θ sin θ 1 + =. 2 x 2 x To graph the ellipse + y = 1, we let = cost and y = sin t. Then x = cost, 9 y = sin t, 0 t 2π is a parameterization of the curve. Enter the following lines to graph the ellipse. Be sure you suppress output by ending input with a semicolon.
Plots and Rotations page 5 >> t = [0 : 0.001 : 2*pi]; >> x = *cos(t) ; y = sin(t); >> plot(x,y) >> axis([-.5.5-1.5 1.5]) >> P = [cos(pi/4) -sin(pi/4) ; sin(pi/4) cos(pi/4)] >> R = P*[x;y]; >> xr = R(1,:) ; yr = R(2,:); >> plot(x,y,xr,yr) Example 7: Any parameterized curve can be rotated by the method shown above. Enter the following lines to see an example. First, the curve: >> t = [ -2*pi : 0.01 : 2*pi ]; >> x = t + sin(2*t) ; y = t + sin(*t); >> plot(x,y) Now the rotation of the curve through the angle 2 π : >> P = [ cos(pi/2) -sin(pi/2) ; sin(pi/2) cos(pi/2) ] >> R = P*[x;y] ; >> xr = R(1,:); yr = R(2,:); >> plot(x,y,xr,yr)