You are on page 1of 5

221 MATLAB ASSIGNMENT 01

DUE 20 SEPTEMBER 2018

The goal of this assignment is to ensure you can use MATLAB, save and load files into MATLAB, and run basic
commands.

E NTERING VECTORS AND MATRICES


MATLAB is a program for manipulating matrices. We will be studying matrices and the rules for their manipu-
lation later in this course (or, may have dones so already). For now, all you need to know is that an n by m matrix is
a box of numbers with n rows and m columns. Vectors are a special case where there is only one row (a row vector)
or only one column (a column vector). Try typing
A = [1 2 3]
in the command window and then press the enter key. MATLAB answers with
A =
1 2 3
What has happened here? MATLAB has assigned the 1 by 3 matrix (row vector) [1 2 3] to the variable A. What
does this mean? “assignment” and “variables”?
When we say that you have “assigned a variable”, we mean you have told the computer to pick a bit of memory,
and store a particular number (or word, or matrix) in that bit of memory. You’ve also told the computer what to
call that piece of memory, so that later on you can find those numbers again.
So now, if we go and ask the computer what is stored in A, it will know what to say. If we ask it what has been
stored in Z on the other hand, the computer will tell us that nothing is there. Here’s how: Type
A
Z
and press the “enter” key1. Do you notice how the compute gives you a bright red error message when you ask it to
tell you what is in Z ? This is a good sign, and means everything is working well. It is also a message you will need
to get used to: it is a most common error, and you can expect to stumble over it many times before the end of this
course. It simply indicates that the thing you have asked MATLAB to find does not exists. Often this will happen if
you have capitalized a variables name incorrectly—for example, Box and box are not considered the same thing.
Okay, so that’s the very basics. Let’s look around the screen a bit, and see what else there is to see. In one corner
of the screen (possibly the right or possibly the bottom left) is a box called “Workspace”. This contains a list of
all the varibles you currently have stored. There is the name, and the value, and (for some types of variables) the
minimum and maximum value. If you decide to make a new variable and store it, it will appear up there in your
list of variables
Occasionally in these assignments, you will be asked to store the code for something as a variable. For example:
“What code would you need to store the vector (1,2,3) in the variable abc? Save your code as exampleCode” To
answer this you would need to type:
>> exampleCode= ’abc=[1 2 3]’
Here we have told matlab that we want it to remember a string of letters, numbers and symbols—i.e., of charac-
ters. The string to remember is “abc =[1 2 3]”, and we want it stored in exampleCode. We have used the single quote
’ to tell the computer that we are giving it words to remember, otherwise it would currently be trying to actually
DO the instruction, as opposed to just remembering them.
You can also use single quotes to store words and sentences:
>> exampleWords= ’When matlab is remembering words they will often be highlighted in purple.’

1After this point, the instructions will not keep reminding you to press the “enter” key after each command. Instead, they will use the
notation » A to tell you to type “A” and hit enter.
1
2 DUE 20 SEPTEMBER 2018

Your new variables should be stored up in your workspace. Look at your workspace now to check that your
variables are stored there. Notice how the little yellow symbol in front of them has letters on it, as opposed to a grid
pattern. This yellow symbol is useful for telling what type of thing it is that MATLAB has stored.
You can change the value stored in a variable. For instance, you could now write A = [1 2 3 4] to change
what is stored in A, or even A = ’a bunch of words’.
Also, instead of typing everything out again, we can use the ‘up’ (and ‘down’) keys to go back through the list of
commands we already gave MATLAB.
For example, suppose we realise we want A to be the vector [7 8 9 ], we can press ‘up’ until we reach a com-
mand, like A= [ 1 2 3 4 ], setting A equal to something, and then we can edit it.
Now, the title of this section says that we’ll be teaching you how to enter vectors and matrices, so I had better
show you matrices before we move on. When inputing a matrix A to MATLAB, we define it row by row and use a
semicolon ; to separate the rows. For example, the matrix
µ ¶
1 2 3
4 5 6
is defined in MATLAB with the command
>> A = [1 2 3; 4 5 6]
Notice that the semicolon is placed after the last entry of a row and so the first entry of a new row should come
after a semicolon. Also note that the variable A has been reassigned from its previous value. You can see that the
value of the variable A has changed in the workspace window.
The semicolon operator ; has another useful purpose—it suppresses the output of MATLAB when placed at the
end of the command. For example, typing
>> A = [0 0.25 0.5 0.75 1];
defines the vector (0 0.25 0.5 0.75 1) in MATLAB, but silently. Try it yourself. This command is quite useful
if you don’t care about the output, and especially if it is very long. To check that A has been defined correctly, type
>> A
MATLAB replies
A =
0 0.2500 0.5000 0.7500 1.0000
confirming that the value of A is again (0 0.25 0.5 0.75 1).

