Download Algorithmic Operations and Structured Programming in MATLAB and more Slides Computational Methods in PDF only on Docsity!
Chp4 MATLAB
Programming-
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
- Use the MATLAB DeBugger to
Correct Errors (time permitting)
Algorithm Defined
- ALGORITHM ≡ an ORDERED SEQUENCE of PRECISELY defined instructions that performs some task in a finite amount of time. - ORDERED means that the instructions can be NUMBERED, but an algorithm must have the ability to ALTER the order of its instructions using a CONTROL structure.
- There are three categories of algorithmic operations → Next Slide:
Algorithmic Operations
1. SEQUENTIAL OPERATIONS →
Instructions executed in order
- CONDITIONAL OPERATIONS → Control structures that first ask a question to be answered with a true/false response and then select the next instruction based on the answer
- ITERATIVE OPERATIONS (LOOPS): Control structures that repeat the execution of a block of instructions
Structured Program Advantages
- Structured programs are easier to write because the programmer can study the overall problem first and then deal with the details later. 2. Modules (functions) written for one application can be used for other applications (this is called reusable code ).
- Structured programs are easier to debug because each module is designed to perform just one task and thus it can be tested separately from the other modules.
Structured Program Advantages
- Structured programming is effective in a teamwork environment because several people can work on a common program, each person developing one or more modules.
- Structured programs are easier to understand and modify , especially if meaningful names are chosen for the modules and if the documentation clearly identifies the module’s task.
Developing Computer Solns cont
- Work through the solution steps by hand or with a calculator ;
- Use a simpler set of data if necessary
- Test for EXTREME cases with Input or Output = ±∞, 0, ±
- Write and Run the program
- Correct Compile/Execution Time Errors
- Missing Comma or Parens, MisTyped Var names, etc.
- Correct RunTime Errors
- e.g., NaN’s, inf’s, Divide-by-Zero, Complex Numbers, etc.
Developing Computer Solns cont
6. Check the output of the program with your hand solution(s)
- Run the program with your input data and perform a reality check on the output.
- Output MUST NOT Violate the Laws of Physics; e.g., in Statics (ENGR36)
- a CABLE canNOT support NEGATIVE Tension (you can’t PUSH something with a ROPE)
- Objects canNOT have NEGATIVE Weight or Mass (at least not until we figure out AntiGravity methods)
- In ENGR43 Physical Resistors, Capacitors, and Inductors can NOT have NEGATIVE Values
Effective Documentation
- Proper selection of variable names to reflect the quantities they represent
- Use DESCRIPTIVE and/or LONG names
- Use of comments within the program
- Also helps CLARIFY the WRITING of the Code
- Use of structure charts
- Like an ORG Chart for your Software Program
- Structure charts show how variables pass between modules in a computer program.
Anatomy of a Structure Chart Module
the called module
Effective Documentation cont
- Use of flowcharts
- Each flowchart has one starting point and one or more ending points that are drawn with a rounded rectangle or oval.
- Steps, actions, or tasks are drawn with rectangles. It helps to use verbs in describing the steps.
- Decisions are drawn with diamonds with labels at the exits. The arrows show the order the steps are taken.
- The shapes in the flowchart are often numbered to make it easier to refer to them
- Drawing a flowchart of a software component makes the flow of control easier to understand
FlowCharting
Example
Basic House Painting
- Note the Cascade of Decision-Diamonds
PseudoCode Example
- Example Problem: Calculate the bill when someone buys a specific no. of some item:
- PseudoCode:
- PROMPT for number of items being purchased
- READ number of items being purchased
- PROMPT for price per item
- READ price per item
- CALCULATE subtotal
- CALCULATE tax
- CALCULATE total
- DISPLAY total
PsuedoCode: Common Terms
- To Describe input , output , computations , etc., the following terms are often used: - Input: INPUT, READ, GET - Output: PRINT, DISPLAY, SHOW - Compute: COMPUTE, CALCULATE, DETERMINE - Initialize: SET, INIT - Add one: INCREMENT, BUMP, STEP - Decisions: TEST, IF/THEN/ELSE, WHILE/DO