Laboratory Activity 1 - Introduction To MATLAB-2
Laboratory Activity 1 - Introduction To MATLAB-2
I. OBJECTIVES
1. To be familiarized with MATLAB syntax and basic commands
2. To use MATLAB on performing basic mathematical operations
II. DISCUSSION
MATLAB is a high-level language and interactive environment for
numerical computation, visualization, and programming.
Using MATLAB, you can analyze data, develop algorithms, and create
models and applications. The language, tools, and built-in math functions
enable users to explore multiple approaches and reach a solution faster than
with spreadsheets or traditional programming languages such as C/C++ or
Java.
MATLAB can be used for a wide range of applications, including signal
processing and communications, image and video processing, control
systems, test and measurement, computational finance, and computational
biology.
The name MATLAB is a contraction of “Matrix Laboratory”. MATLAB is a
product of MathWorks.
MATLAB is started by clicking the mouse on the MATLAB 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.
Variables are assigned numerical values by typing the expression directly.
Example: Type a = 1+2 in the MATLAB Command Window and then press
Enter.
The answer will not be displayed when a semicolon is put at the end of an
expression. Example: Type a = 1+2; and then press Enter.
MATLAB utilizes the following arithmetic operators:
+ addition
1
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
DIGISIG – DIGITAL SIGNAL PROCESSING
LABORATORY EXPERIMENT MANUAL
- subtraction
* multiplication
/ division
^ power operator
' transpose
A variable can be assigned using a formula that utilizes these operators
and either numbers or previously defined variables. Example: b = 2*a. Note
that a is defined previously.
To determine the value of a previously defined quantity, type the quantity
itself. Example: Type b and press Enter.
If the expression does not fit on one line, use an ellipsis (three or more
periods at the end of the line) and continue on the next line.
Note that the ellipsis must come after an operator.
Example: c = 1+2+3+...
5+6+7;
Some predefined variables which can be used in the same manner as
user-defined variables:
i as sqrt(-1)
j as sqrt(-1)
pi as 3.1416...
Example: y= 2*(1+4*j)
Some predefined functions that can be used when defining a variable:
2
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
DIGISIG – DIGITAL SIGNAL PROCESSING
LABORATORY EXPERIMENT MANUAL
Examples:
c = abs(y)
c = angle(y)
c = cos(a)
c = exp(a)
Note that exp can be used on complex numbers.
Example: c = exp(y)
MATLAB is based on matrix and vector algebra. A scalar is treated as the
element of a 1x1 matrix. Vectors can be defined in two ways.
The first method is used for arbitrary elements.
Example: v = [1 3 5 7]
Note that the previous example creates a 4-element vector with elements
1, 3, 5, and 7. Note that commas may be used in place of spaces to separate
the elements. Additional elements can be added to the vector.
Example: v(5) = 8
The previous example gives v = [1 3 5 7 8].
Previously defined vectors can be used to define a new vector.
Example: a = [9 10]; b = [v a];
The previous example creates vector b = [1 3 5 7 8 9 10].
The second method is used for creating vectors with equally spaced
elements. Example: t = 0:.1:10;
The previous example creates a 101-element vector with the elements 0,
.1, .2, .3,...,10. Note that the middle number defines the increment. If only two
numbers are given, then the increment is set to a default of 1. Example: k =
0:10;
The previous example creates an 11-element vector with the elements 0,
1, 2, ..., 10.
Matrices are defined by entering the elements row by row.
Example: M = [1 2 4; 3 6 8];
3
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
DIGISIG – DIGITAL SIGNAL PROCESSING
LABORATORY EXPERIMENT MANUAL
4
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
DIGISIG – DIGITAL SIGNAL PROCESSING
LABORATORY EXPERIMENT MANUAL
significant decimal places and format bank for placing two significant digits to
the right of the decimal.
The commands who and whos give the names of the variables that have
been defined in the workspace.
The command length(x) returns the length of a vector x and size(x) returns
the dimension of the matrix x.
M-files are macros of MATLAB commands that are stored as ordinary text
files with the extension "m", that is filename.m. An M-file can be either a
function with input and output variables or a list of commands. MATLAB
requires that the M-file must be stored either in the working directory or in a
directory that is specified in the MATLAB path list.
IV. PROCEDURE
1. Create a vector of the even whole numbers between 31 and 75.
2. Let x = [2 5 1 6].
a. Add 16 to each element
b. Add 3 to just the odd-index elements
c. Compute the square root of each element
d. Compute the square of each element
3. Let x = [3 2 6 8]' and y = [4 1 3 5]' (x and y should be column vectors).
a. Add the sum of the elements in x to y
b. Raise each element of x to the power specified by the corresponding
element in y.
c. Divide each element of y by the corresponding element in x.
5
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
DIGISIG – DIGITAL SIGNAL PROCESSING
LABORATORY EXPERIMENT MANUAL
V. QUESTIONS
1. From procedure 3, compute x'*y - w and interpret the result.
2. Make a good plot of the function f(x) = sin(1/x) for 0.01 < x < 0.1. How did
you create x so that the plot will look good?
3. Plot the expression (determined in modelling the growth of the US
population) P(t) = 197,273,000/(1 + e-0.0313(t - 1913.25)) where t is the date, in
years AD, using t = 1790 to 2000. What population is predicted in the year
2020?
6
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
DIGISIG – DIGITAL SIGNAL PROCESSING
LABORATORY EXPERIMENT MANUAL
VI. CONCLUSION
Reference:
http://www.cogsci.ucsd.edu/~ajyu/Teaching/CogSci_bootcamp08/matlab_ex.html