Presentation is loading. Please wait.

Presentation is loading. Please wait.

October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB

Similar presentations


Presentation on theme: "October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB"— Presentation transcript:

1 October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB http://www.etcs.ipfw.edu/~lin

2 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

3 October 3, 2005 Lecture 8 - By Paul Lin 3 Review: MATLAB sequential programs Sequential Program Sequential Program Step 1- Read user inputStep 1- Read user input Step 2 - Process the desired tasks to produce the answerStep 2 - Process the desired tasks to produce the answer Step 3 - Display or plot the answerStep 3 - Display or plot the answer Step 4 - Quit or end the programStep 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 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

4 October 3, 2005 Lecture 8 - By Paul Lin 4 8-1 Top-Down Program Design Technique A formal design process A formal design process Top-down Top-down Divide and Conquer: divide the large and complex task into smaller ones Divide and Conquer: divide the large and complex task into smaller ones Steps (with some refinements if needed) 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

5 October 3, 2005 Lecture 8 - By Paul Lin 5 8-2 Algorithm and Pseudocode Algorithm 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 executedA 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: Pseudocode definition: An artificial and informal language that helps programmers develop algorithmAn 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 programAn intermediate step that helps a programmer to translate the English-language description of problem in to a computer program

6 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

7 October 3, 2005 Lecture 8 - By Paul Lin 7 8-3 MATLAB Data Types 15 Fundamental Data Types in MATLAB 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 handleLogical, 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 All in the form of array Minimum size of 0-by-0 (scalar)Minimum size of 0-by-0 (scalar) 2-dimensional version of these arrays are called matrices2-dimensional version of these arrays are called matrices N-dimensional of any sizeN-dimensional of any size

8 October 3, 2005 Lecture 8 - By Paul Lin 8 8-3 MATLAB Data Types (continue) Logical Data Type Logical Data Type Two Values (True or False)Two Values (True or False) NonZero – True (1)NonZero – True (1) Zero – False (0)Zero – False (0) Produced by two functions Produced by two functions >> a = true a = 1 >> b = false b = 0 Logical and Relational operators can also produce true or false Logical and Relational operators can also produce true or false

9 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

10 October 3, 2005 Lecture 8 - By Paul Lin 10 8-4 Relational Operators (continue) Some Relational Operator Examples >> 2 > 2 < 3 ans = 1 >> 3 > 3 < 2 ans = 0 >> 3 == 3 ans = 1 >> 2 > 2 <= 3 ans = 1 >> 4 >= 3 ans = 1 >> 'A' > 'B' ans = 0

11 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 8 6 6 5 8 3 5 8 3 2 0 8 2 0 8 >> B = fix(rand(3)*10) B = 5 3 6 5 3 6 7 1 3 7 1 3 4 1 5 4 1 5 >> A == B ans = 0 0 1 0 0 1 0 0 0 0 0 0

12 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 Element-Wise – operate on logical arrays & for AND& for AND | for OR| for OR ~ for NOT~ for NOT xor for Exclusive ORxor for Exclusive OR Short-Circuit – operate on scalar, logical expressions Short-Circuit – operate on scalar, logical expressions Bit-Wise – operate on bits of integer values or arrays Bit-Wise – operate on bits of integer values or arrays

13 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

14 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 AND && Short-circuit logical OR || Short-circuit logical OR || Syntax Syntax A && BA && B A || BA || B

15 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

16 October 3, 2005 Lecture 8 - By Paul Lin 16 8-5 Logical Functions Logical Functions and - Element-wise logical AND ( & ) and - Element-wise logical AND ( & ) or - Element-wise logical OR ( | ) or - Element-wise logical OR ( | ) not - Logical NOT (~) not - Logical NOT (~) xor - Logical EXCLUSIVE OR xor - Logical EXCLUSIVE OR any - True if any element of vector is nonzero any - True if any element of vector is nonzero all - True if all elements of vector are nonzero all - True if all elements of vector are nonzero

17 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 ~Anot(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

18 October 3, 2005 Lecture 8 - By Paul Lin 18 8-5 Logical Functions Truth Table for xor ABxor(A,B) ZeroZero0 ZeroNonZero1 NonZeroZero1 NonZeroNonZero0 Example: Given A = [0 0 pi eps] and B = [0 -2.4 0 1], then C = xor(A, B) C = 0 1 1 0 0 1 1 0

19 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 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 ischar(n) – returns true if n is a char. array isempty(n) - returns true if n is an empty array isempty(n) - returns true if n is an empty array isinf(n) – returns true if n is infinite (Inf) isinf(n) – returns true if n is infinite (Inf) isnan(n) – returns true if n is NaN (not a number isnan(n) – returns true if n is NaN (not a number isnumeric(n) – returns true if n is a numeric array isnumeric(n) – returns true if n is a numeric array

20 October 3, 2005 Lecture 8 - By Paul Lin 20 Summary Top-down program design techniques Top-down program design techniques Algorithm and pseudocode Algorithm and pseudocode Data types Data types Relational and logical operators Relational and logical operators Logical functions Logical functionsNext: Control Flow Control Flow Loop Control Loop Control

21 October 3, 2005 Lecture 8 - By Paul Lin 21 Question? Answers Email: lin@ipfw.edu


Download ppt "October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB"

Similar presentations


Ads by Google