53
Komputasi Numerik: Integrasi dan Differensiasi Numerik Agus Naba Physics Dept., FMIPA-UB

Komputasi Numerik : Integrasi dan Differensiasi Numerik

  • Upload
    ossie

  • View
    179

  • Download
    8

Embed Size (px)

DESCRIPTION

Komputasi Numerik : Integrasi dan Differensiasi Numerik. Agus Naba Physics Dept., FMIPA-UB. Ordinary Differential Equation. Ordinary Differential Equation (ODE) is a differential equation in which all dependent variables are functions of a single independent variable. ODE’s Problem. - PowerPoint PPT Presentation

Citation preview

Page 1: Komputasi Numerik : Integrasi dan Differensiasi Numerik

Komputasi Numerik:Integrasi dan

Differensiasi NumerikAgus Naba

Physics Dept., FMIPA-UB

Page 2: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 2

Ordinary Differential Equation

Ordinary Differential Equation (ODE) is a differential equation in which all dependent variables are functions

of a single independent variable.

Page 3: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 3

ODE’s Problem

00 ')0(';)0(

)),(,()(')(

yyyy

tytftydt

tdy

First-order Ordinary Differential Equation (ODE):

?)t(y

Page 4: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 4

Euler’s Method

ttyty

dtdy

ntnttt

nn

t

n

n

)()(

,..2,1,0 ,

1

0

tytfytdtdyyy nnn

tnn

n

),(1

)tt(yy);t(yy nnnn 1

Page 5: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 5

tn tn+1

tdtdyyy

ntnn 1

It enables us to calculate all of yn = y (tn), given y(t0).

y(t)

tdtdy

nt

yn

ntdtdySlope:

Page 6: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 6

Numerical Errors

Truncation Errors, depending on numerical methods

Round-off Errors, depending on capability of computer in storing floating-point number

Page 7: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 7

Truncation ErrorsThe curve y(t) is not generally a straight-line between the neighbouring grid-times tn and tn+1 as assumed.

According to Taylor Series:

...tdt

ydtdtdyyy

nn ttnn

2

2

2

2

1

O(t2)Truncation Error

Each step incurs truncation error ~ t2 Net truncation errors of Euler’s Method ~ t

Page 8: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 8

Round-off ErrorsFor every type of computer, there is a charasteristic number, , which defined as the smallest number which when added to a number of order unity gives rise to a new number. For example: = 2.2 x 10-16 (for double precision number in IBM-PC ) = 1.19 x 10-7 (for single precision number in IBM-PC )

The net round-off errors of Euler’s Method /t.

Page 9: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 9

Net Numerical Errors of Euler’s Method

tt

~

At large t, the error is dominated by the truncation errors, whereas the round-off errors dominates at small t .

Minimum net numerical errors are achieved when

2121 // ~;~t

Page 10: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 10

t

t~1/2

~1/2

Page 11: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 11

1)0(0)()( y,,ty

dttdy

t

y(0)=1

tety )(

Numerical Instalibilities

Page 12: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 12

Solusi Numerik,...,,n,tnttn 210 0

nn y)t(y 11

/t 2 /t 2

Numerical Instabilitiesy(0) y(0)

t t

Page 13: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 13

Defect of Euler’s Method

Not generally used in scientific computing: Truncation errors is far larger than other,

more advanced, methods. Too prone to numerical instabilities

Page 14: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 14

Main reason of large truncation errors:Euler’s method only evaluates derivative at the beginning of the interval [tn,tn+1], i.e., at tn.( Very asymetric with respect to the beginning and the end of the interval)

Page 15: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 15

tn tn+1tn+ h/2

h

y(t)

Runge-Kutta (RK) Methods

221

2ky,htff nn

nn y,tff 1

11 hfk

k1 k2

ynf1

f2

y(t)

22 hfk

k1 /2

11 kyy nn

21 kyy nn

The 2nd order RK Method

Euler’s Method

Page 16: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 16

Modified Euler’s Method

12 ky,htff nn

nn y,tff 1

11 hfk

tn tn + hh

y(t)

f1yn

k1

f2

k2

(k1+k2)/2

22 hfk

yn+1 = yn+ (k1+k2)/2

Modified Euler’s Method

Page 17: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 17

tn tn + h/2 tn + h

f1

f2

f3

f4

The 4th order RK method

Page 18: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 18

344

233

122

11

43211

22

22

2261

ky,hthfhfk

ky,hthfhfk

ky,hthfhfk

y,thfhfk

kkkkyy

nn

nn

nn

nn

nn

The 4th order RK method

Page 19: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 19

Method

Equations

Euler (Error of the order h2)

yxfhkky

,1

1

Modified Euler (Error of the order h3)

12

1

21

,,

21

kyhxfhkyxfhk

kky

Heun (Error of the order h4)

23

12

1

31

32

32

31

31

341

ky,hxfhk

ky,hxfhk

y,xfhk

kky

4th order Runge Kutta (Error of the order h5)

34

23

12

1

4321

,21,

21

21,

21

,

2261

kyhxfhk

kyhxfhk

kyhxfhk

yxfhk

kkkky

Page 20: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 20

Net Numerical Errors of RK Methods

Method-RK oforder , :nhh

~ n

)n/(n)n/( ~~h 111

:at errors Minimum

Page 21: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 21

order hmin min

1 1.5 x 10-8 1.5 x 10-8

2 6.1 x 10-6 3.7 x 10-11

3 1.2 x 10-4 1.8 x 10-12

4 7.4 x 10-4 3.0 x 10-13

5 2.4 x 10-3 9.0 x 10-14

RK Methods Performanceon IBM-PC for double precision

hmin increases and min decreases as order gets larger, but needs more computational effort.

Page 22: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 22

Example

tksinty

k,t?ty

kdtdyy

tkydt

yd

t

1101

000

2

2

?txtkxdt

tyddt

tdx

?)t(xtxdt

)t(dydt

tdxdt

)t(dytx

tytx

212

22

121

2

1

Page 23: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 23

Global integration errors associated with Euler's method (solid curve) and the 4th order Runge-Kutta method (dotted curve) plotted against the

step-length h. Single precision calculation.

err = yanalitic-ynumeric

Page 24: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 24

Global integration errors associated with Euler's method (solid curve) and the 4th order Runge-Kutta method (dotted curve) plotted

against the step-length h. Double precision calculation.

Page 25: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 25

Adaptive Integration Method

xktdtdx

tdtxd 22

2

41

Consider the following ODE:

Analitic solution:

2tksinx

Page 26: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 26

Global integration error associated with a xed step-length (h = 0:01), 4th order RK method, plotted against the independent variable, t, for a system of o.d.e.s in which the

variation scale-length decreases rapidly with increasing t. Double precision calculation.

err = xanalitic-xnumeric

Page 27: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 27

It can be seen that, although the error starts off small, it rises rapidly as the variation scale-length of the solution decreases (i.e., as t increases), and quickly becomes

unacceptably large. Of course, we could reduce the error by simply reducing the step-length, h. However, this is a very

inefficient solution. The step-length only needs to be reduced at large t. There is no need to reduce it, at all, at

small t.

Solution: h should be large at small t but needs to be reduced at large t

Page 28: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 28

51

0

oldnew hh

The step-length h should be increased if the truncation error per step is too small, and vice versa, in such a manner that the

error per step remains relatively constant at 0.

stepper error n truncatiodesired the:0

Page 29: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 29

Global integration errors associated with fixed step-length (h = 0.01), 4th order RK method (solid curve) and a corresponding adaptive method (0 = 10-8) (dotted curve), plotted against the independent variable, t, for a system of o.d.e.s in which the variation scale-length decreases rapidly with increasing t. Double precision calculation.

Page 30: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 30

Differentiation

An object is moving through space, its position as a function of time x(t) is recorded in a table.

Problem: Determine the object’s velocity v(t)=dx/dt and acceleration a(t)=d2x/dt2

Page 31: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 31

Method: Numeric

h

xfhxflimxfdx

xdfh

0

1

Even a computer runs into errors with such a method because of its subtraction operations: the numerator tends to fluctuate between 0 and the machine precision as the denominator approaches zero.

Page 32: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 32

Method: Forward Difference (FD)

c denotes a computed expression.

h

xfhxfxfc

1

Page 33: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 33

x x+h

f(x)

FD: using two points to represent the 1st derivative function by a straight line in the interval from x to x+h

xfc1 xf 1

h

Page 34: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 34

...xfhxfhxhfxfhxf 33

22

1

62

...xfhxfhxf

,h

xfhxffc

32

21

1

62Error

Page 35: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 35

Example of FD

2bxaxf bxxf 21

Page 36: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 36

FD solution

This clearly becomes a good approximation only for small h, i.e., h << 2x

bhbx

xfhxfhxf

hxfhxfxfc

2

...62

,

32

21

1

Page 37: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 37

Method: Central Difference (CD)

h,xfDh

/hxf/hxfxf cc

221

Page 38: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 38

x-h/2 x x+h/2

f(x)

CD: using two points to represent the function by a straight line in the interval from x-h/2 to x+h/2

21 /hxfc

xf 1

h

Page 39: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 39

...xfhxfhxfhxfhxf

...xfhxfhxfhxfhxf

33

22

1

33

22

1

48822

48822

...xfhxf

h/hxf/hxfxfc

32

1

1

24

22

Error

Page 40: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 40

Example of CD

2bxaxf bxxf 21

Page 41: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 41

CD solution

CD Method gives the exact answer regardless of the size of h !

bx

...xfhxf

h/hxf/hxfxfc

224

22

32

1

1

Page 42: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 42

Method:Extrapolated Difference (ED)

The error in FD ~ hThe error in CD ~ h2

The error in ED ~ h4

Page 43: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 43

...xfhxf

/h/hxf/hxf/h,xfDc

32

1

96

2442

Page 44: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 44

x-h/2 x x+h/2

f(x)

h,xfDc xf 1

x-h/4 x+h/4

2/h,xfDc

h

h/2

Page 45: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 45

Extrapolated Difference

3241 h,xfD/h,xfDxf cc

c

Page 46: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 46

x-h/2 x x+h/2

f(x)

h,xfDc xf 1

x-h/4 x+h/4

2/h,xfDc

h

h/2

24 /h,xfDc

Page 47: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 47

xfhxf

h,xfD/h,xfDxf ccc

34

1

1

1641920

324

Error

Page 48: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 48

22448

311 hxfhxfhxfhxfh

fc

A Good Way of Computing for ED

It reduces the loss of precision that occurs when large and small numbers are added together, only to be subtracted from other large numbers.

Subtract the large number from each other and then add the difference to the small numbers !

Page 49: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 49

Attention !

Regardless of the algorithm, evaluating the derivative of f(x) at x requires us to know the values of f surrounding x !

HOW ?Once we have the derivative of f(x) at x, USE the integration methods, ex., RK Method, to approximate the values of f surrounding x !

Page 50: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 50

Error Analysis The approximation/truncation errors in numerical differentiation decrease with decreasing step size h while roundoff errors increase with a smaller step size. Total error is minimum if

minimum. This occurs when

rotrunc

rotrunc

Page 51: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 51

Roundoff ErrorThe limit of roundoff error is essentially machine precision:

h

hhxfhxf'f

ro

Page 52: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 52

Truncation Errors

Truncation Error of FD:

Truncation Error of CD:

24

223

2

hf

hf

cdtrunc

fdtrunc

Page 53: Komputasi Numerik : Integrasi dan Differensiasi Numerik

AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 53

Best hThe h value for which roundoff and truncation errors are equal is

Ex., for single precision 10-7 for f(x)=ex or cos(x)hfd 0.0005 and hcd 0.01

33

22

232

242242

fh

fh

hfh

hfh

cdfd

cdro

fdtrunc