matlab script - loop control

17
MATLAB SCRIPTS LOOP CONTR O L – FOR, WHILE electricalenggtutorial.blogspot.com 1

Upload: shameer-ahmed-koya

Post on 08-Jan-2017

441 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Matlab Script - Loop Control

MATLAB SCRIPT

S

LOOP CONTROL –

FOR,

WHIL

E

electricalenggtutorial.blogspot.com

1

Page 2: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

INTRODUCTION

In this lecture we will discuss about another flow control method – Loop control.

A loop control is used to execute a set of commands repeatedly

The set of commands is called the body of the loop

MATLAB has two loop control techniques1. Counted loops - executes commands a

specified number of times2. Conditional loops - executes commands as

long as a specified expression is true

2

Page 3: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

INTRODUCTION ….

• Counted loops are called ‘for’ loop• Conditional loops are called ‘while’ loop

• Both Scripts and functions also allow to loop using for and while loops.

• The keyword “end” is used to show the end of a Loop

• In while loops the Code for each condition is separated by the keyword “end”

3

Page 4: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

FOR LOOP SYNTAX

for varname = min:max statementsend

4

N=10;for I = 1:NA(I) = 1/(I+J-1);

end

forj=1:10

Statements

done

Page 5: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

5 WHILE LOOP SYNTAX

whileI<N

computations

done

change I

Initialize I, N

I=1; N=10;while I<=N A(I)=1/(I+1);I=I+1;

end

while condition is true statementsend

Page 6: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

6 FOR VS WHILE

FORRepeats for specified

number of timesALWAYS executes

computation loop at least once!!!

Can use + or – increments

Can escape (BREAK) out of computational loop

WHILEWill do computational loop ONLY if while condition is met

Be careful to initialize while variable

Can loop forever if while variable is not updated within loop!!!

Page 7: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

THE FOR LOOP

Other Forms

7

Page 8: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

THE FOR LOOP …

• Matlab creates vector from 1 with an increment of 1,([ 1 2 … 10]), and execute the commands for each value of ii.

• Matlab creates a vector with an increment of 2, as [ 1 3 5 …9],and execute the commands for each value of ii.

• Here the values of ii are defined by user as [5 9 7]. The commands are executed for these specific values.

• Here the increment is negative

8

Page 9: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

FOR LOOP EXAMPLES

1. A program to print the numbers from 1 to 10for i=1:10 disp(i)end 2. A program to calculate the sum of squares of numbers from 1 to n.

n=input('plz enter the no: ');s=0;for i=1:n s=s+i^2;enddisp(s)

9

Page 10: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

FOR LOOP EXAMPLES

3. Calculate the factorial of any number nn=input ('enter any integer ');f=1;for i=1: n

f=f*i;enddisp(f)4. Find the number of positive numbers in a vectorx = input( 'Enter a vector: ' );count = 0;for ii = 1:length(x),

if ( x(ii) > 0 ), count = count + 1; end

endfprintf('Number of positive numbers is %d\n', count);

10

Page 11: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

FOR LOOP ELECTRICAL ENGINEERING APPLICATION

Where Vci is the initial capacitor voltage; Vcf is the voltage the capacitor will reach if it charges for an infinite amount of time.

11

Plot the switching response of a given RC circuit

msforteVVVmstforV

tV RCttcfcicf

cic 5.1)(

5.10/)( 0

Page 12: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

FOR LOOP ELECTRICAL ENGINEERING APPLICATION …

Vci = input('Enter initial capacitor voltage, Vci: ');

Vcf = input('Enter Final capacitor voltage, Vcf: ');

R = input('Enter Resistance value, R: ');

C = input('Enter Capacitance value, C: ');

t0 = input('Enter Switching time, t0: ');

tf = input('Enter Simulation end time, tf: ');

t=linspace(0,tf,1000);

Vc=zeros(1,1000);

for i=1:1000

if t(i)<t0

Vc(i)=Vci;

else Vc(i)=Vcf+(Vci-Vcf)*exp(-(t(i)-

t0)/(R*C));

end

end

plot(t*1000,Vc);

title('RC Step Response')

ylabel('Capacitor voltage')

xlabel('time in msec')

grid on

12

Page 13: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

THE WHILE LOOP

• The while- loop used when the number of loop iterations is unknown

• Number of iteration is controlled by a condition.• We can test the condition in each iteration and

stop looping when it is false. • For example,•Keep reading data from a file until you reach the end of the file•Keep adding terms to a sum until the difference of the last two terms is less than a certain amount.

13

Page 14: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

1. Loop evaluate conditional expression2. If conditional-expression is true,

executes code in body, then goes back to Step 1

3. If conditional-expression is false, skips the body and goes to code after end-statement

Note:Body of loop must change value of variableThere must be some value of the variable that makes the conditional expression be false

14 THE WHILE LOOP …

Page 15: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

INFINITE LOOP

If the conditional expression never becomes false, the loop will keep executing... forever! This condition is called as an infinite loop. This can happen in different ways• No proper condition statement• The condition variable is not updated in the body of loop

If your program gets caught in an infinite loop, Press CTRL+C

15

Page 16: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

WHILE LOOP EXAMPLES

1. A program to print the numbers from 1 to 10

i = 1;while i<=10 disp(i)i = i +1end 2. A program to display powers of 2.x = 1while x <= 15 x = 2*xend

16

Page 17: Matlab Script - Loop Control

e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m

WHILE LOOP EXAMPLES …

3. Program ask the user to input a number between 1 and 10 

value = input ('Please Enter a Number between 1 and 10 (1-10)'); while ( value < 1 || value > 10)   fprintf('Incorrect input, please try again.\n');   value = input ('Enter a Number between 1 and 10 (1-10)'); end

17