Introduction to MATLAB

51,322 views 53 slides Apr 08, 2013
Slide 1
Slide 1 of 53
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53

About This Presentation

Please Download and view it. Presentation on MATLAB


Slide Content

Do you speak MATLAB!

MathWorks®
MATLAB
®
R2012a (7.14)

MATLAB
Introducing MATLAB –Language of technical computing 2
Isanintegratedtechnicalcomputing
environmentthatcombinesnumericcomputation,advanced
graphicsandvisualization,andahigh-levelprogramminglanguage.
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

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
MicorosoftExcel
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 ToolboxTest and Measurement
Financial Toolbox
Econometrics Toolbox
DatafeedToolbox
Database Toolbox
Computational FinanceSpreadsheet 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

Agenda
Introducing MATLAB –Language of technical computing
Minimal MATLAB
4

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

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

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 subwindowlists all variables that you have generated so far along with there type
and size.
Workspace
MATLAB -Desktop
Minimal MATLAB7

MATLAB -Desktop
The main window characterized by the MATLAB command prompt (>>)
Help Window
Minimal MATLAB8

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

Basic Data and Variables
Computing with MATLAB
Class
Variable
SizeValue
Minimal MATLAB10

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
singledouble
Minimal MATLAB11

Arithmetic Operator &
Their Precedence
Computing with MATLAB
Operations Operators Examples
Addition
Subtraction
Multiplication
Right Division
Left Division
Exponentiation
Precedence OrderOperators
1
2
3
4
Parentheses ( ). For nested parentheses, the innermost
are executed first.
Exponentiation, ^
Multiplication, *; Division, /,\
Addition, +;Subtraction, -
help precedence
Minimal MATLAB12

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
BasicHyperbolic 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 MATLAB13

Predefined Variables and
Keywords
ans
pi
eps
inf
i,j
NaN
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 MATLAB14

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 MATLAB15

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

Overview
Array
5
5
7
45 7 4
510 56
64 6
778 86
Scalar of 1 X 1Row Vector of 1 X 3 Column vector of 3 X 1
One Dimensional
Matrix of 3 X 3
510 56 85 23
64 6 23 2
778 86 53 6
514 12 10 56
610 504 4 6
89 0
Two Dimensional
Matrix of 3 X 5
510 56 85 23
64 6 23 2
778 86 53 6
514 12 10 56
6
0
514 12 10 56
89
0
Three Dimensional
Matrix of 3 X 5
Minimal MATLAB17

Creation
Array
One Dimensional Array
variable_name= [ type vector elements ]
Minimal MATLAB18
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

Array
Minimal MATLAB19
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 firstand last terms, and the number of terms:
variable_name= linspace(xi, xf, n)
xi = first element
xf= Last element
n = number of elements
linspaceConstant spacing

Array
Minimal MATLAB20
Array Operators
+ Addition
- Subtraction
.* Element-by-element multiplication
./ Element-by-element division
.\ Element-by-element left division
.^ Element-by-element power
‘ Transpose
Operations Operation

Data Manipulation
Array
Minimal MATLAB21
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.

Array
Minimal MATLAB22
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 9position or index of element
Data Manipulation

Array
Minimal MATLAB23
Address matrix )
3 11 6 5
4 7 10 2
13 9 0 8
MAT =
(1,1) (1,2) (1,3) (1,4)
(2,1) (2,2) (2,3) (2,4)
(3,1) (3,2) (3,3) (3,4)
3 11 6 5
4 7 10 2
13 9 0 8
MAT =
Data Manipulation

