You are on page 1of 75

Logical Functions and

-5
Rate of Change

Selection Structures
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5
Chapter 8
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change
Objectives
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5
After studying this chapter you
should be able to:
Understand how MATLAB interprets
relational and logical operators
100

200 Use the find function


Understand the appropriate uses of the if/else
300

400

500
100 200 300 400 500
family of commands
Understand the switch/case structure

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Structures
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Sequence
-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Selection Sequence Selection Repetition (Loop)

Repetition
100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change
8.1 Relational and Logical Operators
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Sequence and Repetition


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

structures require comparisons to


work
100

200
Relational operators make those
300

400
comparisons
Logical operators allow us to
500
100 200 300 400 500

combine the comparisons

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Relational Operators
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5
< Less than
<= Less than or equal to
> Greater than
100

200 >= Greater than or equal to


300

400

500
100 200 300 400 500
== Equal to
~= Not equal to

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Comparisons are either true
or false
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Most computer programs use the


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

number 1 for true and 0 for false

100

200

300
The results of a comparison are used in
selection structures and repetition
400

500
100 200 300 400 500

structures to make choices

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
MATLAB compares corresponding
elements and determines if the
result is true or false for each
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
In order for MATLAB to decide a
-5
Rate of Change

comparison is true for an entire


Rate of temperature change, degrees/hour

-4.5

-4

-3.5

matrix, it must be true for every


-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5

element in the matrix

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Logical Operators
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

& and
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

~ not
100
| or
200

300
xor exclusive or
400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change
8.2 Flow Charts and Pseudo-Code
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

As you write more complicated


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

programs it becomes more and


more important to plan your code
100
before you write it
Flow charts graphical approach
200

300

400

Pseudo-code verbal description


500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Pseudo-code
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

Outline a set of statements


-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

describing the steps you will take


to solve a problem
100
Convert these steps into
200

300
comments in an M-file
400

500
100 200 300 400 500
Insert the appropriate MATLAB
code into the file between the
comment lines
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Pseudo-code Example
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Youve been asked to create a


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

program to convert miles/hr to ft/s.


The output should be a table,
100
complete with title and column
headings
200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Outline the steps
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Define a vector of mph values


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Convert mph to ft/s


Combine the mph and ft/s vectors
100

200
into a matrix
Create a table title
300

400

500
100 200 300 400 500

Create column headings


Display the table
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Convert the steps to M-file
comments
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Insert the MATLAB code
between the comments
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Flow Charting
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Especially appropriate for more


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

complicated programs
Create a big picture graphically
100

200
Convert to pseudo-code
300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change
Simple Flow Chart Symbols
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

An oval indicates the


-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

beginning of a section of
code
A parallelogram indicates
100
an input or output
A diamond indicates a
200

300

decision point
400

500
100 200 300 400 500

Calculations are placed


in rectangles

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Start
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5
Define a vector
-3

-2.5
of miles/hour
-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

Calculate the
time, hour

ft/sec vector
This flowchart
Combine into a represents the
100
table mph to ft/s
200

Create an output
problem
300

400
table using disp
500
100 200 300 400 500
and fprintf

End

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change 8.3 Logical Functions
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

MATLAB offers traditional


-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

programming selection structures


if
100
if/else
200

300
switch/case
400

500
100 200 300 400 500
And a series of logical functions
that perform many of the same
tasks
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change find
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

The find command searches a


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

matrix and identifies which


elements in that matrix meet a
100
given criteria.
200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change For example
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

The US Naval Academy requires


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

applicants to be at least 56 tall


Consider this list of applicant
100

200
heights
300

400
63, 67, 65, 72, 69, 78, 75
Which applicants meet the
500
100 200 300 400 500

criteria?
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
The find function returns the index number
Rate of Change
for elements that meet a criteria
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

index numbers
100

200

300

400

500
100 200 300 400 500

element values

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

index numbers
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
You could use the disp and fprintf
functions in this program to create a
Rate of Change

more readable report


-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

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

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
By combining relational and logical
operators you can create fairly
Rate of Change

complicated search criteria


-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Assume applicants must be at


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

least 18 years old and less than


35 years old
100

200
They must also meet the height
300

400
requirement
500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Applicant pool
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Height Age
time, hour

