0% found this document useful (0 votes)
24 views32 pages

Introduction To MATLAB: Experiment-1

Uploaded by

Md Al Amin Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views32 pages

Introduction To MATLAB: Experiment-1

Uploaded by

Md Al Amin Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

International Islamic University Chittagong

Dept. of Electrical and Electronic Engineering

Experiment-1
Introduction to MATLAB

EEE-3604 Digital Signal Processing Sessional

Prepared By
Mohammed Abdul Kader
Assistant Professor, Dept. of EEE, IIUC
Objectives:

a) To familiarize with MATLAB and some basic commands of MATLAB.


b) To know about variable and variable types in MATLAB.
c) To know about Matrix manipulation in MATLAB.
d) To learn about plotting 2D graphs in MATLAB.
e) To become familiar with MATLAB script/editor.
f) To become familiar with conditional operators and loops in MATLB.
g) To learn about debugging program in MATLAB.
h) To know how to develop an user defined function in MATLAB

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
What is MATLAB?
 MATLAB Stands for MATrix LABoratory.
 MATLAB is a programming and numeric computing environment used by millions of engineers and
scientists to analyze data, develop algorithms, and create models.
 It has many in built functions, tool boxes (signal and image processing, control systems, wireless
communications, computational finance, robotics, deep learning and AI etc) and apps.

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
History of MATLAB
 Invented by Prof. Cleve Moler (American mathematician and computer programmer specializing in
numerical analysis.) to make programming easy for his students.
>> Late 1970.
>> University of New Mexico.
 The MathWorks, Inc. was formed in 1984
>> By Moler and Jack Little.
>> One Product: MATLAB.
 Today
>> 100 products
>> As of 2020, MATLAB has more than 4 million users worldwide.
>> Taught in 5,000 universities (2015).
 MatlabVersion (Release history)
>> 1st version: MATLAB 1.0 in 1984.
>> Latest Version: MATLAB 9.9 (September 17, 2020).

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Some useful MATLAB Commands

>> version % this will tell you the running MATLAB version
ans = 9.0.0.341360 (R2016a)
>> help % lists available packages/toolboxes on system.
>> help elfun % lists functions in elementary functions package
>> help sin % instructions on the sine function
>> lookfor sine % if you don’t know the function name …
>> doc % start matlab help documentation
>> doc sin % for full details of function
>> Ctrl+C (Press ‘Ctrl+C’ to stop execution of instruction)
>> quit % to quit MATLAB
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Some useful MATLAB Commands (Cont.)

>> format loose % line space increased in command window


>> format compact % line space decreased in command window
>> format long % displays more digits after decimal points
>> format short % displays less digits after decimal points
Note: ‘format long/short’ has no effect on accuracy during the calculation. The
commands just show larger or smaller no of digits after decimal point in the
display.
>> exist(‘name of variable/function’) % Check if variables or functions are
defined.

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Preview

Current Directory

Command Window

Workspace

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Variables
 Don’t have to declare type, is case sensitive
 variable begins with a letter, e.g., A2z or a2z
 can be a mix of letters, digits, and underscores (e.g., vector_A)
 Variable name can be up to 63 characters
 Don’t even have to initialise
 Just assign in command window
>>
Try the same line without the
>> a=12; % variable a is assigned 12 semicolon and comments

comment operator
Matlab prompt
assign operator suppress command output

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Size of Variables

 All numerical variables in MATLAB are matrices, a mathematical data type


corresponding to a two-dimensional array of numbers.
>> m=3;
>> size(m) Remember these terms
ans = Scalar: Single element variable like 1,5,42 etc.
Vector: If you group (row or column wise) a number of scalars
1 1 together you end up with a vector.
>> a=[1,2,3]; Example: a=[1, 2, 3];
b=[6 ,7 ,8];
>> size(a) Matrix: A list of equal sized vector.
ans = Example: A= [a;b]
Tensor (Array): Three or more dimensional matrix
1 3 Example: Color Picture.

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Audio Vector
Signal {a1,a1,a3,a4,…………….}

Gray Scale Image


Color Image

Matrix

Tensor

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Workspace
• The workspace is Matlab’s memory.
• Displaying contents of workspace.
>> a=12;
>> b=10;
>> c=a+b;
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x1 8 double
c 1x1 8 double
• Delete variable(s) from workspace
>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos
>> clc % clear command window (workspace remain unchange)
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Workspace (Cont.)

>> save % save workspace variable to current directory. (before closing


data one can save data for using in the next session)
>> load % reload data.
>> save my_file a b % create a new file named ‘my_file’in current
directory and save variable a and b in that file.
>> load my_file % load data from my_file to workspace.

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Numeric Variable Types
Floating Point Numbers (Double Precision and Single precision)
Integer Numbers (signed and unsigned integer of 8,16,32,64-bits)
Floating-Point Numbers

MATLAB® represents floating-point numbers in either double-precision or single-precision format. The default is double precision.

Double-Precision Floating Point


 MATLAB constructs the double-precision (or double) data type according to IEEE® Standard 754 for double precision.
 Any value stored as a double requires 64 bits.

Bits Usage
63 Sign (0 = positive, 1 = negative)
62 to 52 Exponent, biased by 1023
51 to 0 Fraction f of the number 1.f 𝑽𝒂𝒍𝒖𝒆 = (−𝟏)𝑺 × 𝟐𝑬−𝒃𝒊𝒂𝒔 × (𝟏. 𝒇)𝟐
realmax or realmax(‘double’)
realmin or realmin(‘double’) 𝑽𝒂𝒍𝒖𝒆 = (−𝟏)𝑺 × 𝟐𝑬−𝟏𝟎𝟐𝟑 × (𝟏. 𝒇𝟓𝟏 𝒇𝟓𝟎 … 𝒇𝟎 )𝟐
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
𝑽𝒂𝒍𝒖𝒆 = (−𝟏)𝑺 × 𝟐𝑬−𝟏𝟎𝟐𝟑 × (𝟏. 𝒇𝟓𝟏 𝒇𝟓𝟎 … 𝒇𝟎 )𝟐
Numeric Variable Types (Cont.)
Single-Precision Floating Point
 MATLAB constructs the single-precision (or single) data type according to IEEE Standard 754 for single precision.
 Any value stored as a single requires 32 bits.

Bits Usage
31 Sign (0 = positive, 1 = negative)
30 to 23 Exponent, biased by 127
22 to 0 Fraction f of the number 1.f 𝑽𝒂𝒍𝒖𝒆 = (−𝟏)𝑺 × 𝟐𝑬−𝒃𝒊𝒂𝒔 × (𝟏. 𝒇)𝟐
𝑽𝒂𝒍𝒖𝒆 = (−𝟏)𝑺 × 𝟐𝑬−𝟏𝟐𝟕 × (𝟏. 𝒇𝟐𝟐 𝒇𝟐𝟏 … 𝒇𝟎 )𝟐

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Numeric Variable Types (Cont.)

Integer Type Variables (Signed and Unsigned)


int8 8-bit signed integer arrays Related functions:
int16 16-bit signed integer arrays intmax(‘type’), Example: intmax(‘uint8’)
int32 32-bit signed integer arrays intmin(‘type’), Example: intmin(‘uint8’)
int64 64-bit signed integer arrays
uint8 8-bit unsigned integer arrays
uint16 16-bit unsigned integer arrays
uint32 32-bit unsigned integer arrays
uint64 64-bit unsigned integer arrays

S D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0

Range: −27 𝑡𝑜 27 − 1 ≫ −128 𝑡𝑜 + 127 Range: 0 𝑡𝑜 28 − 1 ≫ 0 𝑡𝑜 255

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Matrices and Access to matrix elements

 Don’t need to initialise type, or dimensions  Remember Matrix(row,column)


>>A = [3 2 1; 5 1 0; 2 1 7]  Naming convention: Matrix variables
start with a capital letter while vectors
A=
square brackets to or scalar variables start with a simple
3 2 1 define matrices letter.
5 1 0
semicolon for next row in matrix
2 1 7
 Access elements of a matrix
>> A = [3 2 1; 5 1 0; 2 1 7]
>>A(1,2)
ans= 2

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
The colon (:) Operator and Matrices (Accessing Parts of Matrix)
 The colon is one of the most useful operators in MATLAB. It can create vectors, subscript arrays,
and specify for iterations.
Use of colon (:) operator >> 1:10
 Create Unit-Spaced Vector ans =
1 2 3 4 5 6 7 8 9 10
i:k

>> 1:2:10
 Create Vector with Specified Increment ans =
i:j:k 1 3 5 7 9

 Subscript vector- A(j:k) equivalent to the vector [A(j), A(j+1), ..., A(k)].

>> m=[2 6 3 1 8 9 0 2 4];


>> p=m(2:7)
p=6 3 1 8 9 0
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
The colon (:) Operator and Matrices (Accessing Parts of Matrix)
 Index Matrix Rows and Columns
• A(:,n) is the nth column of matrix A.
• A(m,:) is the mth row of matrix A.
• A(:) reshapes all elements of A into a single column vector. This has no effect if A is already a
column vector.
• A(:,j:k) includes all subscripts in the first dimension but uses the vector j:k to index in the
second dimension
>> A(:)
>>A(:,2) >>A(3,2:3) ans =3 What’ll happen if you type
A=
ans = 5 A(:,:) ?
3 2 1 ans =
2
2
5 1 0 1 7 2 A(1:end, 1) ?
1 1
2 1 7 1 >> A(3,:)
1 A(end-1, end-2) ?
ans = 1
2 1 7 0 sum(A(:))
7
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Manipulating Matrices
>> A ' % transpose
>> B*A % matrix multiplication
>> B.*A % element by element multiplication (Array Multiplication)
>> B/A % matrix division
>> B./A % element by element division (B over A)
>> B.\A % element by element division (B under A)
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
A= B=
3 2 1 1 3 1 Task: Create matrices A and B and try out
5 1 0 4 9 5 the matrix operators in this slide
2 1 7 2 7 2
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Graphics

 Line plot
 Bar graph
 Surface plot
 Contour plot
 MATLAB has 2D, 3D visualization tools as well as