Array
Minimal MATLAB24
Addressingvector : (colon operator)
Data Manipulation
Addressingmatrix : (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.

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

Creation
Plotting
Minimal MATLAB26
Example
Damped Oscillation equation:

Creation
Plotting
Minimal MATLAB27
Example
Damped Oscillation equation:
a = 0.1; b = 1;
t = 0:0.1:20;
y = exp(-a*t).*cos(b*t);
plot(t,y)
grid on;
xlabel('Time in second');
ylabel('Response in mm');
title('Damped Oscillation');
legend('a = 0.1,b = 1)');
1-
2-
3-
4-
5-
6-
7-
8-
9-

Creation
Plotting
Minimal MATLAB28
Overview
ylabel
xlabel
title
legend

Graphing
Plotting
Minimal MATLAB29
Plot functionplot(x,y)
wherethevariablesxandyarevectorandmustbeofsamesizei.ethenumberof
elementsshouldbeequal.
x=[1 2 3 5 7 7.5 8 10];
y=[2 6.5 7 7 5.5 4 6 8];
plot(x,y)

Graphing
Plotting
Minimal MATLAB30
Plot function
plot(x,y,‘linespecifiers’,‘PropertyName’,PropertyValue)
Linespecifiersareoptionalandcanbeusedtodefinethestyleandcolorofthe
lineandthetypeofmarkers(ifmarkersaredesired).
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

Graphing
Plotting
Minimal MATLAB31
Plot function
plot(x,y,‘linespecifiers’,‘PropertyName’,PropertyValue)
Linespecifiersareoptionalandcanbeusedtodefinethestyleandcolorofthe
lineandthetypeofmarkers(ifmarkersaredesired).
Line specifiers
plus sign
circle
asterisk
point
cross
triangle (pointed up)
triangle (pointed down)
Marker type Specifier
square
diamond
five pointed star
six pointed star
triangle (pointed left)
triangle (pointed right)
Marker type Specifier
s
d
p
h
<
>

Graphing
Plotting
Minimal MATLAB32
Plot function
plot(x,y,‘linespecifiers’,‘PropertyName’,PropertyValue)
Tospecifythethicknessoftheline,thesizeofthemarker,andthecolorsofthemarker’s
edgelineandfill
Property Name

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

Creation
Script
Minimal MATLAB34
Overview
Sequence of MATLAB statements in fileScript
A script file is an external file that contains a sequence of MATLAB statements. Script
files have a filename extension of .m.

Basics
Script
Minimal MATLAB35
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

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

Creation
Script
Minimal MATLAB37
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

Creation
Script
Minimal MATLAB38
Example
Plot
g1
g2
g3
t = linspace(-5,10);
g1 = -4 -2*t;
g2 = -4 + 3*t;
g3 = 16 -2*t;
g = g1.*(-2<t & t<=0)...
+ g2.*(0<t & t<=4)...
+ g3.*(4<t & t<=8);
plot(t,g)
1-
2-
3-
4-
5-
6-
7-
g1
g2
g3

if/elseif/else Statement
Script
Minimal MATLAB39
if
statement
Statement
Group 1
False
True
End
Statement
Group 2
if expression
statements
elseifexpression
statements
else
statements
end

Creation
Script
Minimal MATLAB40
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

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

Loop control statements
Script
Minimal MATLAB42
for (Iterative Loop)
while (Conditional Loop)
continue, break
for index = start:increment:end
statements
end
while expression
statements
end

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

Creation
Function
Minimal MATLAB44
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

Creation
Function
Minimal MATLAB45
Example
Black BoxX 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

Creation
Function
Minimal MATLAB46
Example

Creation
Function
Minimal MATLAB47
Example
Black Box
Source: MATLAB An Introduction with Applications, Amos Gilat, 4e

Creation
Function
Minimal MATLAB48
Example
function [Td, RH] = DewptRhum(T,Tw,BP)
es = 6.112*exp((17.67*T)/(T + 243.5));
ew= 6.112*exp((17.67*Tw)/(Tw + 243.5));
e = ew-BP*(T -Tw)*0.00066*(1 + 0.00115*Tw);
RH = 100*(e/es);
Td = (243.5*log(e/6.112))/(17.67 -log(e/6.112));
1-
2-
3-
4-
5-

BEFORE
LEAVING

More on MATLAB
1.MATLAB Tutorials
2.Demo and Webinars
3.Documentations
4.MATLAB Central (User Community)
www.mathworks.com

Reference
1.Getting started with MATLAB, A quick introduction for scientists and engineers,
RudraPratap, 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

THANKS
We speak MATLAB!
ASHISH MESHRAM
[email protected]
www.facebook.com/ashishmeet