CSC567 - Lab 1 Introduction To MATLAB
CSC567 - Lab 1 Introduction To MATLAB
LAB 1
INTRODUCTION TO MATLAB
MATLAB, which stands for Matrix Laboratory, is a compelling program for performing numerical and symbolic
calculations. It is widely used in science and engineering, as well as in mathematics. The basics of the technical
language of MATLAB is a technical language to ease scientific computations, and the name derived from Matrix
Laboratory. It provides many of the attributes of spreadsheets and programming languages. MATLAB is a case
sensitive language (a variable named “c” is different than another one called “C”). In interactive mode, MATLAB
scripts are platform independent (right for cross-platform portability). MATLAB works with matrices. Everything
MATLAB understands is a matrix.The MATLAB environment shown in Figure 1.
Figure 1
• Command window: This is the main window. It is characterized by the MATLAB command
prompt “>>.” When you launch the MATLAB application, the program starts with window and all
commands, including those for user-written programs, are typed into this window at the MATLAB
prompt.
• Graphics window: The output of all graphics commands typed in the command window are flushed to
the graphics or figure window, which is a separate gray window with a white background color. The
user can create as many windows as the system memory will allow.
• Edit window: This is where you write, edit, create, and save your programs in files called M
files.
1
CSC567 LAB HANDOUT
• Input–output: MATLAB supports interactive computation taking the input from the screen and
flushing the output to the screen. Also, it can read input files and write output files.
All programs and commands can be entered either in the command window or in the M file using
the MATLAB editor; then you can save all M files in the folder “work” in the current directory.
a. This instruction indicates a vector Y which as initial value 0 and final value 20 with an increment of 2.
Y = 0:2:20
Y = [0 2 4 6 8 10 12 14 16 18 20]
b. This instruction indicates a vector M which as initial value 40 and final value 100 with an increment of 5.
M = 40:5:100
M = [40 45 50 55 60 65 70 75 80 85 90 95 100]
c. This instruction indicates a vector N which as initial value 0 and final value 1 with an increment of 1/pi.
N = 0: 1/pi: 1
N = [0, 0.3183, 0.6366, 0.9549]
d. Creates a vector of one row and five columns whose values are zero Output = [0 0 0 0 0].
zeros (1,5)
ones (2,6)
Output = 1 1 1 1 1 1
1 1 1 1 1 1.
a = [2 2 –5]
b = [3 5 4]
a*b = [6 10 –20]
2
CSC567 LAB HANDOUT
h. Plot discrete sequence data. It plots the data sequence, Y, at values specified by X. The X and Y inputs
must be vectors or matrices of the same size.
stem (x, y)
X = linspace(0,2*pi,50)';
Y = cos(X);
figure
stem(X,Y)
k. ABS(X) is the absolute value of the elements of X. When X is complex, ABS(X) is the complex modulus (magnitude) of the elements
of X.
ABS Absolute value.
3
CSC567 LAB HANDOUT
p. Quits MATLAB.
Quit
L=length(t);
for i=1:L
if t(i)<0
x1(i)=0;
x2(i)=0;
else
x1(i)=1;
x2(i)=t(i);
end;
end;
figure;
plot(t, x1);
xlabel('t');
ylabel('amplitude');
title('unit step');
grid
4
CSC567 LAB HANDOUT