Algorithmic Operations and Structured Programming in MATLAB, Slides of Computational Methods

An introduction to algorithms, algorithmic operations, and structured programming using matlab as an example. It covers the concepts of iterative operations (loops), structured programming, and developing computer solutions. The document also discusses the importance of proper documentation and effective use of pseudocode.

Typology: Slides

2012/2013

Uploaded on 04/30/2013

bipasha
bipasha 🇮🇳

4.4

(30)

159 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chp4 MATLAB
Programming-1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

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

  1. 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
  2. ITERATIVE OPERATIONS (LOOPS): Control structures that repeat the execution of a block of instructions

Structured Program Advantages

  1. 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 ).
  2. 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

  1. Structured programming is effective in a teamwork environment because several people can work on a common program, each person developing one or more modules.
  2. 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

  1. 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, ±
  1. 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)

  1. 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

  1. Proper selection of variable names to reflect the quantities they represent
  • Use DESCRIPTIVE and/or LONG names
  1. Use of comments within the program
    • Also helps CLARIFY the WRITING of the Code
  2. 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

  1. 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