Top Banner
Matlab for Engineers 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 R ate ofC hange tim e, hour Rate of tem perature change, degrees/hour Logical Functions and Control Structures Chapter 8
108

Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Jan 04, 2016

Download

Documents

Hubert Matthews
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Logical Functions and Control Structures

Chapter 8

Page 2: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

In this chapter we’ll introduce the…

• relational and logical operators

• find function

• if/else family of commands

• switch/case structure

• for and while loops

Page 3: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Structures

• Sequence• Selection• Repetition

Sequence Selection Repetition (Loop)

Page 4: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Sequence and Repetition structures require comparisons to work

• Relational operators make those comparisons

• Logical operators allow us to combine the comparisons

Page 5: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Relational Operators

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to

== Equal to

~= Not equal to

Page 6: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Comparisons are either true or false

• Most computer programs use the number 1 for true and 0 for false

The results of a comparison are used in selection structures and repetition structures to make choices

Page 7: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Matlab compares corresponding elements and determines if the result is true or false for each

Page 8: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

In order for Matlab to decide a comparison is true for an entire matrix, it must be true for every element in the matrix

Page 9: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Logical Operators

& and

~ not

| or

xor exclusive or

Page 10: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Flow Charts and Pseudo-Code

• As you write more complicated programs it becomes more and more important to plan your code before you write it

• Flow charts – graphical approach

• Pseudo-code – verbal description

Page 11: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Pseudo-code

• Outline a set of statements describing the steps you will take to solve a problem

• Convert these steps into comments in an M-file

• Insert the appropriate Matlab code into the file between the comment lines

Page 12: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Pseudo-code Example

• You’ve been asked to create a program to convert miles/hr to ft/s. The output should be a table, complete with title and column headings

Page 13: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Outline the steps

• Define a vector of mph values

• Convert mph to ft/s

• Combine the mph and ft/s vectors into a matrix

• Create a table title

• Create column headings

• Display the table

Page 14: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Convert the steps to M-file comments

Page 15: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Insert the Matlab code between the comments

Page 16: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Flow Charting

• Especially appropriate for more complicated programs

• Create a big picture graphically

• Convert to pseudo-code

Page 17: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Simple Flow Chart Symbols

• An oval indicates the beginning of a section of code

• A parallelogram indicates an input or output

• A diamond indicates a decision point

• Calculations are placed in rectangles

Page 18: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Start

Define a vector of miles/hour

Calculate the ft/sec vector

Combine into a table

Create an output table using disp and fprintf

End

This flowchart represents the mph to ft/s problem

Page 19: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Logical Functions

• Matlab offers traditional programing selection structures• if• if/else• switch/case

• Series of logical functions that perform many of the same tasks

Page 20: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

find

• The find command searches a matrix and identifies which elements in that matrix meet a given criteria.

Page 21: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

For example…

• The US Naval Academy requires applicants to be at least 5’6” tall

• Consider this list of applicant heights

• 63”, 67”, 65”, 72”, 69”, 78”, 75”

• Which applicants meet the criteria?

Page 22: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

The find function returns the index number for elements that meet a criteria

Page 23: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

index numbers

element values

Page 24: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Page 25: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

You could use the disp and fprintf functions in this program to create a

more readable report

Page 26: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

You could also make a table of those who do not meet the height requirement

Page 27: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

By combining relational and logical operators you can create fairly

complicated search criteria

• Assume applicants must be at least 18 years old and less than 35 years old

• They must also meet the height requirement

Page 28: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Applicant pool

Height Age Inches years 63 18 67 19 65 18 72 20 69 36 78 34 75 12

Page 29: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Let’s use Pseudo-code to plan this program

• Create a 7x2 matrix of applicant height and age information

• Use the find command to determine which applicants are eligible

• Use fprintf to create a table of results

Page 30: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

This is the M-file program to determine who is eligible

Page 31: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Because we didn’t suppress all the output, the intermediate calculations were sent to the command window

Page 32: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

The find command can return either…

• A single index number identifying an element in a matrix

• A matrix of the row numbers and the column numbers identifying an element in a matrix• You need to specify two results if you want

the row and column designation• [row, col] = find( criteria)

Page 33: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Imagine you have a matrix of patient temperature values

measured in a clinic

Station 1 Station 2 Station 3

95.3 100.2 98.6

97.4 99.2 98.9

100.1 99.3 97

Page 34: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Use the find command to determine which patients have elevated

temperatures

These elements refer to the single index number identification scheme

1 4 7

2 5 8

3 6 9

Page 35: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

If we want the row and column…

1,1 1,2 1,3

2,1 2,2 2,3

3,1 3,2 3,3

Page 36: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Using fprintf we can create a more readable report

Page 37: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Flow charting and Pseudo-code for find CommandsStart

Define a vector of x values

Find the index numbers in the x matrix for values greater than 9

Use the index numbers to find the x values

Create an output table using disp and fprintf

End

%Define a vector of x values x=[1,2,3; 10, 5,1; 12,3,2;8, 3,1]%Find the index numbers of the values in %x >9 element = find(x>9)%Use the index numbers to find the x %values greater than 9 by plugging them %into x values = x(element)% Create an output table disp('Elements greater than 9') disp('Element # Value') fprintf('%8.0f %3.0f \n', [element';values'])

Page 38: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Page 39: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Section 8.4Selection Structures

• Most of the time the find function should be used instead of an if

• However, there are certain situations where if is the appropriate process to use

Page 40: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Simple if

if comparison statementsendFor example….

if G<50 count = count +1; disp(G);end

Page 41: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

If statements

• Easy to interpret for scalars

• What does an if statement mean if the comparison includes a matrix?• The comparison is only true if it is

true for every member of the array

Page 42: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Consider this bit of code

G=[30,55,10]

if G<50

count = count +1;

disp(G);

end

The code inside the if statement is not executed, because the comparison is not true!!

Page 43: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

This statement is false because at least one of the elements in G has a value >= 50

The value of count remains unchanged, because the statements inside the if structure are not executed

Page 44: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

This statement is true because all of the elements in G are < 50

The value of count increments by 1, because the statements inside the if structure not executed

Page 45: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

The if/else structure

• The simple if triggers the execution of a block of code if a condition is true

• If it is false that block of code is skipped, and the program continues without doing anything

• What if instead you want to execute an alternate set of code if the condition is false?

Page 46: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Block of code to excute if the

comparison is true

ComparisonTrue False

Block of code to excute if the

comparison is false

Flow chart of an if/else structure

Page 47: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Use an if structure to calculate a natural log

• Check to see if the input is positive• If it is, calculate the natural log• If it isn’t, send an error message to

the screen

Page 48: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

M-file Program

Page 49: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Interactions in the Command Window

Page 50: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

The if/else/elseif structure

• Use the elseif for multiple selection criteria

• For example• Write a program to determine if an

applicant is eligible to drive

Page 51: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Start

if age<16 TrueSorry – You’ll have to wait

age<18You may have a youth license

age<70

True

You may have a standard license

True

Drivers over 70 require a special license

End

elseif

elseif

else

Page 52: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Page 53: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Always test your programs – making sure that you’ve covered all the possible calculational paths

Page 54: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

switch/case

• This structure is an alternative to the if/else/elseif structure

• The code is generally easier to read• This structure allows you to choose

between multiple outcomes, based on some criterion, which must be exactly true

Page 55: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

When to use switch/case

• The criterion can be either a scalar (a number) or a string.

• In practice, it is used more with strings than with numbers.

Page 56: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

switch variablecase option1

code to be executed if variable is exactly equal to option 1

case option2code to be executed if variable is exactly

equal to option 2…

case option_ncode to be executed if variable is exactly equal to option n

otherwisecode to be executed if variable is not equal to any of the options

end

The structure of switch/case

Page 57: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Suppose you want to determine what the airfare is

to one of three cities