other graphics packages.

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Graphics: 2D-Line Plot
plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.
 If X and Y are both vectors, then they must have equal length. The plot function plots Y versus X.
 If X and Y are both matrices, then they must have equal size. The plot function plots columns of Y versus columns of X.
>> t = 0:pi/100:2*pi; >> A=[1 2; 3 4; 5 6; 7 8]
>> y = sin(t); >> B=[0 2; 1 3; 2 4; 3 5]
>> plot(B,A)
>> plot(t,y)
A= B=

1 2 0 2
3 4 1 3
5 6 2 4
7 8 3 5

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Graphics: 2D-Line Plot (Cont.)
plot(Y) creates a 2-D line plot of the data in Y versus the index of each value.
 If Y is a vector, then the x-axis scale ranges from 1 to length(Y).
 If Y is a matrix, then the plot function plots the columns of Y versus their row number. The x-axis scale ranges from 1
to the number of rows in Y.
>> y=rand(1,8)
>> plot(y)
>> A=magic(4)

A=

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> plot(A)

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Graphics: Line Plot (Cont.)
>> xlabel(‘t’);
>> ylabel(‘sin(t)’);
>> title(‘The plot of t vs sin(t)’);
>> axis([0,12,-10,20]); % axis([XMIN XMAX YMIN YMAX]) sets scaling for the x- and y-axes
on the current plot.
>> grid on
>> grid MINOR
>> grid off
>> axis off
>> axis on
>> close(1)
>> close all

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Graphics: 2D-Line Plot (Cont.)
plot(X,Y,LineSpec)
plot(Y,LineSpec)
LineSpec: sets the line appearance and behavior (style, marker symbol, and color)

>> x=0:100;
>> y=rand(1,length(x));
>> plot(x,y,':r','LineWidth',1.5)

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Graphics: 2D-Line Plot (Cont.)
>> t=0:0.01:1;
>> y=sin(2*pi*3*t);
>>plot(t,y,'--gs','LineWidth',2,'MarkerSize',6,'MarkerEdgeColor','b','MarkerFaceColor',[0.5,0.5,0.5])

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Graphics: 2D-Line Plot (Cont.)

Multiple Vectors in Single Plot/Combine Plots in Same Axes (Method-1)


plot(X1,Y1,...,Xn,Yn) plots multiple X,Y pairs using the same axes for all lines.

>> x = linspace(0,360);
>> y1=sind(x);
>> y2=cosd(x);
>> plot(x,y1,x,y2)

Linearly spaced vector.


linspace(X1, X2) generates a row vector of 100
linearly equally spaced points between X1 and X2.

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
MATLAB Graphics: 2D-Line Plot (Cont.)
Multiple Vectors in Single Plot/Combine Plots in Same Axes (Method-2)

hold ON holds the current plot and all axis properties, including the current color and linestyle.
hold OFF returns to the default mode whereby PLOT commands erase the previous plots and reset all axis properties
before drawing new plots.
x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)
title('Combine Plots')
hold on
y2 = sin(x/2);
plot(x,y2)
y3 = 2*sin(x);
scatter(x,y3)
hold off

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Combine Multiple Plots (Cont.)
Create Multiple Axes in Figure Using Subplots

subplot(2,1,1);
x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)
title('Subplot 1')

subplot(2,1,2);
y2 = rand(50,1);
scatter(x,y2)
title('Subplot 2')

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Combine Multiple Plots (Cont.)
Create Subplot that Spans Multiple Grid Positions

figure
subplot(2,2,1);
x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)

subplot(2,2,2);
y2 = rand(50,1);
scatter(x,y2)

subplot(2,2,[3 4]);
y3 = rand(50,1);
plot(x,y2)

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Combine Multiple Plots (Cont.)
Modify Subplot Appearance
figure
ax1 = subplot(2,1,1);
x = linspace(0,10,50);
y1 = sin(x);
plot(ax1,x,y1)
title(ax1,'Subplot 1')
ax1.FontSize = 14;
ax1.XColor = 'red';

ax2 = subplot(2,1,2);
y2 = rand(50,1);
scatter(ax2,x,y2)
title(ax2,'Subplot 2')
grid(ax2,'on')
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Combine Multiple Plots (Cont.)
Add Super Title to Grid of Subplots

subplot(2,1,1);
x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)
title('Subplot 1')

subplot(2,1,2);
y2 = rand(50,1);
scatter(x,y2)
title('Subplot 2')

sgtitle('My Subplot Grid Title')

Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC

You might also like