In this project you will write some simple programs in MATLAB. For this project you may work individually or in pairs (no groups larger than two). Two students working together need only turn in one printout with both names on it. You should not share answers with anybody other than your "teammate". Make sure you are familiar with the student honor code.
Important reminder! As soon as you enter MATLAB, remember to start saving your session, either by using the diary command as in Project 1, or by whatever other means you found works for you.
Your search path is a set of directories or subdirectories predetermined by whoever installed MATLAB on the system you are using. The search path may include your home directory (in which case .m-files in your home directory will be found by MATLAB), but almost certainly will include a subdirectory of your home directory called "matlab" (lower case). To see what your search path is, if your home directory is on a Unix system, type "pwd" (stands for "print working directory") at the Unix prompt. This will tell you what your home directory is. Then start up MATLAB and type "path" at the prompt. You will probably get a long list of directories the first of which will probably be
(your home directory)/matlab
(An object to the right of a slash is a subdirectory of the directory to the left of the slash.) If that's the case you should create the subdirectory "matlab" in your home directory, which you should do as follows (you will need to exit MATLAB to do this, unless you can bring up a separate Unix shell to work in). At the Unix prompt, in your home directory, type
mkdir matlab
If you have already created a .m-file, say gronk.m, and want to move it from your home directory to this matlab subdirectory, type
mv gronk.m matlab
(Don't do this before you create the matlab subdirectory, or mv will just rename your file instead of moving it.) To create or edit a file gronk.m in the matlab subdirectory, any of the following four ways should work if your text editor is emacs: (1) at the Unix prompt in your home directory type "emacs"; then in the emacs window that comes up type "control-x control-f matlab/gronk.m" ("control" here stands for the control key, pressed at the same time as the x or f; don't type in the word "control" or the hyphen); (2) at the Unix prompt in your home directory type "emacs matlab/gronk.m"; (3) at the Unix prompt in your home directory type "cd matlab" (this puts you in the subdirectory matlab), then type "emacs", then in the emacs window type "control-x control-f gronk.m"; or (4) at the Unix prompt in your home directory type "cd matlab" then type "emacs gronk.m". If you already have an emacs window running that you brought up by typing "emacs" from within your home directory, then in the emacs window type "control-x control-f matlab/gronk.m".
To have a sample .m-file, create a file with the contents below, and entitle it "whatdoIdo.m" (without the quotation marks). Lines beginning with a % sign are comment lines. You don't need to type in the comment lines, except for the two with the multiple % signs (type these in so that on your printout the program will stand out). Punctuation at the end of lines is not important in this file; a line can end with a comma, a semicolon, or just a carriage return.
%%%%%%%%% File called whatdoIdo.m function c=whatdoIdo(a,b) % This tells MATLAB that you are defining a function called % whatdoIdo. whatdoIdo is a function of two variables. In the body of % the program these variables are called a and b; the output of the % function is called c. These are local variables; MATLAB will not % confuse them with anything outside the program you may have named a,b, % or c, and will not remember them after the function has been % computed. Note that the function name must be the same as the part % of the file name that precedes .m [m,n]=size(a); [k,p]=size(b); if m~=k | n~=p, % "~" means "not", so "~=" means "does not equal" % "|" means "or" error('matrices not the same size'); % this tells MATLAB to return the indicated % error message whenever the "if" condition is met return; end; %this closes the "if" statement c=zeros(m,n); for i=1:m, for j=1:n, c(i,j)=a(i,j)+b(i,j); end end %%%%%%%%%%%%%%% end of file
Don't forget to save the file (if you are using emacs, hit control-x control-s to save).
Now start up MATLAB. Type "help function", "help size", "help if", and "help for" to make sure you understand you these are used in whatdoIdo.m.
In your MATLAB window, type "type whatdoIdo". The whole file should appear on the screen.
Figure out what the function whatdoIdo does. Later, either write your answer on your printout near the whatdoIdo text.
Enter a few matrices, name them, and try out the function whatdoIdo. (For instance, if you named matrices were named x and y, you would type
whatdoIdo(x,y)
to do this.) Find some cases in which whatdoIdo returns an error message and some in which it does not.
Figure out what the function whatdoIdo does. Later, either write your answer on your printout near the whatdoIdo text.
Write a MATLAB program to compute the transpose of a matrix, without making use of MATLAB's built-in transpose function (the apostrophe). The program should handle matrices of all sizes. If you name your file, say, mytrans.m, test your program out on various pairs of matrices A by typing
mytrans(A)-A'
and seeing whether you always get the zero matrix. As in Project 2, MATLAB's "rand" function can be useful for generating test matrices.
Put useful comment lines in your file. (The file whatdoIdo.m is an example of a file with [intentionally] inadequate comments--nothing tells you what the program does or what the individual lines do.) Begin and end the file with comment lines that contain good visual separators as in Problem 1 so that your program will stand out from the rest of your output.
When you are satisfied that your program is correct, use the "type" command as in Problem 1 to include the file contents in your output.
Write a MATLAB program to multiply two matrices, without making use of MATLAB's built-in matrix multiplication operation "*". (You may want to review the Row-Column Rule in the box on p. 103 first). The program should handle matrices of all sizes, and return an appropriate error message when the matrices cannot be multiplied in the given order. If you name your file, say, mymult.m, test your program out on various pairs of matrices A, B by typing
mymult(A,B)-A*B
and seeing whether you always get the zero matrix (for compatibly sized A and B). Test at least one pair of incompatibly sized matrices.
Again, put useful comment lines in your file, and begin and end the file with good visual separators.
When you are satisfied that your program is correct, again use the "type" command to include the file contents in your output.