matlab loops

Upload: venki249

Post on 10-Jan-2016

231 views

Category:

Documents


1 download

DESCRIPTION

loops in MATLAB

TRANSCRIPT

  • MATLAB Applications to electrical Engineering

    9/2/2015 11:09:45 PM MATLAB PRESENTATION 1

    V R SIDDHARTHA ENGINEERING COLLEGE

    EEE DEPARTMENT

    S.V.R.LAKSHMI KUMARI ASSOCIATE PROFFSSOR

    P.VENKATESH ASSISTANT PROFESSOR

  • 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 3 MATLAB PRESENTATION

  • Boolean operators in MATLAB Symbol Meaning Example

    ==

    ~=

    >

    >=

    <

    y

    if x>=c

    if x3)

    if x~=y

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

  • Decision making in MATLAB

    For simple decisions?

    IF END `

    More complex decisions?

    IF ELSEIF ELSE ... END

    9/2/2015 11:09:46 PM 5 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

    if ((a>3) & (b==5)) Some Matlab Commands; end if (a

  • 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 7 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 8 MATLAB PRESENTATION

  • if-elseif-else construction

    if

    elseif

    else

    end

    if height>170

    disp(tall)

    elseif height

  • 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 10 MATLAB PRESENTATION

  • 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(atten75)&(atten80)&(atten90)&(atten
  • 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 12 MATLAB PRESENTATION

  • Control Structures

    For loop syntax

    for i=Index_Array

    Matlab Commands

    end

    Examples: for i=1:100 Some 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 13 MATLAB PRESENTATION

  • Program: To initialize each element of a to 1 for i=1:10 a(i)=1; End Output : a = 1 1 1 1 1 1 1 1 1 1

    9/2/2015 11:09:46 PM 14 MATLAB PRESENTATION

  • while loop

    While Loop Syntax

    while (condition)

    Matlab Commands

    end

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

    9/2/2015 11:09:46 PM 15 MATLAB PRESENTATION

  • Program: To initialize each element of a to 1 using while loop i=1; while i
  • FOR and WHILE

    sum=0;

    for i=1:10

    sum=sum+i;

    end

    disp(sum)

    sum=0; i=0; while i

  • 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 18 MATLAB PRESENTATION

  • 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 19 MATLAB PRESENTATION

  • 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:

    Example :

    for i=1:5

    if i==3;

    break

    end

    fprintf('i=%d\n',i);

    end

    disp('end of loop');

    Break statement

  • 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