SlideShare a Scribd company logo
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 introduction
Matlab introductionMatlab introduction
Matlab introduction
Vikash Jakhar
 
Matlab anilkumar
Matlab  anilkumarMatlab  anilkumar
Matlab anilkumar
THEMASTERBLASTERSVID
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
prashantkumarchinama
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
kebeAman
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
chestialtaff
 
Kevin merchantss
Kevin merchantssKevin merchantss
Kevin merchantss
dharmesh69
 
KEVIN MERCHANT DOCUMENT
KEVIN MERCHANT DOCUMENTKEVIN MERCHANT DOCUMENT
KEVIN MERCHANT DOCUMENT
tejas1235
 
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
Mukesh 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 Matlab
Amr Rashed
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
Ragu Nathan
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
Tarun Gehlot
 
Matlab
MatlabMatlab
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
Infinity Tech Solutions
 
interfacing matlab with embedded systems
interfacing matlab with embedded systemsinterfacing matlab with embedded systems
interfacing matlab with embedded systems
Raghav 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 4
Randa Elanwar
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
aboma2hawi
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
Vinay 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

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 

Recently uploaded (20)

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 

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