DSP 1
DSP 1
DSP 1
Lab no 1
Introduction to MATLAB
The objective of this lab is to introduce you to the basic operations of MATLAB. Read
through the handout sitting in front of a computer that has a MATLAB software. Practice
each new command by completing the examples and exercise. Turn-in the answers for all
the exercise problems as your lab report. When answering the problems, indicate the
commands you entered and the output displayed. It may be easier to open a word
document, and after each command you may copy and paste the commands and outputs to
the word document. Similarly, plots can also be pasted to your word document.
1. Introduction
Once the program is running, you will see a screen similar to Figure 1.3
The Command Window: is where you type commands. Hit Enter to run the command you
just typed.
18ELE09
The Current Directory: shows the directory that you are working in. This directory is
where MATLAB expects to find your files (M-files, audio, images, etc). If you encounter a
‘file not found’ error, it means the file is not in your Current Directory. You can change the
working directory by typing into it or clicking through the file browser.
The Workspace Window: displays information about all the variables that are currently
active. In particular, it will tell you the type (int, double, etc) of variable, as well as the
dimensions of the data structure (such as a 2x2 or 8000x1 matrix). This information can be
extremely useful for debugging!
The Command History Window: keeps track of the operations that you’ve performed
recently. This is handy for keeping track of what you have or haven’t already tried.
4 MATLAB Commands
4.1 Help
MATLAB has two important help commands to find the right syntax and a description of a
command with its all options.
Typing help by itself on the command line gives you a list of help topics.
If you want to find more about the syntax of a certain command, you type in
The other helpful command is lookfor. If we are not sure the exact name of the function,
we can make a search for a string which is related to the function, and MATLAB displays all
m-files (commands) that have a matching string. MATLAB searches for the string on the first
comment line of the m-file. For example:
>>lookfor inverse
will display all m-files which contain the string ‘inverse’ in the comment line.
>>whos
18ELE09
Which lists all your active variables (this info also appears in your Workspace Window), and
>> clear
Which clears and deletes all variables (for when you want to start over)?
MATLAB has tab-completion for when you’re typing long function names repeatedly. This
means that you can type part of a command and then press <Tab> and it will fill in the rest
of the command automatically. Moreover, MATLAB stores command history, and previous
commands can be searched by pressing the up or down arrow keys.
MATLAB is designed to operate efficiently on matrices (hence the name MATLAB = “Matrix
Laboratory). Consequently, MATLAB treats all variables as matrices, even scalars!
Like many other programming languages, variables are defined in MATLAB by typing:
<VariableName> = <Assignment>
MATLAB will then acknowledge the assignment by repeating it back to you. The
following are what you would see if you defined a scalar x, a vector y, and a matrix z:
>> x = 3
x=
3
>> y = [1, 2, 3]
y=
1 2 3
>> z = [1, 2, 3; 4, 5, 6]
z=
1 2 3
4 5 6
You can see from the above examples that scalar variables require no special syntax; you
just type the value after the equals sign. Matrices are denoted by square brackets [ ].
Commas separate the elements within a row, and semicolons separate different rows. A
row array, such as y, is just a special case of a matrix that has only one row.
18ELE09
Defining a Scalar:
Code:
x=1
Output:
Code:
v=[5;7;6]
Output:
Code:
v=[5 7 6]
Output:
18ELE09
Transpose a vector:
Code:
v=[5 7 6];
V=v'
Output:
Code:
v=[5:0.5:10]
Output:
Code:
v=[]
Output:
18ELE09
Defining a Matrix:
Code:
Output:
Code:
v=zeros(4,3)
Output:
Code:
v=ones(4,3)
Output:
Code:
v=eye(4)
Output:
Code:
v=rand(2,3)
Output:
Code:
v=[2 5 4; 6 9 3]
v(1,:)
Output:
Code:
18ELE09
v=[2 5 4; 6 9 3]
v(:,1)
Output:
Code:
v=[2 5 4; 6 9 3]
size(v)
Output:
Code:
v=[2 5 4; 6 9 3]
length(v)
Output:
Code:
v=[2 5 4; 6 9 3];
length(v);
x=3;
who
Output:
Code:
v=[2 5 4 6 9 3]
v=v*3
Output:
Dividing by a scalar:
Code:
v=[2 5 4 6 9 3]
v= v / 2
Output:
Code:
v=[2 5 4 6 9 3]
y=[1 2 3 4 5 6]
x=v+y
Output:
Code:
v=[2 5 4 6 9 3]
y=[1 2 3 4 5 6]
x=v.*y
Output:
18ELE09
Output:
Code:
round([1.5 2; 2.2 3.1])
Output:
Code:
v=[2 5 4 6 9 3]
sum(v)
Output:
18ELE09
Code:
v=[2 5 4 6 9 3]
mean(v)
Output:
Code:
v=[2 5 4 6 9 3]
std(v)
Output:
Code:
v=[2 5 4 6 9 3]
max(v)
Output:
18ELE09
Code:
v=[2 2 7 8 2]
int=(v==2)
Output:
Code:
v=[2 2 7 8 2]
int=(v<2)
Output:
Code:
v=[2 2 7 8 2]
18ELE09
int=(v>2)
Output:
Code:
v=[2 2 7 8 2]
int=(v<=2)
Output:
Code:
v=[2 2 7 8 2]
int=(v>=2)
Output:
Code:
v=[2 2 7 8 2]
int=(v~=2)
18ELE09
Output:
Code:
for n=0:1
x(n+1) = sin(pi*n)
end
Output:
Code:
H = zeros(3);
for k=1:3
for l=1:3
H(k,l) = 1/(k+l-1)
end
end
Output:
Code:
c = pi;
while c > 0.01 c = c/2
end
Output:
Code:
18ELE09
clc;
p= -10*pi:pi/100:10*pi;
q= sin(p)./p;
plot(p,q)
grid
Output:
Code:
clc;
p=[1 13 7 2 1];
q= p*2;
stem(p,q)
grid
Output:
18ELE09
Output:
18ELE09