Uploaded by beardr8

Math 7 Lab 4

advertisement
Math 7 Elementary Linear Algebra
Lab Exercise No. 4
Creating Matrices
How to complete this lab:
1. Open a document in Microsoft Word. At the top, put your name, today’s date, and the
title “Lab #4: Creatin Matrices”. This will be referred to as the Lab Document.
2. Work through the lab assignment, recording answers or output in your Lab Document
when indicated. Label each separate exercise. When you are done and ready to submit
your work make sure you save the document as a PDF, name your document as follows
"yourlastname_Lab4" where yourlastname should be your last name .
3. The lab is due by the begin of the next class. Make sure you submit your assignment as a
PDF file under the assignment named Lab 4 in the Canvas website.
Purpose:
In this lab you will explore some properties of matrix operations applied to diagonal matrices.
Producing Column Vectors that have Incremental Spacing:
Recall that you can create a column matrix by inputting the entries followed by semicolons.
For example we have:
X=[1; 2; 3; 4; 5; 6; 7]
Which is a 1 x 7 column matrix.
There is a simpler way.
First let us recall that the 7 x 1 row matrix will have entries 1, 2, 3, 4, 5, 6 and 7 in that order.
A=[1:1:7]
{This reads, state the row matrix A where the entries begin at 1 with each
entry having an increment of 1 until you reach 7.}
Using the transpose operation in MatLab we get:
X=A'
Try it on your own.
Exercise 1) Without typing all of the entries, produce and copy all of the inputs and outputs for
1
1
a column vector of 5 regularly spaced rational entries starting at − , with an increment of .
3
2
Copy your commands and outputs to your document.
Math 7 Elementary Linear Algebra
Lab Exercise No. 4
Creating Matrices
Producing Matrices using Partitions and Concatenation:
Suppose you have a 7 x 8 matrix
0
⎡0
⎢0
⎢
𝐴𝐴 = ⎢0
⎢1
⎢1
⎣1
0
0
0
0
1
1
1
We can view A as the following partitions,
𝐴𝐴 = �
Where
0
𝐵𝐵 = �0
0
0
0
0
0
0
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0�, 𝐶𝐶 = �1
1
0
1
0
1
1
1
1
0
0
0
0
1
1
1
𝐵𝐵
𝐷𝐷
1
1
1�, 𝐷𝐷 = �1
1
1
1
Is there an easy way to write A in MatLab?
0
0
0
0
1
1
1
𝐶𝐶
�
𝐸𝐸
1
1
1
1
2
0
0
1 1
1 1
1 1
1
1
1
1
0
5
0
1
1⎤
1⎥
⎥
1⎥
0⎥
0⎥
8⎦
2 0
1 1
�
and
𝐸𝐸
=
�
0 5
1 1
0 0
1 1
Yes,
First of all we can define a zero matrix in MatLab using the following command:
zeros(m,n)
This gives us an m x n matrix with all entries being zero.
We can then set:
B = zeros(4,5)
To define C and D we use the ones command in MatLab:
ones(m,n)
This gives us an m x n matrix with all entries being one.
So we can set:
C = ones(4,3)
and
D = ones(3,5)
0
0�
8
Math 7 Elementary Linear Algebra
Lab Exercise No. 4
Creating Matrices
Finally, we can define E using the diagonal command in MatLab:
E = diag([2, 5, 8])
or
E=diag([2:3:8])
To compute A we want to talk about concatenation:
We can concatenate two matrices the following way
Suppose we want the following 4 x 8 matrix:
0
𝑇𝑇 = � 0
0
0
Notice that
We can obtain T from MatLab by:
𝑇𝑇 = [𝐵𝐵
𝐶𝐶 ]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
𝑇𝑇 = [𝐵𝐵
𝐶𝐶 ]
1
1
1
1
1
1
1
1
1
1�
1
1
The output is;
T=
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
Which is what we want!
Notice that this worked because both B and C have 4 rows. So this type of concatenation only
works when the number of rows is the same.
Suppose you want the following 7 x 5 matrix,
It is clear to see that
0
⎡0
⎢0
⎢
𝐾𝐾 = ⎢0
⎢1
⎢1
⎣1
0
0
0
0
1
1
1
0
0
0
0
1
1
1
0
0
0
0
1
1
1
0
0⎤
0⎥
⎥
0⎥
1⎥
1⎥
1⎦
Math 7 Elementary Linear Algebra
Lab Exercise No. 4
Creating Matrices
We can obtain K from MatLab by:
𝐵𝐵
𝐾𝐾 = � �
𝐷𝐷
𝐾𝐾 = [𝐵𝐵; 𝐷𝐷]
The output is;
K=
0
0
0
0
1
1
1
0
0
0
0
1
1
1
0
0
0
0
1
1
1
0
0
0
0
1
1
1
0
0
0
0
1
1
1
Which is what we want!
Notice that this worked because both B and D have 5 columns. So this type of concatenation only
works when the number of rows is the same.
Now to obtain the matrix A we can just combine the two processes in one go.
Hence
𝐴𝐴 = [𝐵𝐵 𝐶𝐶; 𝐷𝐷 𝐸𝐸]
Which has the following output;
A=
0
0
0
0
1
1
1
0
0
0
0
1
1
1
0
0
0
0
1
1
1
0
0
0
0
1
1
1
0
0
0
0
1
1
1
1
1
1
1
2
0
0
1
1
1
1
0
5
0
1
1
1
1
0
0
8
Which is what we want!
Exercise 2) Without typing in any of the entries, produce and copy all of the inputs and
outputs for the matrix below.
Math 7 Elementary Linear Algebra
Lab Exercise No. 4
Creating Matrices
1 0 0 1 1 
0 1 0 1 1 


