Matlab is an interactive computing environment for numerical computation, visualization, and programming. The document provides an introduction and overview of Matlab, including what Matlab is, the Matlab screen interface, variables and arrays, basic math operations, plotting functions, control structures like if/else and for loops, using m-files, and writing user-defined functions. Key features of Matlab covered are the command window, workspace, command history, generating matrices and vectors, element-wise operations, relational and logical operators, and file input/output statements.
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
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
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
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