3. MATLAB
Introducing MATLAB – Language of technical computing 2
Is an integrated technical computing
environment that combines numeric computation, advanced
graphics and visualization, and a high-level programming language.
The MathWorks, Inc.
High-level language of technical computing
Development environment for engineers, scientists
Interactive tools for design, problem solving
Mathematical function libraries
Graphics and data visualization tools
Custom GUIs
External Interfaces: C, C++, Fortran, Java, COM, Excel, .NET
4. MATLAB product family
Toolboxes: Collections of specialized Functions
Graphics
2D Graphics
3D Graphics
Color & Lighting
Animation
Image, Audio & Video
Computations
Linear Algebra
Data Analysis
Polynomials
Interpolations
Solutions of ODE
External Interfaces
C
C++
JAVA
.net
Micorosoft Excel
Parallel Computing
Math, Statistics, and Optimization
Control System Design and Analysis
Signal Processing and Communications
Image Processing and Computer Vision
Test and Measurement
Built-in Functions
User-written Functions
MATLAB
Computational Finance
Computational Biology
Code Generation and Verification
Application Deployment
Database Connectivity and Reporting
MATLAB Programming
Language
Parallel Computing Toolbox
MATLAB Distributed Computing Server
Parallel Computing Symbolic Math Toolbox
Partial Differential Equation Toolbox
Statistics Toolbox
Curve Fitting Toolbox
Optimization Toolbox
Global Optimization Toolbox
Neural Network Toolbox
Model-Based Calibration Toolbox
Math, Statistics, and Optimization
Control System Toolbox
System Identification Toolbox
Fuzzy Logic Toolbox
Robust Control Toolbox
Model Predictive Control Toolbox
Aerospace Toolbox
Control System Design and Analysis Signal Processing Toolbox
DSP System Toolbox
Communications System Toolbox
Wavelet Toolbox
Fixed-Point Toolbox
RF Toolbox
Phased Array System Toolbox
Signal Processing and Communications
Image Processing Toolbox
Computer Vision System Toolbox
Image Acquisition Toolbox
Mapping Toolbox
Image Processing and Computer Vision
Data Acquisition Toolbox
Instrument Control Toolbox
Image Acquisition Toolbox
OPC Toolbox
Vehicle Network Toolbox
Test and Measurement
Financial Toolbox
Econometrics Toolbox
Datafeed Toolbox
Database Toolbox
Computational Finance
Spreadsheet Link EX (for Microsoft Excel)
Fixed-Income Toolbox
Financial Derivatives Toolbox
Bioinformatics Toolbox
SimBiology
Computational Biology
MATLAB Coder
HDL Coder
HDL Verifier
Filter Design HDL Coder
Code Generation and Verification
MATLAB Compiler
MATLAB Builder NE (for Microsoft .NET Framework)
MATLAB Builder JA (for Java language)
MATLAB Builder EX (for Microsoft Excel)
Spreadsheet Link EX (for Microsoft Excel)
Application Deployment
Database Toolbox
MATLAB Report Generator
Database Connectivity and Reporting
Introducing MATLAB – Language of technical computing 3
6. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
5
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Conditional control statements
Loop control statements
Functions – creating & executing a function file
7. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Functions – creating & executing a function file
6
Conditional control statements
Loop control statements
8. The main window characterized by the MATLAB command prompt (>>)
Command Window
This is where you write, edit, create, and save your own script or programs in files called
m-files.
Editor Window
This is where all your files from the current directory are listed.
Current Directory
All commands typed on MATLAB prompt in the command window get recorded, even
across multiple session in this window.
Command History
This subwindow lists all variables that you have generated so far along with there type
and size.
Workspace
MATLAB - Desktop
Minimal MATLAB 7
9. MATLAB - Desktop
The main window characterized by the MATLAB command prompt (>>)
Help Window
Minimal MATLAB 8
10. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Functions – creating & executing a function file
9
Conditional control statements
Loop control statements
11. Basic Data and Variables
Computing with MATLAB
Class
Variable
Size
Value
Minimal MATLAB 10
12. Basic Data and Variables
Computing with MATLAB
Multidimensional Array
[full or sparse]
Scalar
logical
ab
char
{ }
cell struct
function
handle (@)
numeric
int8, uint8,
int16, unit16,
int32, unit32,
int64, unit64
single double
Minimal MATLAB 11
13. Arithmetic Operator &
Their Precedence
Computing with MATLAB
Operations Operators Examples
Addition
Subtraction
Multiplication
Right Division
Left Division
Exponentiation
+
−
*
/
^
>> 𝟓 + 𝟑
>> 𝟓 − 𝟑
>> 𝟓 ∗ 𝟑
>> 𝟓/𝟑
>> 𝟓𝟑 = 𝟑/𝟓
>> 𝟓^𝟑 (means 𝟓𝟑
= 𝟏𝟐𝟓)
Precedence Order Operators
1
2
3
4
Parentheses ( ). For nested parentheses, the innermost
are executed first.
Exponentiation, ^
Multiplication, *; Division, /,
Addition, +; Subtraction, -
help precedence
Minimal MATLAB 12
14. Computing with MATLAB
Mathematical Functions
Trigonometry
sind
cosd
tand
cotd
secd
cscd
Degree
sin
cos
tan
cot
sec
csc
sinh
cosh
tanh
coth
sech
csch
asin
acos
atan
acot
asec
acsc
Basic Hyperbolic Inverse
Radian
Basic
Exponential
exp
log
log10
log2
sqrt
Complex
abs
conj
imag
real
angle
Matrix
det
trace
rank
inv
expm
sqrtm
Discrete Math
factorial
gcd
lcm
primes
help <type function name>
Minimal MATLAB 13
15. Predefined Variables and
Keywords
ans
pi
eps
inf
i,j
NaN
A variable that has the value of the last expression that was not assigned to a
specific variable
The number π.
The smallest difference between two numbers. Equal to 2^(–52), which is
approximately 2.2204e–016.
Used for infinity.
Defined as −1, which is: 0 + 1.0000i.
Stands for Not-a-Number. Used when MATLAB cannot determine a valid
numeric value. Example: 0/0.
Predefined Variables
break case catch classdef continue else elseif
end for function global if otherwise parfor
persistent return spmd switch try while
Keywords
iskeyword
help <Predefined Variable Name>
Computing with MATLAB
Minimal MATLAB 14
16. Managing Variables
clear
clear x y z
clear all
who
whos
Clears the workspace, all variables are removed from the memory
Clears only variables x, y, and z
Clears all variables and functions from workspace
Lists variables currently in the workspace
Lists variables currently in the workspace with their sizes together
with information about their bytes and class
Command Outcome
Computing with MATLAB
Minimal MATLAB 15
17. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Functions – creating & executing a function file
16
Conditional control statements
Loop control statements
18. Overview
Array
5
5
7
4
5 7 4
5 10 56
6 4 6
7 78 86
Scalar of 1 X 1 Row Vector of 1 X 3 Column vector of 3 X 1
One Dimensional
Matrix of 3 X 3
5 10 56 85 23
6 4 6 23 2
7 78 86 53 6
5 14 12 10 56
6 10 504 4 6
89 0
Two Dimensional
Matrix of 3 X 5
5 10 56 85 23
6 4 6 23 2
7 78 86 53 6
5 14 12 10 56
6
0
5 14 12 10 56
89
0
Three Dimensional
Matrix of 3 X 5
Minimal MATLAB 17
19. Creation
Array
One Dimensional Array
variable_name = [ type vector elements ]
Minimal MATLAB 18
Two Dimensional Array
variable_name = [1st row elements; 2nd row elements; 3rd row
elements; ... ; last row elements]
from a known list of numbers
with constant spacing by specifying the first term, the spacing, and the
last term
Known lists of numbers
20. Array
Minimal MATLAB 19
Creation
Creating a vector with constant spacing by specifying the
first term, the spacing, and the last term:
variable_name = [m:q:n]
or
variable_name = m:q:n
m = first term
q = spacing
n = last term
: (colon operator)
Constant spacing
Creating a vector with linear (equal) spacing by specifying
the first and last terms, and the number of terms:
variable_name = linspace(xi, xf, n)
xi = first element
xf = Last element
n = number of elements
linspace
Constant spacing
22. Data Manipulation
Array
Minimal MATLAB 21
Addressing - Extraction (Subset)
Addressing an element(s) individually or in subgroups:
To redefine only some of the elements,
When specific elements are to be used in calculations,
When a subgroup of the elements is used to define a new variable.
23. Array
Minimal MATLAB 22
Address vector
>> ve = [35 46 78 23 5 14 81 3 55]
ve =
35 46 78 23 5 14 81 3 55
1 2 3 4 5 6 7 8 9 position or index of element
Data Manipulation
25. Array
Minimal MATLAB 24
Addressing vector : (colon operator)
Data Manipulation
Addressing matrix : (colon operator)
A(:,n) Refers to the elements in all the rows of column n of the matrix A.
A(n,:) Refers to the elements in all the columns of row n of the matrix A.
A(:,m:n) Refers to the elements in all the rows between columns m and n
of the matrix A.
A(m:n,:) Refers to the elements in all the columns between rows m and n
of the matrix A.
A(m:n,p:q)
Refers to the elements in rows m through n and columns p
through q of the matrix A.
26. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Functions – creating & executing a function file
25
Conditional control statements
Loop control statements
30. Graphing
Plotting
Minimal MATLAB 29
Plot function plot(x,y)
where the variables x and y are vector and must be of same size i.e the number of
elements should be equal.
x=[1 2 3 5 7 7.5 8 10];
y=[2 6.5 7 7 5.5 4 6 8];
plot(x,y)
31. Graphing
Plotting
Minimal MATLAB 30
Plot function
plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)
Line specifiers are optional and can be used to define the style and color of the
line and the type of markers (if markers are desired).
Line specifiers
solid (default)
dashed
dotted
dash-dot
Line Style Specifier
-
--
:
-.
red
green
blue
cyan
Line color Specifier
r
g
b
c
magenta
yellow
black
white
Line color Specifier
m
y
k
w
32. Graphing
Plotting
Minimal MATLAB 31
Plot function
plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)
Line specifiers are optional and can be used to define the style and color of the
line and the type of markers (if markers are desired).
Line specifiers
plus sign
circle
asterisk
point
cross
triangle (pointed up)
triangle (pointed down)
Marker type Specifier
+
o
*
.
×
^
v
square
diamond
five pointed star
six pointed star
triangle (pointed left)
triangle (pointed right)
Marker type Specifier
s
d
p
h
<
>
33. Graphing
Plotting
Minimal MATLAB 32
Plot function
plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)
To specify the thickness of the line, the size of the marker, and the colors of the marker’s
edge line and fill
Property Name
34. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Functions – creating & executing a function file
33
Conditional control statements
Loop control statements
35. Creation
Script
Minimal MATLAB 34
Overview
Sequence of MATLAB statements in file
Script
A script file is an external file that contains a sequence of MATLAB statements. Script
files have a filename extension of .m.
36. Basics
Script
Minimal MATLAB 35
variable_name = input(‘prompt message’)
Accepting input from user
Displaying output to user
disp(name of a variable)
disp(‘text as string’)
Saving script file
Creating script file
Executing script file
37. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Functions – creating & executing a function file
36
Conditional control statements
Loop control statements
38. Creation
Script
Minimal MATLAB 37
Conditional Statement
Conditional expression consisting of relational and/or logical operators.
Operations Operators
Less than
Greater than
Less than or equal to
Greater than or equal to
Equal to
Not Equal to
<
>
<=
>=
==
~=
Relational Operators
Operations Operators
And
Or
Not
&&
||
~
Logical Operators
40. if/elseif/else Statement
Script
Minimal MATLAB 39
if
statement
Statement
Group 1
False
True
End
Statement
Group 2
if expression
statements
elseif expression
statements
else
statements
end
41. Creation
Script
Minimal MATLAB 40
Switch Statement
False
True
Flowchart
match
case, k
case
end
False
case
statement, k
True
switch switch_expression
case case_expression
statements
case case_expression
statements
:
otherwise
statements
end
42. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Functions – creating & executing a function file
41
Conditional control statements
Loop control statements
43. Loop control statements
Script
Minimal MATLAB 42
for (Iterative Loop)
while (Conditional Loop)
continue, break
for index = start:increment:end
statements
end
while expression
statements
end
44. Agenda
Introducing MATLAB – Language of technical computing
Minimal MATLAB
MATLAB Desktop
Computing with MATLAB
Array – creation & working
Plotting – creating & visualizing
Automating commands – creating, saving & executing script file
Functions – creating & executing a function file
43
Conditional control statements
Loop control statements
45. Creation
Function
Minimal MATLAB 44
Overview
SYSTEM
[Mathematical model of
Physical Phenomenon]
(function file)
Set of inputs Set of outputs
1
2
3
m
1
2
3
n
[System]
Electrical Networks
Communication System
Control System
Thermodynamic System
Computer system, etc
[Input variables]
current, voltage,
power, energy,
temperature,
data
[Output variables]
current, voltage,
power, energy,
temperature,
Processed data
function [out1, out2, ...] = func_name(in1, in2, ...)
declares the function func_name, and its inputs and outputs
46. Creation
Function
Minimal MATLAB 45
Example
𝐻 =
𝑦
𝑥
=
1
1 + (𝜔𝑅𝐶)2
Black Box
X Y
function H = lp(R,C)
w = logspace(-2,6);
H = 1./sqrt(1 + (w*R*C).^2);
semilogx(w,H)
xlabel('Angular frquency, w (rad/s)');
ylabel('System Response');
title('Low-Pass frequency response');
1-
2-
3-
4-
5-
6-
Source: MATLAB An Introduction with Applications, Amos Gilat, 4e
51. More on MATLAB
1. MATLAB Tutorials
2. Demo and Webinars
3. Documentations
4. MATLAB Central (User Community)
www.mathworks.com
52. Reference
1. Getting started with MATLAB, A quick introduction for scientists and engineers,
Rudra Pratap, Indian edition
2. MATLAB An Introduction with Applications, Amos Gilat, 4e
3. Matlab - A Practical Introduction to Programming and Problem Solving, Stormy
Attaway, 2e
4. Essential MATLAB for Engineers and Scientists, Brian D. Hahn & Daniel T.
Valentine
5. A Guide to MATLAB for Beginners and Experienced Users, Hunt Lipsman &
Rosenberg