1 / 32

Announcements

Announcements. Exercise Sets 2 and 3 Due Thursday. MatLab m-file. M-file : Used to Store Commands Script : list of Commands to Run as if Typed Functions : Code that Call Be Called Later On. MatLab Scripts. Can Be Called from Command Window Named “ scriptName.m ”

kforeman
Télécharger la présentation

Announcements

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. Announcements • Exercise Sets 2 and 3 Due Thursday

  2. MatLab m-file • M-file : Used to Store Commands • Script : list of Commands to Run as if Typed • Functions : Code that Call Be Called Later On

  3. MatLab Scripts • Can Be Called from Command Window • Named “scriptName.m” • Called from Command Window by Name (scriptName) • May Use “input” statement to Get User Input • Semi-colon (';') to Suppress Output • MatLab Version of a Program

  4. Creating MatLab m-Files • Click on “New” in Upper Left Hand Corner • Opens Editor Window Above Command Window • Type Script (Code) in Editor Window • Save in “.m” File

  5. Running MatLab m-Files • Type File Name (without “.m”) in Command Window • Runs All Statements in .m File

  6. MatLab Errors • Syntax Error : Error in the Script Grammar • Missing parenthesis • Misspelled identifiers • Semantic Error : Error when Running the Script (also called Runtime Error) • Miscalculation • Unexpected Results

  7. Selection (if-then-else) • Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else): Control Proceeds Dependent on Conditions Iteration (looping): Control Repeated until Condition Met

  8. MatLab if Statement Syntax • Syntax if condition statements; end; • If condition Is True, statements Are Executed • If condition Is False, statements Are Not Executed

  9. Conditional Operators • Relational Operators: < , > , >= , <= • Equality Operators: == , ~= • Comparing Strings : • strcmp(str1, str2) • Returns true or false

  10. Selection Examples if age < 30 disp('You are very very Young'); end if strcmp(grade , 'A') disp('Congratulations!'); end if ~strcmp(grade,'F') disp('You passed!'); end

  11. else Statement • Syntax: if (condition) statement(s);//condition true else statement(s);//condition false end

  12. if-else Example if myGrade >= 60 disp('You passed!'); else disp('How about them Cubs?'); end

  13. Nested if Statements if myGrade >= 80 if myGrade >= 90 disp('You have an A!'); else disp('You have a B!'); end else disp('We''ll give you a C.'); end

  14. if/else (Cascaded) if myGrade > 90 disp('A!'); else if myGrade > 80 disp('B!'); else if myGrade > 70 disp('C!'); else disp('Oh-oh!'); end end end

  15. Logical Operators • A Boolean Expression Is an Expression with a Value of Either True or False • A Logical Operator Is One Used to Further Specify True or False in an Expression • Connects Two or More Expressions Together • && Is Logical “AND” • || Is Logical ‘OR” • &&: Both Operands Must Be True for Entire Expression to Be True • ||: Only One (or Both) of the Operands Must Be True for Entire Expression to Be True • ~ is Logical “Not” (Take Opposite of Boolean Expression)

  16. Logical Operators if numStudents > MIN && numStudents < MAX classRun = 1; else classRun = 0; end if numStudents > MAX || numInstructors == 0 classRun = 0; end if classRun disp('Class will Run!'); end

  17. Operator Precedence • () • ~ (not) • *, /, % • +, - • <, <=, >, >= (Relational Operators) • ==, ~= (Equality Operators) • && (Logical AND) • || (Logical OR) • = (ASSIGNMENT)

  18. Multiple Logical Operators if num1 > MAX || num2 == 0 && num3 == 0 disp('num1 is MAX or something is 0'); end disp('I think…..');

  19. Switch Statements • Also Called Switch/Case Statement • Just Case in Other Languages • Selects Among Several Different Actions • If Value Is Matched, Statements under Control of that Case Block Are Executed

  20. Switch Case Example n = input('Enter a number: '); switch n case -1 disp('negative one') case 0 disp('zero') case 1 disp('positive one') otherwise disp('other value') end

  21. Switch Case Example BASERATE = 200; n = input('Enter Passengers: '); switch n case 2 rate = BASERATE * 0.80; case 4 rate = BASERATE * 0.75; case 5 rate = BASERATE * 0.55; otherwise rate = BASERATE; end out1 = sprintf('Your rate is %.2f',rate); disp(out1);

  22. Switch Case Example BASERATE = 200; n = input('Enter Passengers: '); switch n case {2,3} rate = BASERATE * 0.80; case 4 rate = BASERATE * 0.75; case 5 rate = BASERATE * 0.55; otherwise rate = BASERATE; end out1 = sprintf('Your rate is %.2f',rate); disp(out1);

  23. Switch Case Example cmd = input('Enter a command: ','s'); switch cmd case 'left' disp('going left') case 'right' disp('going right') case 'up' disp('going up') otherwise disp('unknown command') end

More Related