0.7 repetition structures chapter 9 0mimoza.marmara.edu.tr/~byilmaz/es117_lecture10.pdf · the loop...

67
Repetition Structures Chapter 9 10 0 10 1 10 2 10 3 0.5 0.6 0.7 0.8 0.9 1 Value of the Alternating Harmonic Series Number of terms Sum of the terms

Upload: others

Post on 23-Sep-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Repetition Structures Chapter 9

100

101

102

103

0.5

0.6

0.7

0.8

0.9

1Value of the Alternating Harmonic Series

Number of terms

Sum

of

the t

erm

s

Page 2: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Objectives

After studying this chapter you should be able to:

Write and use for loops

Write and use while loops

Create midpoint break structures

Measure the time required to execute program components

Understand how to improve program execution times

Page 3: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Common mistakes,

Do not use turkish letters in variable names

Do not use turkish letters and blanks in m-file names

Do not use two relational operators in one expression, divide the expression and then combine it using logical operators such as and, or or xor etc…

0<x<10 incorrect

x>0 & x<10 correct

Page 4: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Structures

Sequence

Selection

Repetition Sequence Selection Repetition (Loop)

Page 5: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Types of Loops

Loops are used when you need to repeat a set of instructions multiple times

MATLAB supports two types of loops

for

while

Page 6: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Flow Diagram of Loop Choice Process

FOR Loop

WHILE Loop

Page 7: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

When to use loops

In general loops are best used with scalars, or with the values stored in a matrix used one at a time

Many of the problems you may want to attempt with loops can be better solved by vectorizing your code or with MATLAB’s logical functions such as find

Page 8: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

for loop_variable =

start:step:finish

(k = m : s : n)

….

Statements

….

end

Check to see if the

index has been

exceeded

Calculations

True; You’ve run out of

values in the index

matrix

Flow chart

for a for

loop

9.1 For Loops

Page 9: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Note the following rules when using for loops with the loop variable expression k = m:s:n:

· The step value s may be negative.

Example: k = 10:-2:4 produces k = 10, 8, 6, 4.

· If s is omitted, the step value defaults to one.

· If s is positive, the loop will not be executed if m is greater

than n.

· If s is negative, the loop will not be executed if m is less

than n.

· If m equals n, the loop will be executed only once.

· If the step value s is not an integer, round-off errors can

cause the loop to execute a different number of passes

than intended.

Page 10: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

% program to test a for

loop

for i=1:10

disp(i*0.2)

end

>> testloop

0.2

0.4

0.6

2.0

Example: testloop.m

% program to test a for

loop

for i=1:10

disp(i)

end

>> testloop

1

2

3

.

.

10

>> disp(i)

10

The FOR loop

>> disp(i)

10

% program to test a for

loop

for i=1:0.1:2

disp(i*5)

end

>> testloop

5

5.5

6

..

9.5

10

>> disp(i)

2

Page 11: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

A simple example of a for loop is

for k = 5:10:35

x = k^2

end

The loop variable k is initially assigned the value 5, and x

is calculated from x = k^2.

Each successive pass through the loop increments k by

10 and calculates x until k exceeds 35. Thus k takes on the values 5, 15, 25, and 35, and x takes

on the values 25, 225, 625, and 1225.

The program then continues to execute any statements

following the end statement.

4-50

The FOR loop

Page 12: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The FOR loop

Example : Assume we have series of natural numbers

up to 100 and want to find the summation of

these numbers.

•Use a for loop, using the loop variable as

the index

•At every step of the loop, we want to add

the value corresponding to the index.

Summation is done by addition of 1st

elements, obtain partial result, add 2nd

element, obtain partial result, etc….

Loop variable: i

Result variable: sum

% program to test a for loop

sum=0;

for i=1:100

sum=sum+i;

end

disp(sum)

disp([‘result=‘,num2str(sum)])

sum=sum + next value

Page 13: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The FOR loop

Applications and usage of “for” loops

Use loop_variable as a counter

Example : We want to calculate the …… 100 times

•Use a for loop, using the loop variable as the index

of the vector.

