Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

(29)

160 documents

1 / 45

Toggle sidebar

Related documents


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-1 Docsity.com 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) Docsity.com Algorithmic Operations 1. SEQUENTIAL OPERATIONS → Instructions executed in order 2. 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 3. ITERATIVE OPERATIONS (LOOPS): Control structures that repeat the execution of a block of instructions Docsity.com Structured Programming • STRUCTURED PROGRAMMING ≡ A technique for organizing and coding computer programs in which a hierarchy of modules is used, each having a single entry and a single exit point, and in which control is passed downward through the structure withOUT UNconditional branches to higher levels of the structure. Three types of control flow are used: (1) sequential, (2) test, and (3) iteration. Docsity.com 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). 3. 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. Docsity.com Developing Computer Solns cont 4. 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 5. 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. Docsity.com Developing Computer Solns cont 6. Check the output of the program with your hand solution(s) 7. 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 Docsity.com Developing Computer Solns cont 8. If you will use the program as a general tool in the future, test it by running it for a range of reasonable data values; – Perform a Reality Check on the results Docsity.com Example: Pizza Pricing Problem • Write a program to find the UNIT price for a given pizza. The size of the pizza will be provided in inches. The result must be cost/inch2 Docsity.com Effective Documentation cont 4. 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 Docsity.com FlowCharting Example  Basic House Painting • Note the Cascade of Decision-Diamonds Docsity.com 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 Docsity.com PsuedoCode Algorithms • Ensure that the the task is completely specified • Issues to Consider – What data is known before the program runs? – What data must be input by the user? – What computations will be performed on the data? – What data will be output (displayed) to the user? Docsity.com Solve Quadratic Equation  PsuedoCode 1. Get coeffs a, b, & c 2. Let d = b2 − 4ac 3. If d < 0 print 'no real solutions' and go to step 5 4. Let X1 = (−b + SQRT(d))/(2a) 5. Let X2 = (−b − SQRT(d))/(2a) 6. Print X1 and X2 7. Stop 02 =++ cbxax Docsity.com Program Design - FlowChart • Flowchart ≡ a graphical representation of algorithms • Rectangle is used for calculations • Parallelogram is used for input and output • Circle is used as connector • Diamond is used as decision • Symbols are connected by arrows to represent the order of the operations; i.e., the direction of program flow Docsity.com FlowChart Symbols – Calc’s • Calculations (e.g. arithmetic expressions) are shown in rectangles – Example: Total = Cost + Tax • Example Num = Num + 1 – add one to the current value of Num and make that the new value of Num Total = Cost + Tax Num = Num + 1 Docsity.com FlowChart Symbols – In/Out Put • Data input and output are shown in parallelograms  Input ≡ a read operation of data from a peripheral device to memory • e.g., a user typing in data at a keyboard READ Num WRITE Num  Output ≡ a write operation of data to a peripheral device from memory • e.g., data displayed to the monitor Docsity.com FlowChart Example • Sequential-FlowChart for the Previous Pseudocode Algorithm: Allow1 = Age1 x Rate input Age1 start Print Allow1 stop prompt Age1 Allow2 = Age2 x Rate Print Allow2 input Age2 prompt Age2 • PROMPT Age of Child1 • READ for Age of Child1 • PROMPT Age of Child2 • READ for Age of Child2 • CALCULATE – Allowance for Child1 = Age of Child1 x Rate • CALCULATE – Allowance for Child2 = Age of Child2 x Rate • DISPLAY Allowance for Child1 • DISPLAY Allowance for Child2 Docsity.com Common FlowChart Symbols Terminator. Shows the starting and ending points of the program. A terminator has flowlines in only one direction, either in (a stop node) or out (a start node). Data Input or Output. Allows the user to inputdata and results to be displayed. Processing. Indicates an operation performed by the computer, such as a variable assignment or mathematical operation. Decision. The diamond indicates a decision structure. A diamond always has two flowlines out. One flowlineout is labeled the “yes” branch and the other is labeled the “no” branch. Predefined Process. One statement denotes a group of previously defined statements. For instance, “Calculate m!” indicates that the program executes the necessary commands to compute m factorial. Connector. Connectors avoid crossing flowlines, making the flowchart easier to read. Connectors indicate where flowlines are connected. Connectors come in pairs, one with a flowline in and the other with a flowline out. Off-page connector. Even fairly small programs can have flowcharts that extend several pages. The off-page connector indicates the continuation of the flowchart on another page. Just like connectors, off-page connectors come in pairs. Flowline. Flowlines connect the flowchart symbols and show the sequence of operations during the program execution. Common Flowchart Symbols Docsity.com Top-Down Dsgn: Structure Chrt • As we Have Seen a FlowChart: – Details exactly HOW a program will do each task • In contrast, the next design tool, the Structure Chart: – Shows only WHAT tasks your program will do (not HOW it will complete them) Docsity.com Structure Charts – Data Flow • Passing of data between tasks is shown by arrows going up from or down to the task’s rectangle • An upward arrow indicates that the data value has been set inside the task and is being passed out for use by other tasks • A downward arrow indicates a data value previously set in some other task is now being passed into this task • Data may also be passed both into a task (downward arrow), modified, and passed back out again (upward arrow). Task SubTask1 SubTask2 Data1 Data1 Docsity.com Sample Structure Chart • Given a generic program that reads some data from the user, runs some calculations using that data, and displays results to the user, the structure chart could look like this: main Get_Input Process_Data Display_Results Data Result Data Result Docsity.com Structure Chart: Sequential Program • Using our previous sequential program design example (calculate the two childrens’ allowances, based upon 75¢ per year-age), the structure chart might be: main Get_Ages Calculate_Allowances Display_Allowances Age1, Age2 Allow1, Allow2 Age1, Age2 Allow1, Allow2 Docsity.com All Done for Today LBL Org Chart  In Orgs or SW-Programs → Form FOLLOWS Function Docsity.com Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Appendix ( ) 6972 23 −+−= xxxxf Docsity.com Yet More Org Charts Division Chief Sormal Police Department Organizational Chart Updated Jane 17, 2004 ‘Walt F. Brooks Chief of Police Kent D. Crutcher I neaers ae Assistant Chiaf Deputy Chief Richard Bleicher Assistant Chief Bai ra hearin Ana L. Grad! ; 1 a (acting) f I T 1 Eumcwy Samper cevkes Ts Psa crectator Serene Petanert inka Ub Fes Kigaa eea>r Ue Mok Rete ieee sareenytortor |] | sex Dave stearene JJ 0 ce we | I I ] eStcan Lt l a I Applications Engineering Branch Physics Simulation Research Branch Corman sy Sere ‘dD a La Tazo Pad ties Branch Steven Walworth and Modeling Office Bryan Biegel reaten Ci Steers Cts asscciate Dochan Kwak (Acting) Nagi Mansour T T [ae | View Sutealves ieertves Taare Pro athe rt Pc ea Senioe Cheese (eee | Lae Says tba S92 Srith Feprscestvse | [urns nasa sane Se RoHosull mm fly Sq Deu Bey ‘Set SreaLeicod Coicerce Wareger Peyareah Dest I Z Peval cicers TEasense <p. “Techr can I Pils Ser Pina a Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved