INTRODUCTION TO MATRIX LABORATORY [ MATLAB ]
The Language of Technical & Physical Computing
Engr. Arif Hussain, Dept. Of Electrical Engineering ,SIBAU
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRIX LABORATORY [ MATLAB ]
MATLAB (Matrix Laboratory) is a very powerful
programing Environment for performing
Scientific/Numerical and symbolic calculations
It is a high-performance Matrix-Based language for
Technical & Physical computing
It is an integrated development environment (IDE)
-integrates computation, visualization & programming environment
More than 5000 colleges and universities around the
world use MATLAB and Simulink for teaching and research
in a broad range of technical disciplines.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRIX LABORATORY [ MATLAB ]
MALAB is Used
Scientific & Mathematical and Computation
Algorithm Development & System Modeling
Simulation & Visualization
Analysis & Decision Making
Scientific and Engineering graphics
Interactive Programs & GUI development
Millions of engineers and scientists worldwide use MATLAB®
to analyze and design the systems and products.
Transforming Living Standards.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
GETTING STARTED WITH MATRIX LABORATORY [2016]
A Minimum MATLAB Session
The goal of this minimum session (also called starting
and exiting sessions) is to learn the first fundamental
steps:
Invoke MATLAB
Explore MATLAB Environment & some Basic
Commands
Do few simple calculations
Quit MATLAB
To enter in MATLAB double click on the MATLAB icon
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
GETTING STARTED WITH MATRIX LABORATORY [2016]
MATLAB has the following basic window components:
Command Window
– to execute commands in the MATLAB environment
Current Directory Window
– to quickly access files on the MATLAB path
Figure Window
– to display graphical output from MATLAB code
Workspace Window
– to view variable definitions and variable memory allocations
Command History Window
– displays all commands issued in MATLAB since the last session
(good for learning and verification)
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
GETTING STARTED WITH MATRIX LABORATORY [2016]
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
SOME FUNDAMENTAL MATLAB COMMANDS
command Purpose / Functionality
help displays the help for the functionality
Ver check version of installed MATLAB & List of
toolboxes
clc clears the command window and homes the cursor
clear removes all variables from the workspace.
close ALL closes all the open figure windows.
who Lists/ display the variables in the current
workspace
whos returns a structure with the fields i.e more
details
When MATLAB starts the command prompt >> appears.
All MATLAB commands are executed from this prompt.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
USING MATLAB AS A CALCULATOR & QUITING
In order to evaluate any expression type at
prompt command >>
For example, let’s suppose you want to calculate the
expression, 1 + 2×3.
>> 1+2*3 >> 2.3+4.2 >> 2*pi
ans = 7 ans = 6.5000 ans = 6.2832
You will have noticed that if you do not specify an
output variable, MATLAB uses a default variable ans,
Note that the variable ans is created (or overwritten,
if it is already existed).
To avoid this, you may assign a value to a variable or
output argument name. For example,
>> x = 1+2*3 >> 4*x
x=7 ans = 28.0000
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
MATLAB NUMERICAL EXPRESSIONS & OPERATORS
By default MATLAB returns numerical expressions as
decimals with 5 digits.
The format command is used to change the format of the
output.
Type format rat to have MATLAB return rational
expressions.
>> 5.1-3.3 >> x=2+3; >> 1+2*3
ans =
1.8000 >> y=2+3 ans = 7
>> format rat y = >> (1+2)*3
>> 5.1-3.3
ans = ans = 9
5
9/5
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
MATLAB OPERATORS & SPECIAL CHARACTERS
+ Plus; addition operator.
- Minus; subtraction operator.
* Scalar and matrix multiplication operator.
/ Division operator
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.
Colon; generates regularly spaced elements and represents
: an entire row or column.
Parentheses; encloses function arguments and array
() indices; overrides precedence.
[] Brackets; enclosures array elements.
. Decimal point.
… Ellipsis; line-continuation operator.
; Semicolon; separates columns and suppresses display.
= Assignment (replacement) operator.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
PREDEFINED MATHEMATICAL FUNCTIONS IN MATLAB
MATLAB offers many predefined mathematical functions for
technical computing.
Typing help elfun and help specfun calls up full lists
of elementary and special functions respectively.
There is a long list of mathematical functions that
are built into MATLAB. These functions are called
built-ins
Many standard mathematical functions, such as sin(x),
cos(x), tan(x), ex, ln(x), can be evaluated.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
MATHEMATICAL FUNCTIONS IN MATLAB
List of some commonly used functions, where
variables x and y can be numbers, vectors, or
matrices.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
MATHEMATICAL FUNCTIONS IN MATLAB EXAMPLES
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
Matrices are the basic elements of the MATLAB environment.
A matrix is a two-dimensional array consisting of m rows
and n columns.
Special cases: column vectors (n = 1)& row vectors (m = 1).
a11 a12 ... a1n b1
a a22 ... a2 n b
21 2
. . . . . A a1 a2 ... an
M B
. . . . .
. . . . .
am1 am 2 ... amn
bn
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
To create row vector A simply type in:
>>A = [2 0 1 4 7 1 5 6 4]
>>A(2) %% indexing
>>A(6:9) %% Colon operator
>> A(:) %%produces a column vector,
>> size(A)
1 2 3 4 5 6 7 8 9
1x9 vector
A(2) A(6:9)
>> B=A’ %% Transpose
16
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
To create a column vector B simply type in:
B = [3; 5; 0; 0; 1; 4; 9; -1; 1]
3 1
5 B(3)
B(1:4) 2
0 3
0 4
B = 1 5 9x1 vector
4 6
9 7
-1 8
19
B(6)
17
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
Practice 1: Investigate the effect of the
following commands:
V=[2 4 7 5] w=[1 3 8 9] z=[1; 1; 0; 0]
(i)diff= v – w (ii)sum = v + w (iii) v(2)
(iv) vw = [v w] (v) vw(2: 6) (vi) v’
(vii) v./w (viii) v.*w (ix) whos
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
Practice 2: Investigate the effect of the
following commands:
V=[2 4 7 5] w=[1 3 8 9] z=[1; 1; 0; 0]
(i) z’ (ii) z*v
(iii) [v; w] (iv) v*z
(v) [z; v’] (vi) z + v’
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
A matrix is an array of numbers. To type a matrix
into MATLAB you must
begin with a square bracket,
separate elements in a row with spaces or commas (,)
use a semicolon (;) to separate rows
end the matrix with another square bracket, ].
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
Matrix Indexing
>> A(2,1)
ans = 4
>> A(3,3)
ans = 9
>>A(3,3) = 0
A = 1 2 3
4 5 6
7 8 0
Elements of a matrix are accessed as A(i,j),where i≥1 & j≥1.
Zero or negative subscripts are not supported in MATLAB.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
COLON OPERATOR
The colon operator will prove very useful and
understanding how it works is the key to efficient and
convenient usage of MATLAB.
For example, suppose we want to enter a vector x
consisting of points (0,0.1,0.2,0.3,··· ,5).
We can use the command
>> x = 0:0.1:5;
Typing j:i:k defines a row vector with increment i
starting at j and ending at k.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRICES & VECTORS IN MATLAB
A = 1 2 3 Deleting Row/ Column
4 5 6
7 8 0 >> A(:,2)=[]
ans = 1 3
>> A(2,:) 4 6
ans = 4 5 6 7 0
is the second row elements of A row or a column of a matrix can
be deleted by setting it to a null
A.
vector, [ ].
>> A(:,2:3) >> B = A([2 3],[1 2])
ans = 2 3
5 6 B = 4 5
8 0 7 8
A(:,2:3) is a sub-matrix with
Extracting a submatrix B consisting
the last two columns of A. of rows 2 and 3 and columns 1 and 2
of the matrix A.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO MATRIX GENERATORS IN MATLAB
MATLAB provides functions that generates elementary
matrices.
The matrix of zeros, the matrix of ones, and the
identity matrix are returned by the functions zeros,
ones, and eye, respectively.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO BASIC PLOTTING IN MATLAB
MATLAB has an excellent set of graphic tools.
Plotting a given data set or the results of computation is
possible with very few commands.
Creating simple plots
The basic MATLAB graphing procedure, for example in 2D
1. take a vector of x coordinates, x = (x1,...,xN)
2. a vector of y-coordinates, y = (y1,...,yN)
3. The MATLAB command to plot a graph is plot(x,y).
NOTE: SIZE OF X & Y VECTOR MUST BE SAME.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO BASIC PLOTTING IN MATLAB
The vectors x = (1,2,3,4,5,6) and y = (3,−1,2,4,5,1)
produce the picture
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
>> plot(y,x)
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO BASIC PLOTTING IN MATLAB
The vectors x = (1,2,3,4,5,6) and y = (3,−1,2,4,5,1)
produce the picture
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
>> plot(y,x)
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO BASIC PLOTTING IN MATLAB
Plot the function sin(x) on the interval [0,2π]
first create a vector of x values ranging from 0 to 2π,
then compute the sine of these values, and finally plot
the result:
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
0:pi/100:2*pi yields a vector that
– starts at 0,
– takes steps (or increments) of π/100,
– stops when 2π is reached.
If you omit the increment, MATLAB automatically
increments by 1.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
ADDING TITLES, AXIS LABELS, AND ANNOTATIONS
MATLAB enables you to add axis labels and titles. For
example, using the graph from the previous example, add
an x- and y-axis labels.
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
>> xlabel('x = 0:2\pi')
>> ylabel('Sine of x');
>> title('Plot of the Sine function')
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
INTRODUCTION TO BASIC PLOTTING IN MATLAB
Multiple (x,y) pairs arguments create multiple graphs with a
single call to plot.
For example, these statements plot three related functions
of x: y1 = 2cos(x), y2 = cos(x), and y3 = 0.5∗cos(x), in the
interval 0≤x≤2π
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,'--',x,y2,'-',x,y3,':')
>> xlabel('0 \leq x \leq 2\pi')
>> ylabel('Cosine functions')
>> legend('2*cos(x)','cos(x)','0.5*cos(x)')
>> title('Typical example of multiple plots')
>> axis([0 2*pi -3 3])
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
PLOTTING IN MATLAB
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018
BASIC OPERATIONS ON SIGNALS ( LAB 2 )
Objective:
Following are the main objectives of this lab.
1. To perform basic operations of signals
Time Scaling
Time Reversal
Time Shift
2. To perform combinations of these operations on a
signal
3. To plot the signals and their operated
counterparts using Matlab
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
BASIC OPERATIONS ON SIGNALS ( LAB 2 )
Plot x(t)=sawtooth(wt), its compressed version
x(2t), and its expanded version x(0.5t).
f=1;
w=2*pi*f;
t=-5:0.01:5;
x = sawtooth(w*t);
b = 2; % Scaling Factor
x1 = sawtooth(w*b*t);
b=0.5; % Scaling Factor
x2 = sawtooth(w*b*t);
plot(t, x, 'red', t, x1,'green', t, x2, 'blue');
axis([-3 3 -5 5]);
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
BASIC OPERATIONS ON SIGNALS ( LAB 2 )
Plot function x(t)=et, and its time reversed
version x(-t).
t=-3:0.01:3;
x = exp(t);
xrev= exp(-t);
plot(t, x, 'red', t, xrev, 'blue');
axis([-3 3 0 5]);
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
BASIC OPERATIONS ON SIGNALS ( LAB 2 )
Plot function x(t)=e-3t, its time delayed x(t-1)
and its time advanced version x(t+1).
t=-3:0.01:3;
t1 = 1;
a=3;
x = exp(-a*t); % original
x1 = exp(-a*(t-t1)); %delayed
t1=-1;
x2 = exp(-a*(t-t1)); %advanced
plot(t, x, 'red', t, x1, 'green', t, x2, 'blue');
axis([-3 3 0 5]);
grid on
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
FOURIER SERIES REPRESENTATION OF SIGNALS ( LAB 2 )
Generating various complex waveforms to verify
Fourier theorem.
Following are the main objectives of this lab.
1. To verify the Fourier series representation of the
following signals (Square & Sawtooth)
2. To plot the actual signals and their
approximated counterparts using Matlab.
Fourier theorem
The Fourier theorem states that an non-
sinusoidal signal of fundamental frequency f is
in fact combination of sinusoidal signal of the
same frequency (f) and its integer multiples,
i.e., 2f, 3f, 4f, etc.
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
FOURIER SERIES REPRESENTATION OF SIGNALS ( LAB 2 )
The above sawtooth waveform is sum of all the terms of the
Fourier series, i.e., from 1 to infinite. The
trignometric Fourier series to represent the above sawtooth
waveform is given as
AT
f (t ) sin 2ft
1
sin 4ft
1
sin 4ft
1
sin 8ft ...
2 3 4
Y = sinwt + (1/2)sin2wt + (1/3) + sin3wt + …….
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
FOURIER SERIES REPRESENTATION OF SIGNALS ( LAB 2 )
f=1; % frequency of the signal
w=2*pi*f; % angular frequency
t=0:0.01:2; % total time of the signal
% Using the first 7 Fourier series coefficients expansion terms %
constructing the sawtooth waveform
a=1*sin(w*t);
b=(1/2)*sin(2*w*t);
c=(1/3)*sin(3*w*t);
d=(1/4)*sin(4*w*t);
e=(1/5)*sin(5*w*t);
f=(1/6)*sin(6*w*t);
g=(1/7)*sin(7*w*t);
y=a+b+c+d+e+f+g; % Adding the coefficient terms
plot(t,y) % Plotting the response
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
FOURIER SERIES REPRESENTATION OF SIGNALS ( LAB 2 )
The above can also be implemented using the for loop to make the things
efficient.
y=0;
f=1;
w=2*pi*f;
t=0:0.01:2;
for n=1:10
y= y + (1/n)*sin(n*w*t);
end
plot(t,y)
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
FOURIER SERIES REPRESENTATION OF SIGNALS ( LAB 2 )
The Square wave is mathematically approximated, using
trigonometric Fourier series, as
Y = sinwt+(1/3)sin3wt+(1/5)sin5wt + (1/7)sin7wt
+(1/9)sin9wt + (1/11)sin11wt +(1/13)sin13wt +
….
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
FOURIER SERIES REPRESENTATION OF SIGNALS ( LAB 2 )
% Fourier_ Square wave
f=1;
w=2*pi*f;
t=0:0.01:2;
% Using 7 terms to plot the Fourier series of a
Square wave
a=1*sin(w*t);
b=(1/3)*sin(3*w*t);
c=(1/5)*sin(5*w*t);
d=(1/7)*sin(7*w*t);
e=(1/9)*sin(9*w*t);
f=(1/11)*sin(11*w*t);
g=(1/13)*sin(13*w*t);
y = a+b+c+d+e+f+g;
plot(t,y)
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | MARCH 12, 2018
INTRODUCTION TO MATRIX LABORATORY [ MATLAB ]
The Language of Technical & Physical Computing
QUESTIONS & ANSWERS
arif.hussain@iba-suk.edu.pk
SIGNALS & SYSTEMS [Spring 2018] | MATLAB WORKSHOP | JANUARY 29, 2018