Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CSC567 LAB HANDOUT

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

1.1 Matlab windows

MATLAB works through three basic windows.

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

• Dimensioning: Dimensioning is automatic in MATLAB. No dimension statements are required


for vectors or arrays. We can find the dimensions of an existing matrix or a vector with the size and
length commands.

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.

1.2 Basic Commands in Matlab

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)

e. Creates a vector of two rows and six columns

ones (2,6)

Output = 1 1 1 1 1 1
1 1 1 1 1 1.

f. Multiplication between two vectors

a = [2 2 –5]
b = [3 5 4]
a*b = [6 10 –20]

g. Display a figure window which indicates the plot of x versus t.


x = [6 7 8 9]
t = [1 2 3 4]
figure
plot (t, x)

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)

i. Convolves vectors and b.

Conv Syntax: y = conv(a,b)


a = [2 2 –5]
b = [3 5 4]
conv(a,b)

j. Disp(X) displays an array


Disp Syntax: disp(X)

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

l. Lists variables currently in the workspace.


Who

m. Lists variables currently in the workspace with their size.


Whos

n. Clears the workspace, all the variables are removed.


Clear

o. Clears only variables x,y,z.


Clear x,y,z

p. Quits MATLAB.
Quit

1.3 Generation of Continuous signals in Matlab

a. The following is a MATLAB program to generate unit step continuous-time signal.

%generate unit Step


clc;
clear all;
close all;
t=-20:0.01:20;

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

b. The following is a MATLAB program to generate sinusoidal signal.


t=0:0.1:1;
L=length(n);
f=10; % frequency
x1=2 sin(2*pi*f*t);
stem(n, x1);
xlabel('n');
ylabel('amplitude');
title('sinusoidal signal');

You might also like