Mat Lab Project 1
Mat Lab Project 1
This “Justin’s Guide” is an edited version of a guide by Justin Wyss-Gallifent. It is instructional guide
before the actual Project 1, with information you will need to have. You do not need to turn in anything
for this part. Project 1 starts a few pages down.
1. Method
The way this guide is written is that it is assumed that you will sit down at a MATLAB terminal
and start. The commands that you give to MATLAB are given but the output is not. The output is,
however, talked about, with the understanding that you will put in the commands and see the output.
Then you can read about it and keep going.
2. Starting MATLAB
Find a system on campus and run MATLAB. The important window (there will probably be a couple)
will have a bunch of subwindows in it but the major panel on the right has a >> in it. This is the
prompt, it’s where we tell MATLAB what we want it to do.
3. Simple Calculation
For example, to do 4+5 we just type it in and hit “return”
>> 4+5
4. Solving a System
Now let’s do something having to do with MATH 240.
First we’ll solve a system of linear equations. We do this using MATLAB’s solve command. Due
to the quirkiness with which this command operates we have to make sure we interpret the result
correctly. For example, suppose we wish to solve the system
3x1 + x2 − 5x3 = 0
x1 − 5x2 + x3 = 2
x1 + x2 − 5x3 = −1
we do
>> A=[2 3 ; 5 -1]
6. Matrix Operations
Suppose now you put in a matrix and you wish to manually do row operations to it. MATLAB doesn’t
have any functions for doing row operations, instead we have to execute low-level commands to do so.
This is easier than it seems.
Here is an augmented matrix
>> A=[1 -1 -3 7 ; -3 1 4 -16 ; 4 -3 -5 12]
To get this to row echelon form, first we’ll clean out the entries below the upper-left 1. To do this we
need to add 3 times row 1 to row 2. In MATLAB the code is
>> A(2,:)=A(2,:)+3*A(1,:)
Confused? I was! The expression A(rownumber,:) refers to the entire row, so basically this line is
saying:
3
1 0 2 | 2
2 1
0 1 3 | 6
Note that the third column is not a pivot column so x3 is free. Hence we have the solution
3
x1 = − 2x3
2
1 2
x2 = − x3
6 3
x3 = free
8. Lastly, note that we can deal with matrices with unknown constants in them but we have to explicitly
tell MATLAB that they are unknown constants. For example, suppose we wish to enter the matrix
−2 3 | h
5 −1 | k
First we must tell MATLAB that h and k are to be dealt with symbolically. We do this with the syms
command
>> syms h
>> syms k
And now we can do
>> A=[-2 3 h ; 5 -1 k]
>> rref(A)
MATH 240 – Fall 2018 – MATLAB Project 1 – due in class Tuesday, 9/18.
What to turn in: A paper version of the completed project should be handed in to your TA in class on
the due date (or earlier). Your printout must include all of your commands, as well as the output
produced by your commands. Here are two suggestions on how to do that:
1. You can turn in a printout of the command window of a clean final session. A clean session is one in
which no mistakes were made. Since mistakes inevitably happen, you may want to save the commands
which work correctly (say, copy and paste them into a text editor), and for the final output, enter the
commands in a single session.
2. An alternative, and better, approach is to write a script (.m file) and then “publish” it in MATLAB.
Here, you can enter all the necessary commands in the script, and you can edit them if there is a
mistake. If you do this, be sure to separate your code problem-by-problem. This can be done by either
creating a new .m file for each problem, or by typing %% in the script. This signifies to MATLAB
that it is a new portion of code, and MATLAB separates it accordingly when published. For example,
you can write
%% Problem 1
followed by all your commands for Problem 1 (then the same for Problem 2 and so on).
Regardless of how you present your project, you will need to include items in what you turn in that are
not MATLAB commands. For example, your name. You can do this by typing into the MATLAB prompt
>> your name, preceded by the percent symbol %. MATLAB ignores the “comment” you type on a line
following the symbol %. For example, if your name were Homer Simpson, your first lines could look like
Also, when you begin a new problem part, indicate that on the output with something like
In the project, you will see questions marked with a ?. These require you to type an answer that is not just
a MATLAB command. Type your answer after a %.
Rules for working in groups: You can work in groups of 1, 2, or 3 with anyone in the course who has
the same TA and instructor as you. List all members of your group at the beginning of your project.
Everyone in a group must type up the project individually, and hand it in to the TA. You can’t
learn MATLAB without typing. You should work side-by-side, either in a computer lab or working on your
own laptops. One project from a group will be graded and everyone in the group will get that grade.
But first: Before you begin the project, go through “Justin’s Guide” and type along in MATLAB as you
read. That should be enough to get you through this project. Don’t turn in anything from what you did in
“Justin’s Guide”. To start fresh, you can enter the command clc to clear the command window, and you
can enter clear to clear out all variables that you have stored.
Format: The command format rat will cause your numerical output to appear as fractions. You might find
that convenient or necessary at times. The command format short changes back to the decimal format. The
command format long produces decimal output with more decimal places. Sometimes, certain problems
will insist that you use certain formats.
x1 + x2 − x3 − x4 + 5x5 = −8
−6x − 8x + 4x
− 34x5 = −10
1 2 3
4. Do problem # 3 on p. 87 of the textbook. For each part, be sure to explictly give the appropriate
system of equations (as a comment) before entering the appropriate matrices into MATLAB. Show all
of your necessary MATLAB computations.
5. Suppose you wish to show that
4 1 −2
5 is not in the span of −2 , 1
1 3 2
is linearly dependent.
(a) Enter a matrix A which can help you solve this and use rref on it.
(b) ? Explain how you can tell from the rref that the vectors are linearly dependent.
(c) ? Give a nontrivial linear combination of the vectors which yields 0. (Hint: Make a free variable
nonzero.)
(d) ? Find a theorem in §1.7 which allows us to conclude, without doing any of the previous compu-
tations, that this set must be linearly dependent.
(e) ? Does this set of vectors span R4 ? Justify your answer.