eciv 301 programming & graphics numerical methods for engineers lecture 3 programming and...

45
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Post on 21-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

ECIV 301

Programming & Graphics

Numerical Methods for Engineers

Lecture 3

Programming and Software

Page 2: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Last TimeMathematical Model

Physical Problem

Mathematical Model

Data Theory

Numeric or Graphic Results

Implementation

Page 3: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Last TimeMathematical Model

A formulation or equation that expresses the essential features of a physical system or process in mathematical terms

Dependent Variable = f

Independent Variables

Forcing FunctionsParameters

Page 4: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Mathematical Model

Fundamental Laws

• Conservation of Mass

• Conservation of Momentum

• Conservation of Energy

Page 5: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Mathematical Model

Change = Increase - Decrease

Change 0 : Transient Computation

Change = 0 : Steady State Computation

Expressed in terms of

Page 6: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Analytic vs Numerical Solution

0

15

30

45

60

0 5 10 15 20 25

Time (s)

Ve

loc

ity

(m

/s)

Analytic Solution

Numerica Solutionl

t (s) v (m/s)

0.0 0

2 19.6

4 32

6 39.85

8 44.82

10 47.97

12 49.96

Page 7: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Software Packages

Users

Standard Power

Limited to standard software capabilities

Write your own macros and/or software

Page 8: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Objectives

• Computer Implementation of Numerical Methods

• Introduce Algorithms• Discuss Flowcharts• Discuss Pseudocodes• Programming Tools and Styles

Page 9: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Computer Implementation

• Algorithm

• Flowchart

• Pseudocode

• Computer Program

Page 10: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Algorithms & Flowcharts

AlgorithmThe step by step procedure that describes the implementation of a solution strategy.

Flowchart

The visual representation of an algorithm.

Page 11: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Flowchart

Symbols Used in Flowcharts

Page 12: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Pseudocode and Computer Code

Computer CodeSet of Instructions in a computer language that direct the computer to perform a certain task.

PseudocodeSet of code-like instructions that express the flowchart in a narrative form

Page 13: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

VISUAL C++

Page 14: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

FORTRAN

Page 15: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Visual Basic

Page 16: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

MathCAD

Page 17: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Building Software…Instructions in TEXT form

Page 18: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Building Software…Compiled Form….

Page 19: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Building Software…Compiled Form….

Page 20: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Building Software…Linked Form…. <Name>.exe

Page 21: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Programming Topics• Simple Information Specification

– Constants, variables, type declarations

• Advanced Information Representation– Arrays, data structures, records

• Mathematical Formulas– Operators, priority rules, intrinsic functions

• Input/Output• Logical Representation

– Sequence, selection and repetition

• Modular Programming– Functions and Subroutines

Page 22: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Programming Topics• Simple Information Specification

– Constants, variables, type declarations

• Advanced Information Representation– Arrays, data structures, records

• Mathematical Formulas– Operators, priority rules, intrinsic functions

• Input/Output• Logical Representation

– Sequence, selection and repetition

• Modular Programming– Functions and Subroutines

Page 23: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Simple Information Specifications

• Constants: Value does not change

e.g. acceleration of gravity

g_SI=9.81

e.g.

pi=3.141592654

Page 24: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Simple Information Specifications

• Variables: Value depends on the problem– User Input

Assign descriptive names,

e.g. mass instead of m

Page 25: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Simple Information Specifications

• Type Declaration

real mass 8 decimal accuracy

int istep integer

double g_SI 16 decimal accuracy

BOOL trivial TRUE/FALSE

e.t.c.

Page 26: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Programming Topics• Simple Information Specification

– Constants, variables, type declarations

• Advanced Information Representation– Arrays, data structures, records

• Mathematical Formulas– Operators, priority rules, intrinsic functions

• Input/Output• Logical Representation

– Sequence, selection and repetition

• Modular Programming– Functions and Subroutines

Page 27: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Advanced Information Representation

• Arrays– N-dimensional matrices of same data type

