12
CSIS Computer Simulation in Science Introduction to Computer Simulation Prof. Dr. Francesco Knechtli Dr. Tomasz Korzec and Dr. Roman H¨ ollwieser Department of Physics, Bergische Universitt Wuppertal, Gaussstr. 20, D-42119 Wuppertal, Germany January 18, 2018 Abstract The goal of this lecture and tutorial series is to introduce con- cepts and applications of computer simulation. These lecture notes are based on the course ”Computational Physics I” by Prof. Dr. Ulli Woland Dr. B. Bunk [1]. Further literature recommendations are [2–4]. At http://csis.uni-wuppertal.de/courses/ics17.html, you can find a sample of programs and scripts discussed in the lecture or in the tutorial. Life recording and lecture notes of the winter term 14/15 are available at http://www.particle.uni-wuppertal.de/knechtli/. [email protected], oce D.10.24 [email protected], oce D.10.03 [email protected], oce G.11.37

Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

CSISComputer Simulation in Science

Introduction to Computer Simulation

Prof. Dr. Francesco Knechtli∗

Dr. Tomasz Korzec† and Dr. Roman Hollwieser‡

Department of Physics, Bergische Universitt Wuppertal,

Gaussstr. 20, D-42119 Wuppertal, Germany

January 18, 2018

Abstract

The goal of this lecture and tutorial series is to introduce con-cepts and applications of computer simulation. These lecture notes arebased on the course ”Computational Physics I” by Prof. Dr. Ulli Wol↵and Dr. B. Bunk [1]. Further literature recommendations are [2–4].At http://csis.uni-wuppertal.de/courses/ics17.html, you canfind a sample of programs and scripts discussed in the lecture or in thetutorial. Life recording and lecture notes of the winter term 14/15 areavailable at http://www.particle.uni-wuppertal.de/knechtli/.

[email protected], o�ce D.10.24†[email protected], o�ce D.10.03‡[email protected], o�ce G.11.37

Page 2: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

Contents

1 Error, accuracy and stability 11.1 Range of numbers . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Accuracy/precision . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Numerical derivative . . . . . . . . . . . . . . . . . . . . . . . 21.4 Numerical limit . . . . . . . . . . . . . . . . . . . . . . . . . . 41.5 Recurrence relations . . . . . . . . . . . . . . . . . . . . . . . 7

2 Root finding 102.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2 Special Method . . . . . . . . . . . . . . . . . . . . . . . . . . 102.3 Bisection Method . . . . . . . . . . . . . . . . . . . . . . . . . 122.4 Newton-Raphson Method . . . . . . . . . . . . . . . . . . . . 132.5 The Secant Method . . . . . . . . . . . . . . . . . . . . . . . . 152.6 Newton-Raphson and Fractals . . . . . . . . . . . . . . . . . . 162.7 MATLAB functions . . . . . . . . . . . . . . . . . . . . . . . . 18

3 Numerical integration 203.1 Interpolation polynomials . . . . . . . . . . . . . . . . . . . . 203.2 Trapezoidal and Simpson’s rules . . . . . . . . . . . . . . . . . 223.3 Extended trapezoidal/Simpson’s rules . . . . . . . . . . . . . . 233.4 Gaussian quadrature . . . . . . . . . . . . . . . . . . . . . . . 253.5 Adaptive step size ! ODE . . . . . . . . . . . . . . . . . . . . 27

4 Fourier transformation 284.1 Discrete lattice . . . . . . . . . . . . . . . . . . . . . . . . . . 284.2 MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304.3 Example Helmholtz . . . . . . . . . . . . . . . . . . . . . . . . 314.4 Continuum limit . . . . . . . . . . . . . . . . . . . . . . . . . 324.5 Thermodynamic limit ⌘ infinite volume limit . . . . . . . . . 344.6 Fourier integral . . . . . . . . . . . . . . . . . . . . . . . . . . 344.7 Sampling theorem . . . . . . . . . . . . . . . . . . . . . . . . . 354.8 Several dimensions . . . . . . . . . . . . . . . . . . . . . . . . 374.9 Fast Fourier Transform (FFT) . . . . . . . . . . . . . . . . . . 374.10 Real functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 404.11 Fourier transformation with specified boundary conditions . . 42

i

Page 3: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

