hydroinformatics module 4: numerical methods i · pdf filei.popescu:numerical methods...

56
I.Popescu:Numerical methods 1/16/2012 1 Hydroinformatics Module 4: Numerical Methods I Lecture 1 and 2: Models, ODE, Explicit and implicit methods I.Popescu Remarks: 1. Schedule 2. Prerequisites Basic mathematics, Fluid dynamics and fluid mechanics, Mathematical representation of fluid flow equations 3 Marking 1/16/2012 I.Popescu: Numerical Methods 2 3. Marking Assignments (15%) Written examination (30%)

Upload: lamthu

Post on 21-Mar-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

I.Popescu:Numerical methods 1/16/2012

1

Hydroinformatics yModule 4: Numerical Methods I

Lecture 1 and 2: Models, ODE, Explicit and implicit methods

I.Popescu

Remarks:1. Schedule2. Prerequisites

• Basic mathematics, • Fluid dynamics and fluid mechanics,• Mathematical representation of fluid flow equations

3 Marking

1/16/2012 I.Popescu: Numerical Methods 2

3.Marking• Assignments (15%)• Written examination (30%)

I.Popescu:Numerical methods 1/16/2012

2

Course

Main aimintroduce numerical techniques which can be used on computersused on computersoverview of

WHAT it can be doneHOW it can be done

At the end of lectures you will:apply standard methods to simple flow problems and

1/16/2012 I.Popescu: Numerical Methods 3

apply standard methods to simple flow problems and determine the optimal values for time steps and grid sizeassess simulation results produced by different methodsevaluate the relevance of their useidentify and analyse the reason for failure of particular methods and, possibly, provide solutions

1-Why Numerical Methods?

I.Popescu:Numerical methods 1/16/2012

3

Computational Fluid Dynamics

Computational Fluid Dynamics (CFD) is the science of predicting fluid flow, heat transfer mass transfer chemical reactionstransfer, mass transfer, chemical reactions, and related phenomena by solving the mathematical equations which govern these processes using a numerical process (that is, on a computer).

1/16/2012 I.Popescu: Numerical Methods 5

Solution of Equations Phenomena in nature -described in terms of properties that prevail at each point of space and time separately

Formulating PDE’s governing the phenomena

SOLUTIONS

HOW ??

1/16/2012 I.Popescu: Numerical Methods 6

I.Popescu:Numerical methods 1/16/2012

4

Solution of Equations Phenomena in nature -described in terms of properties that prevail at each point of space and time separately

Formulating PDE’s governing the phenomena

Analytical

SOLUTIONS

Too complex equations?

1/16/2012 I.Popescu: Numerical Methods 7

Separationof

variables

Integral Solution(Green)

Integraltransforms(Fourier

& Laplace)

Solution of Equations Phenomena in nature -described in terms of properties that prevail at each point of space and time separately

Formulating PDE’s governing the phenomena

Analytical

SOLUTIONS

1/16/2012 I.Popescu: Numerical Methods 8

Separationof

variables

Integral Solution(Green)

Integraltransforms(Fourier

& Laplace)

I.Popescu:Numerical methods 1/16/2012

5

What is a model?

Is this a model?

1/16/2012 I.Popescu: Numerical Methods 9

What is a model?

Is this a model?

1/16/2012 I.Popescu: Numerical Methods 10

I.Popescu:Numerical methods 1/16/2012

6

What is a model?

Is this a model?

– Naomi Campbel

1/16/2012 I.Popescu: Numerical Methods 11

What is a model?

Is this a model?

1/16/2012 I.Popescu: Numerical Methods 12

I.Popescu:Numerical methods 1/16/2012

7

What is a model?

Is this a model?

1/16/2012 I.Popescu: Numerical Methods 13

What is a model?

Is this a model?

1/16/2012 I.Popescu: Numerical Methods 14

I.Popescu:Numerical methods 1/16/2012

8

What is a model?

1/16/2012 I.Popescu: Numerical Methods 15

What is a model?A simplification of reality, but with essential features -- an example:

RealityReality ModelModel

1/16/2012 I.Popescu: Numerical Methods 16

I.Popescu:Numerical methods 1/16/2012

9

What is a model?

Different formsEngineering problem solving

– A model is a simplified, schematic representations of the real world, a representations that retains enough aspects of the original system to make it useful to the modeller.

In fluid dynamics– physical models (scale models);

1/16/2012 I.Popescu: Numerical Methods 17

– physical models (scale models);– mathematical models

– conceptual– simulation models

Error and uncertainty in modellingDefinitions of model:

"A system which will convert a given input (geometry, boundary conditions, force, etc.) into an output (flow

t l l t ) t b d i i ilrates, levels, pressures, etc.) to be used in civil engineering design and operation" (Novak)"A simplified representation of the natural system" (Resfgaard)

It is not reasonable to expect that a "simplified

1/16/2012 I.Popescu: Numerical Methods 18

representation" convert input into exact output. Errors are inevitable. The actual values of the output variables are uncertain.

I.Popescu:Numerical methods 1/16/2012

10

What’s the difference between a modeling program, and a model?

Modelling program = softwareSoftware with an array of equations that simulate river flowsimulate river flow

Model = software + dataA modelling program + data specific to a certain river

1/16/2012 I.Popescu: Numerical Methods 19

What is a model?

Mathematical models

Q

QQ

1/16/2012 I.Popescu: Numerical Methods 20

H

I.Popescu:Numerical methods 1/16/2012

11

Solution of Equations Phenomena in nature -described in terms of properties that prevail at each point of space and time separately

Formulating PDE’s governing the phenomena

Analytical Numericalt h i

SOLUTIONS

1/16/2012 I.Popescu: Numerical Methods 21

techniquesSeparation

of variables

Integral Solution(Green)

Integraltransforms(Fourier

& Laplace)

FDM FVM FEMBEM

Ph i l

DifferentialEquations

AnalyticalMethods

SimpleDifferentialEquations

Why numerical methods?

Physicalphenomenon

Equations+

BoundaryConditions

ApproximateMethods

FDMor

FVMFEM,

BEM

1/16/2012 I.Popescu: Numerical Methods 22

BEMGeneral differential equations are too complicated to be solved analyticallyFDM, FEM, FVM, BEM are approximate numerical approaches for solving differential equations

I.Popescu:Numerical methods 1/16/2012

12

Why Numerical Methods?

Analytical solutions– derived only for few engineering problems – limited number of closed form solutions– useful to provide insight to the behaviour of an

engineering system

Numerical solutions

1/16/2012 I.Popescu: Numerical Methods 23

– fill the gap between theory and experiment

Why Numerical Methods?

Easy input - preprocessor.Solves many types of problemsy yp pModular design

– fluids, dynamics, heat, etc.

Can run on PC’s now.Relatively low cost, even sometimes

1/16/2012 I.Popescu: Numerical Methods 24

cheaper

I.Popescu:Numerical methods 1/16/2012

13

Why Numerical Methods?

Models are no longer restricted to closed solution problemsRepresent accurately realistically complex system such as:

– non-linear systems– time dependent state variables

Sometimes physical experiments are

1/16/2012 I.Popescu: Numerical Methods 25

Sometimes physical experiments are impossible to be simulated

Simple Mathematical Models

( )var var , ,Dependent iable f independent iable parameters forcing function=

Dependent variableh h ll fl h b h– A characteristic that usually reflects the behavior or

state of the system

Independent variable– Are usually dimensions, such as time and space

Parameters

1/16/2012 I.Popescu: Numerical Methods 26

– Are reflective of system’s properties or compositions

Forcing functions– External influences acting on the system

I.Popescu:Numerical methods 1/16/2012

14

Basic principle of Numerical Methods

DISCRETISATIONAn exact ‘continuous’ solution is represented by an approximate ‘discrete’ solution at a number of points.

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

1/16/2012 I.Popescu: Numerical Methods 27

0 1 2 3 4 5 6 7-1

-0.8

0 1 2 3 4 5 6 7-1

-0.8

Exact solution Discretised approximation

Basic principle of Numerical Methods

DISCRETISATION of space and time

Continuous in time

Discrete in timeApproximation

of the derivative

t

( ) ( )

1/16/2012 I.Popescu: Numerical Methods 28

t4t3t2t1

Δt

t ( ) ( )2 1 2 1

2 1 2 1

U t U tU U Ut t t t t

−Δ −= =

Δ − −

I.Popescu:Numerical methods 1/16/2012

15

Example - simple mathematical model

Newton Second LawFF ma or am

= =m

c

g

( ) ( ) ( ) ( )1 1

new value old value slope x ste

i i i i icv t v t g v t t tm

+ +

= +

⎡ ⎤= + − −⎢ ⎥⎣ ⎦↑ ↑ ↑ ↑

p size

1/16/2012 I.Popescu: Numerical Methods 29

/( ) 1 ct mgmv t ec

−⎡ ⎤= −⎣ ⎦Analytical solution -

Example-simple mathematical model

Newton Second Law- Falling objectM=68.1 kg

c=12 5 kg/sc=12.5 kg/s

t step= 2 secTime (s) Computed velocity (m/s)

Numerically Analytically0.0 0.000 0.0002.0 19.600 16.4044.0 32.005 27.769

1/16/2012 I.Popescu: Numerical Methods 30

6.0 39.856 35.6418.0 44.824 41.09510.0 47.969 44.87312.0 49.959 47.490

: 53.390 53.390

I.Popescu:Numerical methods 1/16/2012

16

Example - simple mathematical model

Newton Second Law- Falling object60

0

10

20

30

40

50

Velo

city

M=68.1 kg

c=12.5 kg/s

t step= 2 sec

1/16/2012 I.Popescu: Numerical Methods 31

00 10 20 30 40 50

Time

numerically analitically

2- Differential equations

- Quick review -

I.Popescu:Numerical methods 1/16/2012

17

Introduction and First Definitions1

Differential equations are equations that contain derivatives

In an algebraic equation, the unknown is a number. InIn an algebraic equation, the unknown is a number. In a differential equation, the unknown is a function.Most often the unknown function we seek, expresses the behavior of the state variable or variables as a function of time. This function it is called state equation

The order of the differential equation is the order

1/16/2012 Review of Mathematics 33

The order of the differential equation is the order of the derivative of the unknown function involved in the equation.

Introduction and First Definitions2

A linear differential equation of order n is a differential written in the following form:

)()()(...)()( 011

1

1 xfyxadxdyxa

dxydxa

dxydxa n

n

nn

n

n =++++ −

where an(x) is not the zero function.

Note: Some books may use the notation y’, ’’ ’’’ (4) f th d i ti

1/16/2012 Review of Mathematics 34

y’’, y’’’, y(4), …for the derivatives.

I.Popescu:Numerical methods 1/16/2012

18

Linear Differential EquationsA linear equation obliges the unknown function y(x) to have some restrictions. Accepted operation on y are:Accepted operation on y are:

Differentiating yMultiplying y and its derivatives by a function of the variable x.Adding what you obtained in

1/16/2012 Review of Mathematics 35

Adding what you obtained in previous step and let it be equal to a function of x.

Solving differential equations (DE)A major field of study

Existence: Does a differential equation have a solution?Uniqueness: Does a differential equation have more than one solution? If yes, how can we find a solution which satisfies particular y , pconditions?

Differential equations require integration to be solvedIf the values of the unknown function y(x) and its derivatives at some point are known the solution to DE is called an initial value problem (in short IVP). If no initial conditions are given, the description of all solutions to the DE is called the general solution

Some kinds of D E ’s can be solved exactly; many not

1/16/2012 Review of Mathematics 36

Some kinds of D.E. s can be solved exactly; many not for those that cannot, there are various ways to approximate the solution

I.Popescu:Numerical methods 1/16/2012

19

d

Single VariableWe know:

Differential Equations

)( yFdtdy

=

We want to know:)(tfy =

1/16/2012 Review of Mathematics 37

Several VariableWe know: We want to know:

Differential Equations

),...,,(1 qxyFdtdy

=

),...,,(2 qxyFdtdx

=

M M

)(2 tfx =

)(1 tfy =

1/16/2012 Review of Mathematics 38

),...,,( qxyFdtdq

n=

M M)(3 tfq =

I.Popescu:Numerical methods 1/16/2012

20

Methods for solving DEs

Solving a differential equation can be done in three major ways:

l i ll- analytically, - qualitatively, - numerically.

1/16/2012 Review of Mathematics 39

Partial derivatives

When a function changes with more than one variable at once – for example, both time and space – we use partial derivativesspace we use partial derivativesNotation:

= means the rate of change in y with tty∂∂

1/16/2012 Review of Mathematics 40

= means the rate of change in y with xxy∂∂

I.Popescu:Numerical methods 1/16/2012

21

Total time derivative

The total time derivative is used to describe the change in a variable in elation to a mo ing (as opposed torelation to a moving (as opposed to

stationary) objectExample:

xyu

ty

DtDy

∂∂

+∂∂

=

1/16/2012 Review of Mathematics 41

u is velocity

xtDt ∂∂

Time & space derivatives: reviewNotation:

= ordinary derivative: y changes only with tdy ordinary derivative: y changes only with t

= partial derivative: change in y with t at a fixed point

= total time derivative: change in y as oneDy

ty∂∂dt

1/16/2012 Review of Mathematics 42

total time derivative: change in y as one moves about (e.g., in a fluid)Dt

I.Popescu:Numerical methods 1/16/2012

22

Classification of DE ( Clasess)dimension of unknown:

ordinary differential equation (ODE) – unknown is a function of one variable, e.g. y(t)partial differential equation (PDE) – unknown is a function of multiple variables, e.g. u(t,x,y)p , g ( , ,y)

number of equations:single differential equation, e.g. y’=ysystem of differential equations (coupled), e.g. y1’=y2, y2’=-g

ordernth order DE has nth derivative, and no higher, e.g. y’’=-g

linear & nonlinear:linear differential equation: all terms linear in unknown and its

1/16/2012 Review of Mathematics 43

qderivativese.g x’’+ax’+bx+c=0 , x’=t2x – linear;x’’=1/x – nonlinear

Classification of PDEs2 2 2

2 2

u u u u ua b c d e fu gx x y y x y

∂ ∂ ∂ ∂ ∂+ + = + + +

∂ ∂ ∂ ∂ ∂ ∂2 4b acΔ = −

elliptic PDE - b2- 4ac < 0 – smooth solutions

hyperbolic PDE - b2- 4ac > 0 – solutions with discontinuities

4b acΔ

1/16/2012 I.Popescu: Numerical Methods 44

parabolic PDE - b2- 4ac =0

I.Popescu:Numerical methods 1/16/2012

23

General features of PDEs

Elliptic PDEs – describe processes that have already reached

steady state– time-independent (e.g. steady-state aquifer flow or

heat diffusion).

Hyperbolic PDEs – describe time-dependent, conservative physical

processes, such as convection, that are not evolving towards steady state (e.g. free surface flow, pipe

1/16/2012 I.Popescu: Numerical Methods 45

y ( g , p pflow).

Parabolic PDEs – describe time-dependent, dissipative physical

processes, such as diffusion, that are evolving towards steady state. (e.g. transient aquifer flow).

What do we need to solve DE?

Bou

ndar

y va

lues

Bou

ndar

y va

lues

Problem domain

1/16/2012 I.Popescu: Numerical Methods 46

a b

BB

Initial values

I.Popescu:Numerical methods 1/16/2012

24

Boundary and initial conditions

Initial value problems (IVP)derivative with respect to time (either dU/dt, or d2U/dt2 etc ) in the equation it is necessaryor d2U/dt2, etc.) in the equation, it is necessary to know the initial value of U in order to compute its value in the future, within the model domain. initial condition of U must be given. this problems are also called initial value

1/16/2012 I.Popescu: Numerical Methods 47

this problems are also called initial value problems

Boundary and initial conditions

Boundary value problems (BVP)– Initial conditions are not always sufficient to ensure

the existence of a unique solutions. Conditions onthe existence of a unique solutions. Conditions on the boundary of the domain of the problem must be given as well. These conditions are called boundary conditions.

C CComes from the boundary

time t time t + Δ t

1/16/2012 I.Popescu: Numerical Methods 48

x x

I.Popescu:Numerical methods 1/16/2012

25

Reminder - Basic formulas

What is a derivative?– Rate of change of a function

slope of the tangent line to the graph of a function– slope of the tangent line to the graph of a function– best linear approximation to a function

f

Real derivative

( ) ( )( ) limx o

f x x f xf xxΔ →

+ Δ −′ =Δ

1/16/2012 I.Popescu: Numerical Methods 49

Approx.derivatives

xx2x2x1 ( ) ( )( ) f x x f xf xx

+Δ −′ ≈Δ

Δx is small

Reminder - Basic formulas

Taylor series expantion2 3 4 5

( ) ( ) ( ) ( ) ( ) ( ) ( )2! 3! 4! 5!x x x xf x x f x xf x f x f x f x f xΔ Δ Δ Δ′ ′′ ′′′ ′′′′ ′′′′′±Δ = ±Δ + ± + ± +K( ) ( ) ( ) ( ) ( ) ( ) ( )2! 3! 4! 5!

f f f f f f f

2 3( ) ( ) 1 ( ) ( ) ( ) ( ) ( )2! 3!

f x x f x x xxf x f x f x f x O xx x

⎡ ⎤+ Δ − Δ Δ′ ′′ ′′′ ′= Δ + + + = + Δ⎢ ⎥Δ Δ ⎣ ⎦K

32( ) ( ) 1 2 ( ) 2 ( ) ( ) ( )f x x f x x xxf x f x f x O x

⎡ ⎤+ Δ − − Δ Δ′ ′′′ ′Δ + + + Δ⎢ ⎥

first order accurate in xΔ

1/16/2012 I.Popescu: Numerical Methods 50

2 ( ) 2 ( ) ( ) ( )2 2 3!

xf x f x f x O xx x

= Δ + + = + Δ⎢ ⎥Δ Δ ⎣ ⎦K

second order accurate in xΔ

I.Popescu:Numerical methods 1/16/2012

26

Reminder - Basic formulas

( ) ( )f x f x x− − Δf(x )

fo rw a rd d i f fe r e n c eb a c k w a rd d i f fe r e n c e

c e n tra l d i f fe r e n c e

( ) ( )f x x f xx

+ Δ −Δ

( ) ( )2

f x x f x xx

+ Δ − −ΔΔ

1/16/2012 I.Popescu: Numerical Methods 51

xx xx x xi-1 i i+ 1= x

2 xΔ

Why study Numerical Methods ?NO numerical method is completely trouble free in all situations!

– How should one choose/use an algorithm to get trouble free and accurate answers?trouble free and accurate answers?

No numerical method is error free!,– What level of error/accuracy do one take in the way

the problem is solved?

No numerical method is optimal for all types/forms of an equation!

1/16/2012 I.Popescu: Numerical Methods 52

yp qIMPORTANT : In order to solve a physical problem numerically, you must understand the behavior of the numerical methods used as well as the physics of the problem

I.Popescu:Numerical methods 1/16/2012

27

Typical difficulties with NM

Instability

1/16/2012 I.Popescu: Numerical Methods 53

Typical difficulties with NM

Inconsistency

1/16/2012 I.Popescu: Numerical Methods 54

I.Popescu:Numerical methods 1/16/2012

28

Typical difficulties with NM

Inaccurate

1/16/2012 I.Popescu: Numerical Methods 55

Errors

Errors due to computers: – Round-off error

Exponent underflow– Exponent underflow – Exponent overflow

Errors due to numerical methods – Truncation error – Propagated error

M f

1/16/2012 I.Popescu: Numerical Methods 56

Measures for errors– absolute error– relative error

I.Popescu:Numerical methods 1/16/2012

29

Errors

1/16/2012 I.Popescu: Numerical Methods 57

Hydroinformaticianas a developer and user must understand how a numerical method performs for his/her given problemhis/her given problemunderstanding how various numerical algorithmsare derived,how the physics effects the numericshow the numerics effects the physics

h t th / t bilit ti

1/16/2012 I.Popescu: Numerical Methods 58

what are the accuracy/stability propertieswhat are the cost of a method for given level ofaccuracy (this varies substantially from method tomethod )

I.Popescu:Numerical methods 1/16/2012

30

Properties of Numerical Methods

Convergence– numerical scheme solution is convergent if it comes

closer and closer to the analytical solution of the realcloser and closer to the analytical solution of the realODE/PDE when the time step decreases;

LaxTheorem:2conditions need it for convergence– Consistency; and– Stability

1/16/2012 I.Popescu: Numerical Methods 59

Properties of Numerical Methods

ConsistencyA scheme is consistent if it gives a correct

i ti f th ODE/PDE thapproximation of the ODE/PDE as thetime/space step is decreasedverified using Taylor Series expansion

1/16/2012 I.Popescu: Numerical Methods 60

I.Popescu:Numerical methods 1/16/2012

31

Properties of Numerical Methods

StabilityA scheme is stable if any initially finite

t b ti i b d d tiperturbation remains bounded as time grows

Stability verificationMatrix methodFourier methodDomains of dependence

1/16/2012 I.Popescu: Numerical Methods 61

Domains of dependence

Notations

Un = U (tn)Uj = U (xj)Uj

n = U (tn, xj)t

n + 1

Δ t

Δ x

1/16/2012 I.Popescu: Numerical Methods 62

x

n

j -1 j j+ 1

I.Popescu:Numerical methods 1/16/2012

32

4-Finite difference methods for ODE

The general Initial Value Problem1

Given

),( tufdu=

and initial condition given

If you know values at time n, find the

),(fdt

1/16/2012 I.Popescu: Numerical Methods 64

values at time n+1

I.Popescu:Numerical methods 1/16/2012

33

The general IVP2

All initial value problems are solved by Integrating forward in tThere are two main types of integration yp gprocedure

– One-step methods– Multi-step methods

u u

1/16/2012 I.Popescu: Numerical Methods 65

t0 ti ti+1

t tt0

General classification

Numerical scheme– a particular discretization of a differential equation

Schemes:Schemes: – Explicit (Euler method) - the value of the variable at

time level n+1 can be computed directly (or explicitly) from the value at time level n.

– Implicit (Improved Euler) - involves values of the variable at time level n+1, there is an implicit

1/16/2012 I.Popescu: Numerical Methods 66

relationship between the derivative and the variable which has to be computed.

– Mixed (Crank-Nicholson ) - schemes in which the explicit and implicit schemes are combined.

I.Popescu:Numerical methods 1/16/2012

34

Explicit schemes/Euler/One Step Method

u

Forward difference approximation

= ),( tufdtdu

u

Δt∑−

=

+

Δ+=

⋅Δ+≈

1

0

1

),(

),(

N

n

nninitialfinal

nnnn

tuftuu

tuftuu

dt

1/16/2012 I.Popescu: Numerical Methods 67

This is the same as saying:new value = old value + (slope) x (step size)t n t n+1

These schemes are also called one step methods or “marching in time”, because new value is calculated as one step forward from the old value.

Implicit schemesBackward difference approximation

U ( , )dU f U t=U

Δt( )1 1 1,n n n nU f U t t U+ + +− Δ =

( , )f U tdt

Iteration need it, ussualy

1/16/2012 I.Popescu: Numerical Methods 68

t n t n+1

I.Popescu:Numerical methods 1/16/2012

35

Mixed schemesAveraging implicit and explicit schemes

U

Use this “average” slopeto predict Un+1

1/16/2012 I.Popescu: Numerical Methods 69

( ) ( )1 11

, ,

2

n n nnn n

f U t f U tU U t

+ ++

+= + Δ

{t n t n+1

Numerical schemes for ODE - Example

Ground water reservoir

Q

h

dh hdt

α= −

1/16/2012 I.Popescu: Numerical Methods 70

toh h e α−=

-exact solution

I.Popescu:Numerical methods 1/16/2012

36

Multi step methods: Midpoint MethodsA better estimate of the function U would come from evaluating the derivative at the midpoint of the Δt interval.

U

Δuu n+1/2=?

The problem: we know t at the midpoint but we don’t know the u(t) at the midpoint (yet). The solution is to use Euler’s method to estimate Δu and then re-estimate Δu using the derivatives evaluated halfway t n t n+1/2 t n+1

Δt

u

t

1/16/2012 I.Popescu: Numerical Methods 71

along the line segment encompassing the original Δu.This method is called the second order Runge Kutta method, or the midpoint method.

Mixed schemes - Midpoint Methods

tkakauu nn Δ++=+ )( 22111

Where for two point schemes

U(analytical)

U(Midpoint)

U(t)

),(1nn tufk =

),( 11112 tpttkqufk nn ΔΔ= ++

;21

211

,aa

tscoefficienweightedaa−=−

Where for two point schemesU(Euler)

tn ttn+1

1/16/2012 I.Popescu: Numerical Methods 72

21

12 =pa

21

112 =qa

;21 1 aa

I.Popescu:Numerical methods 1/16/2012

37

ExerciseApply the Euler and Mid-point methods to the following problem (with known solutions!):

fdu )(

utufutufttufttuf

fortuf

dtdu

−====

=

),(.4),(.3),(.2),(.1

),(

2

1/16/2012 I.Popescu: Numerical Methods 73

In each case, compare the errors in the two schemes after taking the same number (10, say) of identical timeteps (start with ). Is the mid-point method noticeably superior to Euler?

Mixed schemes - Multi Step Methods

The midpoint method can be extended by considering other intermediate estimates. The most frequently used variation is the fourth-order q yRunge-Kutta method which considers one estimate at the initial point, two estimates at the midpoint, and one estimate at a trial endpoint.This is popular for two reasons

It is easy to program

1/16/2012 I.Popescu: Numerical Methods 74

It is stable

HoweverIt requires more computing time

I.Popescu:Numerical methods 1/16/2012

38

Runge-Kutta Method - 44thth--orderorder

f2uu

f1

f3

f4

f

u

1/16/2012 I.Popescu: Numerical Methods 75

ti ti + h/2 ti + h

( )4321 2261 fffff +++=

t

Rungge Kutta - 4 point scheme

tkakakakauu nn Δ++++=+ )( 443322111

WhereWhere

)()2/,2/()2/,2/(

),(

),(),(

23

12

1

kfktttkufktttkufk

tufk

tuatderivativethetuf

ΔΔΔ+Δ+=Δ+Δ+=

=

−U(analytical)

U(t)

2

3

4

1/16/2012 I.Popescu: Numerical Methods 76

),( 34 tttkufk Δ+Δ+=tn

ttn+1

12

I.Popescu:Numerical methods 1/16/2012

39

Runge-Kutta 4th order - solution

(tn + k1Δt/2, un+ k1Δt/2)

Δt/2

slope = k1

t

u

)(

),(),(

tfk

tuatderivativethetuf −(tn ,un )

(k1Δt/2)

1/16/2012 I.Popescu: Numerical Methods 77

),()2/,2/()2/,2/(

),(

34

23

12

1

tttkufktttkufktttkufk

tufk

Δ+Δ+=Δ+Δ+=Δ+Δ+=

=

point to

Runge-Kutta versus Euler-example

2007.06 uudtdu

−=

Solve the equation below using both Euler and Runge Kutta methods

point to estimate

Problem: estimate the slope to

calculate Δu

Δuu

1/16/2012 I.Popescu: Numerical Methods 78

Δ t = 0.5

I.Popescu:Numerical methods 1/16/2012

40

u0 = 10 analytical u

Euler solution

u

k1 = du/dt at u0

k1 = 6*10-.007*(10)2

Δu = k1*Δt

uest= u0 + Δu

Δu

estimated u

1/16/2012 I.Popescu: Numerical Methods 79

Δ t = 0.5

Step 1: Evaluate slope at current value of state variable.

u0 = 10

k1 = du/dt at u0

k1 = 6*10-.007*(10)2

k1 = 59.3k1=slope 1

u

1/16/2012 I.Popescu: Numerical Methods 80

I.Popescu:Numerical methods 1/16/2012

41

Step 2: Calculate u1at t +Δt/2 using k1.

Evaluate slope at u1.

u1 = u0 + k1* Δt /2

u = 24 82u1 = 24.82

k2 = du/dt at u1

k2 = 6*24.8-.007*(24.8)2

k2 = 144.63k2=slope 2

u1

u

1/16/2012 I.Popescu: Numerical Methods 81

Δ t = 0.5/2

1

Step 3: Calculate u2 at t +Δt/2 using k2.

Evaluate slope at u2.

u2 = u0 + k2* Δt /2

u2 = 46.2 k3 = slope 3

k3 = du/dt at u2

k3 = 6*46.2-.007*(46.2)2

k3 = 263.0

u2

u

1/16/2012 I.Popescu: Numerical Methods 82

Δ t = 0.5/2

I.Popescu:Numerical methods 1/16/2012

42

Step 4: Calculate u3 at t +Δt using k3.

Evaluate slope at u3.

u3 = u0 + k3* Δt

u3 =141.0 k4 = slope 4u3

k4 = du/dt at u2

k4 = 6*141.0-.007*(141.0)2

k4 = 706.9

u2

u

1/16/2012 I.Popescu: Numerical Methods 83

Δ t = 0.5

Step 5: Calculate weighted slope.

Use weighted slope to estimate u at t +Δt

1 2 3 41 ( 2 2 )6

k k k k+ + +weighted slope =

)22(14321

1 kkkktuu nn +++Δ+=+

true value

estimated valueweighted slope

)22(6 4321 kkkktuu +++Δ+

u

1/16/2012 I.Popescu: Numerical Methods 84

Δ t = 0.5

I.Popescu:Numerical methods 1/16/2012

43

4th order Runge-Kutta offers substantial improvement over Eulers.Both techniques provide estimates, not “true” values.The accuracy of the estimate depends on the size of the step used in the algorithm.

Conclusions

Runge-Kutta

Analytical

Euleru

1/16/2012 I.Popescu: Numerical Methods 85

Numerical schemes for ODE - Example

Ground water reservoir

Q

h

dh hdt

α= −

1/16/2012 I.Popescu: Numerical Methods 86

toh h e α−=

-exact solution

I.Popescu:Numerical methods 1/16/2012

44

Numerical schemes for ODE - ExampleSolution methods :1. Explicit(Euler)

The right hand side is determined at current time (hn)

1 hh nn+

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

nh1+nh

)(th

)1(

1

1

thh

ht

hh

nn

nnn

Δ−=

−=Δ−

+

+

α

α

1/16/2012 I.Popescu: Numerical Methods 87

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50 t

)1(1

th

hn

n

Δ−=+

α

The right hand side is determined at future time (hn+1)

Numerical schemes for ODE - ExampleSolution methods :2. Implicit

11

++

−=Δ− n

nn

ht

hh α

11un+0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

nh1+nh

)(th

1/16/2012 I.Popescu: Numerical Methods 88

)1(

1tu

un Δ+=

α0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

0 t

I.Popescu:Numerical methods 1/16/2012

45

Numerical schemes for ODE - Example

toh h e α−=

h

dh hdt

α= −1 (1 )n nh t hα+ = − Δ

Q

1/16/2012 I.Popescu: Numerical Methods 89

1

1

nn hh

tα+ =

+ Δ

The BIG question

≈≠=1/16/2012 I.Popescu: Numerical Methods 90

I.Popescu:Numerical methods 1/16/2012

46

The BIG question

If equations are approximation then itshould:

behave in the same way as the function to which it• behave in the same way as the function to which itis an approximation, and

• become a better approximation as the time-step isreduced.

1/16/2012 I.Popescu: Numerical Methods 91

0.9

1Comparison of different solution methods

exactexplicit

Explicit vs Implicit

1nh +

)exp()exp

)1(exp 1

t-αtαn(

t)n(-αh

hn

n

Δ=Δ−Δ+

=+

Exact solution

0.3

0.4

0.5

0.6

0.7

0.8

pimplicit nh

1/16/2012 I.Popescu: Numerical Methods 92

0 0.5 1 1.5 2 2.5 30

0.1

0.2

Exact solution lies between explicit

and implicit solutions

tΔα

I.Popescu:Numerical methods 1/16/2012

47

0.9

1Comparison of different solution methods

exact

1

n

n

hh +

Three different solution methods -comparison

0.3

0.4

0.5

0.6

0.7

0.8

exactexplicitimplicitcombined (Crank-Nicolson)

Crank-Nicolson is closest to

exact solution

1/16/2012 I.Popescu: Numerical Methods 93

0 0.5 1 1.5 2 2.5 30

0.1

0.2

tΔα

Stability, Consistency, Convergence

Lax Theorem:

Consistency + stability ⇔ convergence

This is why you can trust your models !This is why you can trust your models !

Consistency : The scheme approximates the ODE more correctlyas the discretisation is refined.

Stability: Any initial perturbation remains bounded.Convergence: The numerical solution becomes closer to the real

one as the discretisation is refined

1/16/2012 I.Popescu: Numerical Methods 94

I.Popescu:Numerical methods 1/16/2012

48

The BIG question

If equations are approximation then itshould:

behave in the same way as the function to which it• behave in the same way as the function to which itis an approximation, and

• become a better approximation as the time-step isreduced.

1/16/2012 I.Popescu: Numerical Methods 95

Stability, Consistency, Convergence

Convergence - Explicit method

tn ehh α−→ 0ehh →

ththhthh

)1()1()1(

2012

01

Δ−=Δ−=Δ−=

ααα

K

1/16/2012 I.Popescu: Numerical Methods 96

non thh )1( Δ−= α

on

n hnth ⎥⎦

⎤⎢⎣

⎡⎟⎠⎞

⎜⎝⎛−+=

α1

I.Popescu:Numerical methods 1/16/2012

49

Stability, Consistency, Convergence

Convergence - Explicit method (cont)

⎥⎤

⎢⎡

⎟⎞

⎜⎛

⎟⎞

⎜⎛ ⋅⎟

⎞⎜⎛⎟⎞

⎜⎛ ⋅ 2

210 ntnt αα

⎥⎥⎦

⎢⎢⎣

⎡+⎟⎟⎠

⎞⎜⎜⎝

⎛⎟⎠⎞

⎜⎝⎛−+⎟⎟

⎞⎜⎜⎝

⎛⎟⎠⎞

⎜⎝⎛−+= −− K

11

111 210 n

ntn

nthh nnnn αα

where

!)!(!

rrnn

rn

−=⎟

⎠⎞⎜

⎝⎛

r⎞⎛

1/16/2012 I.Popescu: Numerical Methods 97

!)!( rrn⎠⎝

rnrnnnrrn

n→−−=

−))...(1(

!)!(! !r

nrn r

=⎟⎠⎞⎜

⎝⎛

Stability, Consistency, Convergence Convergence - Explicit method (cont)

nn ntnthh αα⎥⎤

⎢⎡

+⎟⎞

⎜⎛ ⋅

+⎟⎞

⎜⎛ ⋅

+→22

0 1

Back to the equation

tn ehtth

nnhh

ααα −→⎥⎥⎦

⎢⎢⎣

⎡+⎟

⎠⎞

⎜⎝⎛ ⋅−+⎟

⎠⎞

⎜⎝⎛ ⋅−+→

⎥⎥⎦⎢

⎢⎣

+⎟⎠

⎜⎝−+⎟

⎠⎜⎝−+→

02

0

!21

111

!2!11

K

K

1/16/2012 I.Popescu: Numerical Methods 98

⎥⎦⎢⎣ ⎠⎝⎠⎝ !211

Taylor series for te ⋅−α

I.Popescu:Numerical methods 1/16/2012

50

Stability, Consistency, Convergence

1

1nh +

Stability condition

111

≤≤+nh

Stability - Explicit schemes

1nh≤

αα 20

111 =Δ⇒⎨⎧ ≤Δ−

⇒≤Δ−≤− tt

t

11 ≤≤− nh

1/16/2012 I.Popescu: Numerical Methods 99

ααα

2111 maxΔ⇒

⎩⎨ ≤Δ

⇒≤Δ≤ tt

t

Stability, Consistency, Convergence

Example

1

2210

max

=

==Δ⇒=

hand

tTakeα

α

) d

dt

dt=0.125

a)0<dt<1

1/16/2012 I.Popescu: Numerical Methods 100

dt=0.5

dt=0.25

dt 0 5

I.Popescu:Numerical methods 1/16/2012

51

b)1<=dt<2