•At every step of the loop, we want to add the value

corresponding to the index.

•Operation done by taking the square root of

the preceding result, 100 times

Loop variable: j

Result variable: S

% program to test a for loop

for j=1:100

end

S=sqrt(S)

S=pi; Initialization

S= S

. .

Page 14: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

9.1 For Loops

for index = [matrix]

commands to be executed

end

The loop is executed once for each element of the index

matrix identified in the first line

Page 15: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Here’s a simple example

In this case k is the

index – the loop is

repeated once for

each value of k

the index can be

defined using any

of the techniques

we’ve learned

Page 16: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Here’s a simple example

In this case k is the

index – the loop is

repeated once for

each value of k

the index can be

defined using any

of the techniques

we’ve learned

Page 17: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

One of the most common ways to use a loop is to define a matrix

Page 18: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Hint

Most computer programs do not have MATLAB’s ability to handle

matrices so easily, and therefore rely on loops similar to the one on the

previous slide to define arrays. It would be easier to create the vector a

in MATLAB with the following code

k=1:5

a = k.^2

which returns

k =

1 2 3 4 5

a =

1 4 9 16 25

This is an example of vectorizing the code.

Page 19: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Another use is in combination with an if statement to determine how many times something is true

Each time through

the loop we evaluate

a single element of

the scores matrix

Page 20: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary of the for loop structure

The loop starts with a for statement, and ends with the word end.

The first line in the loop defines the number of times the loops will repeat, using an index number.

The index of a for loop must be a variable. (The index is the number that changes each time through the loop.) Although k is often used as the symbol for the index, any variable name can be used. The use of k is a matter of style.

Page 21: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Any of the techniques learned to define a matrix can be used to define the index matrix. One common approach is to use the colon operator.

for index = start:inc:final

If the expression is a row vector, the elements are used one at a time – once for each time through the loop.

Page 22: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

If the expression is a matrix (not common), each time through the loop the index will contain the next column in the matrix. This means that the index will be a column vector!!

Once you’ve completed a for loop, the index is the last value used.

for loops can often be avoided by vectorizing the code.

Page 23: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The FOR loop

Assume 1000 values

We want to stop the loop when we obtain enough preicison in the

calculation

What will happen if the precision is obtained after 10

calculations?

The FOR loop will not stop until the 1000 .

Page 24: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The while loop is used when the looping process

terminates because a specified condition is satisfied, and

thus the number of passes is not known in advance. A

simple example of a while loop is

x = 5;

while x < 25

disp(x)

x = 2*x - 1;

end

The results displayed by the disp statement are 5, 9, and

17.

The WHILE loop

Page 25: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The typical structure of a while loop follows.

while logical expression

statements

end

For the while loop to function properly, the following

two conditions must occur:

1. The loop variable must have a value before the while

statement is executed.

2. The loop variable must be changed somehow by the

statements.

The WHILE loop

Page 26: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Flowchart of the

while loop.

Page 27: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The WHILE loop

% program to test while loop

while i<10

i=i+1;

disp(i)

end

>> exloop1

1

2

3

4

5

6

7

8

9

10

>> disp(i)

10

% program test

for i=1:10

disp(i)

end

i=0;

Example: exloop1.m

Have to

create a

counter

Page 28: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The WHILE loop

>> exloop2

9.5000 2.2513

9.0000 2.1972

8.5000 2.1401

8.0000 2.0794

7.5000 2.0149

7.0000 1.9459

6.5000 1.8718

6.0000 1.7918

5.5000 1.7047

5.0000 1.6094

4.5000 1.5041

4.0000 1.3863

3.5000 1.2528

3.0000 1.0986

2.5000 0.9163

2.0000 0.6931

1.5000 0.4055

1 0

% program to test while loop

x=10;

while x>1

x=x-0.5;

y=log(x);

disp([x,y])

end

Example: exloop2.m

Page 29: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The WHILE loop

>> exloop3

% BAD while loop

x=1;

while x>=0

x=x+0.5;

y=sin(x);

end

disp(‘END of program’)

Example: exloop3.m