0 0 1 1 1 


1 1 1 0 0 
1 1 1 0 0 
Copy and paste all of your commands and outputs on your document.
Randomly Generated Matrices:
You can create a matrix of randomly chosen numbers.
The rand function will create a matrix of uniformly distributed random numbers {this means the
numbers chosen all have an equal probability}, each between 0 and 1 (0 included).
Enter the following to see what happens:
A=rand(5)
B=rand(3,5)
{this gives you a 5 x 5 matrix with random numbers between 0 and 1}
{this gives you a 3 x 5 matrix with random numbers between 0 and 1}
You can use scalar multiplication and any of several commands that round numbers to create
matrices of integers.
The next command multiplies each entry of a 5×5 matrix of random numbers by the scalar 10,
to produce a matrix where at least some entries have a non-zero entry in the ones place.
C=10*rand(5)
Now, you can produce a matrix of integers by rounding off the entries in C. There are four
different types of “round-off” functions:
a. The floor function rounds each entry to the greatest integer less than or equal to the
given number. (Example: the floor of 2.5 is 2 because 2 is the largest integer less
than or equal to 2.5; the floor of 3 is 3 because 3 is the largest integer less than or
equal to 3.) Try
Fl=floor(C)
Compare Fl to the original matrix C.
b. The ceiling function (ceil) rounds each entry to the least integer greater than or equal
to the given number. (Example: the ceiling of 2.5 is 3 because 3 is the smallest
integer greater than or equal to 2.5; the ceiling of 3 is 3 because 3 is the smallest
integer greater than or equal to 3.) Try
Math 7 Elementary Linear Algebra
Lab Exercise No. 4
Creating Matrices
Cl=ceil(C)
Compare Cl to the original matrix C.
c. The round function performs conventional arithmetical rounding. Try
R=round(C)
d. The fix function truncates a number by taking its integer part (no rounding). Try it:
F=fix(C)
You can use a nested command structure to generate a random matrix with integer entries by
combining the rand function with scalar multiplication by 10 (to move the decimal point) and
any of the round-off functions. For instance, to create a 5×5 matrix of integers, you could
enter
round(10*rand(5))
Of course, you could use floor, ceil, or fix in place of round.
Exercise 3) Produce the output of a 5×7 matrix A of randomly generated integers (your choice of
round-off method). Copy and paste your commands and outputs into your document.
Exercise 4) Produce three 3×3 randomly generated matrices of integers called A, B, and C and a
1×3 randomly generated matrix called D.
a)
Copy the commands and outputs of A, B, C and D.
a)
Copy the commands and output of A3
b)
Copy the commands and output of ( DB )
c)
Copy the commands and output of 2 A − 3 ( B + C T )
T
(
)
T
Download