The purpose of this introduction is to illustrate some basic MATLAB commands. Complete the simple examples below while working at a computer terminal. It is assumed that you are using a UNIX operating system. According to the UF Computing Help Desk, you should be able to to sign on to MATLAB just by typing "matlab" from a Unix shell at any of the computer labs on campus; let me know if this does not work. If you are working from your own computer, you may have to figure out a different way of accessing MATLAB.
Once you have signed on to MATLAB, you should type in your commands to the right of the prompt >>. It may take a minute for this prompt to appear.
diary project1 {RETURN}
(here and below, {RETURN} means hit the "return" or "enter" key). This tells MATLAB to start recording everything that follows in a file called "project1" in your home directory (or whatever subdirectory you started MATLAB from). You can of course use a file name other than "project1" if you like.
Enter the matrix a by typing
a = [1 2 3; 4 5 6; 7 8 0] {RETURN}
(In case your browser is crowding things together, what I've asked you to type is [1 (space) 2 (space) 3; etc.].) What matrix did you obtain? Notice that the entries in each row are separated by spaces and the rows are terminated with semicolons.
It is also okay to separate entries within each row by commas or by commas and spaces. Enter the matrix b by typing
b = [1 2 3; 4,5 6; 7,8, 0; 1 1, 1] {RETURN}
Now type
c = [2 3; 4,5]; {RETURN}
The semicolon at the end of the line above suppresses the output, but MATLAB still knows what meaning you just assigned to c. Check this by typing
c {RETURN}
Describe what the following elementary commands do to your matrices a and b. Hit the "return" or "enter" key at the end of each entry; I won't bother telling you to do this anymore.
a'
b'
Note that each time you perform an operation, MATLAB assigns the variable name "ans" to it unless you have already given a name to what you want the answer to be. If you want to save the output of the operation but forgot to give a name previously, you can do so now:
d = ans
Note that any older meaning of "ans" is lost. If you type
ans
now it gives you b', not a'.
Now try the following operations and describe what happens. (By now, the matrices a,b,c, and d may have scrolled off the screen. If you want to see them at any time before doing the operations below, just type the matrix's name, hit "return", and the matrix will be echoed to the screen.)
3*a
rref(b)
rref(d)
[a a]
[a, a]
[a; a]
[a, b]
[a; b]
diag(a)
diag(diag(a))
diag(b)
Describe the effects of the following commands on the matrix a.
a(2,3)
a(2,:)
a(:,3)
a(2:3,1:2)
Enter the matrices A = [1 2 3; 4 5 6; 7 8 10] and B = [1 2 3; 4 5 6; 7 8 9]. Enter the vectors (3 by 1 matrices) C = [1; 1; 1] and D = [1; 1; 2]. MATLAB is case-sensitive so this does not wipe out the earlier assignments of a,b,c, and d. You can solve the system of linear equations Ax=C in several ways.
A\C
rref([A C])
Now see what happens if you try
B\C
You can check whether MATLAB gave a correct answer by typing
B*ans
and seeing whether this gives you back C. Now repeat this process with B\D. (Note: an output such as "ans = 1.0e+15 *" followed by a matrix (or vector) means that to get "ans", each entry of the matrix (or vector) should be multiplied by 10^15 (10 to the fifteenth power).)
Next try
rref([B C])
and
rref([B D])
Can you explain why MATLAB was able to give a correct solution to Bx=C (using B\C) but not to Bx=D (using B\D)? Did B\C give you the general solution to Bx=C? Which method (backslash or rref) gives you more reliable and useful information if you want to know whether a system of linear equations is consistent and (if it is consistent) you want to find its general solution?
Enter the matrices E = [1 2; 3 4] and F = [2 0;-3 5] and perform the following matrix operations.
E+F
E-F
E*F
(this operation is matrix multiplication, discussed in section 2.1 of your textbook).
E*F(:,2)
What did this last operation accomplish?
What do the following commands do?
zeros(3,5)
eye(5)
rand(3,4)
(Try this last operation at least twice before deciding what it does.)
diag(e)
e(1)
max(e)
[u i]=max(e)
The second component of the result of "[u i]=max(e)" is the entry in which max(e) occurs.
abs(e)
Recall the matrix a from item 2. Try
max(a)
clear i
Now enter the vector f=[i;1;1+i;1-i],and describe the output of the following operations.
real(f)
imag(f)
f'
diary
The portion of your session from the line below "diary project1" to the line "diary" is now recorded in the file project1. Subsequent lines will not be recorded, unless you type "diary" again.
help diary
help rref
help max
help rand
help clear
help exit