11.07.2015 Views

Introduction to MATLAB 7 for Engineers - The University of Jordan

Introduction to MATLAB 7 for Engineers - The University of Jordan

Introduction to MATLAB 7 for Engineers - The University of Jordan

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Lecture 6 Computer Applications 0933201 Chapter 4Effective documentation can beaccomplished with the use <strong>of</strong>1. Proper selection <strong>of</strong> variable names <strong>to</strong>reflect the quantities they represent.2. Use <strong>of</strong> comments within the program.3. Use <strong>of</strong> structure charts.4. Use <strong>of</strong> flowcharts.5. A verbal description <strong>of</strong> the program,<strong>of</strong>ten in pseudo-code. (in which naturallanguage and mathematical expressions areused <strong>to</strong> construct statements that look likecomputer statements but without detailedsyntax).Documenting with ChartsTwo types <strong>of</strong> charts aid in developing structuredprograms and in documenting them.<strong>The</strong>se are structure charts and flowcharts.A structure chart is a graphical description showing howthe different parts <strong>of</strong> the program are connected <strong>to</strong>gether.4-6 Z.R.KZ.R.K4-7Structure chart <strong>of</strong> a game program. Figure 4.1–14-8Flowcharts are useful <strong>for</strong>developing and documentingprograms that containconditional statements,because they can display thevarious paths (called“branches”) that a program cantake, depending on how theconditional statements areexecuted.Flowchart representation<strong>of</strong> the if statement.Figure 4.1–2Z.R.K4-9Finding BugsDebugging a program is the process <strong>of</strong> finding andremoving the “bugs,” or errors, in a program.Such errors usually fall in<strong>to</strong> one <strong>of</strong> the followingcategories.1. Syntax errors such as omitting a parenthesis orcomma, or spelling a command name incorrectly.<strong>MATLAB</strong> usually detects the more obvious errorsand displays a message describing the error and itslocation.2. Errors due <strong>to</strong> an incorrect mathematical procedure.<strong>The</strong>se are called runtime errors. <strong>The</strong>y do notnecessarily occur every time the program isexecuted; their occurrence <strong>of</strong>ten depends on theparticular input data. A common example isdivision by zero.Z.R.K4-10To locate a runtime errors, try the following:1. Always test your program with a simpleversion <strong>of</strong> the problem, whose answers canbe checked by hand calculations.2. Display any intermediate calculations byremoving semicolons at the end <strong>of</strong>statements.3. To test user-defined functions, trycommenting out the function line andrunning the file as a script.4. Use the debugging features <strong>of</strong> theEdi<strong>to</strong>r/Debugger, which is discussed inSection 4.7.Z.R.KLogical opera<strong>to</strong>rs Table 4.3–1Opera<strong>to</strong>r Name Definition~ NOT ~A returns an array the same dimension as A; thenew array has ones where A is zero and zeroswhere A is nonzero.& AND A & B returns an array the same dimension as Aand B; the new array has ones where both A and Bhave nonzero elements and zeros where either Aor B is zero.| OR A|Breturns an array the same dimension as Aand B; the new array has ones where at least oneelement in A or B is nonzero and zeros where Aand B are both zero.&& Short-Circuit Opera<strong>to</strong>r <strong>for</strong> scalar logical expressions. A&&BAND returns true if both A and B evaluate <strong>to</strong> true, andfalse if they do not.|| Short-Circuit Opera<strong>to</strong>r <strong>for</strong> scalar logical expressions. A ||BOR returns true if either A or B or both evaluate <strong>to</strong> true,4-11and false if they Z.R.Kdo not.Z.R.K. 2008 Page 2 <strong>of</strong> 7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!