eciv 301 programming & graphics numerical methods for engineers lecture 3 programming and...
Post on 21-Dec-2015
223 views
TRANSCRIPT
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
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
Mathematical Model
Fundamental Laws
• Conservation of Mass
• Conservation of Momentum
• Conservation of Energy
Mathematical Model
Change = Increase - Decrease
Change 0 : Transient Computation
Change = 0 : Steady State Computation
Expressed in terms of
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
Software Packages
Users
Standard Power
Limited to standard software capabilities
Write your own macros and/or software
Objectives
• Computer Implementation of Numerical Methods
• Introduce Algorithms• Discuss Flowcharts• Discuss Pseudocodes• Programming Tools and Styles
Computer Implementation
• Algorithm
• Flowchart
• Pseudocode
• Computer Program
Algorithms & Flowcharts
AlgorithmThe step by step procedure that describes the implementation of a solution strategy.
Flowchart
The visual representation of an algorithm.
Flowchart
Symbols Used in Flowcharts
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
VISUAL C++
FORTRAN
Visual Basic
MathCAD
Building Software…Instructions in TEXT form
Building Software…Compiled Form….
Building Software…Compiled Form….
Building Software…Linked Form…. <Name>.exe
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
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
Simple Information Specifications
• Constants: Value does not change
e.g. acceleration of gravity
g_SI=9.81
e.g.
pi=3.141592654
Simple Information Specifications
• Variables: Value depends on the problem– User Input
Assign descriptive names,
e.g. mass instead of m
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.
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
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.
Advanced Information Representation
• Structures and Records– Collection of data types
struct STUDENT{
char Namereal GPAint AgeBOOL InState
}
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
Mathematical Formulas
• Operators, priority rules, intrinsic functions
2**)100/()*10sin(*0.5 ytx
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
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
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
Logical Representation
• Repetition
Implement instructions repeatedly
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
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
Example
Consider Quadratic Equation
02 cbxax
a
acbbx
2
42
With closed form solution
Example
If a,b,c are given
Compute x1 and x2
What Do I Need?
a
acbbx
2
42
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
Example
• Check if b2-4ac>0 – Two Real Roots
• Check if b2-4ac<0 – Imaginary Roots
a
acbbx
2
42
ExampleSTART
a=0
Loop
Input a, b, c
Print Solution
1
FALSE
2
TRUE
SolveAgain
TRUEEND
FALSE
Example1
d
a
dbx
21
a
dbx
22
Complex Roots
d<0 d>0
d=0
a
bxx
221
acbd 42
Example2
b=0
b
cx
1Trivial Solution
TRUE FALSE
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
Additional Homework
• Problem 2.8
Do not write a program.
Draw the flowchart and pseudocode