Page 58: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Page 59: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

You can tell the input command to expect a string

by adding ‘s’ in a second field.

Page 60: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Page 61: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Menu

• The menu function is often used in conjunction with a switch/case structure.

• This function causes a menu box to appear on the screen with a series of buttons defined by the programmer.

input = menu(‘Message to the user’,’text for button 1’,’text for button 2’, etc)

Page 62: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

• Because the input is controlled by a menu box, the user can’t accidentally enter a bad choice

• This means you don’t need the otherwise portion of the switch/case structure

Page 63: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Note that the otherwise portion of the switch/case structure wasn’t used

Page 64: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

When you run this code a menu box appears

Instead of entering your choice from the command window, you select one of the buttons from the menu

Page 65: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

If I select Honolulu…

Page 66: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Section 8.5Repetition Structures - Loops

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

• Matlab supports two types of loops• for• while

Page 67: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

When to use loops

• In general loops are best used with scalars

• 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 68: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 69: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Check to see if the index has been exceeded

Calculations

You’ve run out of values in the index matrix

Flow chart for a for loop

Page 70: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Here’s a simple exampleIn 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 71: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Here’s a simple exampleIn 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 72: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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

Page 73: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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:5a = k.^2

which returnsk = 1 2 3 4 5a = 1 4 9 16 25This is an example of vectorizing the code.

Page 74: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 75: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 76: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

• 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 77: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

• 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 78: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

While Loops

• While loops are very similar to for loops.

• The big difference is the way Matlab decides how many times to repeat the loop.

• While loops continue until some criterion is met.

while criterion commands to be executedend

Page 79: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Check to see if the criterion is still true

Calculations

The criterion is no longer true and the program exits the loop

Flow Chart for a while loop

Page 80: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

We have to increment the counter (in this case k) every time through the loop – or the loop will never stop!!

Page 81: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

This loop creates the matrix a, one element at a time

Page 82: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

This program counts how many scores in the array are greater than 90, and displays the result in the command window

Page 83: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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. Exit the calculation manually with ctrl c

Page 84: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 85: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 86: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

To run this video, click anywhere on the screen. To move to the next slide, use the down arrow on your keyboard

Page 87: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 88: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

The continue command is similar to break, however instead of terminating the

loop, the program just skips to the next pass.

Page 89: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

To run this video, click anywhere on the screen. To move to the next slide, use the down arrow on your keyboard

Page 90: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Notice that n had a value of 10 when the program terminated

Page 91: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 92: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 40,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 93: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

This code accomplishes the same thing with a for loop

Page 94: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 95: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

An alternate syntax for timing uses the tic and toc functions

Page 96: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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.

Page 97: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Summary

• Sections of computer code can be categorized as• sequences• selection structures• repetition structures

Page 98: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Summary – Sequence

• Sequences are lists of instructions that are executed in order

Page 99: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Summary – Selection Structure

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

Page 100: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 101: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Summary – Relational Operators

• Matlab uses the standard mathematical relational operators• <• <=• >• >=• ==• ~=

Recall that = is the assignment operator, and can not be used for comparisons

Page 102: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Summary – Logical Operators

• Matlab uses the standard logical operators • && and• || or• ~ not• xor exclusive or

Page 103: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Summary – Logical Functions

• The find command is unique to Matlab, and should be the primary logical function used in your programming

• It allows the user to specify a condition using both logical and relational operators, which is then used to identify elements of a matrix that meet the condition.

Page 104: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Summary – if family

• The family of if structures allows the programmer to identify alternate computing paths dependent upon the results of conditional statements. • if• else• elseif

Page 105: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 106: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

Summary break and continue

• These commands are used to exit a loop prematurely• 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 107: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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 108: Matlab for Engineers Logical Functions and Control Structures Chapter 8.

Matlab for Engineers

100 200 300 400 500

100

200

300

400

500

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-5

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0

Rate of Change

time, hour

Rat

e of

tem

pera

ture

cha

nge,

deg

rees

/hou

r

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.