1 / 26

Lecture 3

Lecture 3. What will I learn in this lecture?. What are relational operators and logical values ? How to use the input and disp functions. Learn to use if , if-else and else-if conditional statements. What are logical operators ? Using while to perform loops.

liesel
Download Presentation

Lecture 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3

  2. What will I learn in this lecture? What are relationaloperators and logicalvalues? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements. What are logicaloperators? Using while to perform loops. Readings: Matlab by Pratap Chapters 3.2.2, 3.2.3, 4.3.4, 4.3.5

  3. Logical values 1. Problem Definition Write a Matlab function named count_axles to compute the number of 4 axle vehicles traveling past a specific location on a road over a fixed time interval. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.)The input to count_axles will be a vector containing the number of axles for each vehicle that passes a specific location. A second input will be the number of axels for which you want to obtain a count. In our example this value is 4.

  4. Logical values • 3. Develop Algorithm (processing steps to solve problem) • We first identify each vehicle with the desired number of axles. • For example if, • >> data = [2 3 4 4 2 4 4]; • then the vector identifying the 4 axle vehicles is, • match = [0 0 1 1 0 1 1] Use the relational operator == to find matches • >> match = data == 4 • match = • [0 0 1 1 0 1 1] • Next, use the sum function to add all the 1’s. • Note that match is a logical valued vector • >> whos match Name Size Bytes Class match 1x7 7 logical

  5. Logical values 4. Write the “Function" (Code) function count = count_axles(data, target)% function count = count_axles(data, target) match = data == target ; count = sum( match);

  6. Relational Operators The relational operators are: < <= > >= == ~= Relational operators generate logical values. >> match = [2 3 4 4 2 4 4] ~= 4 match = 1 1 0 0 1 0 0

  7. Subscripting with Logicals Example: Extract the bad data from the data vector. Suppose negative values are bad data. >> data = [2 -5 3 4 -1 4 2 4 4 -9];>> match = data > 0 match = 1 0 1 1 0 1 1 1 1 0 >> data = data(match) data = 2 3 4 4 2 4 4

  8. Subscripting with Logicals Problems subscripting with logicals. Rather than using a relational operator to generate a logical array we can use the logical function. >> data = [2 -5 3 4 -1 4 2 4 4 -9];>> match = logical([1 0 1 1 0 1 1 1 1 0]); >> data = data(match) data = 2 3 4 4 2 4 4 Note that the following does NOT work!>> match = [1 0 1 1 0 1 1 1 1 0]; >> data = data(match) ??? Subscript indices must either be real positive integers or logicals.

  9. Guessing Game-1 1. Problem Definition Write a Matlab function named “guess” that plays a guessing game with the user. The user will guess an integer number between 1 and x where x is given by the user as an input argument to the function. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.)This function has one input parameter x. Using a loop the function gets successive inputs from the user and compares to the correct answer. At each guess the user is told whether the guess is correct or not.

  10. Guessing Game-1 3. Develop Algorithm (processing steps to solve problem) To generate a random number between 1 and x that the user must guess we use the Matlab rand function. For example (from lab 2) we learned about the use of the ceil function, so that if you typed, >> x = 10; >> correct_answer = ceil(rand()*x) then the correct_answer will have a randomly chosen value between 1 and x. We can use the Matlab input function to prompt the user for a guess and to read the guess from the keyboard. For example, you can type,>> guess = input(‘Enter your guess: ‘);

  11. Guessing Game-1 3. Develop Algorithm (processing steps to solve problem) To check whether the user has entered a correct value we can use the Matlab if-else statement as follows: if correct_answer == guess disp(‘Match!!!’); else disp(‘No Match!’); end

  12. Guessing Game-1 3. Develop Algorithm (processing steps to solve problem) To repeat one or more Matlab statements ( a loop) we can use the while statement as follows: while flag == 1 % one or more Matlab statements here end where the flag variable keeps track of whether the game is over (flag == 0) or is still in progress (flag = 1).

  13. tstart function generate random number and set flag = 1 iflag == 1? FALSE TRUE tend function obtain next guess icorrect guess? FALSE TRUE display NO MATCH display MATCH set flag = 0

  14. Guessing Game-1 4. Write the “Function" (Code) function guess(x) % x is an integer %generate random integer between 1 and x correct_answer = ceil(rand()*x); flag = 1; % why??? while flag == 1 guess = input(‘Enter your guess: ‘); if correct_answer == guess disp(‘Match!!!’); flag = 0; elsedisp(‘No Match!’); end end Loop

  15. Guessing Game-2 1. Problem Definition Modify the guessing game function by giving the user feedback if their guess is higher or lower than the correct answer. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.)We will use a variation of the if-else statement which we call the elseif statement.

  16. Guessing Game-2 3. Develop Algorithm (processing steps to solve problem) To check whether the user has entered a correct value we can use the Matlab else-if statement as follows: if correct_answer < guess disp(‘Answer is too high!’); elseif correct_answer > guess disp(‘Answer is too low!’); else disp(‘Match!!!’); flag = 0; end

  17. Guessing Game-2 4. Write the “Function" (Code) function guess(x) % x is an integer %generate random integer between 1 and x correct_answer = ceil(rand()*x); flag = 1; while flag == 1 guess = input(‘Enter your guess: ‘); if correct_answer < guess disp(‘Answer is too high!’); elseifcorrect_answer > guess disp(‘Answer is too low!’); else disp(‘Match!!!’); flag = 0; end end else-if

  18. Guessing Game-3 1. Problem Definition Modify the guessing game function by giving the user a fixed number of guesses. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.)We will use the logical AND operator in the while loop. We will add one more input parameter to our function, named num_tries, which contains the maximum number of guesses that will be allowed. We assume that the user enters positive integer values for x and num_tries. If the user runs out of attempts before guessing the correct number we will notify the user that the game is over and display the correct answer.

  19. Guessing Game-3 3. Develop Algorithm (processing steps to solve problem) To check whether the user should be allowed another attempt at guessing we can add a simple if statement that uses the AND operator ( & ) as follows: if (flag == 1) & (attempts == num_tries) disp(‘Game Over!’) disp(‘The correct answer is’) disp(correct_answer) flag = 0; end

  20. Guessing Game-3 4. Write the “Function" (Code) function guess(x, num_tries) % intitialize the number of attempts attempts = 0; %generate random integer between 1 and x correct_answer = ceil(rand()*x); flag = 1; while flag == 1 guess = input(‘Enter your guess: ‘); if correct_answer < guess disp(‘Answer is too high!’); elseifcorrect_answer > guess disp(‘Answer is too low!’); else disp(‘Match!!!’); flag = 0; end attempts = attempts + 1; % update the number of attempts if (flag == 1) & (attempts == num_tries) disp(‘Game Over!’) disp(‘The correct answer is’) disp(correct_answer) flag = 0; end end simple if

  21. Logical Operators-Truth Tables

  22. Function - minmax 1. Problem Definition Write a function named minmax that given a vector of values, returns two scalar values, the min and max values of the vector. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) If the input value is [-1 -2 3 5 4] then minmax returns two values, -2 and 5.

  23. Function - minmax 3. Develop Algorithm (processing steps to solve problem) Natural-Language Algorithm If x is the input vector then min(x) is the minimum value and max(x) is the maximum.

  24. Function - minmax 4. Write the “Function" (Code) (instruction sequence to be carried out by the computer) Use the Matlab editor to create a file minmax.m . function [smallest,largest] = minmax(x) % function [smallest,largest] = minmax(x)% x is a vector. % Programmer: Tom Gambill% Date: 2/2/01% Input: a vector, x% Output: smallest and largest values of x.smallest = min(x);largest = max(x);

  25. Function - minmax Note the use of [lower, upper] to receive the values that minmax(x) returns .

  26. What have I learned in this lecture? Logical values can be generated by the relational operators or the logical function. A while statement can be used to execute a block of statements until the logical condition is false. The logical condition can use logicaloperators. We can use a flag variable to control when to exit a loop. We can control the flow of execution of statements in a Matlab function by using if, if-else and else-if conditional statements. Although input parameters are used when one function calls another function but we can use the input and disp functions to receive and display values from the command terminal.

More Related