Introduction
MATLAB (Matrix Laboratory),
a product of Mathworks, is a scientific software package designed to provide integrated numeric
computation and graphics visualization in high-level programming language.
MATLAB program consists of standard and specialized toolboxes allowing
users to take advantage of the matrix algorithm based on LINPACK1 and EISPACK2 projects. MATLAB offers interactive features allowing the users
great flexibility in the manipulation of data and in the form of matrix
arrays for computation and visualization. MATLAB inputs can
be entered at the "command line" or from "mfiles", which contains
a programming-like set of instructions to be executed by MATLAB. In
the aspect of programming, MATLAB works differently from FORTRAN, C,
or Basic, e.g. no dimensioning required for matrix arrays and no object
code file generated. MATLAB offers some standard toolboxes and many
optional toolboxes (at extra cost, of course!) such as financial toolbox
and statistics toolbox. Users may create their own toolboxes consisted
of "mfiles" written for specific applications. The original version
of MATLAB was written in FORTRAN but later was rewritten in C. You are encouraged to use MATLAB's on-line help files for functions
and commands associated with available toolboxes. MATLAB consists of a collection of toolboxes. These toolboxes contain library files called M-Files, which are also functions or command names, executable from the Command window.
Conventions Used in This Tutorial
Bold face: signifies a command or string of commands to be entered exactly as shown, a display by Matlab echo, or a Matlab reserved word, e.g., function name, command, etc. It also represents a keyboard command, e.g. CTRL+Q, or menu selection sequence when used with ">" sign.
Italic: when used with a command, it signifies an input to be specifed by the user. For example, eig(matrix name) means a user-defined matrix (variable) to be entered in place of matrix name.
|
Getting
Started
To start Matlab in an Unix or Linux shell, enter matlab at system prompt.
To start Matlab in Windows environment, select the executable file located at:
> All Programs > Matlab > R2008a > Matlab R2008a
Alternately you may double-click on Matlab icon located on the desktop.
Once Matlab's initializing process is completed, a Matlab desktop environment will appear providing a set of subwindows, a.k.a browsers. In this desktop environment you may define variables, manage files and objects, execute programs, and view command history.
A typical Matlab desktop is shown below.
Workspace window displays the defined variables. You may also list all defined variables in a Matlab session by issuing the command who in Command Window. The "traditional" Command window is where the user normally defines variables and enters Matlab pre-defined functions. You may close, restore, and resize any of these windows.
Defining Variables...
One of the easy ways to
learn MATLAB is to understand how MATLAB handles matrices. Think
in terms of arrays and vectors when you work with MATLAB. For example, an array of data
A = 1, 0, 9, 11, 5 is a 1x5 matrix, and a scalar number 9 is an 1x1
matrix. To store the array A in MATLAB, at the command prompt >> (in Command window), enter:
MATLAB will display or echo your input:
To suppress the echo, add a ";"
at the end of the input line.
To verify the size of the
input array or matrix, use the command "size" as shown below:
,
which verifies the dimension of matrix A as 1x5 (one row and five
columns).
In MATLAB, rows are separated
by ";" and columns are separated by ",". For example, a 3x5 matrix
B with the following elements:
first row: 1, 0, 9, 4, 3
second row: 0, 8, 4, 2, 7
third row: 14, 90, 0, 43, 25
would be entered in MATLAB
as follow:
Note that you may use a
space in place of the comma in separating the column entries.
You may add, subtract,
multiply, and divide matrices with simple operation in MATLAB. Please
keep in mind of the rules concerning matrix operations such as the computability
issue, e.g., you may not multiply a 2x3 matrix by a 4x2 matrix - the
dimensions must agree.
You may extract a certain
group or element from an existing matrix. Say you wish to create
a new array C from the second row of matrix B. Specify the row number
and ":" for all columns in that row as shown below:
Similarly, You may also
form a matrix from the element of an existing matrix:
Here, a square matrix D
has been created from the specified elements of matrix B.
You may also delete rows
and columns from a matrix using a pair of square brackets. For example, to delete the third column of matrix B, you simply enter
and MATLAB will return:
Note that the column 3
is excluded.
Alternately, you may define these variables (matrices) by clicking on the New Variable icon in Workspace browser as shown below.
Once a variable name is entered, you may double click on the icon associated with the variable name (in Workspace browser) to bring up the Array Editor. From the Array Editor, you may enter the values as you would in a typical spreadsheet program like Excel.