Stability, Consistency, Convergence

dt=1.25

d 1

1/16/2012 I.Popescu: Numerical Methods 101

=>Stable But Oscillatory Approximations

dt=1.5dt=1.5

c)2<dtStability, Consistency, Convergence

dt=4.5

dt=3

1/16/2012 I.Popescu: Numerical Methods 102

=>Unstable (i.e. Bad) Approximations

dt=2.5

I.Popescu:Numerical methods 1/16/2012

52

Stability, Consistency, Convergence

Stability - Explicit schemes

H

0 1 1 1 or 0 t tαα

≤ − Δ ≤ ≤ Δ ≤

Δt

1/16/2012 I.Popescu: Numerical Methods 103

t n t n+1

Stability, Consistency, Convergence

Stability - Explicit schemes

H

ΔtΔt0 1/α 2/α

oscillatory unstablestable

1/16/2012 I.Popescu: Numerical Methods 104

t n t n+1

I.Popescu:Numerical methods 1/16/2012

53

Stability, Consistency, Convergence

Consistency - explicit schemes

( )2

2dh t d hh O tΔ+ Δ( )2

22h O t

dt dtα+ = − − Δ

Truncation error - first order accurate

1/16/2012 I.Popescu: Numerical Methods 105

Stability, Consistency, Convergence

