DSP 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

18ELE09

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

In this first lab, we will learn how to


- perform basic mathematical operations on simple variables, vectors,
matrices and complex numbers.
- generate 2-D plots.
- use and write script files (MATLAB programs). MATLAB script files have a file
extension name .m and are, therefore, usually referred as M-files.
- Use the ‘help’ and ‘lookfor’ commands to debug codes.
-
2. Starting MATLAB

Start MATLAB by clicking on it in the Start menu.

Once the program is running, you will see a screen similar to Figure 1.3

Figure 1: The MATLAB Graphical User Interface (GUI)

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

>> help function_name


where the word function name is in the above entry is substituted by the actual name of a
function for which description is sought. For example, we can type in

>> help plot


and MATLAB will display the description, syntax and options of the plot function.

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.

Two other useful commands are

>>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.

4.2 Matrix Operations

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:

Defining a Column vector:

Code:
v=[5;7;6]

Output:

Defining a Row vector:

Code:

v=[5 7 6]

Output:
18ELE09

Transpose a vector:

Code:

v=[5 7 6];
V=v'

Output:

Defining a range for a vector:

Code:

v=[5:0.5:10]

Output:

Defining empty vector:

Code:

v=[]

Output:
18ELE09

Defining a Matrix:

Code:

v=[4 5 6;11 13 16]

Output:

Defining a Zero Matrix:

Code:

v=zeros(4,3)

Output:

Defining a Ones Matrix:

Code:

v=ones(4,3)

Output:

Defining a Identity Matrix:


18ELE09

Code:

v=eye(4)

Output:

Defining a Random Matrix:

Code:

v=rand(2,3)

Output:

Access a row or column of matrix

Code:

v=[2 5 4; 6 9 3]
v(1,:)

Output:

Code:
18ELE09

v=[2 5 4; 6 9 3]
v(:,1)

Output:

Defining a size of matrix:

Code:

v=[2 5 4; 6 9 3]
size(v)

Output:

Defining a length of matrix:


18ELE09

Code:

v=[2 5 4; 6 9 3]
length(v)

Output:

Your variables Names:

Code:

v=[2 5 4; 6 9 3];
length(v);
x=3;
who

Output:

Multiplication with scalar:


18ELE09

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:

Add Operation of two vectors:


18ELE09

Code:

v=[2 5 4 6 9 3]
y=[1 2 3 4 5 6]
x=v+y

Output:

Point by point multiplication:

Code:
v=[2 5 4 6 9 3]
y=[1 2 3 4 5 6]
x=v.*y

Output:
18ELE09

Matlab has a large number of built in functions, some operate on each


point of a vector/matrix:
Code:
log([4 6 9 3])

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

1. Relational operators in MATLAB


Relational operators: =(equal), ~=3D (not equal), etc.

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:

Use Of For Loop:

Code:
for n=0:1
x(n+1) = sin(pi*n)
end

Output:

Nested For loops:


18ELE09

Code:
H = zeros(3);
for k=1:3
for l=1:3
H(k,l) = 1/(k+l-1)
end
end

Output:

Use of while in Matlab:

Code:
c = pi;
while c > 0.01 c = c/2
end

Output:

Plot Command in Matlab:

Code:
18ELE09

clc;
p= -10*pi:pi/100:10*pi;
q= sin(p)./p;
plot(p,q)
grid

Output:

Stem Command in Matlab:

Code:
clc;
p=[1 13 7 2 1];
q= p*2;
stem(p,q)
grid

Output:
18ELE09

Subplot Command in MATLAB:


Code:
clc;
income = [3.2 4.1 5.0 5.6];
outgo = [2.5 4.0 3.35 4.9];
subplot(2,1,1);
plot(income)
subplot(2,1,2);
plot(outgo)

Output:
18ELE09

You might also like