KEMBAR78
Matlab Script - Loop Control | PPTX
electricalenggtutorial.blogspot.com
1
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 techniques
1. Counted loops - executes commands a
specified number of times
2. Conditional loops - executes commands
as long as a specified expression is
true
2
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
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
statements
end
4
N=10;
for I = 1:N
A(I) = 1/(I+J-1);
end
for
j=1:10
Statements
done
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
while
I<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
statements
end
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
FOR
Repeats for specified
number of times
ALWAYS executes
computation loop at
least once!!!
Can use + or –
increments
Can escape (BREAK)
out of
computational loop
WHILE
Will 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!!!
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
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
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
10
for 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;
end
disp(s)
9
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 n
n=input ('enter any integer ');
f=1;
for i=1: n
f=f*i;
end
disp(f)
4. Find the number of positive numbers in a vector
x = input( 'Enter a vector: ' );
count = 0;
for ii = 1:length(x),
if ( x(ii) > 0 ),
count = count + 1;
end
end
fprintf('Number of positive numbers is %dn',
count);
10
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
 





 
msforteVVV
mstforV
tV RCtt
cfcicf
ci
c
5.1)(
5.10
/)( 0
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
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.
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
13
1. Loop evaluate conditional expression
2. 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
variable
There must be some value of the variable
that makes the conditional expression be
false
14
THE WHILE LOOP …
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
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 +1
end
2. A program to display powers of 2.
x = 1
while x <= 15
x = 2*x
end
16
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 betwee
n 1 and 10 (1-10)');
while ( value < 1 || value > 10)
fprintf('Incorrect input, please try again
.n');
value = input ('Enter a Number between 1 a
nd 10 (1-10)');
end
17
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

Matlab Script - Loop Control

  • 1.
  • 2.
    INTRODUCTION In this lecturewe 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 techniques 1. Counted loops - executes commands a specified number of times 2. Conditional loops - executes commands as long as a specified expression is true 2 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
  • 3.
    INTRODUCTION …. • Countedloops 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 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
  • 4.
    FOR LOOP SYNTAX forvarname = min:max statements end 4 N=10; for I = 1:N A(I) = 1/(I+J-1); end for j=1:10 Statements done 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.
    5 WHILE LOOP SYNTAX while I<N computations done changeI Initialize I, N I=1; N=10; while I<=N A(I)=1/(I+1); I=I+1; end while condition is true statements end 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.
    6 FOR VS WHILE FOR Repeatsfor specified number of times ALWAYS executes computation loop at least once!!! Can use + or – increments Can escape (BREAK) out of computational loop WHILE Will 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!!! 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
  • 7.
    THE FOR LOOP Other Forms 7 el 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
  • 8.
    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 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
  • 9.
    FOR LOOP EXAMPLES 1.A program to print the numbers from 1 to 10 for 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; end disp(s) 9 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
  • 10.
    FOR LOOP EXAMPLES 3.Calculate the factorial of any number n n=input ('enter any integer '); f=1; for i=1: n f=f*i; end disp(f) 4. Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x), if ( x(ii) > 0 ), count = count + 1; end end fprintf('Number of positive numbers is %dn', count); 10 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
  • 11.
    FOR LOOP ELECTRICALENGINEERING 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          msforteVVV mstforV tV RCtt cfcicf ci c 5.1)( 5.10 /)( 0 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
  • 12.
    FOR LOOP ELECTRICALENGINEERING 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 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
  • 13.
    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. 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 13
  • 14.
    1. Loop evaluateconditional expression 2. 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 variable There must be some value of the variable that makes the conditional expression be false 14 THE WHILE LOOP … 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
  • 15.
    INFINITE LOOP If theconditional 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 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
  • 16.
    WHILE LOOP EXAMPLES 1.A program to print the numbers from 1 to 10 i = 1; while i<=10 disp(i) i = i +1 end 2. A program to display powers of 2. x = 1 while x <= 15 x = 2*x end 16 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
  • 17.
    WHILE LOOP EXAMPLES… 3. Program ask the user to input a number between 1 and 10 value = input ('Please Enter a Number betwee n 1 and 10 (1-10)'); while ( value < 1 || value > 10) fprintf('Incorrect input, please try again .n'); value = input ('Enter a Number between 1 a nd 10 (1-10)'); end 17 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