Professional Documents
Culture Documents
The goal of this assignment is to ensure you can use MATLAB, save and load files into MATLAB, and run basic
commands.
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.