1 / 27

CS005 Introduction to Programming Matlab

CS005 Introduction to Programming Matlab. Eamonn Keogh eamonn@cs.ucr.edu. Quick Review of Functions. Let us write a function that gets the user age as an integer. (nothing). We need a good name for the function…

Télécharger la présentation

CS005 Introduction to Programming Matlab

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. CS005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs.ucr.edu

  2. Quick Review of Functions Let us write a function that gets the user age as an integer (nothing) We need a good name for the function… We need to make sure that even if the use types in a real number, we return an integer. The built-in functionround will help. GetAge Say the user types in 10.5 10

  3. If the person entering their age is under 18, I want to display a warning “You cannot vote” on the screen. This is called conditional branching

  4. Conditional Branching Get the Users Age Check if age less than 18 Yes No Display Warning Message Return rounded version of Age

  5. Conditional Branching We do conditional Branching with the if statement functionUsersAge = GetAge() UsersAge = input('Please Enter your age in years : '); ifUsersAge < 18 disp('You cannot vote') end UsersAge = round(UsersAge);

  6. We do conditional Branching with the if statement ifUsersAge < 18 disp('You cannot vote') end ifsome logical expression is true do these statements end

  7. We do conditional Branching with the if statement ifUsersAge < 18 disp('You cannot vote') end (Minor note: You can write this on a single line, so long as you put commas after each statement) ifUsersAge < 18, disp('You cannot vote') , end

  8. We do conditional Branching with the if statement ifsome logical expression is true do these statements end Matlab uses ‘0’ to mean false Matlab uses ‘1’ to mean true (actually any non-zero)

  9. When Matlab sees an arithmetic expression such as 1 + 2, it evaluates it: • EDU>> 1 + 2 • ans = • 3 • When you evaluate a arithmetic expression, the answer is always a single number. • When Matlab sees a logical (or relational )expression such as 10 < 20, it evaluates it: • EDU>> 10 < 20 • ans = • 1 • When you evaluate a logical expression, the answer is always true or false, which is matlab are represented as 1 and 0

  10. We can practice these skills on the Matlab command line. Here we as saying.. “Matlab, please tell us if two is less than four?” And Matlab will respond by returning a ‘1’ if it is true (as it happens to be) but a ‘0’ if it is false

  11. EDU>> 2 < -7 ans = 0 EDU>> 2 < abs(-7) ans = 1 EDU>> 2 < max(1,0) ans = 0 EDU>> 2 < max(1,17) ans = 1 EDU>> 2 < 2.3 ans = 1 EDU>> 2 < round(2.3) ans = 0 EDU>> 2 < 1+2 ans = 1 EDU>> 2 < sqrt(9) ans = 1

  12. EDU>> HisAge = 17; EDU>> HerAge = 34; EDU>> HisAge < HerAge ans = 1 EDU>> 20 < max(HerAge,HisAge) ans = 1 EDU>> HisAge = 17; EDU>> 2 < HisAge ans = 1 EDU>> 2 < min(1,HisAge) ans = 0 EDU>> 2 < HisAge - 500 ans = 0 EDU>> HisAge < 21 ans = 1

  13. The relational operators Math Matlab  ==  <  >  >=  <=  ~= It is important to differentiate between assignment and comparison for equality A = B % A is assigned to B A == B % A equal to B? ~ is the tilde (TILL-duh or TILL-day) It is usually to the left of ‘1’ key on keyboard

  14. EDU>> 2 == 4 ans = 0 EDU>> 2 == 2 ans = 1 EDU>> 2 ~= 2 ans = 0 EDU>> 2 ~= 7 ans = 1 EDU>> 2 <= 50 ans = 1 EDU>> 2 <= 2 ans = 1 EDU>> 2 < 2 ans = 0

  15. A Golden Rule( For all computer languages, not just Matlab) We should never test for equality or inequality with real numbers EDU>> 2.1 == 2.1 % Do not do this! ans = 1 EDU>> HisAge = GetAge(); EDU>> HisAge == 18 % Do not do this! EDU>> round(HisAge) == 18 % But thisis OK! We will see why later….

  16. Let us return to this example… ifUsersAge < 18 disp('You cannot vote') end (Minor note: You can write this on a single line, so long as you put commas after each statement) ifUsersAge < 18, disp('You cannot vote') , end

  17. Let us return to this example… EDU>> UsersAge = 14; EDU>> ifUsersAge < 18, disp('You cannot vote') , end You cannot vote EDU>> UsersAge = 44; EDU>> ifUsersAge < 18, disp('You cannot vote') , end EDU>>

  18. More examples… EDU>> UsersAge = 14; EDU>> ifUsersAge < 21, disp('You cannot drink') , end You cannot drink EDU>> EDU>> UsersAge = 55; EDU>> ifUsersAge < 21, disp('You cannot drink') , end EDU>> EDU>> ifUsersAge >= 55, disp(' senior discount!') , end senior discount! EDU>>

  19. Boolean Variables(sort of) • Just as we can have variables to hold numbers, for example: • EDU>> HisAge = 12; • EDU>> HerBMI = 23.23; • We can have variables to hold truth values, for example • EDU>> IsMarried = 1; % I have assigned this TRUE • EDU>> IsIrish = 0; % I have assigned this FALSE • It is a great idea to name these variables IsSomething. We may call these variables, Boolean variables (But, at least in Matlab, they are just ordinary numbers)

  20. We can have variables to hold truth values, for example • EDU>> IsMarried = 1; % I have assigned this TRUE • EDU>> IsIrish = 0; % I have assigned this FALSE • EDU>> ifIsMarried, disp('Hard Luck') , end • Hard Luck • EDU>> ifIsIrish, disp('You lucky dog!') , end • EDU>>

  21. Let us write a function that asks a user their age, and returns their voting status. Their voting status is binary (or Boolean), you can either legally vote or not. Here is how we might use it.. • EDU>> IsAbleToVote = GetVotingStatus(); • EDU>> ifIsAbleToVote, disp(‘Please Pull Lever Now:') , end (nothing) GetVotingStatus Only return a 0 or 1, a true or false 0

  22. functionVoteStatus = GetVotingStatus() VoteStatus = 0; % Let us assume she cannot vote UsersAge = input('Please Enter your age in years : '); ifUsersAge >= 18 VoteStatus = 1; % Let us change our assumption end

  23. Our function seems to work… EDU>> GetVotingStatus Please Enter your age in years : 1 ans = 0 EDU>> GetVotingStatus Please Enter your age in years : 33 ans = 1

  24. Lab assignment will be online during lab • Quiz on Monday? • Review Session: Tomorrow 10 to 12, meet here If you are late, you will miss it (late means, one second after 10:00am)

More Related