1 / 42

Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

Engr/Math/Physics 25. Chp4 MATLAB Programming-2. Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu. Learning Goals. Write MATLAB Programs That can MAKE “Logical” Decisions that Affect Program Output Write Programs that Employ LOOPing Processes

nani
Télécharger la présentation

Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

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. Engr/Math/Physics 25 Chp4 MATLABProgramming-2 Bruce Mayer, PE Licensed Electrical & Mechanical EngineerBMayer@ChabotCollege.edu

  2. Learning Goals • Write MATLAB Programs That can MAKE “Logical” Decisions that Affect Program Output • Write Programs that Employ LOOPing Processes • For→ No. Loops know a priori • while → Loop Terminates based on Logic Criteria

  3. Writing and testing of individual modules (the unit-testing phase). Writing of the top-level program that uses the modules (the build phase). Not all modules are included in the initial testing. As the build proceeds, more modules are added. Develop Software as a Series of Incremental Builds; i.e., Build-a-Little, Test-a-Little, Repeat Large Program Development - 1

  4. Testing of the first complete program (the alpha release phase). This is usually done only in-house by technical people closely involved with the program development. There might be several alpha releases as bugs are discovered and removed. Testing of the final alpha release by in-house personnel and by familiar and trusted outside users, who often must sign a confidentiality agreement. This is the beta release phase, and there might be several beta releases. Large Program Development - 2

  5. Errors in Software Code are Called “Bugs” Contrary to the Popular Notion, using the term "bug" to describe inexplicable defects or flaws did NOT originate with Computers “Bug” had been Used for Decades-Prior to describe Mechanical Malfunctions; Consider this Letter from Thomas Edison to a Associate in 1878 Finding Programming Errors It has been just so in all of my inventions. The first step is an intuition, and comes with a burst, then difficulties arise—this thing gives out and [it is] then that “Bugs” - as such little faults and difficulties are called - show themselves and months of intense watching, study and labor are requisite before commercial success or failure is certainly reached.

  6. 1st Computer Bug – Circa 1945 • attributed to Grace Hopper, who publicized the cause of a malfunction in an early electromechanical computer

  7. Debugging a program is the process of finding and removing the “bugs,” or errors, in a software program. Program errors usually fall into one of the following categories. Syntax errors such as omitting a parenthesis or comma, or spelling a command name incorrectly. MATLAB usually detects the more obvious errors and displays a message describing the error and its location. Finding Errors (DeBugging)

  8. Errors due to an incorrect mathematical procedure. Called RUNtime errors. They do not necessarily occur every time the program is executed; their occurrence often depends on the particular input data. A common example is Division by Zero Another example is the production of COMPLEX results when not expected Types of Bugs cont

  9. Locating RUNtime Errors • Always test your program with a simple version of the problem, whose answers can be checked by hand calculations. • Display any intermediate calculations by removing semicolons at the end of statements • ADD & Display TEMPORARY Intermediate results • Can Comment Out later if Desired

  10. Locating RUNtime Errors cont. • To test user-defined functions, try commenting out the function line and running the file as a script. • Use the debugging features of the Editor/Debugger, as Discussed in §4.8 of the TextBook

  11. ReCall RELATIONAL Operators • Expressions Evaluated With Relational Operators Produce a Quantitative Result in BINARY form; either “1” or “0”

  12. Given x = [6,3,9] and y = [14,2,9] A MATLAB Session Relational Operator Examples • relational operators can be used for array addressing • With x & y as before >> z = (x < y) z = 1 0 0 >> z = (x ~= y) z = 1 1 0 >> z = (x > 8) z = 0 0 1 >> z = x(x<y) z = 6 • finds all the elements in x that are less than the corresponding elements in y • e.g. x(1) = 6

  13. The arithmetic operators +, -, *, /, and \ have precedence over the relational operators Thus the statement Operator Precedence • Parentheses Can Change the Order of Precedence; for example >> z = 5 > 2 + 7 z = 0 >> z = (5 > 2) + 7 z = 8 >> z = (5>2) z = 1 z = 5 > 2 + 7 • Is Equivalent to z = 5 >(2+7) • Returns Result z = 0

  14. When the RELATIONAL operators are used, such as The Logical Class x = (5 > 2) they CREATE a LOGICAL variable, in this case, x • Logical variables may take only two values: • 1 (true) • 0 (false)

  15. Just because an array contains only 0s & 1s, however, it is not necessarily a logical array. For example, in the following session k and w appear the same, but k is a logical array and w is a numeric array, and thus an error message is issued The Logical Class cont >> x = [-2:2]; k = (abs(x)>1) k = 1 0 0 0 1 >> z = x(k) z = -2 2 >> w = [1,0,0,0,1]; v = x(w) ??? Subscript indices must either be real positive integers or logicals.

  16. When a logical array is used to ADDRESS another array, it extracts from that array the ELEMENTS in the locations where the logical array has 1s. Accessing Arrays w/ Logicals • So typing A(B), where B is a logical array of the same size as A, returns the VALUES of A at the indices (locations) where B is 1.

  17. Specifying array subscripts with logical arrays extracts the elements that correspond to the true (1) elements in the logical array Given 3x3 array: A = [5,6,7; 8,9,10; 11,12,13] and Accessing Arrays w/ Logicals cont >> B = logical(eye(3)) B = 1 0 0 0 1 0 0 0 1 • Extract the diagonal elements of A >> C = A(B) C = 5 9 13

  18. Logical Operators for Arrays

  19. The Logical Operators Form The Basis of BOOLEAN (Two Value, T & F, 1 & 0, or Hi & Lo) Logic Developed by George Boole (1815-1864) The Action of the Boolean Operators are Often Characterized with TRUTH Tables Boolean Logic

  20. AND Operator ALL Terms are Present Venn Diagrams for Booleans • OR Operator • ANY ONE of the terms are present • AND represents the INTERSECTION (∩) of Sets • OR represents the UNION (U) of Sets

  21. MATLAB SynTax Xor → EXCLUSIVE or • Xor Truth Table C = xor(A, B) • xor Evaluates as TRUE Only If EACTLY ONE of A or B is True, but NOT BOTH “Logic Gate” Symbol used in Electrical Engineering • Xor Venn Diagram

  22. NAND  ~& “Not AND” Other Logic Gates • NOR  ~| • “Not OR” • NANDs & NORs are easier to implement in HARDWARE than are ANDs & ORs • i.e., They take Fewer Transistors

  23. Boolean Logic can be Mathematically Formalized with the use of Math Operators The Math Operators Corresponding to Boolean Logic Operations: Boolean Algebra • A and B can only be TRUE or FALSE • TRUE represented by 1; FALSE by 0

  24. Commutative: A.B = B.A and A+B = B+A Distributive: A.(B+C) = (A.B) + (A.C) A+(B.C) = (A+B).(A+C) Identity Elements: 1.A = A and 0 + A = A Inverse: A.A = 0 and A + A = 1 Boolean Algebraic Properties • Associative: • A.(B.C) = (A.B).C and A+(B+C) = (A+B)+C • DeMorgan's Laws: • A.B = A + B and • A+B = A.B Verify DeMorgan in MATLAB

  25. Work Problem 14 → Case-a: DeMorgan in MATLAB >> % case-a >> x = 7 x = 7 >> a1 = ~((x < 10)&(x >= 6)) a1 = 0 >> a2 = (~(x < 10))|(~(x >= 6)) a2 = 0 1 1 0 0

  26. Work Problem 14 → Case-b DeMorgan in MATLAB >> % case-b >> y = 3 y = 3 >> b1 = ~((y == 2)|(y > 5)) b1 = 1 >> b2 = (~(y == 2))&(~(y >5)) b2 = 1 0 0 1 1

  27. Logical Opers  Short-Circuit

  28. Avoid Occasional Division-by-Zero Short Circuit Example a = 73 >> b = 3 b = 3 >> x = (b ~= 0) & (a/b > 18.5) x = 1 >> x = (b ~= 0) && (a/b > 18.5) x = 1 b = 0 >> x = (b ~= 0) & (a/b > 18.5) Warning: Divide by zero. x = 0 >> x = (b ~= 0) && (a/b > 18.5) x = 0

  29. Precedence  Operator Types

  30. Logical Functions

  31. More Logical Functions

  32. More Logical Functions

  33. find(v) The find Function • Computes an array containing the indices of the nonzero elements of the vector v. • [u,v,w]=find(A) • Computes the arrays u and v containing the row and column indices of the nonzero elements of the array A and computes the array w containing the values of the nonzero elements. • The array w may be omitted.

  34. For the Session Logical Ops and find Function >> x = [5, -3, 0, 0, 8];y = [2, 4, 0, 5, 7]; >> z = find(x&y) z = 1 2 5 • Note that the find function returns the indices, and not the values.

  35. Remember, the find function returns the indices, and not the values. In the following session, note the difference between the result obtained by y(x&y) and the result obtained by find(x&y) in the previous slide. Logical Ops and find Fcn cont >>x = [5, -3, 0, 0, 8];y = [2, 4, 0, 5, 7]; >> values = y(x&y) values = 2 4 7 >> how_many = length(values) how_many = 3

  36. All Done for Today George Boole(1815 - 1864)

  37. Engr/Math/Physics 25 Appendix Time For Live Demo Bruce Mayer, PE Licensed Electrical & Mechanical EngineerBMayer@ChabotCollege.edu

  38. Find Demo >> x = [5, -3, 0, 0, 8];y = [2, 4, 0, 5, 7]; >> u = x&y u = 1 1 0 0 1 >> v = x(u) v = 5 -3 8 >> w = y(u) w = 2 4 7 >> z = find(x&y) z = 1 2 5

  39. Work Problem 14 → Case-a: DeMorgan in MATLAB >> % case-a >> x = 7 x = 7 >> a1 = ~((x < 10)&(x >= 6)) a1 = 0 >> a2 = (~(x < 10))|(~(x >= 6)) a2 = 0 1 1 0 0

  40. Work Problem 14 → Case-b DeMorgan in MATLAB >> % case-b >> y = 3 y = 3 >> b1 = ~((y == 2)|(y > 5)) b1 = 1 >> b2 = (~(y == 2))&(~(y >5)) b2 = 1 0 0 1 1

  41. Software Class • Defined: A software module that provides both procedural and data abstraction. It describes a set of similar objects, called its instances • A Software object is defined via its CLASS, which determines everything about an object. Objects are individual instances of a class. For example, you may create an object call Spot from class Dog. The Dog class defines what it is to be a Dog object, and all the "dog-related" messages a Dog object can act upon.

  42. Software Objects & Classes • http://www.softwaredesign.com/objects.html • Object-oriented software is all about OBJECTS. An object is a "black box" which receives and sends messages. A black box actually contains code (sequences of computer instructions) and data (information which the instructions operates on). Traditionally, code and data have been kept apart. For example, in the C language, units of code are called functions, while units of data are called structures. Functions and structures are not formally connected in C. A C function can operate on more than one type of structure, and more than one function can operate on the same structure. • Not so for object-oriented software! In o-o (object-oriented) programming, code and data are merged into a single indivisible thing -- an object. This has some big advantages, as you'll see in a moment. But first, here is why SDC developed the "black box" metaphor for an object. A primary rule of object-oriented programming is this: as the user of an object, you should never need to peek inside the box! • How are objects defined? An object is defined via its CLASS, which determines everything about an object. Objects are individual instances of a class. For example, you may create an object call Spot from class Dog. The Dog class defines what it is to be a Dog object, and all the "dog-related" messages a Dog object can act upon.

More Related