Read worksheet6.pdf text version

Worksheet 6: Vectors and Matrices

Patrick Young October 9, 2006

Contents

1 Introduction 2 Vectors 3 Matrices 4 Three Useful Commands 5 Solving a Linear System 6 Printing In and Exiting From M-Files 7 Homework 1 2 3 3 4 5 5

1

Introduction

So far we have learned how to use MatLab to plot functions, solve differential equations, and how to use if statements and for loops. If you don't remember any of this, look back at the old worksheets posted online. Today, we learn how to deal with vectors and matrices in MatLab, as well as some built in tools that can make linear algebra much easier and faster.

1

2

Vectors

We have been using vectors this semester in MatLab for pretty much everything, but there is more to them than I've told you. Before, we were treating them as just a list of numbers. Now we'll learn the details. There are two types of vectors, a column vector and a row vector. The first is organized as a column and the second as a row. To input a column vector "col", type the following col = [1;2;3;4] We get a column of dimension 4 × 1 as an output. Note the semi-colon separating each entry. To input a row vector "row", type row = [1,2,3,4] This gives a row vector of dimension 1 × 4. Note that we used commas as our separator. You can also use a blank space for a row vector. If you want to display row as a column vector, just type row' You can change col into a row vector in the same manner. Say we'd like to get the dot product between "row" and "col". This can only be done if the inner matrix dimensions agree. In this case, "row" dotted with "col" has an inner matrix dimension of 4 × 4 so we can do it. The outer matrix dimensions give the size of the resulting matrix, in this case it is 1 × 1, so its the scalar dot product we seek. Just type row*col and you have it. What if we reverse the order of the multiplication? Well, the inner matrix dimension is now 1 × 1 so we can do it, but the outer dimensions are 4 × 4, which definately isn't a dot product. Try it! Do you see how it did the matrix multiplication? If you don't want to worry about the dimensions and you just want a dot product, type the following command 2

dot(row,col) The order you give it the vectors and their orientation does not matter. There is a similar command called "cross" for taking the cross product of vectors with a length of 3. Remember when I said you need to put a "." in front of the operators you use? Type row*col row.*col' Do you see the difference? I needed to put the apostrophe after col to orient it in the same direction of row, then it did the multiplication element wise. Try it with "/". Problems can occur if you do not understand when you need a period and when you don't.

3

Matrices

Matrices work just like vectors as far as operations are concerned. If you get a "Inner Matrix Dimensions Do Not Agree." error, then you know what the problem is! You can transpose a matrix by putting an apostrophe after it, just like how we switched a row vector to a column vector. Let's input two matrices, the ones from page 117 in your book. Type A=[1,-1,3;0,4,2] B=[3,1;2,-4;-1,0] You enter them a row at a time, each row separated by a semi-colon. Try finding AB and BA. What happens?

4

Three Useful Commands

The first command that you'll find useful is how to put a matrix in reduced row echelon form. Try the following examples:

3

rref(A) rref(B) rref(B*A) The second is for taking determinants. Remember you can only take the determinant of a square matrix! Remember that AB and BA are square, so try det(A*B) det(B*A) Finially, we can find the inverse of a square matrix that is invertible. Try inv(A*B) inv(B*A) Can you guess why we got the error on the second inversion? Look at the determinant! Also try A*B*inv(A*B) B*A*inv(B*A) We see that it definitely does not give the correct inverse for BA.

5

Solving a Linear System

Let's solve the system x+z =2 2x - 3y + 5z = 4 3x + 2y - z = 4 The first step is to input the matrix A and vector b. Type A=[1 0 1;2 -3 5;3 2 -1] b=[2;4;4] Now check to see if A has an inverse by seeing if the determinant is zero. Type 4

det(A) We're ok. We want to augment A with b. To see how large A is, type size(A) so it's 3 × 3, meaning we want to put b in the fourth column. To do this, type A(:,4)=b Now that A has been augmented with b, we just type rref(A) The fourth column is our solution x!

6

Printing In and Exiting From M-Files

Say we had an M-File that checked if a matrix was singular. If the matrix is singular, we could print the phrase "The matrix is singular." to the command line by using the command disp(['The matrix is singular.']) If we want to exit an M-File before it has run all the commands contained in it, just type "return" in the M-File at the point where you want to exit.

7

Homework

The heading of your email should be "lastname HW6 recitationtime" if you don't finish in class and attach the M-File to your email. CC it to yourself to make sure I receive it! Create a function M-File called "linear solver.m" that that solves the system Ax = b

5

It should take as inputs a matrix A and a vector b, and the output should be a vector x that contains the solution. I want this program to check and see if the matrix A is invertible. If not, then it should print out an error message saying the matrix is not invertible and then exit the M-File. If it is invertible, you can use the "rref" command to solve the system and find x. Hint: You'll need to use if-statements to check if it is invertible or not! In your code, use the "%" symbol to make comments about what each step in the m-file does, so I can understand what you are doing. There are many ways to make this work! Use the following example from the lecture to see if your program works x+z =2 2x - 3y + 5z = 4 3x + 2y - z = 4 The solution should be x = 1, y = 1, and z = 1. Extra Credit: If your program can check the dimensions of A and b and see that they make sense, this will make up for a missed class or assignment. You've got to figure this part out for yourself, so don't ask me for help :)

6

Information

6 pages

Find more like this

Report File (DMCA)

Our content is added by our users. We aim to remove reported files within 1 working day. Please use this link to notify us:

Report this file as copyright or inappropriate

349700


You might also be interested in

BETA
"Shader Metaprogramming"
D:\Docs\Talks\ox\2001ESTE\este.dvi
Portfolio Optimization with R/Rmetrics