october 3, 2005 lecture 8 - by paul lin 1 cpet 190 lecture 8 problem solving with matlab lin

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

Upload: philip-barber

Post on 02-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 11

CPET 190 CPET 190

Lecture 8Lecture 8

Problem Solving with MATLABProblem Solving with MATLAB

http://www.etcs.ipfw.edu/~linhttp://www.etcs.ipfw.edu/~lin

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 22

Lecture 8: MATLAB Program Design Lecture 8: MATLAB Program Design and Flow Controland Flow Control

8-1 Top-Down Program Design 8-1 Top-Down Program Design TechniquesTechniques

8-2 Algorithm and Pseudocode8-2 Algorithm and Pseudocode

8-3 Data Types8-3 Data Types

8-5 Relational and Logical Operators8-5 Relational and Logical Operators

8-5 Logical Functions8-5 Logical Functions

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 33

Review: MATLAB sequential programsReview: MATLAB sequential programs

Sequential ProgramSequential Program• Step 1- Read user inputStep 1- Read user input• Step 2 - Process the Step 2 - Process the

desired tasks to desired tasks to produce the answerproduce the answer

• Step 3 - Display or plot Step 3 - Display or plot the answerthe answer

• Step 4 - Quit or end the Step 4 - Quit or end the programprogram

Sequence statements in a Sequence statements in a program are executed one program are executed one after the other in the order after the other in the order in which they are writtenin which they are written

Add grade to total

Add 1 to counter

Sequence FlowchartSequence Flowchart

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 44

8-1 Top-Down Program Design 8-1 Top-Down Program Design TechniqueTechnique

A formal design processA formal design process Top-downTop-down Divide and Conquer: divide the large and Divide and Conquer: divide the large and

complex task into smaller onescomplex task into smaller ones Steps (with some refinements if needed)Steps (with some refinements if needed)

1.1. Clearly state the problemClearly state the problem2.2. Planning the program (define inputs and Planning the program (define inputs and

outputs)outputs)3.3. Design the algorithm and data structuresDesign the algorithm and data structures4.4. Coding (Translate algorithm into MATLAB Coding (Translate algorithm into MATLAB

statements)statements)5.5. Test the MATLAB programTest the MATLAB program6.6. Document the program and the resultDocument the program and the result

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 55

8-2 Algorithm and Pseudocode8-2 Algorithm and Pseudocode

AlgorithmAlgorithm• A procedure for solving a problem in terms of A procedure for solving a problem in terms of

the actions to be executed, and the order in the actions to be executed, and the order in which these actions are to be executedwhich these actions are to be executed

Pseudocode definition:Pseudocode definition:• An artificial and informal language that helps An artificial and informal language that helps

programmers develop algorithmprogrammers develop algorithm• An intermediate step that helps a programmer An intermediate step that helps a programmer

to translate the English-language description of to translate the English-language description of problem in to a computer programproblem in to a computer program

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 66

8-2 Algorithm and Pseudocode8-2 Algorithm and Pseudocode

An example of Psedocode for algorithmAn example of Psedocode for algorithm

1.1. Prompt user to enter temperature in Prompt user to enter temperature in degrees Fahrenheitdegrees Fahrenheit

2.2. Read the temperature (temp_f)Read the temperature (temp_f)

3.3. Convert to temp_k in kelvin: temp_k <- Convert to temp_k in kelvin: temp_k <- (temp_f – 32) + 273.15(temp_f – 32) + 273.15

4.4. Write (display) temperature in kelvinsWrite (display) temperature in kelvins

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 77

8-3 MATLAB Data Types8-3 MATLAB Data Types

15 Fundamental Data Types in MATLAB15 Fundamental Data Types in MATLAB• Logical, Char, Numeric (int8, uint8, int16, Logical, Char, Numeric (int8, uint8, int16,

uint16, int32, uint32, int64, uint64), Single, uint16, int32, uint32, int64, uint64), Single, Double, Cell, Structure (user classes), Java Double, Cell, Structure (user classes), Java classes, Function handleclasses, Function handle

All in the form of arrayAll 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 2-dimensional version of these arrays are

called matricescalled matrices• N-dimensional of any sizeN-dimensional of any size

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 88

8-3 MATLAB Data Types 8-3 MATLAB Data Types (continue)(continue)

Logical Data TypeLogical 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 functionsProduced by two functions>> a = true>> a = truea = 1a = 1>> b = false>> b = falseb = 0b = 0

Logical and Relational operators can also Logical and Relational operators can also produce true or falseproduce true or false

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 99

8-4 Relational Operators8-4 Relational Operators

Relational Operators – making quantitative Relational Operators – making quantitative comparisonscomparisons

SymbolsSymbols==== EqualEqual~=~= Not equalNot equal<< Less thanLess than> > Greater thanGreater than<=<= Less than or equal Less than or equal >=>= Greater than or equalGreater than or equal

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1010

8-4 Relational Operators 8-4 Relational Operators (continue)(continue)

Some Relational Operator ExamplesSome Relational Operator Examples

>> 2 < 3>> 2 < 3ans = 1ans = 1>> 3 < 2>> 3 < 2ans = 0ans = 0>> 3 == 3>> 3 == 3ans = 1ans = 1>> 2 <= 3>> 2 <= 3ans = 1ans = 1>> 4 >= 3>> 4 >= 3ans = 1ans = 1>> 'A' > 'B'>> 'A' > 'B'ans = 0ans = 0

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1111

8-4 Relational Operators 8-4 Relational Operators (continue)(continue)

Some More ExamplesSome More Examples

>> A = fix(rand(3)*10)>> A = fix(rand(3)*10)A =A =

8 6 68 6 6 5 8 35 8 3 2 0 82 0 8

>> B = fix(rand(3)*10)>> B = fix(rand(3)*10)B =B =

5 3 65 3 6 7 1 37 1 3 4 1 54 1 5

>> A == B>> A == Bans =ans =

0 0 10 0 1 0 0 10 0 1 0 0 00 0 0

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1212

8-4 Logical Operators8-4 Logical Operators

Three Types of Logical Operators and FunctionsThree Types of Logical Operators and Functions Element-Wise – operate on logical arraysElement-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 Short-Circuit – operate on scalar, logical expressionsexpressions

Bit-Wise – operate on bits of integer values or Bit-Wise – operate on bits of integer values or arraysarrays

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1313

8-4 Logical Operators 8-4 Logical Operators (continue)(continue)

Example: Element-wiseExample: Element-wise>> A = [1 0 1 1]>> A = [1 0 1 1]>> B = [0 1 0 1]>> B = [0 1 0 1]>> A & B>> A & Bans = 0 0 0 1 ans = 0 0 0 1 >> A | B>> A | Bans = 1 1 1 1ans = 1 1 1 1>> xor(A,B)>> xor(A,B)ans = 1 1 1 0 ans = 1 1 1 0

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1414

8-4 Logical Operators 8-4 Logical Operators (continue)(continue)

Logical Operators for evaluating logical Logical Operators for evaluating logical expressions (scalar value)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

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1515

8-4 Relational and Logical Operators8-4 Relational and Logical Operators

Example: a statement that avoids divide-by-zero Example: a statement that avoids divide-by-zero errors when b equals zero: errors when b equals zero:

>> a = input(‘Enter a = ‘)>> a = input(‘Enter a = ‘)

>> b = input(‘Enter b = ‘)>> b = input(‘Enter b = ‘)

>> x = (b ~= 0) && (a/b > 18.5)>> x = (b ~= 0) && (a/b > 18.5)

Case 1: b = 1, a = 20, x = true && true = 1Case 1: b = 1, a = 20, x = true && true = 1

Case 2: b = 1, a = 10, x = true && false = 0Case 2: b = 1, a = 10, x = true && false = 0

Case 3: b = 0, a = 20, x = false && Not-evaluated Case 3: b = 0, a = 20, x = false && Not-evaluated = 0= 0

Case 4: b = 0, a = 10, x = false = 0 Case 4: b = 0, a = 10, x = false = 0

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1616

8-5 Logical Functions8-5 Logical Functions

Logical FunctionsLogical 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 ORxor - Logical EXCLUSIVE OR any - True if any element of vector is any - True if any element of vector is

nonzerononzero all - True if all elements of vector are all - True if all elements of vector are

nonzerononzero

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1717

8-5 Logical Functions8-5 Logical FunctionsExample: Using functions and, or, notExample: Using functions and, or, not

A = [0 1 1 0 1]; B = [1 1 0 0 1];A = [0 1 1 0 1]; B = [1 1 0 0 1];

A & B A & B and(A, B)and(A, B)asn = 01001asn = 01001

A | B A | B or(A, B)or(A, B)ans = 11101ans = 11101

~A~Anot(A)not(A)ans = 10010ans = 10010

xor - Returns 1 for every xor - Returns 1 for every element location that is element location that is true (nonzero) in only one true (nonzero) in only one array, and 0 for all other array, and 0 for all other elementselements

xor(A,B)xor(A,B)ans =10100ans =10100

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1818

8-5 Logical Functions8-5 Logical Functions

Truth Table for xorTruth Table for xor

AA BB xor(A,B)xor(A,B)ZeroZero ZeroZero 00ZeroZero NonZeroNonZero 11NonZeroNonZeroZeroZero 11NonZeroNonZeroNonZeroNonZero 00

Example: Given Example: Given A = [0 A = [0 0 0 pi pi eps] and eps] and B = [0 B = [0 -2.4 -2.4 0 0 1], then 1], then C = xor(A, B)C = xor(A, B)C =C = 0 1 1 00 1 1 0

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 1919

8-5 Logical Functions8-5 Logical Functions

More Logical FunctionsMore Logical Functions logical(n) – convert numerical value to logical logical(n) – convert numerical value to logical

values; true for Non-zero, false for Zerovalues; true for Non-zero, false for Zero ischar(n) – returns true if n is a char. arrayischar(n) – returns true if n is a char. array isempty(n) - returns true if n is an empty arrayisempty(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 isnan(n) – returns true if n is NaN (not a

numbernumber isnumeric(n) – returns true if n is a numeric isnumeric(n) – returns true if n is a numeric

arrayarray

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 2020

SummarySummary

Top-down program design techniquesTop-down program design techniques Algorithm and pseudocodeAlgorithm and pseudocode Data typesData types Relational and logical operatorsRelational and logical operators Logical functionsLogical functions

Next:Next: Control FlowControl Flow Loop ControlLoop Control

October 3, 2005October 3, 2005 Lecture 8 - By Paul LinLecture 8 - By Paul Lin 2121

Question?Question?

AnswersAnswers

Email: [email protected]: [email protected]