Need to stop the script manually !!!

CTRL C

This is an Infinite loop !!!!

>>

% BAD while loop

x=1;

while x>=0

x=x+0.5;

y=sin(x);

end

disp(‘END of program’)

Page 30: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The WHILE loop

Example : exloop4.m

Calculate the sum of manually entered

numbers. Entering 0 or a negative number

stops the loop and display the average.

•Need input statement:

•Use the length of A in the logical_expression

•Inside the loop:

•Increment a counter to count how many

value we entered

•Add the value A to the sum S

A>0

% program to test a while loop

while

end

disp(S/i)

Use while loop when the number of operation is unknown

variable: A

S=S+A

i=i+1

>> testloop Value for A:2

Value for A:2

Value for A:2

Value for A:2

Value for A:0

2

A>0

i=i+1;

S=S+A;

S=0;

i=0;

>> testloop Value for A:1

Value for A:5

Value for A:8

Value for A:0

4.6667

A=input('Value for A:');

A=input('Value for A:');

Page 31: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The WHILE loop

Example 5: Series convergence: π2/6

Want to see how many terms you need to obtain

an error of 3x10-6.

% Convergence script

while

end

disp(['N=',num2str(i)])

Use logical_expression for convergence limit

N

1ii

12

>> Testloop

N=333333

•Need to define and use an error variable.

•Need to be part of the logical expression

•Need to be updated during loop

•At every step of the loop, we want to

•Verify if test is true

•Increment counter

•Add the new term to the sum

err

S=S+ 1/i^2

i=i+1

err>3e-6

err=abs(S-pi^2/6);

i=i+1;

S=S+ 1/i^2;

S=0;

i=0;

err=10;

(err>3e-6)

Page 32: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The WHILE loop

% Convergence script

S=0;

i=0;

err=10;

while err>3e-6

i=i+1;

S=S+ 1/i^2;

err=abs(S-pi^2/6); A(i)=err;

end

disp(['N=',num2str(i)])

Use logical_expression for convergence limit

>> Testloop

N=333333

Page 33: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

We have to increment

the counter (in this

case k) every time

through the loop – or

the loop will never

stop!!

Page 34: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

This loop creates the

matrix a, one element

at a time

Page 35: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

This program counts

how many scores in

the array are greater

than 90, and displays

the result in the

command window

Page 36: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Hint

If you accidentally create a loop that just keeps running you should

1. Confirm that the computer is actually still calculating something by checking the lower left hand corner of the MATLAB window for the “busy indicator”

2. Make sure the active window is the command window and exit the calculation manually with ctrl c

Page 37: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

break and continue

break causes the loop to terminate prematurely

continue causes MATLAB to skip a pass through the loop, but continue on until the criteria for ending is met

both are used in conjunction with an if statement

Page 38: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The “BREAK” statement

BREAK

•Break terminates the execution

of a for or while loop. Statements

in the loop that appear after the

break statement are not

executed.

•In nested loops, break exits only

from the loop in which it occurs.

Control passes to the statement

that follows the end of that loop.

% BAD while loop

x=1;

while x>=0

x=x+0.5;

y=sin(x);

end

if x>10000

break

end

Page 39: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

This program prompts the user 10 times

to enter a value, and computes the

natural log.

If a negative value is entered, the break

command causes MATLAB to exit the

loop

Page 40: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Notice that n had a value of 3

when the program terminated – if

it had run to completion it would

have had a value of 10

Page 41: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The “CONTINUE” statement

CONTINUE

• Continue passes control to

the next iteration of the for or

while loop in which it appears,

skipping any remaining

statements in the body of the

loop.

% Problem of division by 0

x=1;

for i= -10:10

y=1/x;

end

if x==0

continue

end

Page 42: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Notice that n had a value of 10

when the program terminated

Page 43: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

9.4 Midpoint Break Loops

Implemented with either a for or a while loop

The loop is

Entered

Calculations processed

Decision is made at an arbitrary point whether or not to continue

Page 44: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

while criterion

do some calculations

make a decision on whether to continue

do some more calculations

end

Page 45: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Results in a