You may create a new variable from selected elements of the existing array by highlighting the group of elements you wish to use, then right-click with the cursor pointing in the selected area to bring up the context menu. Select the Create Variable from Selection choice. Matlab will assign an "unnamed" label to your new varible. To modify this, right-click the unnamed variable and select Rename from the pop-up menu.
Data from the Array Editor browser could be exchanged with OpenOffice Calc spreadsheet program via the clipboard. (OpenOffice works fine for me in both Windows and Linux environments. I don't use Excel but I believe the file I/O interface with Excel is supported by Matlab 7 and R2006a).
Working
with Arrays...
MATLAB treats arithmetic
operations on arrays in an element-by-element manner. This operation
is accomplished by including a dot or a period before the arithmetic
operator such as multiplication, division, etc. The table below gives
a list of such operators with examples of how these operators are being
used.
For examples in the
table below, the following matrices are used:
Transposing a matrix
in MATLAB involves a simple prime notation ( ' ) after the defined
matrix as shown below:
Example:

Sorting columns and rows follow the syntax: B=sort(A,dim), where dim is the dimension of the matrix with the value 1 for column; 2 for row. Matrix A is the variable specified by the user.
Example:
Sorting columns:

Note that without dim being specified, the default value is 1. The default setting is ascending order. The variable name of the sorted matrix can be omitted if no needed.
Sorting column in descending order:

Sorting row in descending order

The inverse of matrix A can be obtained
with the command:

Eigenvalues and Eigenvectors can easily be obtained with the
command [V,E]=eig(matrix name):
where matrix V consists
of Eigenvectors . The corresponding Eigenvalues are shown in matrix
E. Eigen values alone can be obtained without the notation "[V,E]":

Command Input Assistance Feature
Not sure how a command is spelled? Too lazy to type the complete command? Well, Matlab 7 programmers have added a new feature to answer these questions. Now you may type only the first few letters of the command and use the TAB key to see what commands are available or to let Matlab complete the command typing for you. For example, if you wish to enter the command nyqchart. You may simply type nyq then press the TAB key. A list of commands with spelling close to what you enter will appear in a pop-up menu.

If the highlighted command is what you want, then another gentle stroke on the TAB key will complete the command input for you.
Notes on Format
Matlab handles floating-point numbers in either single precsion or double precision (defaut setting) format. While double precision numbers use 64 bits, single precision numbers use 32 bits, based on IEEE Standard 754. You may convert a double precision number to a single precision number using the command single(number).
MATLAB provides mathematical
expression just like most other programming languages. These
expressions are variables, numbers, operators (+, -, *, /, etc.), and
functions. To 'accommodate' EE folks either i or j can be used to
describe the imaginary number (square root of minus one). There are
standard (built-in) functions in MATLAB like sin, cos, inv, exp, sqrt,
etc. Users may also write user-defined functions to perform calculation
like a subroutine function in C or FORTRAN.
You may control the
displayed string by using the commands
format type
format ('type')
Available options for type are:
short e (scientific notation, five-digit floating point)
long e (5 digit for single precision and 15 for double precision)
short g (five digits)
long g (7 digits for single precision and 15 for double precision)
format
bank (two digit decimal)
format rat (rational)
format hex (hexadecimal)
format loose (line feed added)
format compact (line feed suppressed).
Refer to Matlab's help file (help format) for more info. Unfortunately, Matlab's number display control is not set up in the same way as in most calculators, e.g. fixing the display digits trailing the decimal. However, the user may display a number with two digits trailing the decimal by using bank format. The following examples demonstrate how Matlab format command displays numbers.
Examples:
>> w=pi/2
w =
1.5708
short g (5-digit number) is Matlab's default format.
>> format long g
>> w
w =
1.5707963267949
15-digit display indicates double-precision setting (default)
To display single-precision result, use the command single(variable) as illustrated below:
>> single(w)
ans =
1.570796
Note that ω is now truncated to a 7-digit number.
To display ω in scientific long format, use
>> format long e
Matlab display:
w =
1.570796326794897e+000
Short scientific format can be specified with
>> format short e
Matlab's answer:
w =
1.5708e+000
To check with Matlab if the results are the same for long g of 2*w and double(2*w), we could ask Matlab with the following command:
>> double(pi)==2*w
Matlab's answer:
ans =
1
which confirms that the two quantities indeed are the same! (1 for yes and 0 for no).
Equivalently, you may ask the question in a different way:
>> double(pi)~=2*w
ans =
0
It means "no, the two are not unequal!".
Bank format reduces or truncates the number to 2 digit after the decimal (dollars and cents).
>> format bank
>> w
Matlab's answer:
w =
1.57
Quitting Matlab
To quit Matlab, simply enter exit at Matlab command prompt >> or CTRL+Q (Windows). If your Matlab process is "frozen" (crashed) in Windows (XP,2000), you may kill the process from Windows Task Manager (CTRL+ALT+DEL) by selecting Matlab process and click on End Process button. In UNIX or LINUX environment, Matlab rarely crashes. In case you need to kill a Matlab session, find the process ID (PID) number and issue the command kill -9 [process ID].
______________
1LINPACK is a collection of Fortran subroutines
that analyze and solve linear equations and linear least-squares problems. The package solves
linear systems whose matrices are general, banded, symmetric
indefinite, symmetric positive definite, triangular, and tridiagonal
square. In addition,
the package computes the QR and singular value decompositions of
rectangular matrices and applies them to least-squares problems. LINPACK
uses column-oriented algorithms to increase efficiency by preserving
locality of reference. [source: NETLIB]
2EISPACK is a collection of Fortran subroutines that compute the
eigenvalues and eigenvectors of nine classes of matrices: complex
general, complex Hermitian, real general, real symmetric, real symmetric banded,
real symmetric tridiagonal, special real tridiagonal, generalized real,
and generalized real symmetric matices. In addition, two routines are included that
use singular value decomposition to solve certain least-squares
problems
[source:
NETLIB]