ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software
Last Time Mathematical Model Physical Problem Mathematical Model DataTheory Numeric or Graphic Results Implementation
Last Time Mathematical 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 Functions Parameters
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 t (s)v (m/s)
Software Packages Users StandardPower 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 Algorithm The 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 Code Set of Instructions in a computer language that direct the computer to perform a certain task. Pseudocode Set 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…..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=
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 mass8 decimal accuracy int istepinteger double g_SI16 decimal accuracy BOOL trivialTRUE/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 { charName real GPA int Age BOOLInState }
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
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 Analytic 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 Message OK 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 With closed form solution
Example If a,b,c are given Compute x 1 and x 2 What Do I Need?
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 b 2 -4ac=0 –Double Real Root
Example Check if b 2 -4ac>0 –Two Real Roots Check if b 2 -4ac<0 –Imaginary Roots
Example START a=0 Loop Input a, b, c Print Solution 1 FALSE 2 TRUE Solve Again TRUE END FALSE
Example 1 d Complex Roots d<0d>0 d=0
Example 2 b=0 Trivial Solution TRUEFALSE
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 PRINT Continue? INPUT Control IF ( CONTROL == FALSE ) EXIT ENDDO PSEUDOCODE START a=0 Loop Input a, b, c Print Solution 1 FALSE 2 TRUE Solve Again TRUE END FALSE
Additional Homework Problem 2.8 Do not write a program. Draw the flowchart and pseudocode