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

PPTX
Introduction to matlab
PPTX
Mat lab workshop
PPTX
introduction to matlab.pptx
PPT
Introduction to matlab
PPT
Introduction to matlab
PDF
Matlab for beginners, Introduction, signal processing
PPT
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
PPTX
Introduction to Matlab and application.pptx
Introduction to matlab
Mat lab workshop
introduction to matlab.pptx
Introduction to matlab
Introduction to matlab
Matlab for beginners, Introduction, signal processing
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
Introduction to Matlab and application.pptx

Similar to Introduction to Matlab.pdf (20)

PPT
Introduction to matlab
PPTX
MATLAB Workshop for project and research
PPTX
intro2matlab-basic knowledge about Matlab.pptx
PDF
Malab tutorial
PPT
matlab_tutorial.ppt
PPT
matlab_tutorial.ppt
PPT
matlab_tutorial.ppt
PPT
Matlab Overviiew
PDF
MATLAB Programming
PPT
matlab tutorial with separate function description and handson learning
PPT
Introduction of MatLab
PPT
MATLAB-Introd.ppt
PDF
A complete introduction on matlab and matlab's projects
PDF
An Introduction to MATLAB with Worked Examples
PPT
Matlab introduction
PPTX
Raushan's MATLB PPT..pptx
PPT
Matlab1
PDF
Lecture 01 variables scripts and operations
PDF
PDF
Tutorial matlab
Introduction to matlab
MATLAB Workshop for project and research
intro2matlab-basic knowledge about Matlab.pptx
Malab tutorial
matlab_tutorial.ppt
matlab_tutorial.ppt
matlab_tutorial.ppt
Matlab Overviiew
MATLAB Programming
matlab tutorial with separate function description and handson learning
Introduction of MatLab
MATLAB-Introd.ppt
A complete introduction on matlab and matlab's projects
An Introduction to MATLAB with Worked Examples
Matlab introduction
Raushan's MATLB PPT..pptx
Matlab1
Lecture 01 variables scripts and operations
Tutorial matlab
Ad

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Machine learning based COVID-19 study performance prediction
PDF
KodekX | Application Modernization Development
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
Machine learning based COVID-19 study performance prediction
KodekX | Application Modernization Development
Programs and apps: productivity, graphics, security and other tools
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
sap open course for s4hana steps from ECC to s4
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
Ad

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