22
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 4 Programming and Software EXCEL and MathCAD

ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 4 Programming and Software EXCEL and MathCAD

  • View
    229

  • Download
    1

Embed Size (px)

Citation preview

ECIV 301

Programming & Graphics

Numerical Methods for Engineers

Lecture 4

Programming and Software

EXCEL and MathCAD

Last Time Software Packages

Users

Standard Power

Limited to standard software capabilities

Write your own macros and/or software

Last TimeComputer Implementation

• Algorithm

• Flowchart

• Pseudocode

• Computer Program

Last TimeAlgorithms & Flowcharts

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

Flowchart

The visual representation of an algorithm.

Last Time 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

Last TimeProgramming 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

Last Time Logical Representation

• SequenceComputer 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

Last TimeLogical 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

Last TimeLogical Representation

• Repetition

Implement instructions repeatedly

Last TimeModular 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

A Simple Model

0 Dui FFF

0 mgcvdt

dvm

vm

cg

dt

dv

Fu

FD

Fi

Analytic vs Numerical Solution

Start from Governing Equation

vm

cg

dt

dv

Derivative = Slope

Analytic vs Numerical SolutionUse Finite Difference to Approximate Derivative

vi

ti ti+1

vi+1

True Slope

Approximate Slope

ii

ii

tt

tvtv

dt

dv

1

1

Analytic vs Numerical Solution

vm

cg

dt

dv

ii

ii

tt

tvtv

dt

dv

1

1

iii

ii tvm

cg

tt

tvtv

1

1

Analytic vs Numerical Solution

iiiii tttvm

cgtvtv

11

Numerical Solution

SlopeNew Value

Old Value Step Size

Euler Method

Analytic vs Numerical SolutionAlgorithm

1. Input a sequence of time nodesi.e. Start Time, End Time, Time

Step2. Input model parameters

i.e. m, c

4. Input initial velocityi.e. v(t=0)

3. Input forcing functioni.e. g

Algorithm

5. Check if m=0avoid division by 0

iiiii tttvm

cgtvtv

11

6. For each time node evaluate

7. Other secondary checks…

Excel and MathCAD

EXCEL MathCAD-I MathCAD-II

Project # 1 – DUE 9/12/03

This is a group project. You form your own teams. Each team should consist of UP TO THREE students.

Consider the parachutist problem. Write an Excel Spreadsheet solution using Visual Basic macros to calculate terminal velocities numerically for each of the following cases:• Mass in the range of 0-100 Kg with step 10• For each mass vary the drag coefficient c in the range 0

to 20 with step 2• Plot the terminal velocity as a function of drag coefficient

for each mass