Sunteți pe pagina 1din 21

October 3, 2005 Lecture 8 - By Paul Lin 1

CPET 190
Lecture 8
Problem Solving with MATLAB

http://www.etcs.ipfw.edu/~lin
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 Pseudocode
8-3 Data Types
8-5 Relational and Logical Operators
8-5 Logical Functions
October 3, 2005 Lecture 8 - By Paul Lin 3
Review: MATLAB sequential programs
Sequential Program
Step 1- Read user input
Step 2 - Process the
desired tasks to
produce the answer
Step 3 - Display or plot
the answer
Step 4 - Quit or end the
program
Sequence statements in a
program are executed one
after the other in the order
in which they are written
Add grade to total
Add 1 to counter
Sequence Flowchart
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 and
complex task into smaller ones
Steps (with some refinements if needed)
1. Clearly state the problem
2. Planning the program (define inputs and
outputs)
3. Design the algorithm and data structures
4. Coding (Translate algorithm into MATLAB
statements)
5. Test the MATLAB program
6. Document the program and the result
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 of
problem in to a computer program
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 <-
(temp_f 32) + 273.15
4. Write (display) temperature in kelvins
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 are
called matrices
N-dimensional of any size
October 3, 2005 Lecture 8 - By Paul Lin 8
8-3 MATLAB Data Types (continue)
Logical Data Type
Two Values (True or False)
NonZero True (1)
Zero False (0)
Produced by two functions
>> a = true
a = 1
>> b = false
b = 0
Logical and Relational operators can also
produce true or false
October 3, 2005 Lecture 8 - By Paul Lin 9
8-4 Relational Operators
Relational Operators making quantitative
comparisons
Symbols
== Equal
~= Not equal
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
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 <= 3
ans = 1
>> 4 >= 3
ans = 1
>> 'A' > 'B'
ans = 0
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 3
2 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 1
0 0 0
October 3, 2005 Lecture 8 - By Paul Lin 12
8-4 Logical Operators
Three Types of Logical Operators and Functions
Element-Wise operate on logical arrays
& for AND
| for OR
~ for NOT
xor for Exclusive OR

Short-Circuit operate on scalar, logical
expressions
Bit-Wise operate on bits of integer values or
arrays
October 3, 2005 Lecture 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
October 3, 2005 Lecture 8 - By Paul Lin 14
8-4 Logical Operators (continue)
Logical Operators for evaluating logical
expressions (scalar value)
Short-circuit logical AND &&
Short-circuit logical OR ||
Syntax
A && B
A || B
October 3, 2005 Lecture 8 - By Paul Lin 15
8-4 Relational and Logical Operators
Example: a statement that avoids divide-by-zero
errors 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
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 is
nonzero
all - True if all elements of vector are
nonzero
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 one
array, and 0 for all other
elements

xor(A,B)
ans =10100
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 1
NonZero 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
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 a
number
isnumeric(n) returns true if n is a numeric
array
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
October 3, 2005 Lecture 8 - By Paul Lin 21
Question?
Answers

Email: lin@ipfw.edu

S-ar putea să vă placă și