continuous loop

Causes the loop

to terminate

Page 46: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the
Page 47: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Limits the number

of repetitions

Page 48: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Nested loops and combination

Example: exloop6.m Calculate the sum of factorials up to 20

•Need a for loop for sums

•Need a for loop for factorials

•Calculate the factorial of element j

•Do the sum on all elements

% program to test nested loops

S=0;

for i=1:20

F=1;

for j=1:i

F=F*j;

end

S=S+F

end

disp(S)

Using nested loops

S=S+F

j

i

!20....!4!3!2!1

F=F*j

Page 49: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

9.5 Nested Loops

It is often useful to nest loops inside of other loops

To illustrate, consider code to mimic the built-in max function

Page 50: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the
Page 51: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the
Page 52: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

9.6 Improving the Efficiency of Loops

In general, using a for loop (or a while loop) is less efficient in MATLAB than using array operations.

Page 53: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

The amount of time it takes to

run this code will depend on

your computer, and how much

else you have installed on it

Here’s an example. This

code creates a 4,000,000

element matrix of ones,

then multiplies each

element in the matrix by pi

These two lines of code

start a timer to measure

the elapsed time required

to run the lines of MATLAB

code between them

Page 54: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

This code accomplishes the same

thing with a for loop

Page 55: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Most of the time required to run the

previous problem was used to create

the B matrix one element at a time

If we predefine the B matrix and then

replace each element one at a time

with the new value, the run time is

significantly reduced

Page 56: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

An alternate syntax

for timing uses the

tic and toc functions

Page 57: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Hint

Be sure to suppress intermediate calculations when you use a loop.

Printing those values to the screen will greatly increase the amount of execution time.

If you are brave, repeat the example above, but delete the semicolons inside the loop just to check out this claim.

Don’t forget that you can stop the execution of the program with ctrl c. (But the command window needs to be active)

Page 58: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary

Sections of computer code can be categorized as

sequences

selection structures

repetition structures

Page 59: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary – Sequence

Sequences are lists of instructions that are executed in order

Page 60: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary – Selection Structure

Selection structures allow the programmer to define criteria (conditional statements) which the program uses to choose execution paths

Page 61: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary – Repetition Structures

Repetition structures define loops where a sequence of instructions is repeated until some criterion is met (also defined by conditional statements).

Page 62: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary – Relational Operators

MATLAB uses the standard mathematical relational operators <

<=

>

>=

==

~= Recall that = is the assignment operator, and

can not be used for comparisons

Page 63: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary – Logical Operators

MATLAB uses the standard logical operators

&& and

|| or

~ not

xor exclusive or

Page 64: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary - Loops

MATLAB supports both for loops

while loops

For loops are primarily used when the programmer knows how many times a sequence of commands should be executed.

While loops are used when the commands should be executed until a condition is met.

Most problems can be structured so that either for or while loops are appropriate.

Page 65: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary break and continue

These commands are used to exit a loop prematurely and are widely implemented in midpoint break loops

break causes the program to jump completely out of a loop and continue execution of the remainder of the program

continue skips execution of the current pass through a loop, but allows the loop to continue until the completion criteria is met

Page 66: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary - Vectorization

Vectorization of MATLAB code allows it to execute much more efficiently, and therefore more quickly.

Loops in particular should be avoided in MATLAB, although this is not always possible

When loops are unavoidable they can be improved by defining “dummy” variables with placeholder values, such as ones or zeros.

These placeholders can then be replaced in the loop resulting in significant improvements in execution time, which can be confirmed with timing experiments.

Page 67: 0.7 Repetition Structures Chapter 9 0mimoza.marmara.edu.tr/~byilmaz/ES117_Lecture10.pdf · The loop starts with a for statement, and ends with the word end. The first line in the

Summary – timing functions

The clock and etime functions are used to poll the computer clock, and then determine the time required to execute pieces of code

The time calculated is the “elapsed” time. During this time the computer has not only been running MATLAB code, but has also been executing background jobs and housekeeping functions.

The tic and toc functions perform a similar task.