MATLAB - INTRODUCTION TO MATRIX COMPUTATION

advertisement
MATLAB 1 - INTRODUCTION TO MATRIX MANIPULATION
TECHNIQUES
Paul Vaz
The purpose of this lab is for you to see some of the capabilities of a computer algebra
system such as Matlab. We give you commands to type into Matlab mixed with commentary. See
that you can make the commands work and watch the results. Commands for you to type into
Matlab will look like this:
>> A = [1 2;3 4; 5 6]
and commentary looks like this text. You do not have to type the >>, it is the Matlab prompt.
Matlab excels in Matrix computation. The following example commands the creation of a
matrix. You could use a small ‘a ‘ to define the matrix. Notice that the rows are separated by
semicolons (;).
>> A = [1 2 0;2 5 -1;4 10 -1]
Matlab has many types of matrices which are built into the system. A 4 x 4 matrix with
random entries is found by typing the commands followed by a return:
>> rand(4)
>> rand(2,3)
Here are some standard matrices from linear algebra:
>> eye(3)
>> zeros(3,4)
>> ones(3)
>> [eye(2) ; zeros(2)]
>> [eye(2) ; ones(2,3)]
>> [eye(2) ; zeros(3)]
You should have got an error on the last two. WHY?
Here are some simple computations and commands. Study your results of the commands
carefully:
>> 3*A
>> -A
>> A+(-A)
>> A^2
>> A*A
>> triu(A)
>> tril(A)
>> diag(A)
>> diag(diag(A))
>> size(A)
Usually, a dot in front of an operation will change the operation. In case of multiplication, A.*B
will make it into an entry-by-entry multiplication instead of the usual multiplication:
>> B=magic(3)
>> A*B
>> A.*B
To see what a command means, type
>> help command name
For example, to see what magic(n) means type
>> help magic
Try another command with dot:
>> A.^2
There are a many functions we apply to scalars that can be applied to matrices:
>> sin(A)
>> exp(B)
>> log(A)
>> abs(A)
The command for the transpose of a matrix is
>> C = A’
These two matrices can be multiplied by the command
>> D = A*C
The command for the inverse of the matrix A is
>> X = inv(A)
The fact that a matrix multiplied by its inverse is the identity is illustrated by the command
>> inv(A)*A
or
>> A*inv(A)
Using the ‘COLON(:)’ , Matlab also offers some very powerful ways of creating vectors.
>> e=magic(5)
>> e(2,3)
Now we will use the colon notation to get new matrices from the matrix ‘ e’:
>> e(2,: )
>> e(: , 3)
>> e(2:4,: )
>> e(: , 3:5)
>> e(2:4,3:5)
>> e(1: 2: 5,: )
>> e(:, [ 1, 2, 5])
>> e( [ 2, 5] , [ 2, 4, 5])
You can also make assignment statements using a vector or a matrix:
>> f=magic(5)
>> f( [1 2] , : ) = e( [2 1] , : )
>> e( : , [1 2] ) = f( : , [3 5] )
>> e( : , [1 5] ) = e( : , [5 1] )
>> e=e( : , 5 : -1 : 1 )
When you insert a 0-1 vector into the column position, the columns which correspond to the 1’s
are displayed:
>> v = [ 0 1 0 1 1 ]
>> e( : , v )
>> e( v , : )
Some Features
You may have discovered by now that Matlab is case sensitive, that is “a” is not the same as “A”.
It is not always necessary for Matlab to display the results of a command to the screen. If you do
not want the matrix A displayed, type A; to suppress it When Matlab is ready to proceed, the
prompt >> will appear. Try this on a matrix.
Ocassionally you will have spent much time creating matrices in the course of you Matlab
session and you would like to use these same matrices in your next session. You can save these
values in a file by typing
save filename
This creates a file filename.mat which contains the values of the variables from your session. If
you do not want to save all variables there are two options. One is to clear the variables off with
the command
clear a b c
which will remove the variables a, b, c. The other option is to use the command
save filename x y z
which will save the variables x, y, z in the file filename.mat. The variables can be reloaded in a
future session by typing
load filename
When you are ready to print out the results of a session, you can store the results in a file and
print the file from the operating system using the “print” command appropriate for your operating
system. The file is created using the command
diary filename
Once a file name has been established you can toggle the diary with commands
diary on
and
diary off
The command diary filename
will copy anything which goes to the screen(other than graphics) to the specified file,
filename . Since this is an ordinary ASCII file, you can edit it later.
Download