You are on page 1of 7

Summer 2007

DIGITAL SIGNAL PROCESSING


Universidad Catlica de Bolivia

LAB #1: INTRODUCTION TO MATLAB


In this lab, MATLAB is introduced through simple tasks. MATLAB is a very useful computing software
for Electrical Engineers. You will learn how to use built-in functions and how to make your own
functions. In addition, the powerful plotting capabilities of MATLAB will be shown.
1.

Starting MATLAB

In your computer, you will see an icon, which is denoted by MATLABx.x. Double click the MATLAB
icon at the Window. Main window for commands will be open and a prompt of << will be given to you.
All commands will be entered after the prompt.
2.

Simple built-in functions (or commands)

There are many useful built-in functions in MATLAB designed to support computation. As an example,
you will learn how to use some of those built-in functions.
2.1 To evaluate x= je j / 4 , type the followings, >> x = j*exp(j*pi/4) and push the Enter key.
You will have the answer for your equation je j / 4 . Note that pi is the reserved keyword for
mathematical notation and j is the reserved keyword for

1 in the MATLAB. In

addition, exp is the reserved keyword for exponential function.


Ans: -0.7071 + 0.7071i
2.2 To calculate (1-j)5, type the followings, >> y = (1-j)^5 . And push the Enter key.
Ans: -4.0000 + 4.0000i
2.3 Type who followed by the Enter key at the prompt line. Write down what you have. Note
that MATLAB stores the variables that you used. The who function is a built-in command (or
function) to display the stored variables.
Ans: Your variables are:

ans x

2.4 Type x followed by the Enter key at the prompt line. Note how you can call the stored
variables in the MATLAB.
Ans: -0.7071 + 0.7071i
2.5 Type clear x followed by the Enter key at the prompt line. And type who followed by the
Enter key at the prompt line. Note how you can clear a variable. (If you use clear all, it will
delete all stored variables)
Ans: Your variables are:
ans y
3.

Vectors and matrices

Vectors and matrices can be used like variables in MATLB. As an example, you will see how to set a
vector and a matrix and how to access their elements.
3.1 To generate the values of

f (t ) 10e j 3 t for t ranging from 0 to 1 second in 0.001

increment, type the followings. Note the : operator for increment. And the ; operator at
the end of line is used not to display the result.
>> t = 0:0.001:1;
>> f = 10*exp(j*3*pi*t);
3.2 Now, you have vectors, t and f. To access an element of the vector, t, type the followings >>
t(1) . Note that the element index starts from 1 (NOT from 0) in MATLAB. (i.e., 0 is NOT
allowed for an index number for a vector or matrix)
Ans: 0
3.3 Type the followings >> t(10)) .
Ans: 0.0090
3.4 To plot the function f, type the followings >> plot(f) . Note that another window is opened to
plot the given variable, f. Draw it in the following space.

4.

To get a help in MATLAB

As you can realize, the function (or command) plot is also a built-in function. To see how to use those
built-in functions or what kinds of built-in functions are available, you can use help command or doc
command as follows:
>> help plot
>> doc plot
Note that doc command will open a new window denoted by help window. In the help window, you
can have all documents related to MATLAB including the information how to use built-in functions and
what kinds of built-in function are available.
5.

Making your own function.

In MATLAB, you can make your own functions. Whenever you need specific functions for your own
tasks, you can make it with many built-in functions. You will see how to make it.
5.1 Make your own directory under the directory C:
5.2 To change into your directory, type >> cd 'c:\your directory name'
5.3 Open an editing window (i.e., choose File->New->M-file in the main MATLAB window
menu)
5.4 Now, a new window is opened. At the editing window, type the followings. Note the keyword
function to make a function. In the following function called myfunction has an input of t
and outputs of f1 an f2.
function [f1, f2] = myfunction(t)

f1 = cos(2*pi*t);
f2 = f1 + sin(3*pi*t);
figure(1)
plot(f1);
figure(2)
plot(f2);
5.5 Save the above function into your directory made in the step 5.1. (i.e., File->Save as>myfunction.m at the menu in editing window). Note that the extension name of a function in
MATLAB is ????.m.
5.6 To generate input vector t, type >> t = 1:-0.01:0; at the main window of MATLAB.
5.7 To run (or call) your function, type >> [f1, f2] = myfunction(t); at the main window of
MATLAB. Note that two figure windows called Figure No 1 and Figure No 2 will be
opened to display the function f1 and f2. You can open a new window for figure and put your
figure number by the built-in function figure(your own number) as shown in the above
example.

Note: If you use built-in functions related to draw figures such as subplot, title, legend etc, you
can do more useful tasks for drawing.

6.

Lab report

For a Lab report, do the following problems using MATLAB. Print out all your supporting documents
such as your programs, figures etc.
6.1 For a given input x, make one function file to plot three functions f1, f2, and y such as

f 1( x, y ) 3 sin( x 2 ) 2 cos( y 3 ) ,

f 2( x, y ) 3 cos( xy ) 2 y 2 ,

and

y 0.2 x 1.05 . Call your function with an input x ranging from 0 to 5 in 0.001

increments.

6.2

For a given input t, make one function file to plot the function y such as

y (t ) (1 e 2.2t ) cos(60 t ) . Call your function with an input t ranging from 0 to 0.25
in 0.001 increments. (Hint: you have to use .* to do a element by element multiplication. In
other words, if you want to do element by element operation, you have to use . (dot) operator
with the operator you want.)

You might also like