Download Math 7 Elementary Linear Algebra INTRODUCTION TO MATLAB 7

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Eigenvalues and eigenvectors wikipedia , lookup

Determinant wikipedia , lookup

Matrix (mathematics) wikipedia , lookup

System of linear equations wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Covariance and contravariance of vectors wikipedia , lookup

Perron–Frobenius theorem wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Orthogonal matrix wikipedia , lookup

Singular-value decomposition wikipedia , lookup

Rotation matrix wikipedia , lookup

Matrix calculus wikipedia , lookup

Matrix multiplication wikipedia , lookup

Four-vector wikipedia , lookup

Gaussian elimination wikipedia , lookup

Transcript
Math 7 Elementary Linear Algebra
INTRODUCTION TO MATLAB
7. PLOTS and ROTATION OF PLOTS
PLOTTING LINE SEGMENTS
Eample 1: Suppose you wish to use MatLab™ to plot a line segment connecting two points in the xyplane.
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 P1  0, 0  to the point P2  3,1 observe that the x values will
change from x  0 to x  3 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 3]
>> y  [0 1]
>> plot  x, y 
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  ,  3,1 , and  2,3 .
One way to do this is by constructing the plotting vectors for each line segment and then plotting them
all together:
>> x1  [0 3]
>> x 2  [3 2]
>> x3  [2 0]
>> y1  [0 1]
>> y 2  [1 3]
>> y3  [3 0]
>> plot  x1, y1, x 2, y 2, x3, y3
In the plot command above the pairing x1, y1 constructs the line segment from  0, 0  to  3,1 ; the
pairing x 2, y 2 constructs the line segment from  3,1 to  2,3 ; and the pairing x3, y3 constructs the
line segment from  2,3 to  0, 0  .
A more efficient way to plot the triangle, however, is to store the coordinate date in a pair of row
vectors.
MathLab 7 – Plots and Rotation of Plots
4
If we think of
moving along the
sides of the triangle
from the point  0, 0 
to the point  3,1 ,
from there to the
point  2,3 ,
3
4
(2,3)
3
2
2
(3,1)
1
(3,1)
1
1
2
3
4
1
4
and then returning to
the origin,
2
3
(2,3)
3
2
(3,1)
1
(0,0)
1
2
3
4
we see that the x values change from 0 to 3 to 2 to 0. We store that information in a row vector:
>> x  [0 3 2 0]
We can store the information about the y values in the same way:
>> y  [0 1 3 0]
then plot to draw the triangle.
>> plot  x, y 
ROTATION OF PLOTS
 cos   sin  
The matrix P  
 can be used to rotate a figure in the plane through the angle θ. If the
 sin  cos  
ordered pair  x, y  gives the coordinates of a point in the xy-plane, then the multiplication
 x  cos   sin    x 
P   
   will produce the coordinates of the rotated point.
 y   sin  cos    y 
Example 3: To see how this works, we will rotate the triangle plotted above through the angle
T. Henson

6
.
Math 7
4
MathLab 7 – Plots and Rotation of Plots
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,:) (refer to Introduction to Matlab 4; the notation (1,:) selects all entries of row 1 in matrix R)
>> yr  R(2,:) (this selects all entries of row 2 in matrix R)
Now, we will plot the original triangle and the rotated triangle (in red). To plot two separate figures
together, use the “hold” command.
>> plot  x, y 
>> hold
>> 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
3
2
1
0
-1
-1
0
1
2
3
4
5
The line segments that make up the figure are defined by the points
T. Henson
Math 7
MathLab 7 – Plots and Rotation of Plots
 0, 0  ,  2, 0  ,  3, 0  ,  3, 2  ,  2, 2  ,  4, 0  ,  4,3 ,  2,5 , and  0,3
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,3,3, 2, 2,3, 4, 4, 2, 0, 0]
>> y  [0, 0, 0, 2, 2, 0, 0, 0,3,5,3, 0]
>> grid
>> hold
>> plot  x, y 
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
axis([xmin xmax ymin ymax])
Enter the command
>> axis([ -1 5 -1 6 ])
to obtain the view shown above.
2
. Using the method shown in Example 3,
3
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.
Example 5: Now, let’s rotate the house through the angle
>> P = [cos(2*pi/3) -sin(2*pi/3); sin(2*pi/3) cos(2*pi/3)]; (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)
>> grid (turns on the grid)
>> hold (holds the grid plot)
>> plot(x,y,xr,yr,'r') (plots the original house and the rotated house)
>> axis([-6.5 5 -3 6]) (enlarges the viewing window)
Example 6: We are not limited to plotting and rotating figures defined by line segments. In this
example, we will plot and then rotate an ellipse.
Recall the general form of the equation of an ellipse centered at  h, k  is given by the equation
 x  h
a2
T. Henson
2
 y k

b2
2
1
Math 7
MathLab 7 – Plots and Rotation of Plots
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 2   sin 2   1 .
x
x2
 y 2  1 , we let  cos t and y  sin t . Then x  3cos t , y  sin t , 0  t  2 is
3
9
a parameterization of the curve. Enter the following lines to graph the ellipse. Be sure you suppress
output by ending input with a semicolon.
To graph the ellipse
>> t = [0 : 0.001 : 2*pi];
>> x = 3*cos(t) ; y = sin(t);
>> plot(x,y)
>> axis([-3.5 3.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,:);
>> grid
>> hold
>> plot(x,y,xr,yr,’r’)
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(3*t);
>> plot(x,y)

:
2
>> P = [ cos(pi/2) -sin(pi/2) ; sin(pi/2) cos(pi/2) ];
>> R = P*[x;y] ;
>> xr = R(1,:); yr = R(2,:);
>> grid
>> hold
>> plot(x,y,xr,yr,’r’)
Now the rotation of the curve through the angle
T. Henson
Math 7