Ex2Data - Mat: 221 Matlab Assignment 02
Ex2Data - Mat: 221 Matlab Assignment 02
In this assignment we will consider a little bit of manipulating matrices, and we will also use the computer to
solve questions about span and linear independence.
This assignment comes with a file Ex2Data.mat. Import this file as you did in Exercise 1—refer to Exercise 1 if
you can’t remember how.
More chopping up, putting together. This subsection describes some more decomposing and recombining ideas.
You can extract sub-matrices from a matrix. For example, try
>> C(1:4, 2:5)
Figure out for yourself which elements of C appear in this matrix.
An operation, which we have not encountered yet in class, but is sometimes useful in this context is transpose.
In MATLAB, it is given by the apostrophe symbol ’. It simply swaps the rows with the columns. For instance, try
1
2 DUE 4 OCTOBER 2018
>> v1’
and if you like
>> C’
You can also combine more than two things together. For instance try
>> [v1, 2*v1, -v1, ones(12,2)]
You may recall that ones(12,2) gives you a 12 × 2 matrix, entirely filled with 1s.
Exercise 1.
(a) There are 5 vectors in the data file, w1, w2, w3, w4, w5. One of them, w4, was wrongly saved as a row vector,
the others are column vectors. Replace w4 by the column vector having the same entries in the same order.
Then make the matrix having columns w1 w2 w3 w4 w5. Save the answer as OneAmat.
(b) Do the vectors w1, w2, w3, w4, w5 form a linearly independent set (once you’ve made w4 into a column
vector of course)? Store either “yes” or “no” in OneBword.
(c) There is a matrix D in the data file, and a vector y1. Calculate the size of D and y1. Is y1 in the span of the
columns of D? Store either “yes” or “no” in OneCword
(d) There is another vector y2 in the data file. Make the augmented matrix representing the system Dx = y2 —do
not row-reduce this matrix yet. Save the matrix as OneDmat.
(e) Is y2 in the span of the columns of D? Store either “yes” or “no” in OneEword.
R ANDOM NUMBERS
You can use the function
>> rand()
to produce a random1 number between 0 and 1. Notice that rand() has parentheses, to indicate that it is indeed
a function, but those parentheses, (), are empty. Putting a natural number into this, as in
>> rand(4)
generates a 4 × 4 matrix with random entries, between 0 and 1. To make a nonsquare matrix, you can write
>> rand(4,7)
etc.
If you want a matrix filled with random entries that are bigger or smaller than this, you can scale the answer by
multiplying. For instance
>> 0.01 * rand(6)
will produce a 6 × 6 matrix filled with random numbers between 0 and 0.01. To test yourself, try to figure out what
sort of matrix the command
>> 4 * rand(10) - 2 * ones(10)
will produce, then try it out.
1because these numbers are produced by a computer, they are not truly random, merely pseudorandom. We will not worry about this in
this course.
221 MATLAB ASSIGNMENT 02 3
What does a “typical” matrix look like? The purpose of this section is to investigate what a “typical” matrix looks
like after you reduce to row echelon form. To test this out, let’s fix a size of matrix, say 8×6, and let’s make a random
matrix
>> Mrand=rand(8,6)
now take the reduced row echelon form
>> rref(Mrand)
Almost certainly you will see that this matrix has a pivot position in every column. If, somehow, you did not see
this, you have encountered an extremely improbable event. If you try the calculation a second time with a new
random matrix, you should see a pivot in every column.
Now try with a different size of matrix
>> Nrand = rand(7, 11)
and
>> rref(Nrand)
Again, you will see a pivot position in every column, until you run out of columns.
If you prefer to run this experiment with matrices of integers, try
>> Orand = randi(20, 9, 9)
and
>> rref(Orand)
You can repeat this experiment as often as you like, changing the numbers, to see what answers you get.
Exercise 2.
(a) Some vectors are given in the data file: z1, z2, z3, z4, z5. These vectors are not linearly independent,
as you can see as follows: make the matrix having these vectors for columns, in the given order. Save it as
TwoAmat.
(b) Calculate the reduced row echelon form of TwoAmat, storing it as TwoBmat.
(c) Add a small amount of random “noise” or “error”, no more than 0.1, to each of the vectors z1, z2, z3, z4,
z5 and store the result in a single matrix TwoCmat—Hint: you can just do
>> TwoCmat = TwoAmat + 0.1*rand(n,m)
for the appropriate values of n and m.
(d) Finally, determine whether the columns of TwoCmat are linearly independent. Store either “yes” or “no” in
TwoDword.
4 DUE 4 OCTOBER 2018
H ANDING IN
It is now time to turn in your second MATLAB assignment for this course. Again you will be handing in your
assignment as a .mat file—that is a matlab file containing all your variables.
Here’s how:
(1) Check that you have a value stored for each of the questions you were asked this week. Your Workspace
should contain: OneAmat, OneBword, OneCword, OneDmat, OneEword,TwoAmat, TwoBmat, TwoCmat, TwoDword.
Please be aware that these names are case-sensitive. If you submit oneamat, for instance, there is a good
chance we will mark your assignment as wrong. It is ok if your Workspace contains other variables as well,
we will ignore them.
(2) Save all your variables as a .mat file—do not make a .m file by mistake. There are several ways of doing
this, depending on how you are running MATLAB.
• If you’re using matlab.mathworks.com, you should save your variables using the command
>> save Math221Assignment01
This will save the variables in a file called “Math221Assignment1.mat” in mathworks’ cloud storage.
Then you will have to download it to the computer by selecting the file and using the download button:
• If you are running MATLAB on a computer locally, this is done by clicking the “Save workspace” button
near the top of the screen.
(3) Upload your .mat file to Canvas. This is done by going to the Math 221 Canvas site, going to the Assign-
ments page, and following the instructions there.