Introduction To MATLAB: Experiment-1
Introduction To MATLAB: Experiment-1
Experiment-1
Introduction to MATLAB
Prepared By
Mohammed Abdul Kader
Assistant Professor, Dept. of EEE, IIUC
Objectives:
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.)
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
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Audio Vector
Signal {a1,a1,a3,a4,…………….}
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.)
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.
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.)
S D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Matrices and Access to matrix elements
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)].
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.)
>> x = linspace(0,360);
>> y1=sind(x);
>> y2=cosd(x);
>> plot(x,y1,x,y2)
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')
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC