Errors and Uncertainties in Computations Errors and Uncertainties ...

Errors and Uncertainties in Computations Errors and Uncertainties ... Errors and Uncertainties in Computations Errors and Uncertainties ...

physics.orst.edu
from physics.orst.edu More from this publisher
01.12.2014 Views

Errors and Uncertainties in Computations Rubin H Landau With Sally Haerer and Scott Clark Computational Physics for Undergraduates BS Degree Program: Oregon State University “Engaging People in Cyber Infrastructure” Support by EPICS/NSF & OSU Introductory Computational Science 1 © Rubin Landau, EPIC/OSU 2005

<strong>Errors</strong> <strong>and</strong> <strong>Uncerta<strong>in</strong>ties</strong> <strong>in</strong><br />

<strong>Computations</strong><br />

Rub<strong>in</strong> H L<strong>and</strong>au<br />

With<br />

Sally Haerer <strong>and</strong> Scott Clark<br />

Computational Physics for Undergraduates<br />

BS Degree Program: Oregon State University<br />

“Engag<strong>in</strong>g People <strong>in</strong> Cyber Infrastructure”<br />

Support by EPICS/NSF & OSU<br />

Introductory Computational Science 1<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Problem: Life + <strong>Errors</strong> (<strong>Uncerta<strong>in</strong>ties</strong>)<br />

Always part of computation<br />

F<strong>in</strong>ite precision uncerta<strong>in</strong>ties = “errors”<br />

Don't be afraid; Don't play with garbage<br />

<strong>Errors</strong> accumulate with steps U i<br />

start → U1 → U2 → ...→ Un<br />

→ end<br />

(1)<br />

• p = probability U i correct<br />

• P = p n = probability n steps correct<br />

• n= 1000, p = 0.9993 P = 1/2<br />

• (whoops!)<br />

Introductory Computational Science 2<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Theory: Types of <strong>Errors</strong> (4 plagues)<br />

1. Blunders: typos, wrong program, wrong data, …<br />

2. R<strong>and</strong>om errors: electronic fluctuations, cosmic rays, …<br />

– rare, but if 10 8 steps?<br />

– can't control<br />

– repeat calculation<br />

3. Approximation errors, algorithm, approx math:<br />

e −x =<br />

∞X<br />

n=0<br />

(−x) n<br />

n!<br />

'<br />

NX<br />

n=0<br />

(−x) n<br />

n!<br />

= e −x + E(x, N)<br />

(2)<br />

• decreases as N <strong>in</strong>creases<br />

• vanishes as N <br />

4. Round-Off <strong>Errors</strong> (cont)<br />

Introductory Computational Science 3<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


<strong>Errors</strong> (cont)--<br />

Roundoff errors:<br />

Because: numbers via f<strong>in</strong>ite # bits<br />

≈ uncerta<strong>in</strong>ties <strong>in</strong> measurement<br />

Some numbers represent exact (2 n )<br />

Accumulates with steps unstable<br />

garbage: RO ≈ result:<br />

2 ¡ 1<br />

3<br />

¢<br />

−<br />

2<br />

3<br />

=0.66666 − 0.66667 = −0.00001 6=0<br />

(3)<br />

Significant figures<br />

• a = 11223344556677889900<br />

= 1.12233445566778899 * 10 19<br />

(4)<br />

• exponent: stored separately<br />

small full precision<br />

Most significant part: 1.12233<br />

Least significant part: 44556677<br />

• error prone<br />

Introductory Computational Science 4<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Disaster Model: Subtractive Cancellation<br />

If you subtract two large numbers <strong>and</strong> end up with a small<br />

result, there will be less significance <strong>in</strong> the small result<br />

a = b − c ⇒ a c = b c − c c<br />

(5)<br />

a c = b(1 + ² b ) − c(1 + ² c )<br />

(6)<br />

e −x = 1 −x + x2<br />

2 − x3<br />

6 +···= 1 −100 +5000 +··· (9)<br />

⇒<br />

a c<br />

a = 1 + ² b<br />

b<br />

a − c a ² c<br />

(7)<br />

' 1 − b a (² b − ² c ) → ∞<br />

(8)<br />

HW: Cancellation <strong>in</strong> power series, x = 100:<br />

Introductory Computational Science 5<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Model for Multiplicative <strong>Errors</strong><br />

• Propagation of error <strong>in</strong>multiplication<br />

a = b × c ⇒ a c = b c × c c ,<br />

⇒<br />

a c<br />

a =(1 + ² b)(1 + ² c ) ' 1 + ² b + ² c<br />

(10)<br />

(11)<br />

• How add errors? |² b | + |² c |, |² b | − |² c |?<br />