5 Initial value problems (ODEs) 445.1 A simple example from physics . . . . . . . . . . . . . . . . . 455.2 Standard Form . . . . . . . . . . . . . . . . . . . . . . . . . . 465.3 Euler method . . . . . . . . . . . . . . . . . . . . . . . . . . . 475.4 Runge-Kutta method (second order) . . . . . . . . . . . . . . 495.5 Runge-Kutta method 3rd order . . . . . . . . . . . . . . . . . 515.6 Runge-Kutta method 4th order . . . . . . . . . . . . . . . . . 515.7 Adaptive step-size control . . . . . . . . . . . . . . . . . . . . 515.8 MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535.9 A note on Kepler’s 3rd law . . . . . . . . . . . . . . . . . . . . 55

6 A note on Molecular Dynamics 596.1 Preparatory considerations . . . . . . . . . . . . . . . . . . . . 596.2 Equations of motion and the leapfrog algorithmus . . . . . . . 616.3 MATLAB implementation . . . . . . . . . . . . . . . . . . . . 636.4 Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

7 Linear systems of equations 707.1 Naive Gaussian Elimination . . . . . . . . . . . . . . . . . . . 707.2 Pivoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737.3 Iterative improvement of the solution . . . . . . . . . . . . . . 757.4 LU Decomposition . . . . . . . . . . . . . . . . . . . . . . . . 757.5 Householder reduction . . . . . . . . . . . . . . . . . . . . . . 767.6 Crout’s algorithm for LU decomposition . . . . . . . . . . . . 807.7 Note on complex matrices . . . . . . . . . . . . . . . . . . . . 83

8 Fitting of data 858.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858.2 Normal distributed measurements, �2 . . . . . . . . . . . . . . 858.3 Fits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

8.3.1 Minimal �2 . . . . . . . . . . . . . . . . . . . . . . . . 888.3.2 Linear fits . . . . . . . . . . . . . . . . . . . . . . . . . 888.3.3 Fits with a non-linear parameter . . . . . . . . . . . . 89

8.4 Practical considerations . . . . . . . . . . . . . . . . . . . . . 89

References 91

ii

Page 4: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

1 Error, accuracy and stability

Computers store numbers with a finite number of bits (binary digits: 0 or 1)

Integer representation, exact or ! floating point representation:

1.1 Range of numbers

• double precision numbers: 64 bits = 8 bytes (1 byte = 8 bits)

• single precision numbers: 32 bits = 4 bytes

number = ± M · 2E with exponent E and mantissa M =P

ibi2�i, bi = 0/1

sign exponent mantissa ) range64 bits: 1 bit 11 bits 52 bits ⇡ 10�308 . . . 10308

32 bits: 1 bit 8 bits 23 bits ⇡ 10�38 . . . 1038

Remark: range depends on the choice of physical units

1.2 Accuracy/precision

number A in a computer: A = M · 2E

what is the smallest change �A such that A+ �A 6= A?

! �A = 2�52 · A ⇡ 2⇥ 10�16 · A

fractional accuracy: relative change

�����A

A

���� � 2⇥ 10�16

there exists a number ✏m > 0, such that

1 + z = 1 for 0 z ✏m1 + z > 1 for z > ✏m

✏m = machine accuracy/precision = intrinsic representation/roundo↵ error

the numbers A and A+ ✏m · A are numerically indistinguishable

1

Page 5: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

mantissa M defines a grid of representable rational numbers with resolution✏m:

A = 2E ⇥� 0 ✏m · · · 1

| | | | | |�

a real number is approximated by the closest of these representable numbers

✏m is a lower bound for roundo↵ error...let A,B > 0, A ⇡ B, x = A � B,e.g.:

A = 0.375684 . . .⇥ 108 (16digits)

�B = 0.375641 . . .⇥ 108 (16digits)

= x = 0.000043 . . .⇥ 108

= 0.43 . . .⇥ 104 (12digits)

! significance loss...|x|A

⇡ 10�n (above, n = 4)

uncertainty (roundo↵ error):

�����A

A

���� = ✏m =

�����B

B

����

�x ⇡ |�A|+ |�B| = ✏mA+ ✏mB ⇡ ✏mA

)�����x

x

���� ⇡✏mA

10�nA= ✏m10

n

) x is less precise than A, n digits of precision are lost

Reason: cancellation between A and B (significance loss depends on theorganization of computations)

1.3 Numerical derivative

function f(x) is given, compute the derivative f 0(x) =df

dx(x)

Taylor expansion:

f(x+ h) = f(x) + f 0(x)h+1

2f 00(x)h2 +O(h3)

f(x� h) = f(x)� f 0(x)h+1

2f 00(x)h2 +O(h3)

2

Page 6: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

numerical derivative:

- asymmetric derivative: f 0(x) =f(x+ h)� f(x)

h+O(h)

- symmetric derivative: f 0(x) =f(x+ h)� f(x� h)

2h+O(h2)

h small ) f(x+ h) ⇡ f(x) ⇡ f(x� h) ) significance loss, roundo↵ error

h large ) remainder of Taylor series is large, truncation error

) optimal value of h?

numerical experiment:http://csis.uni-wuppertal.de/courses/numerical_accuracy/numdiff.m

1 % program numdif f

2 % ta b u l a t e s the numerical d e r i v a t i v e s o f s in ( x ) at x=1

3 % as a func t i on o f the s tep�s i z e

4 %

5 help numdiff6 %

7 h = 10 . ˆ ( � [ 1 : 1 6 ] ) ; % step�s i z e s

8 x = 1 ;9 dex = cos ( 1 ) ; % exak t

10 d1 = ( sin ( x+h)�sin ( x ) ) . / h ; % asym . formula

11 d2 = ( sin ( x+h)�sin (x�h ) ) . / ( h+h ) ; % sym . formula

12 y = [ h ; ( d1�dex )/ dex ; ( d2�dex )/ dex ] ;13 %

14 fpr intf ( ’ h asymm. symm. \n\n ’ )15 fpr intf ( ’ %5.0 e %10.1 e %10.1 e \n ’ , y )

- asymmetric: hopt ⇠ 10�8, relative error ⇠ 10�8

- symmetric: hopt ⇠ 10�5, relative error ⇠ 10�11

roundo↵ error: �f 0(x) = �

✓f(x+ h)� f(x)

h

◆⇠ ✏mf(x)

h

3

Page 7: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

asym. hopt sym. hopt

erro

r

h

rounoff error ~ 1/hsymmetric ~ h

2

asymmetric ~ h

optimal step-size or increment hopt:

- asymmetric:✏mh

⇠ h ) hopt ⇠ ✏1/2m ⇠ 10�8 ) error: ⇠ 10�8

- symmetric:✏mh

⇠ h2 ) hopt ⇠ ✏1/3m ⇠ 10�5 ) error: ⇠ 10�11

Remark: in these estimates we assumed f(x), f 0(x), f 00(x) ⇠ O(1)

1.4 Numerical limit

taking limits numerically is challenging, e.g., limh!0f(x+ h)� f(x)

hcan be

calculated only to �rel ⇠ 10�8

another example:

ex = limn!1

(1 +x

n)n ! approximate ex ⇡ (1 +

x

n)n for large n

ex = (ex/n)n = (1 +x

n+

1

2

x2

n2+ . . .)n | binomial expansion (a+ b)n

= (1 +x

n)n + n(1 +

x

n)n�11

2

x2

n2+ . . .)

4

Page 8: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

= (1 +x

n)n(1 + n

1

1 + x

n

1

2

x2

n2+ . . .) | large n

= (1 +x

n)n(1 +

1

2

x2

n+O(

1

n2))

! for large n, leading order correction � ⇠ x2

2n

numerical experiment: x = 1 ! e1 = (1 + 1n)n for large n

error: � =e� (1 + 1

n)n

e⇡ 1

2n) n� =

1

2

http://csis.uni-wuppertal.de/courses/numerical_accuracy/test_exp_1.m

1 % the program t e s t e x p 1 .m determines exp ( x ) wi th the he l p o f

2 % the formula (1+x/n)ˆn and compares the r e s u l t

3 % with the exac t one .

4 %

5 clear6 x=1.0 ;7 n=10 . ˆ [ 1 : 1 2 ] ;8 s=(1+x . / n ) . ˆ n ;9 de l t a=(s�exp( x ) )/exp( x ) ; % r e l a t i v e error

10 nde l ta=de l t a .∗n ;1112 tab=[n ; d e l t a ; nde l ta ] ;13 fpr intf ( ’ n de l t a n∗ de l t a \n\n ’ )14 fpr intf ( ’%10.4 e %11.4 e %11.4 e \n ’ , tab )

) up to n ⇠ 107 error decreases, � ⇠ 10�8, n� ⇠ 1/2 as expected

) larger n: 1 + 1/n becomes imprecise and � does not decrease further

) best achievable relative precision � ⇠ 10�8 instead of 10�16

5

Page 9: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

0

0.2

0.4

0.6

0.8

1

0.01 1 100 10000 1x106

1x108

n*

erro

r

n

n*(exp(1)-(1+1/n)n)/exp(1)

