chapter 4 review: manipulating matrices introduction to matlab 7 engineering 161

11
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

Upload: derrick-booker

Post on 03-Jan-2016

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

Chapter 4 Review: Manipulating Matrices

Introduction to MATLAB 7Engineering 161

Page 2: 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.

Page 3: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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.

Page 4: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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;

Page 5: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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??

Page 6: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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.

Page 7: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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

Page 8: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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

Problems with Two Variable V

Page 9: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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 I

Page 10: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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.

Page 11: Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161

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.