Forgot Password?
 

Introduction to Matlab

Introduction to Matlab




MATLAB, which stands for MATrix LABoratory, is a state-of-the-art mathematical software
package, which is used extensively in both academia and industry. It is an interactive program
for numerical computation and data visualization, which along with its programming capabilities
provides a very useful tool for almost all areas of science and engineering. Unlike other
mathematical packages, such as MAPLE or MATHEMATICA, MATLAB cannot perform
symbolic manipulations without the use of additional Toolboxes. It remains however, one of the
leading software packages for numerical computation.
In the 1960s and 1970s before the appearance of personal computers, complex and large
scale calculations were done on large mainframes using code primarily developed with
FORTRAN. As a number of related large subroutines were developed for specific
computational purposes, they were organized into public domain packages and
distributed for free. Matlab was originally created as a front end for one of these, the
LINPACK package -- a group of routines for working with matrices and linear algebra.
The primary developer, Professor Cleve Moler at the University of New Mexico,
eventually founded Mathworks, Inc., to further develop and market the product in a
commercial setting. From the original Matlab, a high powered suite of applications has
evolved.

Figure 1: Matlab GUI.

Definition of Variables
Variables are assigned numerical values by typing the expression directly, for example, typing
a = 1+2 yields: a = 3
The answer will not be displayed when a semicolon is put at the end of an expression, for example type    a = 1+2;.

MATLAB utilizes the following arithmetic operators:

+ addition
- subtraction
* multiplication
/ division
^ power operator
' transpose

Let us define a row vector with components the numbers 1, 2, 3, 4, 5 and assigning it a variable name, say x.

» x = [1 2 3 4 5]
x = 1 2 3 4 5

Note that we used the equal sign for assigning the variable name x to the vector, brackets to enclose its entries and spaces to separate them. (Just like you would using the linear algebra notation). We could have used commas ( , ) instead of spaces to separate the entries, or even a combination of the two. The use of either spaces or commas is essential!
To create a column vector (MATLAB distinguishes between row and column vectors, as it should) we can either use semicolons ( ; ) to separate the entries, or first define a row vector and take its transpose to obtain a column vector. Let us demonstrate this by defining a column vector y with entries 6, 7, 8, 9, 10 using both techniques.
» y = [6;7;8;9;10]
y =
6
7
8
9
10
» y = [6,7,8,9,10]
y =
6 7 8 9 10
» y'
ans =
6
7
8
9
10

The command whos gives all sorts of information on what variables are active.
» whos
Name Size Elements Bytes Density Complex
ans 5 by 1 5 40 Full No
x 1 by 5 5 40 Full No
y 1 by 5 5 40 Full No

A similar command, called who, only provides the names of the variables that are active.

» who
Your variables are:
ans x y
The command clear used by itself, “erases” all the variables from the memory.

MATLAB is started by clicking the mouse on the appropriate icon and is ended by typing exit or by using the menu option. After each MATLAB command, the "return" or "enter" key must be depressed.

Definition of Matrices
MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1 matrices. Therefore,
vector and matrix operations are as simple as common calculator operations.
Vectors can be defined in two ways. The first method is used for arbitrary elements:
v = [1 3 5 7];
creates a 1x4 vector with elements 1, 3, 5 and 7. Note that commas could have been used in place of
spaces to separate the elements. Additional elements can be added to the vector:
v(5) = 8;
yields the vector v = [1 3 5 7 8].

 

Built-in functions
There are numerous built-in functions (i.e. commands) in MATLAB.

Scalar Functions
Certain MATLAB functions are essentially used on scalars, but operate element-wise when
applied to a matrix (or vector). They are summarized in the table below.
sin trigonometric sine
cos trigonometric cosine
tan trigonometric tangent
asin trigonometric inverse sine (arcsine)
acos trigonometric inverse cosine (arccosine)
atan trigonometric inverse tangent (arctangent)
exp exponential
log natural logarithm
abs absolute value
sqrt square root
rem remainder
round round towards nearest integer
floor round towards negative infinity
ceil round towards positive infinity

Vector Functions
Other MATLAB functions operate essentially on vectors returning a scalar value. Some of these
functions are given in the table below.
max largest component
min smallest component
length length of a vector
16
sort sort in ascending order
sum sum of elements
prod product of elements
median median value
mean mean value
std standard deviation

Matrix Functions
Much of MATLAB’s power comes from its matrix functions.

eye identity matrix
zeros matrix of zeros
ones matrix of ones
diag extract diagonal of a matrix or create diagonal matrices
triu upper triangular part of a matrix
tril lower triangular part of a matrix
rand randomly generated matrix
size size of a matrix
det determinant of a square matrix
inv inverse of a matrix
rank rank of a matrix
rref reduced row echelon form
eig eigenvalues and eigenvectors
poly characteristic polynomial
norm norm of matrix (1-norm, 2-norm, ∞ -norm)
cond condition number in the 2-norm
lu LU factorization
qr QR factorization
chol Cholesky decomposition
svd singular value decomposition

Plotting
 By typing help plot you can see the various capabilities of this
main command for two-dimensional plotting, some of which will be illustrated below.
If x and y are two vectors of the same length then plot(x,y) plots x versus y.
For example, to obtain the graph of y = cos(x) from – π to π, we can first define the vector x with
components equally spaced numbers between – π and π, with increment, say 0.01.
» x=-pi:0.01:pi;
We placed a semicolon at the end of the input line to avoid seeing the (long) output.
Note that the smallest the increment, the “smoother” the curve will be.
1 – 2
Next, we define the vector y
» y=cos(x);
(using a semicolon again) and we ask for the plot
» plot(x,y)
At this point a new window will open on our desktop in which the graph (as seen below) will
appear.


Figure 2: Sample plot generated in Matlab.
subplot create an array of (tiled) plots in the same window
loglog plot using log-log scales
semilogx plot using log scale on the x-axis
semilogy plot using log scale on the y-axis
surf 3-D shaded surface graph
surfl 3-D shaded surface graph with lighting
mesh 3-D mesh surface

 

PROGRAMMING IN MATLAB
M-files: Scripts and functions
To take advantage of MATLAB’s full capabilities, we need to know how to construct long (and
sometimes complex) sequences of statements. This can be done by writing the commands in a
file and calling it from within MATLAB. Such files are called “m-files” because they must have
the filename extension “.m”. This extension is required in order for these files to be interpreted
by MATLAB.
There are two types of m-files: script files and function files. Script files contain a sequence of
usual MATLAB commands, that are executed (in order) once the script is called within
MATLAB. For example, if such a file has the name compute.m , then typing the command
compute at the MATLAB prompt will cause the statements in that file to be executed. Script
files can be very useful when entering data into a matrix.

Every MATLAB function
begins with a header, which consists of the following :
(a) the word function,
(b) the output(s) in brackets, (the variable a in the above example)
(c) the equal sign,
(d) the name of the function, which must match the function filename (log3 in the above
example) and
(e) the input(s) (the variable x in the above example).

Loading and Saving Data
When using MATLAB, you may wish to leave the program but save the vectors and matrices you have defined. To save the file to the working directory, type
save filename
where "filename" is a name of your choice. To retrieve the data later, type
load filename

 


Figure 3: Help section in Matlab


Conclusion
Matlab is widely used in today’s industry and has various applications to many fields particularly to that of image processing. Matlab supports a rich set of toolboxes which help to compute complex statistical analysis.

Reference