real v(10) velocity at 10 time steps

int cell(100,100) 100x100 rectangular matrix

e.t.c.

Page 28: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Advanced Information Representation

• Structures and Records– Collection of data types

struct STUDENT{

char Namereal GPAint AgeBOOL InState

}

Page 29: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Programming Topics• Simple Information Specification

– Constants, variables, type declarations

• Advanced Information Representation– Arrays, data structures, records

• Mathematical Formulas– Operators, priority rules, intrinsic functions

• Input/Output• Logical Representation

– Sequence, selection and repetition

• Modular Programming– Functions and Subroutines

Page 30: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Mathematical Formulas

• Operators, priority rules, intrinsic functions

2**)100/()*10sin(*0.5 ytx

Page 31: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Programming Topics• Simple Information Specification

– Constants, variables, type declarations

• Advanced Information Representation– Arrays, data structures, records

• Mathematical Formulas– Operators, priority rules, intrinsic functions

• Input/Output• Logical Representation

– Sequence, selection and repetition

• Modular Programming– Functions and Subroutines

Page 32: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Logical Representation• Sequence

Computer code is to be implemented one instruction at a time

tm

c

ec

gmv 1Analytic Solution

Enter g,m,c

Enter time t

Evaluate v

Print t,v

Page 33: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Logical Representation

• Selection

Computer code flow is split into branches based on a logical decision.

Enter g,m,c

Check if m=0

Print Error MessageOK to continue

Page 34: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Logical Representation

• Repetition

Implement instructions repeatedly

Page 35: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Programming Topics• Simple Information Specification

– Constants, variables, type declarations

• Advanced Information Representation– Arrays, data structures, records

• Mathematical Formulas– Operators, priority rules, intrinsic functions

• Input/Output• Logical Representation

– Sequence, selection and repetition

• Modular Programming– Functions and Subroutines

Page 36: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Modular Programming

• Module is a self contained and independent code segment

• Modules are designed to perform a specific, well designed task

• Functions return a single result

• Subroutines return several results

Page 37: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Example

Consider Quadratic Equation

02 cbxax

a

acbbx

2

42

With closed form solution

Page 38: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Example

If a,b,c are given

Compute x1 and x2

What Do I Need?

a

acbbx

2

42

Page 39: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Example

• Ask user to input a,b,c

• Check if a=0 – Linear equation, this solution does not apply

• Check if a=0 and b=0– Trivial Solution

• Check if b2-4ac=0 – Double Real Root

a

acbbx

2

42

Page 40: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Example

• Check if b2-4ac>0 – Two Real Roots

• Check if b2-4ac<0 – Imaginary Roots

a

acbbx

2

42

Page 41: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

ExampleSTART

a=0

Loop

Input a, b, c

Print Solution

1

FALSE

2

TRUE

SolveAgain

TRUEEND

FALSE

Page 42: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Example1

d

a

dbx

21

a

dbx

22

Complex Roots

d<0 d>0

d=0

a

bxx

221

acbd 42

Page 43: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Example2

b=0

b

cx

1Trivial Solution

TRUE FALSE

Page 44: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

DO INPUT a, b, c d = b**2 – 4*a*c IF a = 0 THEN IF b=0 THEN TRIVIAL SOLUTION ELSE x1 = -c/b ENDIF ELSE IF d<0 THEN COMPLEX ROOTS ELSE IF d = 0 THEN x1=x2=-b/2a ELSE x1 = (-b+SQRT(d))/(2*a) x2 = (-b-SQRT(d))/(2*a) ENDIF ENDIF PRINT Continue? INPUT Control IF ( CONTROL == FALSE ) EXITENDDO

PSEUDOCODESTART

a=0

Loop

Input a, b, c

Print Solution

1

FALSE

2

TRUE

SolveAgain

TRUEEND

FALSE

acbd 42

Page 45: ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software

Additional Homework

• Problem 2.8

Do not write a program.

Draw the flowchart and pseudocode