Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161 Defining Matrices I In MATLAB, a matrix can be defined by typing a list of numbers enclosed by square brackets. A matrix can also be defined by listing each row on a separate line. When you get more comfortable with MATLAB you find other short hand ways to enter matrices, row or column vectors. Use … to extend a line if needed to enter large matrices or long row vectors. Defining Matrices II Suppose s = [1.5, 2.2, 1.7, -4.1], then s(3) = 1.7, and so on. You can use the word end to identify the final element in a row or column, so s(end) = -4.1 A = [ ] is the empty matrix with length equal to 0 For 2 dimensional matrices you can identify an element by either specifying both the row and column or using a single index number where the elements are listed by column. Problems with Two Variables I So far we have only considered problems of the form y = f(x), that is, problems of a single variable. Often in engineering we have to deal with problems of two variables, where z = g(x,y). MATLAB has a built in function called meshgrid to help accomplish this. Consider the following example; Problems with Two Variables II x = 0:5; y = 1:1:3; And we want to compute, for example, z = x2 +xy + y2 If we write the MATLAB statement z = x.^2 + x.*y + y.^2 We will get an error message. Why?? Problems with Two Variables III Now consider; x = 0:5; y = 1:1:3; [X,Y] = meshgrid(x,y); z = X.^2 + X.*Y + Y.^2 The new matrices X and Y created by the meshgrid function are now of the same size and the expression for z makes sense. Problems with Two Variable IV The new matrices look like; X=0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 z = X.^2 + X.*Y + Y.^2 Y= 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 Problems with Two Variable V When we perform the computation as given in the previous slides, all points will be calculated using one MATLAB statement. Let’s look at another example; Suppose we want to calculate d= 1/2gt2 for both the moon and earth for 0< t < 10 seconds. Here g and t are vectors, g = [2.4, 9.8] and for t = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. Consider the following MATLAB statements; t = 0:10; g = [ 2.4, 9.8]; [T, G] = meshgrid(t,g); d = 0.5*G.*T.^2; results = [t’, d’] d would be an 2x11 matrix, results for both the earth and moon Special Matrices I MATLAB contains a number of built in functions that generate special matrices. You’ll find these matrices very useful as you advance with you MATLAB skills. Matrix of zeros: A = zeros(3) or B = zeros(2,4) Matrix of ones: A = ones(2) or B = ones(2,5) Magic matrices: C = magic(4) creates a matrix called a Magic Square that has special properties. Special Matrices II Diagonal matrices: Use the function diag(A) in one of two ways; If A is mxn matrix, B = diag(A) returns B as a column vector of the diagonal elements of A. B = diag(A, 1) would produce the elements of the diagonal above the main diagonal, diag(A, -1), below the main diagonal. If A is a row matrix, B = diag(A) produces a matrix with the elements of A form the diagonal of B, with zeros everywhere else. Chapter 4 Assignments Chapter 4 assignments let you practice working with problems with two variables and matrices. They are, 4.1, 4.8, 4.12.