Professional Documents
Culture Documents
Laboratory 1
Introduction to MATLAB
Check
web site
www.mathworks.com.
At this
sitea check
outintroduction
the features available
In this
lab,out
wethebegin
to use
MATLAB. This lab
gives
simple
to MATLAB
for
this
software
package,
and
the
available
shareware
files
that
can
also
be downloaded.
along with specific questions. MATLAB Primer, that you can find on the
course website, is
a great source of information on MATLAB functions and programming. The following
The following videos found in http://www.mathworks.com/products/matlab/demos.html
linkswill
give
Tutorials with numerous examples and videos:
beMATLAB
of great help:
http://www.mathworks.com/help/matlab/getting-started-with-matlab.html
Getting
Started with MATLAB
Get started with MATLAB and learn how to get more information.
http://www.mathworks.com/help/matlab/
The Writing
lab assignments
a MATLAB should
Programbe neatly completed and turned in as a report with all
questions
The report
needcreating
not bea script
typedand
but
should be clear and easy to
Write aanswered.
MATLAB program,
including
a function.
follow. All graphs and other MATLAB results should be annotated (it is acceptable to
Importing
Data from
Files on the printouts) with the question number to which they
annotate
by simply
writing
Import
data
from
spreadsheets,
text files, and other formats into MATLAB.
refer.
Creating a Basic Plot Interactively
1. Learn
invoke MATLAB
familiarizedesktop
yourself
with the help command.
Createhow
plots to
interactively
from withinand
the MATLAB
environment.
Typing help topic gives help on specific MATLAB function. Use the help command to
Basic
Functions
learnUsing
about
thePlotting
diary
command. Use the diary command to produce a text file
Create
plots
programmatically
using basic
plotting functions.
containing the results of typing help
help.
Working with Arrays
2. BeCreate
sure and
to try
some ofMATLAB
the built-in
demos
in MATLAB.
demo
toindexing.
see the choices.
manipulate
arrays,
including
accessing Type
elements
using
Try out on basic MATLAB commands as convolution.
3. Scan the above links to see videos and examples on the following topics:
2.1 Basic Matlab commands
Getting Started
Variables
Type the given statements into
the command window or create an *.m file through the
File, New, M-file on the menu
bar and then run the m-file through Debug, Run. You can
Matrices
also type the filename in Matlab
Command Window.
Functions
! 2 $
! 6 $
x=#
&, y = #
&
" 4 %
" 8 %
(b) Using the computer, issue the following MATLAB commands:
i. x ! y, , ii. x, ! y , iii. x.! y
(c) Answer the following question:
Justify the dimension of the matrix produced by each of the above commands.
5. In this exercise, we will learn to plot continuous-time (CT) signals by creating a
MATLAB vectors of samples of the signals.
5.1 Let x ( t ) = Asin (! 0t ) , where A = 5 volts and ! 0 = 12 rad/sec. We wish to form the
MATLAB vector of samples of x ( t ) as
x [ n ] = x ( nTs ) ,
where Ts (sampling interval) is chosen such that we get 12 samples/period of x ( t ) .
The period of x ( t ) is denoted as T0 .
(a) Plot this signal in the time interval 0 ! t ! 5 sec, using a time increment Ts = 0.01
sec. Label both axes and title the plot.
(b) Plot the signal y ( t ) in the time interval !2 " t " 3 seconds using a time increment
Ts = 0.01 sec. Note that the signal to plot is the same as the signal in part (a). The
difference is that we need to plot the signal starting at a negative time instant. To deal
with this problem apply the truncation trick used in the following program
t = [-2: Ts :3];
w = (t >=0);
y 1 = y .*w;
plot(t, y 1);
(c) Plot the signal
z ( t ) = ( e!3t ! e!6t ) {u ( t ) ! u ( t ! 1)}
in the time interval !2 " t " 3 seconds using a time increment Ts = 0.01 sec.
Hint: Use the above program with
w = ((t >=0)&(t <=1)),
where & is the logical and operator.
6. In this part, we will learn to write MATLAB script files and MATLAB functions.
6.1. Script Files
Using the text editor of your choice, create a file named example1.m.
This will be a MATLAB script file. A script file is very simple: it contains ordinary
MATLAB commands that you will later execute in MATLAB. The script file you create
should contain the following MATLAB commands
t = [-5:0.01:5];
x = exp(-abs(t)).*cos(2*pi*t);
plot(t,x);
xlabel(time);
ylabel(amplitude);
title(x(t) = exp(-|t|)cos(2*pi*t));
It is important that the filename you created end in .m and that you use a text editor.
On PCs, save the file to g:\\example1.m. On the UNIX machines, save it to
example1.m.
Start MATLAB. At the MATLAB prompt, type in the name of the file you created
above, except for the .m part:
>> cd g: (PC only)
>> example1
If you typed it in correctly, you should see a plot of x ( t ) = e! t cos ( 2" t ) , !5 " t " 5 .
If you get errors, fix them in the file (example1.m), save the file, and then return the
script file until you get the plot. This is the technique for writing things in
MATLAB that you want to save and run later: save the commands in script files. See
also the following video http://www.youtube.com/watch?v=NPd13u4i6fM .
6.2. Function Files
MATLAB functions are very similar to MATLAB script files. In this section, we will
create a text file that will be a MATLAB function. The function file you create should
be named example2.m. This file should contain the following MATLAB commands:
function x = example2(t)
x = exp(-abs(t)).*cos(2*pi*t);
return;
Start MATLAB and enter the following commands at the MATLAB prompt:
>> help example2
>> t = [0:0.01:6];
>> plot(t,example2(t));
If you did everything correctly, you should see the help text in response to help
example2 . Notice that we have added a new command to MATLAB that can be
used as if it were a built-in function. If you get errors, you must correct the file
example2.m, save it, and then return the commands above.
The MATLAB on-line help system has a nice write-up of functions and how to handle
various things like returning more than one value, checking number of arguments,
size of the defined vector, etc.
Please type the following commands and read the online help:
>> more on
>> help function
>> help script
# 2, -1 ! t < 2
%
% 1, 2 ! t < 3
x (t ) = $
.
% "0.5, 3 ! t ! 6
% 0,
else
&
(a) Plot this signal with proper labeling.
(b) Create the following new signal and generate its plot g ( t ) = x ( 2t ! 5 ) .
(c) Create the following new signal and generate its plot h ( t ) = x ( !4t + 2 ) .
7.2. Create new MATLAB functions for the following signals:
"$ 1, t ! 0
"$ t, t ! 0
u (t ) = #
, r (t ) = #
.
$% 0, else
$% 0, else
These are step and ramp functions.
(a) Plot these signals with proper labeling.
(b) Create the following new signal and generate its plot
f ( t ) = 2r ( t + 2.5 ) ! 5r ( t ) + 3r ( t ! 2 ) + u ( t ! 4 )
for !5 " t " 5 . In MATLAB use t=[-5:0.01:5]for the time-axis, use ramp(t)
for r ( t ) and step(t) for u ( t ) .
(c) Create and plot signals being the even and odd components of f ( t ) .
7.3. Define the following signal
# t, 0 ! t < 1
%
z ( t ) = $ e"5(t"1) , 1 ! t < 2.5 .
%
else
%& 0,