• Algorithm Model: N steps r<strong>and</strong>om walk (Chap.5)<br />

² ro ≈ √ N² m<br />

N<br />

• Each step ' mach<strong>in</strong>e precision<br />

If non r<strong>and</strong>om: ² ' N² m , N!² m<br />

E.G.: several hour calculation, 10 10 Flops<br />

⇒ ² ' 10 7 ² m<br />

S<strong>in</strong>gles: ² m ' 10 −7<br />

⇒ ² ' 1 (whoops!)<br />

Introductory Computational Science 6<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005<br />

R<br />

1<br />

4<br />

2 3


Experiment: Determ<strong>in</strong>e <strong>Errors</strong><br />

1. Basic algorithm questions:<br />

a) does it converge?<br />

b) if not, quit<br />

c) how precise are converged results?<br />

2. Converged correct<br />

3. How $$ (time consum<strong>in</strong>g) ?<br />

Introductory Computational Science 7<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Experiment: Expected Behavior<br />

• ² apprx<br />

= approximation error = A ns exact -A<br />

ns algorithm<br />

• Algorithmic errordecreases rapidly<br />

² aprx ' α → 0, (N → ∞),<br />

N<br />

β<br />

(12)<br />

• Round oFF error <strong>in</strong>creases slowly<br />

² RO ' √ N ² m<br />

(13)<br />

• Want m<strong>in</strong>imum of sum<br />

² tot = ² approx + ² RO ,<br />

α<br />

'<br />

N + √ N² β<br />

m<br />

(14)<br />

• Want N small ⇒ faster, accurate<br />

Introductory Computational Science 8<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Experimental Approach<br />

Determ<strong>in</strong>e & via applied math<br />

(see text)<br />

10 -1<br />

<br />

<br />

M<strong>in</strong> via comparison to known<br />

answer<br />

• test (similar) problem<br />

If N looks good, assume 2N exact:<br />

A(N) ≈ A + α<br />

N β + RO<br />

² tot = A(N) − A(2N) ≈ α<br />

N β<br />

Plot log 10 () vs log 10 (N)<br />

• ord<strong>in</strong>ate = # decimal places precision<br />

• slope = <br />

|error|<br />

10 -3<br />

10 -5<br />

10 -7<br />

10 -9<br />

Gaussian<br />

Simpson<br />

trapezoid<br />

10 100<br />

N<br />

Look & underst<strong>and</strong>!<br />

converge diverge<br />

Introductory Computational Science 9<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Time for Exercises<br />

<strong>in</strong> Lab<br />

Introductory Computational Science 10<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Exercise: Subtractive Cancellation<br />

1. Equivalent solutions to<br />

ax 2 + bx + c =0<br />

x 1,2 = −b ± √ b 2 − 4ac<br />

, x 0 1,2 =<br />

2a<br />

−2c<br />

b ± √ b 2 − 4ac .<br />

Subtractive cancellation if 4ac


Subtractive Exercise (cont)<br />

2. Three equivalent (math) sums:<br />

S (1)<br />

N<br />

=<br />

S (2)<br />

N<br />

=<br />

S (3)<br />

N<br />

=<br />

2NX<br />

n=1<br />

NX<br />

n=1<br />

NX<br />

n=1<br />

(−1) n<br />

2n<br />

2n +1 −<br />

n<br />

n +1 . (1)<br />

1<br />

2n(2n +1)<br />

N X<br />

n=1<br />

2n − 1<br />

2n<br />

(even & odd), (2)<br />

(partial sums). (3)<br />

Write program to calculate S(1), S(2), S(3) 1 N 100<br />

Assume S(3) is exact<br />

Log-log plot relative error: log 10 | S(1) N<br />

−S(3) N<br />

| vs log 10<br />

(N)<br />

Get expected straight l<strong>in</strong>e?<br />

S (3)<br />

N<br />

Introductory Computational Science Lab 2<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005


Exercise: error <strong>in</strong> e -x<br />

(cont)<br />

3. Mathematical def<strong>in</strong>ition versus algorithm<br />

e −x =1− x + x2<br />

2! − x3<br />

3! + ··· ' N X<br />

n=0<br />

(−x) n<br />

n!<br />

(1)<br />

a. Exam<strong>in</strong>e cancellation of terms for x ≈ 10<br />

b. Convergence terms decrease<br />

new = (n+1)/x old n+1 ≈ x<br />

c. Demo: near-perfect cancellation here?<br />

d. exp(-x) = 1/exp(x) better, yet still RO<br />

e. Plot error vs N for 1 x 100 (see easyPtPlot.java.)<br />

Introductory Computational Science Lab 5<br />

© Rub<strong>in</strong> L<strong>and</strong>au, EPIC/OSU 2005

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!