SlideShare a Scribd company logo
1 of 38
Download to read offline
Introduction to MATLAB
Introduction to MATLAB
Introduction to Matlab
1
Introduction to MATLAB
Introduction to MATLAB
Outline:
Outline:
‰ What is Matlab?
‰ What is Matlab?
„ Matlab Screen
V i bl t i i d i
„ Variables, array, matrix, indexing
„ Operators (Arithmetic, relational, logical )
„ Display Facilities
„ Flow Control
Flow Control
„ Using of M-File
„ Writing User Defined Functions
„ Writing User Defined Functions
„ Conclusion
2
Introduction to MATLAB
Introduction to MATLAB
What is Matlab?
What is Matlab?
„ Matlab is basically a high level language
y g g g
which has many specialized toolboxes for
making things easier for us
making things easier for us
„ How high?
M l b
Matlab
High Level
Languages such as
g g
C, Pascal etc.
Assembly
3
Introduction to MATLAB
Introduction to MATLAB
What are we interested in?
What are we interested in?
„ Matlab is too broad for our purposes in this
„ Matlab is too broad for our purposes in this
course.
„ The features we are going to require is
Matlab
Matlab
Series of
Matlab
d
Command
Line
m-files mat-files
commands
functions Command execution
like DOS command
Input
Data
storage/
like DOS command
window
p
Output
capability
storage/
loading
4
Introduction to MATLAB
Introduction to MATLAB
Matlab Screen
Matlab Screen
Command Window
„ Command Window
‰ type commands
„ Current Directory
‰ View folders and m-files
„ Workspace
‰ View program variables
‰ Double click on a variable
to see it in the Array Editor
„ Command History
‰ view past commands
‰ save a whole session
‰ save a whole session
using diary
5
Introduction to MATLAB
Introduction to MATLAB
Variables
Variables
„ No need for types i e
„ No need for types. i.e.,
int a;
int a;
double b;
float c;
„ All variables are created with double precision unless
specified and they are matrices.
p y
Example:
>>x=5;
Aft th t t t th i bl 1 1 t i
>>x=5;
>>x1=2;
„ After these statements, the variables are 1x1 matrices
with double precision
6
Introduction to MATLAB
Introduction to MATLAB
Array Matrix
Array, Matrix
t
„ a vector x = [1 2 5 1]
x =
1 2 5 1
1 2 5 1
„ a matrix x = [1 2 3; 5 1 4; 3 2 -1]
x =
1 2 3
5 1 4
5 1 4
3 2 -1
„ transpose y x’ y
„ transpose y = x’ y =
1
2
5
5
1
7
Introduction to MATLAB
Introduction to MATLAB
Long Array Matrix
Long Array, Matrix
„ t =1:10
t =
t
1 2 3 4 5 6 7 8 9 10
„ k =2:-0.5:-1
k =
2 1 5 1 0 5 0 -0 5 -1
2 1.5 1 0.5 0 0.5 1
„ B = [1:4; 5:8]
x =
1 2 3 4
1 2 3 4
5 6 7 8
8
Introduction to MATLAB
Introduction to MATLAB
Generating Vectors from functions
Generating Vectors from functions
„ zeros(M,N) MxN matrix of zeros x = zeros(1 3)
( , ) x = zeros(1,3)
x =
0 0 0
„ ones(M,N) MxN matrix of ones
0 0 0
(1 3)
x = ones(1,3)
x =
„ rand(M,N) MxN matrix of uniformly
di t ib t d d
1 1 1
distributed random
numbers on (0,1)
x = rand(1,3)
x =
0.9501 0.2311 0.6068
9
Introduction to MATLAB
Introduction to MATLAB
Matrix Index
Matrix Index
Th t i i di b i f 1 ( t 0 ( i C))
„ The matrix indices begin from 1 (not 0 (as in C))
„ The matrix indices must be positive integer
Given:
Given:
A(-2) A(0)
A( 2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
10
Introduction to MATLAB
Introduction to MATLAB
Concatenation of Matrices
Concatenation of Matrices
„ x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
A = [ x y]
1 2 4 5
B = [x ; y]
1 2
4 5
4 5
C = [x y ;z]
C [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
11
Introduction to MATLAB
Introduction to MATLAB
Operators (arithmetic)
Operators (arithmetic)
+ addition
+ addition
- subtraction
* multiplication
/ division
^ power
power
‘ complex conjugate transpose
12
Introduction to MATLAB
Introduction to MATLAB
Matrices Operations
Matrices Operations
Given A and B:
Addition Subtraction Product Transpose
13
Introduction to MATLAB
Introduction to MATLAB
Operators (Element by Element)
Operators (Element by Element)
.* element-by-element multiplication
./ element-by-element division
^ element-by-element power
. element-by-element power
14
Introduction to MATLAB
Introduction to MATLAB
The use of “ ” – “Element” Operation
The use of . Element Operation
A = [1 2 3; 5 1 4; 3 2 1]
A
A =
1 2 3
5 1 4
3 2 -1
b x * y c x / y d x ^2
y = A(3 ,:)
b = x .* y
b=
c = x . / y
c=
d = x .^2
d=
x = A(1,:)
K ^2
y=
3 4 -1
3 8 -3 0.33 0.5 -3 1 4 9
x=
1 2 3
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
g g
15
Introduction to MATLAB
Introduction to MATLAB
Basic Task: Plot the function sin(x)
Basic Task: Plot the function sin(x)
between 0≤x≤4π
„ Create an x-array of 100 samples between 0
and 4π.
and 4π.
„ Calculate sin(.) of the x-array
>>x=linspace(0,4*pi,100);
Calculate sin(.) of the x array
>> i ( ) 0 4
0.6
0.8
1
„ Plot the y-array
>>y=sin(x);
-0.2
0
0.2
0.4
y y
>>plot(y)
0 10 20 30 40 50 60 70 80 90 100
-1
-0.8
-0.6
-0.4
0 10 20 30 40 50 60 70 80 90 100
16
Introduction to MATLAB
Introduction to MATLAB
Plot the function e-x/3sin(x) between
Plot the function e sin(x) between
0≤x≤4π
„ Create an x-array of 100 samples between 0
and 4π
and 4π.
>>x=linspace(0,4*pi,100);
„ Calculate sin(.) of the x-array
„ Calculate e-x/3 of the x-array
>>y=sin(x);
„ Calculate e of the x array
>>y1=exp(-x/3);
„ Multiply the arrays y and y1
>> 2 * 1
>>y2=y*y1;
17
Introduction to MATLAB
Introduction to MATLAB
Plot the function e-x/3sin(x) between
Plot the function e sin(x) between
0≤x≤4π
„ Multiply the arrays y and y1 correctly
„ Plot the y2-array
>>y2=y.*y1;
„ Plot the y2 array
>>plot(y2) 0.6
0.7
p (y )
0.3
0.4
0.5
0
0.1
0.2
0 10 20 30 40 50 60 70 80 90 100
-0.3
-0.2
-0.1
18
Introduction to MATLAB
Introduction to MATLAB
Display Facilities
Display Facilities
l t( )
0.5
0.6
0.7
„ plot(.)
Example:
0.2
0.3
0.4
Example:
>>x=linspace(0,4*pi,100);
>>y=sin(x);
>>plot(y) 0 2
-0.1
0
0.1
stem( )
>>plot(y)
>>plot(x,y)
0.7
0 10 20 30 40 50 60 70 80 90 100
-0.3
-0.2
„ stem(.)
0 3
0.4
0.5
0.6
Example:
>>stem(y) 0
0.1
0.2
0.3
>>stem(x,y)
0 10 20 30 40 50 60 70 80 90 100
-0.3
-0.2
-0.1
19
Introduction to MATLAB
Introduction to MATLAB
Display Facilities
Display Facilities
„ title(.)
„ xlabel( )
>>title(‘This is the sinus function’)
0.6
0.8
1
This is the sinus function
„ xlabel(.)
>>xlabel(‘x (secs)’)
0
0.2
0.4
0.6
n(x)
„ ylabel(.)
-0.6
-0.4
-0.2
0
sin
>>ylabel(‘sin(x)’)
0 10 20 30 40 50 60 70 80 90 100
-1
-0.8
0.6
x (secs)
( )
20
Introduction to MATLAB
Introduction to MATLAB
Operators (relational logical)
Operators (relational, logical)
„ == Equal to
„ ~= Not equal to
„ < Strictly smaller
„ < Strictly smaller
„ > Strictly greater
„ <= Smaller than or equal to
>= Greater than equal to
„ >= Greater than equal to
„ & And operator
p
„ | Or operator
21
Introduction to MATLAB
Introduction to MATLAB
Flow Control
Flow Control
„ if
„ for
„ while
„ while
„ break
„ ….
22
Introduction to MATLAB
Introduction to MATLAB
Control Structures
Control Structures
Some Dummy Examples
„ If Statement Syntax
Some Dummy Examples
if ((a>3) & (b==5))
if (Condition_1)
Matlab Commands
Some Matlab Commands;
end
Matlab Commands
elseif (Condition_2)
Matlab Commands
if (a<3)
Some Matlab Commands;
elseif (b~=5)
Matlab Commands
elseif (Condition_3)
Matlab Commands
elseif (b 5)
Some Matlab Commands;
end
Matlab Commands
else
Matlab Commands
if (a<3)
Some Matlab Commands;
l
Matlab Commands
end
else
Some Matlab Commands;
end
23
Introduction to MATLAB
Introduction to MATLAB
Control Structures
Control Structures
Some Dummy Examples
„ For loop syntax
Some Dummy Examples
for i=1:100
S M tl b C d
for i=Index Array
Some Matlab Commands;
end
for i=Index_Array
Matlab Commands
for j=1:3:200
Some Matlab Commands;
end
end for m=13:-0.2:-21
Some Matlab Commands;
Some Matlab Commands;
end
for k [0 1 0 3 13 12 7 9 3]
for k=[0.1 0.3 -13 12 7 -9.3]
Some Matlab Commands;
end
24
Introduction to MATLAB
Introduction to MATLAB
Control Structures
Control Structures
„ While Loop Syntax
while (condition) Dummy Example
while (condition)
Matlab Commands while ((a>3) & (b==5))
Some Matlab Commands;
end
Some Matlab Commands;
end
25
Use of M-File
Use of M File
Click to create
Click to create
a new M-File
• Extension “.m”
• A text file containing script or function or program to run
26
Introduction to MATLAB
Introduction to MATLAB
Use of M-File
Use of M File Save file as Denem430.m
If you include “;” at the
If you include ; at the
end of each statement,
result will not be shown
immediately
immediately
27
Introduction to MATLAB
Introduction to MATLAB
File Input Statements
p
„ Clear memory
„ Clear screen
„ fid = fopen(filename)
„ fid = fopen(filename, permission)
} Argument fid is a file
identifier associated
p ( , p )
„ Statements…..
„ { ……
}
Here opens the file
filename for read
with an open file
„ A = fscanf(fid, format)
filename for read
access
reads data from the file
specified by fid converts it
specified by fid, converts it
according to the specified
format string, and returns it
in matrix A
„ fclose(fid)
28
Introduction to MATLAB
Introduction to MATLAB
File Output Statements Here opens the file
„ count = fprintf(fid, format, A, ...)
p p
filename for write access
formats the data in the
real part of matrix A
A conversion specification
controls the notation,
alignment, significant
di it fi ld idth d
digits, field width, and
other aspects of output
format.
29
Introduction to MATLAB
Introduction to MATLAB
File Output Statements
p
30
Introduction to MATLAB
Introduction to MATLAB
File Output Statements
p
31
Introduction to MATLAB
Introduction to MATLAB
File Output Statements
p
32
Introduction to MATLAB
Introduction to MATLAB
Writing User Defined Functions
Writing User Defined Functions
„ Functions are m-files which can be executed by
specifying some inputs and supply some desired outputs.
„ The code telling the Matlab that an m-file is actually a
function is
function out1=functionname(in1)
function out1=functionname(in1,in2,in3)
( , , )
function [out1,out2]=functionname(in1,in2)
„ You should write this command at the beginning of the
m-file and you should save the m-file with a file name
m-file and you should save the m-file with a file name
same as the function name
33
Introduction to MATLAB
Introduction to MATLAB
Writing User Defined Functions
g
„ Examples
p
‰ Write a function : out=squarer (A, ind)
„ Which takes the square of the input matrix if the input
q p p
indicator is equal to 1
„ And takes the element by element square of the input
t i if th i t i di t i l t 2
matrix if the input indicator is equal to 2
Same Name
34
Introduction to MATLAB
Introduction to MATLAB
Writing User Defined Functions
Writing User Defined Functions
„ Another function which takes an input array and returns the sum and product
of its elements as outputs
„ The function sumprod(.) can be called from command window or an m-file as
35
Introduction to MATLAB
Introduction to MATLAB
Notes:
Notes:
„ “%” is the neglect sign for Matlab (equaivalent
„ % is the neglect sign for Matlab (equaivalent
of “//” in C). Anything after it on the same line
is neglected by Matlab compiler
is neglected by Matlab compiler.
„ Sometimes slowing down the execution is
g
done deliberately for observation purposes.
You can use the command “pause” for this
You can use the command pause for this
purpose
pause %wait until any key
pause(3) %wait 3 seconds
pause(3) %wait 3 seconds
36
Introduction to MATLAB
Introduction to MATLAB
Useful Commands
Useful Commands
„ The two commands used most by Matlab
„ The two commands used most by Matlab
users are
>>help functionname
>>lookfor keyword
37
Introduction to MATLAB
Introduction to MATLAB
Th k Y
Thank You…
38

More Related Content

Similar to Introduction to Matlab.pdf

MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxprashantkumarchinama
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.pptkebeAman
 
Kevin merchantss
Kevin merchantssKevin merchantss
Kevin merchantssdharmesh69
 
KEVIN MERCHANT DOCUMENT
KEVIN MERCHANT DOCUMENTKEVIN MERCHANT DOCUMENT
KEVIN MERCHANT DOCUMENTtejas1235
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsMukesh Kumar
 
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab sessionDr. Krishna Mohbey
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to MatlabAmr Rashed
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 introRagu Nathan
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabTarun Gehlot
 
interfacing matlab with embedded systems
interfacing matlab with embedded systemsinterfacing matlab with embedded systems
interfacing matlab with embedded systemsRaghav Shetty
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Randa Elanwar
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshopVinay Kumar
 

Similar to Introduction to Matlab.pdf (20)

Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Matlab anilkumar
Matlab  anilkumarMatlab  anilkumar
Matlab anilkumar
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Kevin merchantss
Kevin merchantssKevin merchantss
Kevin merchantss
 
KEVIN MERCHANT DOCUMENT
KEVIN MERCHANT DOCUMENTKEVIN MERCHANT DOCUMENT
KEVIN MERCHANT DOCUMENT
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
 
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab session
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Matlab
MatlabMatlab
Matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
 
interfacing matlab with embedded systems
interfacing matlab with embedded systemsinterfacing matlab with embedded systems
interfacing matlab with embedded systems
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 

Recently uploaded

AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseWSO2
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...caitlingebhard1
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 

Recently uploaded (20)

AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 

Introduction to Matlab.pdf

  • 1. Introduction to MATLAB Introduction to MATLAB Introduction to Matlab 1
  • 2. Introduction to MATLAB Introduction to MATLAB Outline: Outline: ‰ What is Matlab? ‰ What is Matlab? „ Matlab Screen V i bl t i i d i „ Variables, array, matrix, indexing „ Operators (Arithmetic, relational, logical ) „ Display Facilities „ Flow Control Flow Control „ Using of M-File „ Writing User Defined Functions „ Writing User Defined Functions „ Conclusion 2
  • 3. Introduction to MATLAB Introduction to MATLAB What is Matlab? What is Matlab? „ Matlab is basically a high level language y g g g which has many specialized toolboxes for making things easier for us making things easier for us „ How high? M l b Matlab High Level Languages such as g g C, Pascal etc. Assembly 3
  • 4. Introduction to MATLAB Introduction to MATLAB What are we interested in? What are we interested in? „ Matlab is too broad for our purposes in this „ Matlab is too broad for our purposes in this course. „ The features we are going to require is Matlab Matlab Series of Matlab d Command Line m-files mat-files commands functions Command execution like DOS command Input Data storage/ like DOS command window p Output capability storage/ loading 4
  • 5. Introduction to MATLAB Introduction to MATLAB Matlab Screen Matlab Screen Command Window „ Command Window ‰ type commands „ Current Directory ‰ View folders and m-files „ Workspace ‰ View program variables ‰ Double click on a variable to see it in the Array Editor „ Command History ‰ view past commands ‰ save a whole session ‰ save a whole session using diary 5
  • 6. Introduction to MATLAB Introduction to MATLAB Variables Variables „ No need for types i e „ No need for types. i.e., int a; int a; double b; float c; „ All variables are created with double precision unless specified and they are matrices. p y Example: >>x=5; Aft th t t t th i bl 1 1 t i >>x=5; >>x1=2; „ After these statements, the variables are 1x1 matrices with double precision 6
  • 7. Introduction to MATLAB Introduction to MATLAB Array Matrix Array, Matrix t „ a vector x = [1 2 5 1] x = 1 2 5 1 1 2 5 1 „ a matrix x = [1 2 3; 5 1 4; 3 2 -1] x = 1 2 3 5 1 4 5 1 4 3 2 -1 „ transpose y x’ y „ transpose y = x’ y = 1 2 5 5 1 7
  • 8. Introduction to MATLAB Introduction to MATLAB Long Array Matrix Long Array, Matrix „ t =1:10 t = t 1 2 3 4 5 6 7 8 9 10 „ k =2:-0.5:-1 k = 2 1 5 1 0 5 0 -0 5 -1 2 1.5 1 0.5 0 0.5 1 „ B = [1:4; 5:8] x = 1 2 3 4 1 2 3 4 5 6 7 8 8
  • 9. Introduction to MATLAB Introduction to MATLAB Generating Vectors from functions Generating Vectors from functions „ zeros(M,N) MxN matrix of zeros x = zeros(1 3) ( , ) x = zeros(1,3) x = 0 0 0 „ ones(M,N) MxN matrix of ones 0 0 0 (1 3) x = ones(1,3) x = „ rand(M,N) MxN matrix of uniformly di t ib t d d 1 1 1 distributed random numbers on (0,1) x = rand(1,3) x = 0.9501 0.2311 0.6068 9
  • 10. Introduction to MATLAB Introduction to MATLAB Matrix Index Matrix Index Th t i i di b i f 1 ( t 0 ( i C)) „ The matrix indices begin from 1 (not 0 (as in C)) „ The matrix indices must be positive integer Given: Given: A(-2) A(0) A( 2), A(0) Error: ??? Subscript indices must either be real positive integers or logicals. A(4,2) Error: ??? Index exceeds matrix dimensions. 10
  • 11. Introduction to MATLAB Introduction to MATLAB Concatenation of Matrices Concatenation of Matrices „ x = [1 2], y = [4 5], z=[ 0 0] A = [ x y] A = [ x y] 1 2 4 5 B = [x ; y] 1 2 4 5 4 5 C = [x y ;z] C [x y ;z] Error: ??? Error using ==> vertcat CAT arguments dimensions are not consistent. 11
  • 12. Introduction to MATLAB Introduction to MATLAB Operators (arithmetic) Operators (arithmetic) + addition + addition - subtraction * multiplication / division ^ power power ‘ complex conjugate transpose 12
  • 13. Introduction to MATLAB Introduction to MATLAB Matrices Operations Matrices Operations Given A and B: Addition Subtraction Product Transpose 13
  • 14. Introduction to MATLAB Introduction to MATLAB Operators (Element by Element) Operators (Element by Element) .* element-by-element multiplication ./ element-by-element division ^ element-by-element power . element-by-element power 14
  • 15. Introduction to MATLAB Introduction to MATLAB The use of “ ” – “Element” Operation The use of . Element Operation A = [1 2 3; 5 1 4; 3 2 1] A A = 1 2 3 5 1 4 3 2 -1 b x * y c x / y d x ^2 y = A(3 ,:) b = x .* y b= c = x . / y c= d = x .^2 d= x = A(1,:) K ^2 y= 3 4 -1 3 8 -3 0.33 0.5 -3 1 4 9 x= 1 2 3 K= x^2 Erorr: ??? Error using ==> mpower Matrix must be square. B=x*y Erorr: ??? Error using ==> mtimes Inner matrix dimensions must agree. g g 15
  • 16. Introduction to MATLAB Introduction to MATLAB Basic Task: Plot the function sin(x) Basic Task: Plot the function sin(x) between 0≤x≤4π „ Create an x-array of 100 samples between 0 and 4π. and 4π. „ Calculate sin(.) of the x-array >>x=linspace(0,4*pi,100); Calculate sin(.) of the x array >> i ( ) 0 4 0.6 0.8 1 „ Plot the y-array >>y=sin(x); -0.2 0 0.2 0.4 y y >>plot(y) 0 10 20 30 40 50 60 70 80 90 100 -1 -0.8 -0.6 -0.4 0 10 20 30 40 50 60 70 80 90 100 16
  • 17. Introduction to MATLAB Introduction to MATLAB Plot the function e-x/3sin(x) between Plot the function e sin(x) between 0≤x≤4π „ Create an x-array of 100 samples between 0 and 4π and 4π. >>x=linspace(0,4*pi,100); „ Calculate sin(.) of the x-array „ Calculate e-x/3 of the x-array >>y=sin(x); „ Calculate e of the x array >>y1=exp(-x/3); „ Multiply the arrays y and y1 >> 2 * 1 >>y2=y*y1; 17
  • 18. Introduction to MATLAB Introduction to MATLAB Plot the function e-x/3sin(x) between Plot the function e sin(x) between 0≤x≤4π „ Multiply the arrays y and y1 correctly „ Plot the y2-array >>y2=y.*y1; „ Plot the y2 array >>plot(y2) 0.6 0.7 p (y ) 0.3 0.4 0.5 0 0.1 0.2 0 10 20 30 40 50 60 70 80 90 100 -0.3 -0.2 -0.1 18
  • 19. Introduction to MATLAB Introduction to MATLAB Display Facilities Display Facilities l t( ) 0.5 0.6 0.7 „ plot(.) Example: 0.2 0.3 0.4 Example: >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y) 0 2 -0.1 0 0.1 stem( ) >>plot(y) >>plot(x,y) 0.7 0 10 20 30 40 50 60 70 80 90 100 -0.3 -0.2 „ stem(.) 0 3 0.4 0.5 0.6 Example: >>stem(y) 0 0.1 0.2 0.3 >>stem(x,y) 0 10 20 30 40 50 60 70 80 90 100 -0.3 -0.2 -0.1 19
  • 20. Introduction to MATLAB Introduction to MATLAB Display Facilities Display Facilities „ title(.) „ xlabel( ) >>title(‘This is the sinus function’) 0.6 0.8 1 This is the sinus function „ xlabel(.) >>xlabel(‘x (secs)’) 0 0.2 0.4 0.6 n(x) „ ylabel(.) -0.6 -0.4 -0.2 0 sin >>ylabel(‘sin(x)’) 0 10 20 30 40 50 60 70 80 90 100 -1 -0.8 0.6 x (secs) ( ) 20
  • 21. Introduction to MATLAB Introduction to MATLAB Operators (relational logical) Operators (relational, logical) „ == Equal to „ ~= Not equal to „ < Strictly smaller „ < Strictly smaller „ > Strictly greater „ <= Smaller than or equal to >= Greater than equal to „ >= Greater than equal to „ & And operator p „ | Or operator 21
  • 22. Introduction to MATLAB Introduction to MATLAB Flow Control Flow Control „ if „ for „ while „ while „ break „ …. 22
  • 23. Introduction to MATLAB Introduction to MATLAB Control Structures Control Structures Some Dummy Examples „ If Statement Syntax Some Dummy Examples if ((a>3) & (b==5)) if (Condition_1) Matlab Commands Some Matlab Commands; end Matlab Commands elseif (Condition_2) Matlab Commands if (a<3) Some Matlab Commands; elseif (b~=5) Matlab Commands elseif (Condition_3) Matlab Commands elseif (b 5) Some Matlab Commands; end Matlab Commands else Matlab Commands if (a<3) Some Matlab Commands; l Matlab Commands end else Some Matlab Commands; end 23
  • 24. Introduction to MATLAB Introduction to MATLAB Control Structures Control Structures Some Dummy Examples „ For loop syntax Some Dummy Examples for i=1:100 S M tl b C d for i=Index Array Some Matlab Commands; end for i=Index_Array Matlab Commands for j=1:3:200 Some Matlab Commands; end end for m=13:-0.2:-21 Some Matlab Commands; Some Matlab Commands; end for k [0 1 0 3 13 12 7 9 3] for k=[0.1 0.3 -13 12 7 -9.3] Some Matlab Commands; end 24
  • 25. Introduction to MATLAB Introduction to MATLAB Control Structures Control Structures „ While Loop Syntax while (condition) Dummy Example while (condition) Matlab Commands while ((a>3) & (b==5)) Some Matlab Commands; end Some Matlab Commands; end 25
  • 26. Use of M-File Use of M File Click to create Click to create a new M-File • Extension “.m” • A text file containing script or function or program to run 26
  • 27. Introduction to MATLAB Introduction to MATLAB Use of M-File Use of M File Save file as Denem430.m If you include “;” at the If you include ; at the end of each statement, result will not be shown immediately immediately 27
  • 28. Introduction to MATLAB Introduction to MATLAB File Input Statements p „ Clear memory „ Clear screen „ fid = fopen(filename) „ fid = fopen(filename, permission) } Argument fid is a file identifier associated p ( , p ) „ Statements….. „ { …… } Here opens the file filename for read with an open file „ A = fscanf(fid, format) filename for read access reads data from the file specified by fid converts it specified by fid, converts it according to the specified format string, and returns it in matrix A „ fclose(fid) 28
  • 29. Introduction to MATLAB Introduction to MATLAB File Output Statements Here opens the file „ count = fprintf(fid, format, A, ...) p p filename for write access formats the data in the real part of matrix A A conversion specification controls the notation, alignment, significant di it fi ld idth d digits, field width, and other aspects of output format. 29
  • 30. Introduction to MATLAB Introduction to MATLAB File Output Statements p 30
  • 31. Introduction to MATLAB Introduction to MATLAB File Output Statements p 31
  • 32. Introduction to MATLAB Introduction to MATLAB File Output Statements p 32
  • 33. Introduction to MATLAB Introduction to MATLAB Writing User Defined Functions Writing User Defined Functions „ Functions are m-files which can be executed by specifying some inputs and supply some desired outputs. „ The code telling the Matlab that an m-file is actually a function is function out1=functionname(in1) function out1=functionname(in1,in2,in3) ( , , ) function [out1,out2]=functionname(in1,in2) „ You should write this command at the beginning of the m-file and you should save the m-file with a file name m-file and you should save the m-file with a file name same as the function name 33
  • 34. Introduction to MATLAB Introduction to MATLAB Writing User Defined Functions g „ Examples p ‰ Write a function : out=squarer (A, ind) „ Which takes the square of the input matrix if the input q p p indicator is equal to 1 „ And takes the element by element square of the input t i if th i t i di t i l t 2 matrix if the input indicator is equal to 2 Same Name 34
  • 35. Introduction to MATLAB Introduction to MATLAB Writing User Defined Functions Writing User Defined Functions „ Another function which takes an input array and returns the sum and product of its elements as outputs „ The function sumprod(.) can be called from command window or an m-file as 35
  • 36. Introduction to MATLAB Introduction to MATLAB Notes: Notes: „ “%” is the neglect sign for Matlab (equaivalent „ % is the neglect sign for Matlab (equaivalent of “//” in C). Anything after it on the same line is neglected by Matlab compiler is neglected by Matlab compiler. „ Sometimes slowing down the execution is g done deliberately for observation purposes. You can use the command “pause” for this You can use the command pause for this purpose pause %wait until any key pause(3) %wait 3 seconds pause(3) %wait 3 seconds 36
  • 37. Introduction to MATLAB Introduction to MATLAB Useful Commands Useful Commands „ The two commands used most by Matlab „ The two commands used most by Matlab users are >>help functionname >>lookfor keyword 37
  • 38. Introduction to MATLAB Introduction to MATLAB Th k Y Thank You… 38