MATLAB is very powerful and varied software package for scientific computing. It is based upon matrices and m-files.
It is invoked by typing
% matlab6
I recommend the creation of a directory called ``matlab.'' Use the two commands
% mkdir matlab (done only once)
% cd matlab
before invoking matlab. Put all .m file in this directory.
Matrices are the main data element. They can be introduced in the following four ways.
>> A = [ 1 2 3 ; 4 5 6 ; 7 8 9]
It immediately outputs
|
in that file.
Then type
>> gena
it will also produce A exactly as above. The file gena.m is called a script file.
The line
>> gena;
will also produce A, but will not print it on the screen.
It is important to remember the distinction between row and column vectors in MATLAB. If you type
>> x = [-1.3 sqrt(3) (1+2+3)*4/5]
you get
|
>> x = [-1.3; sqrt(3); (1+2+3)*4/5]
you get
|
>> help
This lists all help topics
>> help inv
Gives the help information for the function ``inv'' for inverting a matrix.
>> help help
Explains the ``help'' command.
MATLAB m-files contain two types of items
Functions have ``function'' statements as their first line. Two examples are
function A=fun1(x,y,z)
function [A,B,C]=fun2(x,y,z)
These should be placed in files called ``fun1.m'' and ``fun2.m'' respectively. Here ``x,y,z'' are input arguments, and ``A,B,C'' are output arguments. Any of these can be simple variables, vectors, or matrices. For examples of functions look at the files ``fun1.m'' and ``fun2.m'', ``cosx.m'', ``bisect.m'' and ``Fun.m''.
MATLAB supports the usual programming structures ``if'' , ``for'', ``while'' and ``switch.'' The syntax is a bit different from C syntax, but the use is much the same. Information about any of them may be obtained from the help command. The function ``bisect2.m'' contains ``if'' and ``while'' statements. The following ``for'' statement sums the first n integers.
sum=0;
for k=1:n
sum=sum+k;
end;
Two useful MATLAB command are ``save'' and ``load.'' MATLAB creates a workspace for all of your variables. When you are done with a MATLAB session, you type
>> quit
and you are out of MATLAB. However, your workspace and all of the variables that you are using vanish, they will not be there when you log on to MATLAB again.
If, before you quit, you type >> save mysession
the workspace will be saved in the file ``mysession.mat.'' If you type
>> save mysession X Y Z
then only the variables X,Y, and Z will be saved. ``save'' is a completelydestructive operation, ``mysession.mat'' is completely overwritten.
If you want to retreive your workspace from the previous session type
>> load mysession
To know what variables you have type
>> who
Most matrix operations in MATLAB are one line.
The matrix transpose operation is
>> B = A' ;
Thus if A has the contents
|
|
>> C = A+B
just adds A and B.
>> C = A*B
just multplies A and B.
>> C = X'*Y
does the operation C = XT Y.
>> b = A*x
is a matrix-vector multiply. If the dimensions are wrong in any of
these operations, you will get an error message.
There are two important functions associated with matrices, ``size'' and ``length.'' The statement
>> [m,n]=size(A)
returns the dimensions of the array A. If m = 1, A is just a row vector, if n = 1 then A is column vector, and if m = n = 1, then A is scalar.
The statement
>> p=length(A)
returns the larger of the two outputs of ``size.''
There are a number of ways to generate special vectors and matrices. Here are some examples
>> x = 1:5
yields
|
yields
|
|
>> y =exp(z)
yields
|
To see the list of other elementary functions, type help elfun
It also support many special operations that will become useful later.