bruce mayer, pe registered electrical & mechanical engineer bmayer@chabotcollege

45
[email protected] • ENGR-25_Programming-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical & Mechanical Engineer [email protected] Engr/Math/Physics 25 Chp4 MATLAB Programming-1

Upload: arista

Post on 23-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Engr/Math/Physics 25. Chp4 MATLAB Programming-1. Bruce Mayer, PE Registered Electrical & Mechanical Engineer [email protected]. Learning Goals. Write MATLAB Programs That can MAKE “Logical” Decisions that Affect Program Output Write Programs that Employ LOOPing Processes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt1

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Bruce Mayer, PERegistered Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Chp4 MATLAB

Programming-1

Page 2: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt2

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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)

Page 3: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt3

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

What’s an “Algorithm” A postage stamp

issued by the USSR in 1983 to commemorate the 1200th anniversary of Muhammad al-Khowarizmi, after whom algorithms are named.

Page 4: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt4

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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:

Page 5: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt5

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Algorithmic Operations1. SEQUENTIAL OPERATIONS →

Instructions executed in order2. 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

Page 6: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt6

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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.

Page 7: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt7

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Structured Program Advantages1. 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.

Page 8: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt8

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Structured Program Advantages4. Structured programming is effective in

a teamwork environment because several people can work on a common program, each person developing one or more modules.

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

Page 9: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt9

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Developing Computer Solns 1. State the problem concisely

• Use Math Eqns/Relns Whenever possible

2. Specify the data to be used by the program. This is the “input.” • In physical Problems These are typically

– Boundary Conditions– Initial Conditions– Constraints– Parameters

3. Specify the information to be generated by the program. This is the “output.”

Page 10: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt10

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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.

Page 11: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt11

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 12: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt12

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 13: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt13

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Effective Documentation 1. Proper selection of variable names to

reflect the quantities they represent• Use DESCRIPTIVE and/or LONG names

2. Use of comments within the program• Also helps CLARIFY the WRITING of the

Code

3. Use of structure charts• Like an ORG Chart for

your Software Program– Structure charts show how

variables pass between modules in a computer program.

Page 14: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt14

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Anatomy of a Structure Chart Module

the called module

Page 15: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt15

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 16: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt16

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 17: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt17

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

FlowCharting Example

Basic House Painting• Note the Cascade of

Decision-Diamonds

Page 18: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt18

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Effective Documentation cont 5. A verbal description of the program,

often in pseudocode.• Generic way of describing

an algorithm, without use of any specificprogramming language

• Helps programmers to PLAN an algorithm

• Not an actual programming Language, but may borrow syntax from popular programming languages

• Styles vary

Page 19: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt19

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 20: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt20

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 21: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt21

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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?

Page 22: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt22

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Solve QuadraticEquation

PsuedoCode1. Get coeffs a, b, & c2. Let d = b2 − 4ac3. If d < 0 print 'no real solutions'

and go to step 54. Let X1 = (−b + SQRT(d))/(2a)

5. Let X2 = (−b − SQRT(d))/(2a)

6. Print X1 and X2

7. Stop

02 cbxax

Page 23: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt23

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

PsuedoCode Example Real World Problem:

• Calculate the allowance for two children, based upon 75¢ per year-of-age

Known Values• Pay Rate = 75 ¢/year

Inputs• Child Age

Calculations• Allowance = Age x Rate

Outputs• Allowances for each child

Page 24: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt24

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

PsuedoCode Example cont

Careful PseudoCode Algorithm:• PROMPT for Age of Child1• READ Age of Child1• PROMPT for Age of Child2• READ 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

Page 25: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt25

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 26: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt26

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

FlowChart Symbols – Calc’s Calculations (e.g.

arithmetic expressions) are shown in rectangles• Example:

Total = Cost + Tax

• ExampleNum = Num + 1 – add one to the current

value of Num and make that the new value of Num

Total = Cost + Tax

Num = Num + 1

Page 27: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt27

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 28: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt28

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

FlowChart Symbols – Decisions Decisions are shown within

Diamonds and specify a condition to be tested

Based on the condition being TRUE or FALSE, the next operation will be determined

A decision is composed of :• A condition• An operation to be done if

condition is TRUE• Possibly an operation to be done

if condition is FALSE

Gross > 50000

Rate = 0.31

Rate = 0.28True

False

Page 29: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt29

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

FlowChart Symbols – Start/End Every algorithm starts

somewhere and terminates somewhere

Every flowchart must have one start symbol and one end symbol

Start and Stop symbols are ovals• A start symbol denotes the start of

the algorithm• An end symbol indicates the

algorithm has terminated

square = num x num

input num

print square

start

stop

Page 30: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt30

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 31: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt31

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Common FlowChart SymbolsTerminator. 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 commandsto 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 witha flowline in and the other with a flowline out.

Off-page connector. Even fairly small programs can have flowcharts that extend severalpages. 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

Page 32: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt32

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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)

Page 33: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt33

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Structure Chart Structure charts are Hierarchical

diagrams → A form of an “Org Chart”• They diagram the OverAll program

(organization) Structure• They show the Relationship between all

the Tasks (workers) in the program• They indicate which Data (Information) is

shared by the tasks (workers)

Page 34: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt34

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Creating Structure Charts A large program design is

first broken into tasks Tasks are repeatedly divided

into even smaller subtasks• Rectangles are used to represent

tasks/subtasks within the program• Lines are used to hierarchically

connect the tasks/subtasks– Subtasks are diagrammed below

the task that they are part of

Task

SubTask1 SubTask2

Page 35: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt35

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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 SubTask2Data1 Data1

Page 36: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt36

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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 ResultData Result

Page 37: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt37

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 38: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt38

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Summary – Structure Charting Put the name of the program in a

rectangle root of an upside-down tree. Determine the main subtasks that

must be performed by the program to solve the problem.• Put main subtasks in

rectangles below the root, and draw a connecting line from the root to each subtask.

Page 39: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt39

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Summary – Structure Charting Examine each subtask individually,

and break them down into even smaller tasks• Put subtasks in rectangles below the task

that they are part of, and draw a connecting line from task to subtask.

Repeat until the BOTTOM TASKS of the tree are VERY SIMPLE to complete

Page 40: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt40

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

All Done for Today

LBLOrg Chart

In Orgs or SW-Programs → Form FOLLOWS Function

Page 41: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt41

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Appendix 6972 23 xxxxf

Page 42: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt42

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Yet More Org Charts

Page 43: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt43

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

CIA Org Chart (not secret)

Page 44: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt44

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Page 45: Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege

[email protected] • ENGR-25_Programming-1.ppt45

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods