8/12/2019 CPET190_Lect8
1/21
October 3, 2005 Lecture 8 - By Paul Lin 1
CPET 190
Lecture 8
Problem Solving with MATLAB
http://www.etcs.ipfw.edu/~lin
8/12/2019 CPET190_Lect8
2/21
October 3, 2005 Lecture 8 - By Paul Lin 2
Lecture 8: MATLAB Program Design
and Flow Control
8-1 Top-Down Program Design
Techniques
8-2 Algorithm and Pseudocode8-3 Data Types
8-5 Relational and Logical Operators
8-5 Logical Functions
8/12/2019 CPET190_Lect8
3/21
October 3, 2005 Lecture 8 - By Paul Lin 3
Review: MATLAB sequential programs
Sequential Program Step 1- Read user input
Step 2 - Process thedesired tasks toproduce the answer
Step 3 - Display or plotthe answer
Step 4 - Quit or end theprogram
Sequence statements in a
program are executed oneafter the other in the orderin which they are written
Add grade to total
Add 1 to counter
Sequence Flowchart
8/12/2019 CPET190_Lect8
4/21
October 3, 2005 Lecture 8 - By Paul Lin 4
8-1 Top-Down Program Design
Technique
A formal design process Top-down
Divide and Conquer: divide the large andcomplex task into smaller ones
Steps (with some refinements if needed)
1. Clearly state the problem
2. Planning the program (define inputs andoutputs)
3. Design the algorithm and data structures
4. Coding (Translate algorithm into MATLABstatements)
5. Test the MATLAB program
6. Document the program and the result
8/12/2019 CPET190_Lect8
5/21
October 3, 2005 Lecture 8 - By Paul Lin 5
8-2 Algorithm and Pseudocode
Algorithm A procedure for solving a problem in terms of
the actions to be executed, and the order in
which these actions are to be executed
Pseudocode definition: An artificial and informal language that helps
programmers develop algorithm
An intermediate step that helps a programmer
to translate the English-language description ofproblem in to a computer program
8/12/2019 CPET190_Lect8
6/21
October 3, 2005 Lecture 8 - By Paul Lin 6
8-2 Algorithm and Pseudocode
An example of Psedocode for algorithm
1. Prompt user to enter temperature in
degrees Fahrenheit
2. Read the temperature (temp_f)3. Convert to temp_k in kelvin: temp_k
8/12/2019 CPET190_Lect8
7/21
October 3, 2005 Lecture 8 - By Paul Lin 7
8-3 MATLAB Data Types
15 Fundamental Data Types in MATLAB Logical, Char, Numeric (int8, uint8, int16,
uint16, int32, uint32, int64, uint64), Single,
Double, Cell, Structure (user classes), Java
classes, Function handle
All in the form of array
Minimum size of 0-by-0 (scalar)
2-dimensional version of these arrays arecalled matrices
N-dimensional of any size
8/12/2019 CPET190_Lect8
8/21
October 3, 2005 Lecture 8 - By Paul Lin 8
8-3 MATLAB Data Types (continue)
Logical Data Type Two Values (True or False)
NonZeroTrue (1)
ZeroFalse (0)
Produced by two functions>> a = true
a = 1
>> b = false
b = 0 Logical and Relational operators can also
produce true or false
8/12/2019 CPET190_Lect8
9/21
October 3, 2005 Lecture 8 - By Paul Lin 9
8-4 Relational Operators
Relational Operators making quantitativecomparisons
Symbols
== Equal~= Not equal
< Less than
> Greater than
= Greater than or equal
8/12/2019 CPET190_Lect8
10/21
October 3, 2005 Lecture 8 - By Paul Lin 10
8-4 Relational Operators (continue)
Some Relational Operator Examples
>> 2 < 3
ans = 1
>> 3 < 2
ans = 0>> 3 == 3
ans = 1
>> 2 > 4 >= 3
ans = 1
>> 'A' > 'B'
ans = 0
8/12/2019 CPET190_Lect8
11/21
October 3, 2005 Lecture 8 - By Paul Lin 11
8-4 Relational Operators (continue)
Some More Examples
>> A = fix(rand(3)*10)
A =
8 6 6
5 8 32 0 8
>> B = fix(rand(3)*10)
B =
5 3 6
7 1 3
4 1 5
>> A == B
ans =
0 0 1
0 0 10 0 0
8/12/2019 CPET190_Lect8
12/21
October 3, 2005Lecture 8 - By Paul Lin
12
8-4 Logical Operators
Three Types of Logical Operators and Functions Element-Wiseoperate on logical arrays
& for AND
| for OR
~ for NOT
xor for Exclusive OR
Short-Circuitoperate on scalar, logicalexpressions
Bit-Wiseoperate on bits of integer values orarrays
8/12/2019 CPET190_Lect8
13/21
October 3, 2005Lecture 8 - By Paul Lin
13
8-4 Logical Operators (continue)
Example: Element-wise>> A = [1 0 1 1]
>> B = [0 1 0 1]
>> A & B
ans = 0 0 0 1
>> A | B
ans = 1 1 1 1
>> xor(A,B)
ans = 1 1 1 0
8/12/2019 CPET190_Lect8
14/21
October 3, 2005 Lecture 8 - By Paul Lin 14
8-4 Logical Operators (continue)
Logical Operators for evaluating logicalexpressions (scalar value)
Short-circuit logical AND &&
Short-circuit logical OR ||
Syntax
A && B
A || B
8/12/2019 CPET190_Lect8
15/21
October 3, 2005 Lecture 8 - By Paul Lin 15
8-4 Relational and Logical Operators
Example: a statement that avoids divide-by-zeroerrors when b equals zero:
>> a = input(Enter a = )
>> b = input(Enter b = )
>> x = (b ~= 0) && (a/b > 18.5)Case 1: b = 1, a = 20, x = true && true = 1
Case 2: b = 1, a = 10, x = true && false = 0
Case 3: b = 0, a = 20, x = false && Not-evaluated
= 0
Case 4: b = 0, a = 10, x = false = 0
8/12/2019 CPET190_Lect8
16/21
October 3, 2005 Lecture 8 - By Paul Lin 16
8-5 Logical Functions
Logical Functions and - Element-wise logical AND ( & )
or - Element-wise logical OR ( | )
not - Logical NOT (~)
xor - Logical EXCLUSIVE OR
any - True if any element of vector isnonzero
all - True if all elements of vector arenonzero
8/12/2019 CPET190_Lect8
17/21
October 3, 2005 Lecture 8 - By Paul Lin 17
8-5 Logical Functions
Example: Using functions and, or, not
A = [0 1 1 0 1]; B = [1 1 0 0 1];
A & B
and(A, B)
asn = 01001
A | B
or(A, B)
ans = 11101
~A
not(A)
ans = 10010
xor - Returns 1 for every
element location that is
true (nonzero) in only onearray, and 0 for all other
elements
xor(A,B)
ans =10100
8/12/2019 CPET190_Lect8
18/21
October 3, 2005 Lecture 8 - By Paul Lin 18
8-5 Logical Functions
Truth Table for xor
A B xor(A,B)
Zero Zero 0
Zero NonZero 1
NonZero Zero 1NonZero NonZero 0
Example: Given
A = [0 0 pi eps] and
B = [0 -2.4 0 1], then
C = xor(A, B)
C =
0 1 1 0
8/12/2019 CPET190_Lect8
19/21
October 3, 2005 Lecture 8 - By Paul Lin 19
8-5 Logical Functions
More Logical Functions logical(n) convert numerical value to logical
values; true for Non-zero, false for Zero
ischar(n) returns true if n is a char. array
isempty(n) - returns true if n is an empty array isinf(n) returns true if n is infinite (Inf)
isnan(n) returns true if n is NaN (not anumber
isnumeric(n) returns true if n is a numericarray
8/12/2019 CPET190_Lect8
20/21
October 3, 2005 Lecture 8 - By Paul Lin 20
Summary
Top-down program design techniques Algorithm and pseudocode
Data types
Relational and logical operators
Logical functions
Next:
Control Flow Loop Control
8/12/2019 CPET190_Lect8
21/21
October 3, 2005 Lecture 8 - By Paul Lin 21
Question?
Answers
Email: [email protected]