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

More Related Content

PPT
Loops in matlab
PDF
Introduction to Numerical Analysis
PPT
Linear Algebra and Matrix
PPTX
Matrices and System of Linear Equations ppt
PPT
Presentation on laplace transforms
PPTX
Engineering Numerical Analysis Lecture-1
PPTX
Introduction to MATLAB
PPTX
MATLAB - Arrays and Matrices
Loops in matlab
Introduction to Numerical Analysis
Linear Algebra and Matrix
Matrices and System of Linear Equations ppt
Presentation on laplace transforms
Engineering Numerical Analysis Lecture-1
Introduction to MATLAB
MATLAB - Arrays and Matrices

What's hot (20)

PPT
introduction to Numerical Analysis
PPTX
vector space and subspace
PPTX
Impulse Response ppt
PPT
numerical methods
PPTX
Signal Flow Graph, SFG and Mason Gain Formula, Example solved with Masson Gai...
PDF
Linear algebra-Basis & Dimension
PDF
Linear transformations and matrices
PPTX
Gaussian Elimination Method
PDF
Modeling of mechanical_systems
PDF
Lesson02 Vectors And Matrices Slides
PDF
Iaetsd modelling and controller design of cart inverted pendulum system using...
PPT
Matlab Tutorial.ppt
PPTX
Conditional Control in MATLAB Scripts
PPT
LinearAlgebra.ppt
PDF
transientanalysis-170603060752.pdf
PDF
Basics of matlab
PDF
Vector Calculus.
PPTX
Eigenvalue problems .ppt
PDF
SIGNAL OPERATIONS
PPT
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
introduction to Numerical Analysis
vector space and subspace
Impulse Response ppt
numerical methods
Signal Flow Graph, SFG and Mason Gain Formula, Example solved with Masson Gai...
Linear algebra-Basis & Dimension
Linear transformations and matrices
Gaussian Elimination Method
Modeling of mechanical_systems
Lesson02 Vectors And Matrices Slides
Iaetsd modelling and controller design of cart inverted pendulum system using...
Matlab Tutorial.ppt
Conditional Control in MATLAB Scripts
LinearAlgebra.ppt
transientanalysis-170603060752.pdf
Basics of matlab
Vector Calculus.
Eigenvalue problems .ppt
SIGNAL OPERATIONS
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
Ad

Viewers also liked (20)

PPTX
MATLAB Programming - Loop Control Part 2
PPTX
Anonymous and Inline Functions in MATLAB
PPTX
User Defined Functions in MATLAB part 2
PPTX
MATLAB Scripts - Examples
PPTX
User defined Functions in MATLAB Part 1
PPTX
User Defined Functions in MATLAB Part-4
PDF
Matlab loops
PPTX
Abstract Class Presentation
PPTX
Mat lab workshop
PDF
Matlab lec1
DOC
Jumping statements
DOCX
Matlab time series example
PPTX
Introduction to Matlab Scripts
PDF
Circuit analysis i with matlab computing and simulink sim powersystems modeling
PPTX
Do While and While Loop
PPTX
Cruise control simulation using matlab
PDF
Reduction of multiple subsystem [compatibility mode]
PPTX
Polynomials and Curve Fitting in MATLAB
PPTX
While , For , Do-While Loop
PPTX
Matlab solving rlc circuit
MATLAB Programming - Loop Control Part 2
Anonymous and Inline Functions in MATLAB
User Defined Functions in MATLAB part 2
MATLAB Scripts - Examples
User defined Functions in MATLAB Part 1
User Defined Functions in MATLAB Part-4
Matlab loops
Abstract Class Presentation
Mat lab workshop
Matlab lec1
Jumping statements
Matlab time series example
Introduction to Matlab Scripts
Circuit analysis i with matlab computing and simulink sim powersystems modeling
Do While and While Loop
Cruise control simulation using matlab
Reduction of multiple subsystem [compatibility mode]
Polynomials and Curve Fitting in MATLAB
While , For , Do-While Loop
Matlab solving rlc circuit
Ad

Similar to Matlab Script - Loop Control (20)

PPTX
Loops in c language
PPTX
Loops in c language
PPT
Mesics lecture 7 iteration and repetitive executions
PPT
Iteration
PPT
Verilog Lecture3 hust 2014
PPTX
DOCX
Programming Fundamentals lecture 8
PPT
Java căn bản - Chapter6
PDF
Loop and while Loop
PPTX
Ch6 Loops
PPTX
Iterations FOR LOOP AND WHILE LOOP .pptx
PDF
Programming Fundamentals presentation slide
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
PDF
C++ control structure
PPTX
Loops c++
PPTX
PPTX
Lec7 - Loops updated.pptx
PPTX
Iterative control structures, looping, types of loops, loop working
PDF
[ITP - Lecture 11] Loops in C/C++
PPTX
Lecture 4
Loops in c language
Loops in c language
Mesics lecture 7 iteration and repetitive executions
Iteration
Verilog Lecture3 hust 2014
Programming Fundamentals lecture 8
Java căn bản - Chapter6
Loop and while Loop
Ch6 Loops
Iterations FOR LOOP AND WHILE LOOP .pptx
Programming Fundamentals presentation slide
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
C++ control structure
Loops c++
Lec7 - Loops updated.pptx
Iterative control structures, looping, types of loops, loop working
[ITP - Lecture 11] Loops in C/C++
Lecture 4

Recently uploaded (20)

PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
My India Quiz Book_20210205121199924.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Race Reva University – Shaping Future Leaders in Artificial Intelligence
Computer Architecture Input Output Memory.pptx
Uderstanding digital marketing and marketing stratergie for engaging the digi...
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Journal of Dental Science - UDMY (2021).pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Unit 4 Computer Architecture Multicore Processor.pptx

Matlab Script - Loop Control

  • 2. 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
  • 3. 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
  • 4. 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. 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. 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
  • 7. 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
  • 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 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
  • 12. 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
  • 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 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
  • 15. 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
  • 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