Inches years
63 18
67 19
100 65 18
72 20
200

300

400

500
100 200 300 400 500
69 36
78 34
75 12

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Lets use Pseudo-code to
plan this program
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Create a 7x2 matrix of applicant


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

height and age information


Use the find command to
100

200
determine which applicants are
300

400
eligible
Use fprintf to create a table of
500
100 200 300 400 500

results

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

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


300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Because we didnt suppress all the


100

200
output, the intermediate calculations
300
were sent to the command window
400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
The find command can
return either
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

A single index number identifying an


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

element in a matrix
A matrix of the row numbers and the
column numbers identifying an
100

200 element in a matrix


You need to specify two results if you want
300

400

500
100 200 300 400 500
the row and column designation
[row, col] = find( criteria)

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Imagine you have a matrix of
patient temperature values
Rate of Change

measured in a clinic
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

Station 1 Station 2 Station 3


time, hour

95.3 100.2 98.6

97.4 99.2 98.9


100

200

300

400

500
100 200 300 400 500 100.1 99.3 97

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Use the find command to determine
which patients have elevated
Rate of Change

temperatures
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500 1 4
These elements refer7to the single
2 index number
5 identification
8 scheme
3 6 9

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
If we want the row and
column
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

1,1 1,2 1,3


2,1 2,2 2,3
100

200
3,1 3,2 3,3
300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

Using fprintf we can create a


300

400

500
100 200 300 400 500

more readable report

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Flow charting and Pseudo-
code for find Commands
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

Start %Define a vector of x values


-1.5

-1

-0.5

x=[1,2,3; 10, 5,1; 12,3,2;8, 3,1]


0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour
Define a vector of x
values %Find the index numbers of the values in %x
>9
Find the index numbers in the x
matrix for values greater than 9
element = find(x>9)
%Use the index numbers to find the x
100
Use the index numbers to %values greater than 9 by plugging them
find the x values %into x
200

300 values = x(element)


400 Create an output % Create an output table
table using disp
500
100 200 300 400 500
and fprintf disp('Elements greater than 9')
disp('Element # Value')
End
fprintf('%8.0f %3.0f \n', [element';values'])

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change
8.4 Selection Structures
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Most of the time the find function


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

should be used instead of an if


However, there are certain
100

200
situations where if is the
300

400
appropriate process to use
500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Simple if
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

if comparison
-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

statements
end
100
For example.
if G<50
200

300

400

500
100 200 300 400 500
disp(G is a small value equal to:)
disp(G);
end

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change If statements
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Easy to interpret for scalars


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

What does an if statement mean


if the comparison includes a
100

200
matrix?
300

400
The comparison is only true if it is
500
100 200 300 400 500
true for every member of the array

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Consider this bit of code
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

G=[30,55,10]
-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

if G<50
disp(G is a small value equal to:)
100
disp(G);
200

300

400
end
500
100 200 300 400 500
The code inside the if statement is
not executed, because the
comparison is not true!!

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

This statement is
false because at
least one of the
100
elements in G has
200

a value >= 50
300

400

500
100 200 300 400 500 Therefore the code
inside the if statement
does not execute.

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

This statement is
-1.5

-1

-0.5
;
true because all of
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

the elements in G
are < 70

100

200

300
The output would have
400
been cleaner if we had
500
100 200 300 400 500
suppressed line 1 by
adding a semicolon to
the code.

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change The if/else structure
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

The simple if triggers the execution of


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

a block of code if a condition is true


If it is false that block of code is
skipped, and the program continues
100

200 without doing anything


300

400

500
What if instead you want to execute an
alternate set of code if the condition is
100 200 300 400 500

false?

MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Flow chart of an if/else
structure
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

True False
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

Comparison

100 Block of code to Block of code to


200
excute if the excute if the
300

comparison is true comparison is false


400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Use an if structure to
calculate a natural log
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Check to see if the input is


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

positive
If it is, calculate the natural log
100
If it isnt, send an error message to
the screen
200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change M-file Program
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Interactions in the Command
Window
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change The if/else/elseif structure
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Use the elseif for multiple


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

selection criteria
For example
100

200
Write a program to determine if an
300

400
applicant is eligible to drive
500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Start

Sorry
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5
if age<16 True Youll have
-3

