Title: CPET 190
1CPET 190
- Lecture 8
- Problem Solving with MATLAB
- http//www.etcs.ipfw.edu/lin
2Lecture 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
3Review 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
Sequence Flowchart
48-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)
- Clearly state the problem
- Planning the program (define inputs and outputs)
- Design the algorithm and data structures
- Coding (Translate algorithm into MATLAB
statements) - Test the MATLAB program
- Document the program and the result
58-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
68-2 Algorithm and Pseudocode
- An example of Psedocode for algorithm
- Prompt user to enter temperature in degrees
Fahrenheit - Read the temperature (temp_f)
- Convert to temp_k in kelvin temp_k lt- (temp_f
32) 273.15 - Write (display) temperature in kelvins
78-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
88-3 MATLAB Data Types (continue)
- Logical Data Type
- Two Values (True or False)
- NonZero True (1)
- Zero False (0)
- Produced by two functions
- gtgt a true
- a 1
- gtgt b false
- b 0
- Logical and Relational operators can also produce
true or false
98-4 Relational Operators
- Relational Operators making quantitative
comparisons - Symbols
- Equal
- Not equal
- lt Less than
- gt Greater than
- lt Less than or equal
- gt Greater than or equal
108-4 Relational Operators (continue)
- Some Relational Operator Examples
- gtgt 2 lt 3
- ans 1
- gtgt 3 lt 2
- ans 0
- gtgt 3 3
- ans 1
- gtgt 2 lt 3
- ans 1
- gtgt 4 gt 3
- ans 1
- gtgt 'A' gt 'B'
- ans 0
118-4 Relational Operators (continue)
- Some More Examples
- gtgt A fix(rand(3)10)
- A
- 8 6 6
- 5 8 3
- 2 0 8
- gtgt B fix(rand(3)10)
- B
- 5 3 6
- 7 1 3
- 4 1 5
gtgt A B ans 0 0 1 0 0
1 0 0 0
128-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
138-4 Logical Operators (continue)
- Example Element-wise
- gtgt A 1 0 1 1
- gtgt B 0 1 0 1
- gtgt A B
- ans 0 0 0 1
- gtgt A B
- ans 1 1 1 1
- gtgt xor(A,B)
- ans 1 1 1 0
148-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
158-4 Relational and Logical Operators
- Example a statement that avoids divide-by-zero
errors when b equals zero - gtgt a input(Enter a )
- gtgt b input(Enter b )
- gtgt x (b 0) (a/b gt 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
168-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
178-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
188-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
198-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
20Summary
- Top-down program design techniques
- Algorithm and pseudocode
- Data types
- Relational and logical operators
- Logical functions
- Next
- Control Flow
- Loop Control
21Question?
- Answers
- Email lin_at_ipfw.edu