Exercise 1.
(a) Assign your student number to the variable OneAnum
(b) Assign the matrix
 
0 −3 1
8 2 1
1 4 2
to the variable OneBmat.
(c) Assign the column vector (3 × 1 matrix)
 
1
−3
5
to the variable OneCvec. Warning: if you give [1, 3, −5] as a row vector, it will be graded “incorrect”.

T WO USEFUL KEYWORDS
By now, it is likely that you have made a complete mess of the work space- in particular, there is probably the
unfriendly X variable lying around gumming up space. Leaving extra variables lying around is problematic for two
reasons. For one, it means that you are using up your computers memory. For the examples you will deal with in
this class, this is a small enough problem that we are not worried. The second reason is that having un-needed
variables lying around leads to you (the programmer) tripping over yourself: imagine you were solving a problem
221 MATLAB ASSIGNMENT 01 3

about the height H of a pole. When you finish the problem and go to work on a problem about the heat of the oven,
you don’t want your old value of H to still be floating around.
You want the computer to forget all the old values it had stored. To do this, simply type:
clear
this is an instruction to the computer the empty everything it has up in the workspace.
If you only want to clear one variable (for example X) then you can type
>> clear X
To get rid of just that one variable.
Around this point in the exercise, I imagine around half of you have just cleared all the work you have put into
solving exercise one—do not be alarmed! This as an excellent opportunity to make use of MATLAB’s up arrow
feature (press up a couple of times to view past commands, hit enter to run them again).
The second keyword that is important for you to learn about at this time is help. You can type help followed by
any function name or keyword in MATLAB to get an explaination of what that thing does. Although we will aim to
teach you everything you need to know for these labs, learning how to use the help function is useful—especially
when you strike out on your own. An example of the use of the help function:
>> help clear

O PERATIONS ON VECTORS
Here are some common operations on (column) vectors, and how to carry them out in MATLAB:
Define X to be [1 ; 2; 3] and Y to be [-1; 0; 7].
We can add and subtract X and Y :
>> X+Y
Subtraction works exactly the way you would expect it to, for example the command
>> X-Y
subtracts Y from X. We could find out more about addition and subtraction in matlab by typing help + or help -.
We can multiply X by a scalar
>> 2 * X

F UNCTIONS
MATLAB has a large number of built-in functions. Let us explore just a few. Some functions are simply used to
make things which are useful. The function zeros(N) makes an N × N matrix of zeros. eye(N) and ones(N) are
similar. To make non-square matrices, use zeros(N,M), ones(N,M) and eye(N,M).
We will see more functions as the term progresses, but for now we should also be aware of the function rref
that brings a matrix to reduced row-echelon form (also called “reduced echelon form”). To see this working, try
>> C=[1 2 3 ; -2 -4 7]
>> X=rref(C)

L OADING DATA
How you load data into MATLAB depends on how you are running MATLAB and on what kind of computer.
Along with this assignment, there is a document called Ex1Data.mat. Save this on your computer.
• If you are running MATLAB on your own computer, open this file.
• If you are running MATLAB on the web, you will first have to upload the file using the “upload” button at
the top of the window. It should appear in the “Current folder” mini window on the left. You then can open
it by right-clicking on it, or by typing load(’Ex1Data.mat’).
The file contains three new variables vectorA, vectorB and BigMatrix. The vectors are 6-dimensional column
vectors and the big matrix is 8 × 50.
To verify that you really have loaded this correctly, try
>> vectorA
The output should be a column vector.
4 DUE 20 SEPTEMBER 2018

Exercise 2.
(a) Calculate the vector vectorA + 3vectorB and store the result in the variable TwoAvec
(b) Let v be the 6-dimensional column vector having each entry equal to 2. Calculate the vector vectorA − v and
store the result in the variable TwoBvec
(c) Calculate the reduced row echelon form of BigMatrix and store the result in TwoCmat.
(d) Suppose BigMatrix is the augmented matrix corresponding to a system of linear equations. How many free
variables are there in this system of equations? Store your answer in TwoDnum.
221 MATLAB ASSIGNMENT 01 5

H ANDING IN
It is now time to turn in your first MATLAB assignment for this course. For these assignments, you will be
handing in your assignment as a .mat file—that is a special matlab file containing all your variables. (Note, do
NOT save it as a "m" file. This will be much more difficult for us to read and grade).
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: OneAnum,OneBmat,OneCvec, TwoAvec, TwoBvec, TwoCmat, TwoDnum. Please be aware that
these names are case-sensitive. If you submit oneanum, 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.

You might also like