Introduction To MATLAB: ES 156 Signals and Systems 2007 Harvard SEAS
Introduction To MATLAB: ES 156 Signals and Systems 2007 Harvard SEAS
Harvard SEAS
Prepared by Jiajun Gu
Outline
• Introduction and where to get MATLAB
• Data structure: matrices, vectors and
operations
• Basic line plots
• File I/O
Where to get MATLAB
• FAS computing:
– Download from
http://fas.harvard.edu/computing/software/
– You must be on FAS network to use MATLAB
• HSEAS IT
– Maxwell Dworkin Rooms G107-G111
• Mathworks:
– Student version is affordable and complete.
What is MATLAB
• Development Environment
• Mathematical Function Library
• MATLAB language
• Application Programming Language (not
discussed today)
MATLAB Desktop
Menu and toolbar
Workspace
History Command
Matrices & Vectors
• All (almost) entities in MATLAB are
matrices
>> A = [16 3; 5 10]
• Easy to define: A = 16 3
5 10
• Vectors -
special case
–n=1 column vector
–m=1 row vector
Creating Vectors and Matrices
• Define >> A = [16 3; 5 10]
A = 16 3
5 10
>> B = [3 4 5
6 7 8]
B = 3 4 5
6 7 8
• Transpose
Matrix:
Vector : >> A=[1 2; 3 4];
>> a=[1 2 3]; >> A'
>> a' ans =
1 1 3
2 2 4
3
Creating Vectors
Create vector with equally spaced intervals
>> x=0:0.5:pi
x =
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
A(1,2:3)=[0.6068 0.4231]
Adding Elements to a Vector or a Matrix
>> A=1:3 >> C=[1 2; 3 4]
A= C=
1 2 3 1 2
>> A(4:6)=5:2:9 3 4
A= >> C(3,:)=[5 6];
1 2 3 5 7 9 C=
1 2
>> B=1:2 3 4
B= 5 6
1 2
>> B(5)=7; >> D=linspace(4,12,3);
B= >> E=[C D’]
1 2 0 0 7 E=
1 2 4
3 4 8
5 6 12
Graphics - 2D Plots
plot(xdata, ydata, ‘marker_style’);
For example: Gives:
>> x=-5:0.1:5;
>> sqr=x.^2;
>> pl1=plot(x, sqr, 'r:s');
Graphics - Overlay Plots
Use hold on for overlaying graphs
So the following: Gives:
>> hold on;
>> cub=x.^3;
>> pl2=plot(x, cub,‘b-o');
Graphics - Annotation
Use title, xlabel, ylabel and legend for
annotation
>> title('Demo plot');
>> xlabel('X Axis');
>> ylabel('Y Axis');
>> legend([pl1, pl2], 'x^2', 'x^3');
Graphics - Annotation
Graphics-Stem()
• stem()is to plot discrete sequence data
• The usage of stem() is very similar to
plot() cos(n/4)
1
>> n=-10:10;
>> f=stem(n,cos(n*pi/4)) 0.5
>> title('cos(n\pi/4)')
>> xlabel('n') 0
-0.5
-1
-10 -5 0 5 10
n
subplots
• Use subplots to divide a plotting window
into several panes. Cosine Sine
1 1
-1 -1
0 5 10 0 5 10
Save plots
• Use saveas(h,'filename.ext') to save a
figure to a file.
Useful extension types:
bmp: Windows bitmap
>> f=figure;
emf: Enhanced metafile
>> x=-5:0.1:5;
eps: EPS Level 1
>> h=plot(x,cos(2*x+pi/3));
fig: MATLAB figure
>> title('Figure 1');
jpg: JPEG image
>> xlabel('x');
m: MATLAB M-file
>> saveas(h,'figure1.fig')
tif: TIFF image, compressed
>> saveas(h,'figure1.eps')
Workspace
• Matlab remembers old commands
• And variables as well
• Each Function maintains its own scope
• The keyword clear removes all variables
from workspace
• The keyword who lists the variables
File I/O
• Matlab has a native file format to save and
load workspaces. Use keywords load and
save.
• In addition MATLAB knows a large number
of popular formats. Type “help
fileformats” for a listing.
• In addition MATLAB supports ‘C’ style low
level file I/O. Type “help fprintf” for more
information.
Practice Problems
• Plot the following signals in linear scale
x(t ) sin( 3t ) 5 t 5
y (t ) e 2t 3 0t 5
• Plot the following signals, use log scale for y-axis
x(t ) et 2 (2t 1) 0 t 10
• Plot the real part and imaginary part of the following signal
x(t ) e0.5t j (t / 3) 0 t 10
• For the signal in previous question, plot its phase and magnitude