Вы находитесь на странице: 1из 30

V R SIDDHARTHA ENGINEERING COLLEGE

EEE DEPARTMENT

MATLAB Applications to
electrical Engineering
S.V.R.LAKSHMI KUMARI

P.VENKATESH

ASSOCIATE PROFFSSOR

ASSISTANT PROFESSOR

9/2/2015 11:09:45 PM

MATLAB PRESENTATION

Introduction to MATLAB
Matlab Basics
Vectors and Matrices
Plots
Loops
MATLAB examples

Control Flow
If

Conditionally execute statements

Else

IF statement condition

Elseif

IF statement condition

end

Terminate scope of FOR, WHILE, SWITCH, TRY and IF ...

for

Repeat statements a specific number of times

While

Repeat statements an indefinite number of times

Break

Terminate execution of WHILE or FOR loop

Switch

Switch among several cases based on expression

Case

SWITCH statement case

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

Boolean operators in MATLAB


Symbol Meaning

Example

==
~=
>
>=
<
<=
&

if x==1
if x~=0
if x>y
if x>=c
if x<18*s
if x<=11
if (x==1)&(y>3)
if (x==1)|(y>3)
if x~=y

equal
not equal
strictly greater than
greater than or equal to
strictly smaller than
smaller than or equal to
AND
OR
NOT

The operators AND, OR, NOT allow you to combine simpler tests
4

Decision making in MATLAB

9/2/2015 11:09:46 PM

For simple decisions?


IF END `
More complex decisions?
IF ELSEIF ELSE ... END

MATLAB PRESENTATION

Control Structures
If Statement Syntax
if (Condition_1)
Matlab Commands
elseif (Condition_2)
Matlab Commands
elseif (Condition_3)
Matlab Commands
else
Matlab Commands
end
9/2/2015 11:09:46 PM

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

MATLAB PRESENTATION

if thenelse end
EXAMPLE : TO CHECK THE NUMBER EVEN OR ODD
% Generate a random number
%Check whether it is even
%display that it is even if it is even
%display that it is odd if it is not even
a = randi(100, 1);
Disp(a)
if rem(a, 2) == 0 %use shift+enter
disp('a is even')
else
disp('a is odd')
end

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

If and Switch
The switch statement and the if statement are equally powerful,but the
switch statement is especially well suited to handle cases with only a finite set
of choices

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

if-elseif-else construction
if <logical expression>

disp(tall)

<commands>
elseif <logical expression>

elseif height<150

disp(small)

<commands>

else

else

disp(average)

<commands>

end

end

9/2/2015 11:09:46 PM

if height>170

MATLAB PRESENTATION

if-constructs can be interleaved:


if a==b
if b==0
disp(b=0);
end;
else
disp(b is not equal to a);
end;

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

10

Program to calculate your internal marks


MATLAB code:
clear
clc
a1=input('enter assignment1 marks::');
a2=input('enter assignment2 marks::');
s1=input('enter assignment1 marks::');
s2=input('enter assignment2 marks::');
amax=max(a1,a2);
amin=min(a1,a2);
a=((2/3)*amax+(1/3)*amin);
smax=max(s1,s2);
smin=min(s1,s2);
s=((2/3)*smax+(1/3)*smin);
atten=input('enter percentage of attendance::');
if(atten<=75)
atten=0;
elseif((atten>75)&(atten<=80))
atten=3;
elseif((atten>80)&(atten<=90))
atten=4;
else((atten>90)&(atten<=100))
atten=5;
end
ha=input('enter home assignment marks::');
t=a+s+atten+ha;
fprintf('your total marks are %0.1f out of 30::\n',t)

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

11

FOR LOOPS

While Loops

Finite
(will run a specified number of times, as
determined when the loop is first
executed)

Indefinite
(will run until a given logical or relational
condition evaluates to false or 0)

Built in Index
(Matlab will define and Increment an
index automatically)

User Defined Index


(You have to define an index before the
loop and increment the index inside the
body of the loop)

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

12

Control Structures
Examples:

For loop syntax

for i=1:100
Some Matlab Commands;
end

for i=Index_Array
Matlab Commands
end

for j=1:3:200
Some Matlab Commands;
end
for m=13:-0.2:-21
Some Matlab Commands;
end
for k=[0.1 0.3 -13 12 7 -9.3]
Some Matlab Commands;
end

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

13

Program: To initialize each element of a to 1


for i=1:10
a(i)=1;
End

Output :
a=
1

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

14

while loop
While Loop Syntax
while (condition)
Matlab Commands
end

9/2/2015 11:09:46 PM

Dummy Example
while ((a>3) & (b==5))
Some Matlab Commands;
end

MATLAB PRESENTATION

15

Program: To initialize each element of a to 1 using while loop


i=1;
while i<=10
a(i)=1;
i=i+1;
end
disp(a)
Output :
a=
1

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

16

FOR and WHILE


Program to add first 10 numbers :

sum=0;
for i=1:10
sum=sum+i;
end
disp(sum)
Output : 55

sum=0;
i=0;
while i<11
sum=sum+i;
i=i+1;
end
disp(sum)

Switch-case Selection
Switch conditional structures is particularly useful when dealing
with some kind of menu where a variable can take a set of
values and various actions must take place accordingly.
The syntax is:
switch variable
case value1
do this
case value2
do that

end;

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

18

Example of using switch


day=4;

%This corresponds to Thursday=4th day


%after Monday

switch day
case 1 %this effectively means if day==1
disp(Today is Monday);
case 2 % i. e. if day==2
disp(Today is Tuesday);

otherwise
disp(The number should be between 1 and 7)
end;
Note: The otherwise statement is optional. If it isnt there and the variable doesnt
take any of the case values tested then Matlab reaches end and nothing has happened.
9/2/2015 11:09:46 PM

MATLAB PRESENTATION

19

In general, when you have many possible


discrete, known values, switch statements are
easier to read than if statements

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

21

Nested loops
If one loop is completely inside another one ,then
the two loops are called nested loops
Example : calculate the product of two integers
MATLAB CODE :
for i=1:3
for j=1:3
product=i*j;
fprintf('%d*%d=%d\n',i,j,product);
end
end

9/2/2015 11:09:46 PM

MATLAB PRESENTATION

22

Output :
1*1=1
1*2=2
1*3=3
2*1=2
2*2=4
2*3=6
3*1=3
3*2=6
3*3=9
9/2/2015 11:09:46 PM

MATLAB PRESENTATION

23

If a break statement appears inside a set of nested loops


,then the break statement refers to the innermost of the
loops containing it
Example :
for i=1:3
for j=1:3
if j==3;
break;
end
product=i*j;
fprintf('%d*%d=%d\n',i,j,product);
end
fprintf('End of inner loop\n');
end
fprintf('End of outer loop\n');
9/2/2015 11:09:46 PM

MATLAB PRESENTATION

24

Output :
1*1=1
1*2=2
End of inner loop
2*1=2
2*2=4
End of inner loop
3*1=3
3*2=6
End of inner loop
End of outer loop
9/2/2015 11:09:46 PM

MATLAB PRESENTATION

25

Commands and Functions

The break and continue Statements:


Break statement
Example :
for i=1:5
if i==3;
break
end
fprintf('i=%d\n',i);
end
disp('end of loop');

Output:
i=1
i=2
end of loop

continue statement
Example :
for i=1:5
if i==3;
continue
end
fprintf('i=%d\n',i);
end
disp('end of loop');

Output:
i=1
i=2
i=4
i=5
end of loop

Вам также может понравиться