Sei sulla pagina 1di 4

221 MATLAB ASSIGNMENT 03

DUE 18 OCTOBER 2018

This assignment comes with a file Ex3Data.mat. Import this file as you did in Exercise 1—refer to Exercise 1 if
you can’t remember how.

M ATRIX O PERATIONS
MATLAB has functions to handle a great many operations in the algebra of matrices. To see them in action,
define
>> A = [1 2; 3 5]
and
>> B = [1 0 ; 0 2]
We then have commands for taking the transpose
>> B’
or doing scalar multiplication of matrices
>> 2*A
or matrix multiplication
>> B*B’
We can also do matrix addition
>> A + B’*B
You can also see that for dimensional reasons
>> A*B
is not defined whereas
>> B*A
is.
Also, setting
>> C=B’*B
you can verify that
>> A*C-C*A
is not 0, i.e., that AC and CA are different matrices. You can also take powers of matrices
>> A^4
which is to say A*A*A*A. We can form the matrix inverse if the matrix in question is invertible
>> A^-1
but contrast
>> D=5*ones(2)
followed by1
>> D^-1
The notation
>> A^-3

1MATLAB will not report an error here, but instead say that as far as it can tell, the matrix is singular, which is to say, not invertible.

1
2 DUE 18 OCTOBER 2018

is short for A −1 A −1 A −1 .
You can make the n × n identity matrices I n by eye(2), eye(3), . . .

Exercise 1.
(a) There are matrices matA and matB in the data file. Calculate matA*matB, and store the answer in OneAmat.
(b) Similarly, calculate matB * matA and store the answer in OneBmat
(c) Compare matB’ * matA’ with matA*matB. Store the difference between matB’*matA’ and the transpose of
matA*matB in OneCmat.

Linsolve. The MATLAB function linsolve(A,b) is a useful tool for solving a system of linear equations easily.
Specifically, if A is a matrix and b is a vector, then the output of linsolve will be either a solution of
Ax = b
or a message saying that MATLAB encountered a problem. In this assignment, we will not explore all the ways that
this can go wrong. We will simply say that if the matrix A is invertible, then linsolve(A, b) will give the unique
solution A−1 b.
To see this in action, consider the matrix matC in the data file. Check that the columns of matC are linearly
independent by calculating
>> rref(matC)
therefore any equation matC x = b will have a unique solution. Try
>> x=linsolve(matC, [1 ; ;3 ; 0])
then you can check the answer by calculating
>> matC*x
On the other hand, you can see what happens when you try
>> linsolve([1 0; 0 0], [1; 0])
which returns an error even though the system in question has a solution.

Notation. As a reminder, the notation ~ e i denotes a vector in Rn having a 1 in the i -th position and 0 in all other
 
0
positions2. For instance, in R3 , the notation ~e 2 means 1.
0

Exercise 2.
(a) In the associated file, there are two matrices Rot and Str which are associated to linear transformations R3 →
R3 . The matrix Rot represents a rotation of π/6 around the y-axis, the matrix Str represents a stretching in the
direction of the z-axis. Give the matrix TwoAmat that represents the composite transformation given by first
doing the rotation, then the stretch, then the rotation two more times.
(b) The vectors e1, e2 and e3 are already defined in the data file, as are three other vectors v1, v2 and v3. Use
linsolve to find numbers c 1 , c 2 , c 3 such that
c1*v1 + c2*v2 + c3*v3 = e1
Store [c1 c2 c3] (as either a row vector or a column vector) in TwoBvec
(c) Give the matrix that represents the transformation T : R3 → R3 given by reflection across the plane x + y +z = 0,
storing the answer in TwoCmat.
Hint: This transformation has the property
   
1 1
T 1 = − 1 .
1 1

1
 
· ¸
2Which Rn the vector ~ 1
e i is in depends on the context. For example, both and 0 are called ~
e1.
0
0
221 MATLAB ASSIGNMENT 03 3

You should also be able to compute


   
1 1
T −1 and T  0 
0 −1
Then use the previous question to determine T (~
e 1 ), T (~
e 2 ) and T (~
e 3 ).
4 DUE 18 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, OneBmat, OneCmat,TwoAmat, TwoBvec, TwoCmat. 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 Math221Assignment3
This will save the variables in a file called “Math221Assignment3.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.

Potrebbero piacerti anche