Computing and Networking
Using MATLAB |
 |
MATLAB in UNIX
MATLAB is a high-performance interactive software package for scientific
and engineering numeric computation. MATLAB integrates numerical
analysis, matrix computation, signal processing, and graphics. This
is a very brief sketch of some of the things that you can do in MATLAB. If
you need more information, a manual is available in the reference area
located in Room 141 of the Computer Center.
This document covers the following information:
To start MATLAB simply type :
To create a matrix like the one below with a name of 'A':
A =
Type in the following line:
If 'A' and 'B' are two nxn square matrices, the A/B results
in:
While A\B will give you:
You can also compute the inverse of A by inv(A).
Addition and subtraction of matrices are defined whenever the matrices
have the same dimensions:
Also, a scalar can be added or subtracted from all the element:
Multiplication and division of matrices are defined whenever the
first operand has as many rows as the columns of the second operand.
Also, a scalar can be multiplied or divided:
Complex numbers are entered using the special functions "i" or "j".
Complex numbers can be created by typing in:
There are at least two convenient ways to enter complex matrices. They
are created by:
A=[1 2;3 4] + i*[5 6;7 8]
or
A=[1+5*i 2+6*i; 3+7*i 4+8*i]
Complex numbers and matrices are allowed in all mathematical operations
and functions in Matlab.
NOTE: Avoid any blank spaces, when entering a matrix like "1 + 5*i".
There are many available types of graphs in MATLAB, such as linear graph,
log graph, 3-D graph, and etc.
A linear plot of X versus Y can be created by enter the values
of x and y, and execute plot:
X=1:10;
Y= 0. .48 .84 1. .91 .6 .14:;
plot(X,Y)
The plot can be labeled, and has the grid turned on by:
title('Plot A')
xlabel('X-Axis)
Ylabel('Y-Axis)
grid
MATLAB is capable of executing sequences of commands that are stored in
files, called M-files. M-files are created by using regular unix editors
such as, gemacs and vi. They must have a file type of ".m".
There are two types of M-files:
- Scripts
Scripts contain long sequences of commands (MATLAB statements).
The variables in a script file operate globally on the data in the
workspace.
Example:
% An M-file to calculate Fibonnaci numbers
f= 1 1:; i=1;
while f(i)+f(i+1)<100
f(i+2)=f(i)+f(i+1);
i=i+1;
end
plot(f)
This script file is saved under the name " fibno.m ".
To execute this file, simply type fibno at the MATLAB prompt.
- Functions
A function differs from a script in that arguments may be passed, and
variables inside the file are local to the function. A function file
is recognized when the first line of an M-file contains the word "function ".
Example:
function y=mean(x)
%MEAN Average or mean value. For vectors, MEAN(x)
%returns the mean value. For matrices, MEAN(x) is
%a row vector containing the mean value of each column.
m,n:=size(x);
if m == 1
m = n; %Handle isolated row vector
end
y=sum(x)/m;
This is saved as " mean.m ". The mean value function can be used by
typing:
where Z is a vector of the integers from 1 to 99:
FOR loops
MATLAB has its own version of the "DO" or "FOR" loop like other computer
languages. MATLAB's for loop allows a statement, or group of statements,
to be repeated a fixed number of times.
The general form of a for loop is:
for v = expression
statements
end
WHILE loops
MATLAB's while loop allows a statement, or a group of statements to be
repeated under control of a logical condition.
The general form of a while loop is:
while expression
statements
end
IF and BREAK statements
The general form of an if statement is:
if expression
statements
end
if expression
statements
elseif expression
statements
else
statements
end
Here are some of the commands and functions that you can use
with MATLAB:
COMMAND - DEFINITION
help - on-screen help facility.
who - list current variables in memory.
whos - lists current variables and thier sizes
exit - exit MATLAB.
quit - exit MATLAB.
Ctrl-C - stop execution of current command.
save - save variables on matlab.mat
load - restore variables from matlab.mat
what - show M-files on disk
* - multiplication
^ - raise to a power
, - conjugate transpose
\ - left division
/ - right division
... - continue statement to the next line
pi - 3.14156
i,j - square root of -1
plot(x,y) - linear X-Y plot
loglog(x,y) - loglog X-Y plot
semilogx(x,y) - semi-log X-Y plot
semilogy(x,y) - semi-log X-Y plot
mesh(x,y)or(z) - 3-dimensional mesh surface
title - plot title
xlabel and ylabel - label x and y axis respectively
clg - clear graph screen
print - send graph to default printer
meta - graphics metafile
eye(m,n) - generate identity matrix
ones(m,n) - generates matrix of all ones.
zeros(m,n) - generates matrix of all zeros.
rand(m,n) - generates a random matrix.
hilb(m,n) - generates the Hilbert matrix.
magix(n) - generates a magic square.
inv(A) - matrix inverse.
pinv(A) - pseudoinverse.
rref(A) - reduced row echelon form
eig(A) - eigenvalues
det(A) - determinant.
rank(A) - rank.
For More Information
Contact the Information Center at 646-1840
Last Modified: Sunday January 13, 2008
webmaster@nmsu.edu