Implicit schemes– convergence - FOR YOU TO TEST

stability unconditionally stable– stability - unconditionally stable

Mixed schemes– unconditionally stable

1/16/2012 I.Popescu: Numerical Methods 106

I.Popescu:Numerical methods 1/16/2012

54

Amplification factor

U n+1/U n is called the numerical amplification factor

1/16/2012 I.Popescu: Numerical Methods 107

Amplitude portrait

Method EulerImproved Euler

Region of instability

1

0

1

0 1 2 3 4AN

Region of instability

1/16/2012 I.Popescu: Numerical Methods 108

-2

-1

a Δt

Region of instability

I.Popescu:Numerical methods 1/16/2012

55

4-Finite difference methods for ODE

Quick summary

What you should remember

ODEs can be solved numerically by replacing the d/dt operator by a difference of the considered function at two time levels.function at two time levels.When the derivative is estimated using the values of the function at time step level n, the scheme is said to be explicit. When the derivative is estimated using the values at time n+1, the scheme is said to be implicit.

1/16/2012 I.Popescu: Numerical Methods 110

pThe discretized ODE is consistent to the real one if it comes closer to the real ODE as dt (or dx) comes closer to zero.

I.Popescu:Numerical methods 1/16/2012

56

What you should rememberThe difference between the discretised ODE and the real one is called the truncation error. It is obtained by substituting Taylor series expansions into the discretisation.The solution is said to be stable if initial perturbations in the numerical solution remain bounded.

1/16/2012 I.Popescu: Numerical Methods 111

The stability of the solution is investigated by calculating the value of the amplification factor. If |AN| is smaller than unity, the solution is stable.

What you should remember

In general, explicit schemes are not stable for all values of ∆t (or ∆x). I li i h l blImplicit schemes are always stable.The numerical solution is said to converge to the real one if it comes closer to it as ∆t (or ∆x) comes closer to 0.Consistency and stability of a scheme are

1/16/2012 I.Popescu: Numerical Methods 112

Consistency and stability of a scheme are necessary and sufficient conditions to the convergence of the solution