introduction to matlab r2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/eee281/intro_r2012b.pdf ·...
TRANSCRIPT
![Page 1: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/1.jpg)
Introduction to MATLAB R2012b
![Page 2: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/2.jpg)
What is Matlab?• Matlab is basically a high level language which
has many specialized toolboxes for making things easier for us
• How high?
Assembly
High Level Languages such as
C, Pascal etc.
Matlab
![Page 3: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/3.jpg)
MATLAB R2012 Interface
![Page 4: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/4.jpg)
Command Window
![Page 5: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/5.jpg)
Basic Arithmetic Operations in Command Window
![Page 6: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/6.jpg)
Command Window
![Page 7: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/7.jpg)
Workspace
• Double click on “m” variable (m is a 3x4 matrix)
![Page 8: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/8.jpg)
Command History
![Page 9: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/9.jpg)
Current Folder Tab
• We will not use current folder tab so oftenly, so minimize it for now.
![Page 10: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/10.jpg)
Again, MATLAB R2012b General Interface (without “Current Folder” tab)
![Page 11: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/11.jpg)
MATLAB R2012 Interface: Home, Plots, Apps
![Page 12: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/12.jpg)
MATLAB R2012 Interface:Home
![Page 13: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/13.jpg)
MATLAB R2012 Interface:Home
![Page 14: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/14.jpg)
Introduction to MATLAB R2012b: Editor
![Page 15: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/15.jpg)
Introduction to MATLAB R2012b: Editor
![Page 16: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/16.jpg)
Introduction to MATLAB R2012b
![Page 17: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/17.jpg)
Introduction to MATLAB R2012b: Editor
![Page 18: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/18.jpg)
Introduction to MATLAB R2012b: Functions
![Page 19: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/19.jpg)
Introduction to MATLAB R2012b
![Page 20: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/20.jpg)
Introduction to MATLAB R2012b
![Page 21: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/21.jpg)
Introduction to MATLAB R2012b
![Page 22: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/22.jpg)
Introduction to MATLAB R2012b
![Page 23: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/23.jpg)
Introduction to MATLAB R2012b
![Page 24: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/24.jpg)
Introduction to MATLAB R2012b
![Page 25: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/25.jpg)
Introduction to MATLAB R2012b
![Page 26: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/26.jpg)
Function Usage in MATLAB R2012b
![Page 27: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/27.jpg)
Functions in MATLAB
• You do not need always writing new functions.• There are also embedded functions in MATLAB.• Write “help” command in the command window
and see the list of help topics of MATLAB.• If you would like to get some knowledge for any
function, write “help function_name” in command window. For example, write “help factorial” .
• Now, use factorial function to compute factorial of 5 by using the given knowledge.
![Page 28: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/28.jpg)
Functions in MATLAB
• Another example: Write “help linspace” in command window.
• Can you say what linspace command does ?Write in command window:
t=linspace(0,1,100)
What will the content of “t” be?
• There are lots of other functions in MATLAB.
![Page 29: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/29.jpg)
Introduction to MATLAB R2012bCommand Line
(Series of MATLAB commands)
.mat files(Parameters,
Variables)
.m files (Scripts, Functions)
![Page 30: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/30.jpg)
Introduction to MATLAB R2012b• No need for types. i.e.,
• All variables are created with double precision unless specified and they are matrices.
• After these statements, the variables are 1x1 matrices with double precision
int a;double b;float c;
Example:>>x=5;>>x1=2;
![Page 31: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/31.jpg)
Array, Matrix• a vector x = [1 2 5 1]
x =1 2 5 1
• a matrix x = [1 2 3; 5 1 4; 3 2 -1]
x =1 2 35 1 43 2 -1
• transpose y = x’ y =1251
![Page 32: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/32.jpg)
Long Array, Matrix • t =1:10
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
• B = [1:4; 5:8]
x =1 2 3 45 6 7 8
![Page 33: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/33.jpg)
Matrix Index• The matrix indices begin from 1 (not 0 (as in C))• The matrix indices must be positive integer
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)Error: ??? Index exceeds matrix dimensions.
![Page 34: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/34.jpg)
Concatenation of Matrices• x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
1 2 4 5
B = [x ; y]
1 2
4 5
C = [x y ;z] Error:??? Error using ==> vertcat CAT arguments dimensions are not consistent.
![Page 35: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/35.jpg)
Operators (arithmetic)+ addition- subtraction* multiplication/ division^ power‘ complex conjugate transpose
![Page 36: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/36.jpg)
Matrices Operations
Given A and B:
Addition Subtraction Product Transpose
![Page 37: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/37.jpg)
Operators (Element by Element)
.*element-by-element multiplication
./ element-by-element division
.^element-by-element power
![Page 38: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/38.jpg)
The use of “.” – “Element” Operation
K= x^2Erorr:??? Error using ==> mpower Matrix must be square.B=x*yErorr:??? Error using ==> mtimes Inner matrix dimensions must agree.
A = [1 2 3; 5 1 4; 3 2 1]A =
1 2 35 1 43 2 -1
y = A(3 ,:)
y= 3 4 -1
b = x .* y
b=3 8 -3
c = x . / y
c= 0.33 0.5 -3
d = x .^2
d= 1 4 9
x = A(1,:)
x=1 2 3
![Page 39: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/39.jpg)
Determinant of a matrix
![Page 40: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/40.jpg)
Power of a matrix
![Page 41: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/41.jpg)
Power of a matrix
=
![Page 42: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/42.jpg)
Ploting in MATLAB
• Create an x-array of 10 samples between 0 and 1.
• Calculate y function which is given as
• Plot the y
>>x=linspace(0,1,10);
>>y=x;
>>plot(x,y)>>grid
![Page 43: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/43.jpg)
Ploting in MATLAB
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
![Page 44: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/44.jpg)
Ploting in MATLAB
• Now write xlabel('x') ylabel('y') title(‘y=x')
and open the Figure 1 again.
Can you see labels on the axis of plot and title?
![Page 45: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/45.jpg)
Ploting in MATLAB
![Page 46: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/46.jpg)
Ploting in MATLAB
• Write in command window “hold” now.• This command will overwrite the new plot on
to current figure without delete it. • At this step, we will use “stem” command
instead of plot.• Now, write stem(x,y, 'r');• Open Figure 1 again.
![Page 47: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/47.jpg)
Ploting in MATLAB
Open a new figure by using figure comand and do the same plot for 100 points.
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1y=x
y
x
![Page 48: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/48.jpg)
Ploting in MATLAB
• There are several properties of plot command.• To learn these properties, remember that
there is a help command in MATLAB.• Learn how to use the commands related to
plot command such as axis, subplot, ...
![Page 49: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/49.jpg)
Operators (relational, logical)
• == Equal to• ~= Not equal to• < Strictly smaller• > Strictly greater• <= Smaller than or equal to• >= Greater than equal to• & And operator• | Or operator
![Page 50: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/50.jpg)
Flow Control
• if • for • while • break
![Page 51: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/51.jpg)
Control Structures • If Statement Syntax
if (Condition_1)Matlab Commands
elseif (Condition_2)Matlab Commands
elseif (Condition_3)Matlab Commands
elseMatlab Commands
end
Some Dummy Examples
if ((a>3) & (b==5))Some Matlab Commands;
end
if (a<3)Some Matlab Commands;
elseif (b~=5) Some Matlab Commands;
end
if (a<3)Some Matlab Commands;
else Some Matlab Commands;
end
![Page 52: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/52.jpg)
Control Structures
• For loop syntax
for i=Index_ArrayMatlab Commands
end
Some Dummy Examples
for i=1:100Some Matlab Commands;
end
for j=1:3:200Some Matlab Commands;
end
for m=13:-0.2:-21Some Matlab Commands;
end
for k=[0.1 0.3 -13 12 7 -9.3]Some Matlab Commands;
end
![Page 53: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/53.jpg)
Control Structures
• While Loop Syntax
while (condition)Matlab Commands
end
Dummy Example
while ((a>3) & (b==5))Some Matlab Commands;
end
![Page 54: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/54.jpg)
If Examplefunction compare(x,y)
if x>y
display('x is greater than y');
else if x<y
display('y is greater than x');
else
display('x is equal to y');
end % Ending for else if
end % Ending for if
![Page 55: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/55.jpg)
If examplefunction compare(x,y)
if x>y
display('x is greater than y');
else if x<y
display('y is greater than x');
else
display('x is equal to y');
end % Ending for else if
end % Ending for if
![Page 56: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/56.jpg)
“For” example
for i=1:100
x(i)=i^2 ;
end
1
2 3
![Page 57: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/57.jpg)
“For” example
![Page 58: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/58.jpg)
Another Examplex=10*randn(1,100);
for i=1:100
if x(i) > 0
y(i)=1;
elseif x(i)<0
y(i)=-1;
else
y(i)=0;
end
end
![Page 59: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/59.jpg)
Another Example
![Page 60: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/60.jpg)
Another Example
![Page 61: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/61.jpg)
Another Example
• Now, get the same result of y by using the MATLAB function “sign(x)”
• Write in command window “help sign”.• Learn how to use sign command and use it for
determining the sign of elements of x.
![Page 62: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/62.jpg)
MATLAB HW#11. Solve the following linear system
equation using Cramer Rule in MATLAB.
![Page 63: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/63.jpg)
MATLAB HW#12. By applying
find the inverse of following matrix in MATLAB
Verify your result by using “inv” command.
![Page 64: Introduction to MATLAB R2012b - homes.ieu.edu.trhomes.ieu.edu.tr/hozcan/EEE281/Intro_R2012b.pdf · Functions in MATLAB • You do not need always writing new functions. • There](https://reader031.vdocuments.pub/reader031/viewer/2022022604/5b5ecf767f8b9af90c8c90ff/html5/thumbnails/64.jpg)
MATLAB HW#13. Find a matrix X such that AX=B for the given A
and B matrices below