# MATLAB Project # 0 - HOMEWORK INSTRUCTIONS

This project teaches you how to use MATLAB and present
homework . There are two important kind

of les that we will be using throughout the course in the MATLAB Projects: the
M-files and the diary

files. The M- files are text les that you can create with a text editor, and
they contain commands to be

interpreted by MATLAB. An example of an M- file to solve Problem 1 of this
project is the following.

% Solution to

% MATLAB Project 1

% problem 1

format compact

% this command eliminates unnecessary blank lines

% from the output

% We first enter the matrices A and B

A = [1, -2, 3; 4, 5, 6; 7, 8, -10]

B = [-1, 7, 2; -5, 6, 1; 7, -8, 11]

% Now we compute A+B and 3*A

A+B

3*A

% As you already noticed, the symbol % is used to add comments .

An important aspect of M- files is that their names must end with ".m". For the
M- file shown in the

example, I would use the name **p0prob1.m,** but you can use the names you
like ! When you want MATLAB

to fol math -software/solve-prolem-on-lowest-comon-f.html">low the instructions in the M- file, you must type the filename without
the extension .m in the MATLAB

prompt, and you have to be working in the directory where the le is stored. To
go to a directory we use

the cd command. This will be better understood with an example: Suppose that the
le **p0prob1.m** is in

the directory c:nmath461nmatlab, then to run the script in that le you have to
type the following:

>> cd c:\math461\matlab

>> p0prob1

and MATLAB will output

The diary files are text les where MATLAB stores all what
you see on the screen as you run commands

and/or invoke M- files. Suppose that now we modify **p0prob1.m** adding the
line diary p0prob1.txt at the

beginning and the line diary off at the end. Then, as a result, when you invoke
p0prob1 at the MATLAB

prompt, MATLAB will show on the screen the same as before, but at the same time,
it will save that

information on the le p0prob1.txt. If the diary file already existed, MATLAB
would append the lines at

the end.

You will have to use the diary files to hand in the MATLAB projects. Since I
will want the diary files

to contain the commands used in the M- files for performing the required task,
you will have to include the

line echo on at the beginning of the le.

Here is a ** summary of the steps used to prepare homework solutions.**

(1) Create an M-file in your current working
directory to hold the solution. Include echo on near the
top of the le so you can see which commands are producing what output
when you run the M- file.
(2) Continue editing and running the M- file until you are con dent that
it contains the MATLAB
commands that solve the problem.
(3) Add comments to your M- files to explain the method being used to
solve the problem and to
interpret the results.
(4) If you didn't do so before, insert the delete and diary commands
into the M- file (see example
below).
(5) Now run the M- file to produce the final solution. Send the diary
file to the printer. Collect the
pages, staple them together and submit them. |

To illustrate the results of this process, here is the
final version of the M- file for problem 1.

file p0prob1.m

delete p0prob1.txt % we delete the file just in case it existed

diary p0prob1.txt

format compact

echo on

% Solution to

% MATLAB Project 1

% problem 1

% We first enter the matrices A and B

A = [1, -2, 3; 4, 5, 6; 7, 8, -10]

B = [-1, 7, 2; -5, 6, 1; 7, -8, 11]

% Now we compute A+B and 3*A

A+B

3*A

% The results MATLAB gave for A+B and 3*A agree with our definitions

% for adding matrices and multiplying by scalars .

echo off

diary off

% it is important to do this, otherwise MATLAB would

% continue to add lines to the diary file

And the diary file **p0prob1.txt** will look like

file p0prob1.txt

% Solution to

% MATLAB Project 1

% problem 1

% We first enter the matrices A and B

% Now we compute A+B and 3*A

A+B

% The results MATLAB gave for A+B and 3*A agree with our
definitions

% for adding matrices and multiplying by scalars.

echo off

**Final Note**

Each problem should be worked in a separate M-
file, and the results saved in a diary
file. A printout of the diary file should be handed in. Use comment
lines in your M- file
to make appropriate comments and to indicate the problem number . Use the
echo
command to display the commands in your M- files in the command window,
and thus
in the diary file. |

Try the following 4 problems to practice both MATLAB
commands and diary files. You will need both

for MATLAB Project # 1, which will be graded.

**Problem 1: **Enter the matrices

and

Compute A + B and 3A. Do the results agree with our
definitions for adding matrices and multiplying

matrices by scalars?

**Problem 2:** Enter the matrices

and

Compute A+B, A+C, C +B. Do the results agree with our definitions for adding
matrices? Why, or why

not?

**Problem 3: **Enter the vectors (column matrices)

and

Then compute the linear combinations 2u + 3v and 3u - 12v.

**Problem 4: **Enter the matrices

and

Then compute x ยท y by forming the matrix product x '*y.
Explain why this matrix product gives the correct

answer. What does the prime do? What happens if we type x*y? why? What is the
result of typing x*y'?

**Problem 5:** Let

and

(a) Compute AB and BA, using matrix multiplication in
MATLAB. Are they the same? Did you expect

them to be the same? Explain.

(b) Compute (AB)C and A(BC). Are they the same? Did you expect them to be the
same? Why?