explanation: a = 1 + 1nhas an error ✏ = 10�16

) (a+ ✏)n ⇡ an(1 + n✏

a) +O(✏2) ) an has a rounding error of O(n✏)

) balance between roundo↵ and truncation error n✏ ⇠ 1

2n

) optimal n ⇡p1/✏ ⇠ 108, error � ⇠ 10�8

) (1 + x

n)n cannot give ex with machine precision 10�16

alternative: Taylor series

ex =nX

i=0

xi

x!+O(

xn+1

(n+ 1)!

http://csis.uni-wuppertal.de/courses/numerical_accuracy/test_exp_2.m

1 % the program e x p t e s t 2 .m determines exp ( x ) wi th the he l p o f

2 % the Taylor s e r i e s and compares the r e s u l t

3 % with the exac t one .

6

Page 10: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

4 %

5 clear6 % computation o f n !

7 %

8 x=1.0 ;9 nfac (1)=1;10 for i =2:20 , n fac ( i )=nfac ( i �1)∗ i ; end11 %

12 % sum up the e xponen t i a l s e r i e s

13 %

14 s (1)=1+x ;15 for i =2:20 , s ( i )=s ( i�1)+xˆ i / nfac ( i ) ; end16 de l t a=(s�exp( x ) )/exp( x ) ;17 %

18 % pr in t ou t

19 %

20 fpr intf ( ’ i d e l t a \n\n ’ )21 for i =1:20 ,22 fpr intf ( ’%2i %11.4 e \n ’ , i , d e l t a ( i ) )23 end

) for x = 1 we reach machine precision with n = 17 terms

1.5 Recurrence relations

often a sequence Pn, n 2 N, is defined recursively: Pn+1 = f(Pn), P1 given,e.g.:

Pn =

Z 1

0

xnex dx

Pn+1 =

Z 1

0

xn+1ex dx = [xn+1ex]10 � (n+ 1)

Z 1

0

xnex dx = e� (n+ 1)Pn

recurrence relation, start with P0 = e� 1 ) P1 = 1 ) . . .

http://csis.uni-wuppertal.de/courses/numerical_accuracy/recurrence1.m

7

Page 11: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

1 % recurrence1 .m

2 %

3 % compute i n t e g r a l s

4 % 1

5 % /

6 % p n = | xˆn exp ( x ) dx

7 % /

8 % 0

9 %

10 % by us ing the recurrence

11 % p 1 = 1

12 % p n+1 = e � (n+1) p n

1314 p (1 ) = 1 ;1516 for n=1:2017 p(n+1) = exp (1 ) � (n+1)∗p(n ) ;18 fpr intf ( ’ p %2d = %0.16 f \n ’ ,n , p (n ) )19 end

interesting behavior: xnex > 0 for x 2 [0, 1[, but why is P19 negative?

Pn+1 < Pn because xn+1ex = x·xnex where x < 1, but numerically P18 > P17!

error propagation: let P ⇤nbe exact, numerically we have Pn = P ⇤

n+ �n

) Pn+1 = e� (n+ 1)P ⇤n| {z }

exact P⇤n+1

�(n+ 1)�n| {z }�n+1

|�n+1| = (n+ 1)|�n| = (n+ 1)n|�n�1| = . . . = (n+ 1)!|�0|

|�n| / n! ) instable recurrence, even if �0 ⇠ 10�16, �18 ⇠ 1!

luckily, we can turn the table around:

backward iteration: Pn =e� Pn+1

n+ 1) |�n| =

|�n+1|n+ 1

8

Page 12: Computer Simulation in Science Introduction to Computer ...csis.uni-wuppertal.de/courses/scripts/introCS_chapters/introCS1.pdf · CSIS Computer Simulation in Science Introduction

|�n| / 1n! ) stable recurrence, start at arbitrary value for high n

) we get machine precision for Pn with small n, see example code

http://csis.uni-wuppertal.de/courses/numerical_accuracy/recurrence2.m

1 % recurrence2 .m

2 %

3 % compute i n t e g r a l s

4 % 1

5 % /

6 % p n = | xˆn exp ( x ) dx

7 % /

8 % 0

9 %

10 % by us ing the backward recurrence

11 % p 100 = 1

12 % p n = ( e � p n+1) / (n+1)

1314 p (100) = 1 ;1516 for n=99:�1:117 p(n) = (exp (1 ) � p(n+1)) / (n+1);18 end1920 for n=1:2021 fpr intf ( ’ p %2d = %0.16 f \n ’ ,n , p (n ) )22 end

9