-2.5

-2

-1.5
to wait
-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
elseif
time, hour

You may
age<18 True
have a youth
license
elseif

True You may have a


100
age<70 standard license
200

300
else
400
Drivers over 70
500
100 200 300 400 500 require a
special license

End

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

Always test your


-4

-3.5

-3

-2.5

programs making
-2

-1.5

-1

-0.5

sure that youve


0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

covered all the


possible calculational
paths
100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change As a general rule
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

If structures work well for scalars


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

For vectors or arrays use a find


function or..
100

200
Combine if structures with a
300

400
repetition structure
Repetition structures are
500
100 200 300 400 500

introduced in the next chapter


MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change switch/case
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

This structure is an alternative to the


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

if/else/elseif structure
The code is generally easier to read
100
This structure allows you to choose
200

300
between multiple outcomes, based on
400

500
some criterion, which must be exactly
true
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change When to use switch/case
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

The criterion can be either a


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

scalar (a number) or a string.


In practice, it is used more with
100

200
strings than with numbers.
300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change
The structure of switch/case
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1
switch variable
-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5
case option1
code to be executed if variable is exactly
equal to option 1
case option2
code to be executed if variable is exactly
equal to option 2
100

200
case option_n
300

400
code to be executed if variable is exactly
500
100 200 300 400 500
equal to option n
otherwise
code to be executed if variable is not
equal to any of the options
end

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Suppose you want to
-5
Rate of Change
determine what the airfare is
Rate of temperature change, degrees/hour

-4.5

to one of three cities


-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

RememberYou tell the


-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

input command to expect a


string by adding s in the
100

200

300
second field.
400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Menu
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

The menu function is often used


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

in conjunction with a switch/case


structure.
100

200
This function causes a menu box
300

400
to appear on the screen with a
500
100 200 300 400 500
series of buttons defined by the
programmer.
input = menu(Message to the user,text for button 1,text for button 2, etc)

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Because the input is controlled by


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

a menu box, the user cant


accidentally enter a bad choice
100

200
This means you dont need the
300

400
otherwise portion of the
500
100 200 300 400 500
switch/case structure

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500
Note that the otherwise portion of the
switch/case structure wasnt used

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
When you run this code a
menu box appears
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5
time, hour
3 3.5 4 4.5 5
Instead of entering
your choice from
the command
window, you select
one of the buttons
from the menu
100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change If I select Honolulu
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Summary
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Sections of computer code can be


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

categorized as
sequences
100
selection structures
200

300

400
repetition structures
500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Summary Sequence
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Sequences are lists of instructions


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

that are executed in order

100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Summary Selection
Structure
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Selection structures allow the


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

programmer to define criteria


(conditional statements) which
100
the program uses to choose
execution paths
200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Summary Repetition
Structures
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Repetition structures define loops


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

where a sequence of instructions


is repeated until some criterion is
100
met (also defined by conditional
statements).
200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Summary Relational
Operators
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

MATLAB uses the standard


-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

mathematical relational operators


<
100
<=
200

300
>
400
>=
500

Recall that = is the assignment operator, and



100 200 300 400 500

== can not be used for comparisons


~=

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Summary Logical
Operators
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

MATLAB uses the standard


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

logical operators
&& and
100
|| or
200

300

400
~ not
500
100 200 300 400 500
xor exclusive or

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
Summary Logical
Functions
Rate of Change
-5
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

The find command is unique to


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

MATLAB, and should be the primary


logical function used in your
programming
It allows the user to specify a condition
100

200

using both logical and relational


300

400

500
100 200 300 400 500

operators, which is then used to


identify elements of a matrix that meet
the condition.
MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Summary if family
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

The family of if structures allows


-1.5

-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

the programmer to identify


alternate computing paths
dependent upon the results of
conditional statements.
100

200

if
300

400

else
500
100 200 300 400 500

elseif

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
-5
Rate of Change Summary switch/case
Rate of temperature change, degrees/hour

-4.5

-4

-3.5

-3

-2.5

-2

-1.5

Similar to the if/elseif/else


-1

-0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour

structure
Commonly used with menu
100

200

300

400

500
100 200 300 400 500

MATLAB for Engineers 2E, by Holly Moore. 2009 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write
to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.

You might also like