Date post: | 26-Dec-2015 |
Category: |
Documents |
Upload: | richard-ray |
View: | 213 times |
Download: | 0 times |
Simulation of Random Walk• How do we investigate this numerically?• Choose the step length to be a=1
• Use a computer to generate random numbers ri uniformly in the range [0,1]
• if ri p then increase x by 1 => x=x+1
• otherwise decrease x by 1 => x=x-1• calculate total x(N) after N steps • any value in the range -N< x < +N is possible• calculate <x(N)> and <x2(N)>-<x(N)>2 by
repeated trials
Random Walk in 2 dimensions• Let the walker start at the origin and choose
each of the four directions with equal probability (p=1/4)
• at each step choose random number ri
• if ri .25 x=x+1
• if .25< ri .5 x=x-1
• if .5 < ri .75 y=y+1
• if .75 < ri 1 y=y-1
Random walk in 2 dimensions
p x N p x e
p y N p y e
x x
y y
( , ) ( )
( , ) ( )
1
2
1
2
2
2
2
2
2
2
2
2
b g
b g
x y
pqNN
0
44
2
p r rdrd p x p y dxdy
p r er
( , ) ( ) ( )
( , )
1
2 22
2
2
p q 1 4/
p rr
Ne
r
N( )
2
2
Random Walk
x y
x yN N
r x y N
r r Nrms
0
41 1 0 0
22 2
2 2 2
2 1 2
( )
/
Walk2dsimulation
Monte Carlo Method
• Perform many trials and calculate the average of any quantity <A>
• calculate the variance [<A2>-<A>2]
• error in <A> [(<A2>-<A>2)/ntrial]1/2
Random Walks and the Diffusion Equation
• This is the diffusion equation for the probability of finding a particle at position x at time t
• what is the dependence of <x(t)> and <x2(t)> on t?
• the average of any function f(x) is
P x t
tD
P x t
x
( , ) ( , )2
2
zf x t f x P x t dx( , ) ( ) ( , )
zx t x P x t dx( ) ( , )
Multiply the diffusion equation on both sides by x andintegrate over x
xP x t
tdx D x
P x t
xdx
z z( , ) ( , )2
2
Left side becomes
Right side is zero since
Hence
t
x
P x t
P x t
x x
( , )
( , )
0
0
t
x x0 0
To calculate <x2(t)>, two integrations by partsare needed and we find
t
x t D2 2( )
Random walk and diffusion equation have the sametime dependence
x t Dt2 2( )
Monte Carlo Methods Consider the problem of integration in one dimension b F = f(x) dx aThe classical methods of numerical integration are based on the geometrical interpretation of the integral as the area under the curve of f(x) from x=a to x=b
• For some choices of f(x) the integral can be evaluated analytically eg. cos(x)
• classical methods of numerical integration are based on the geometrical interpretation of the integral as the ‘area’ under the curve of the function
f(x) from x=a to x=b
• divide the x-axis into n equal intervals of width x, where x=(b-a)/n
Integration
cos(x)
an estimateof the area is F f x x x a i xn i i
i
n
( ) ; 0
1
Rectangular Approximation
f(x)=cos(x)
• Consider f(x)= cos(x) with a=0 and b=/2
• compare with numerical results
• error decreases as 1/n
F x dx x zcos( ) sin( ) //
0
2
0
2
1
error decreases as 1/n
Trapezoidal Rule• A better approximation to the area is given
by the trapezoidal rule
• rather than using f(xi)x we use the average of f(x) at the beginning and end of the interval (1/2)[f(xi+1)+f(xi)] x
• error decreases as 1/n2
F f x f x f x xn i ni
n
LNM
OQP
1
2
1
201
1
( ) ( ) ( )
Simpson’s Rule• A more accurate method is to use a
quadratic or parabolic interpolation procedure
• Simpson’s rule error decreases as 1/n4
• adequate for well behaved functions
• But a function such as f(x)= x-1/3 is poorly behaved at x=0 and would present problems
F f x f x f x f x
f x f x f x x
n
n n n
1
34 2 4
2 4
0 1 2 3
2 1
[ ( ) ( ) ( ) ( ) ...
( ) ( ) ( )]
Simpson’s Rule
Simpson’sRule
chapter 11integtrap
Can we evaluate this using random numbers?
• Consider a rectangle of height H, width (b-a), and area A= H x (b-a) such that f(x) lies within the rectangle
hit or miss algorithm
• Compute ‘n’ pairs of random numbers
• (xi , yi)
• with a xi b and 0yi H
• The fraction of the points that satisfy y i f( xi) is an estimate of the ratio of the integral to the area of the rectangle
• Fn = A (ns / n)
• where ns is the number of "splashes" below the curve and ‘n’ is the number of trials.
n Fn F-Fn
50000. 3.13968 0.00191 100000. 3.14356 0.00197 150000. 3.14237 0.00078 200000. 3.14144 0.00015 250000. 3.13952 0.00207 300000. 3.13959 0.00200 350000. 3.13782 0.00377 400000. 3.13821 0.00338 450000. 3.13862 0.00297 500000. 3.13882 0.00277 550000. 3.13917 0.00242 600000. 3.13831 0.00328 650000. 3.13941 0.00218 700000. 3.13977 0.00182 750000. 3.14051 0.00108 800000. 3.14103 0.00057 850000. 3.14103 0.00056 900000. 3.14103 0.00056 950000. 3.14154 0.00005 1000000. 3.14244 0.00085
Note: all points have equal probability and points above f(x)have zero contribution to Fn but increase the fluctuations
Eg. f(x) = 4 sqrt( 1 – x2) on the interval 0 x 1 F(exact) = = 3.14159
Hit
Sample mean approach
• Choose the xi at random and sample the value of f(xi)
• Mean-value theorem of calculus:
• F = (b-a) < f >
• For n trials, Fn = (b-a)(1/n) i=1,nf(xi)
• where the xi are distributed uniformly on the interval a xi b
f(x) = 4 sqrt( 1. – x2) [0,1]
• The exact value of the integral is
• = 3.14159
n Fn F-Fn n
1000. 3.17026 0.02867 0.86768 10000. 3.14968 0.00809 0.88455
Note that the standard deviation of the integrand is roughlyconstant but that the error decreases with n as ~ 1/n1/2
c Program Samplingn=10000
h=1. a=0. b=1. sum=0. sum2=0. m=2 do 4 i=1,n x=a+b*r250(idum) f=4.*sqrt(1.-x*x) sum=sum+f sum2=sum2+f*f sig2=sum2/i -(sum/i)*(sum/i) sig=sqrt(sig2) if((i-(i/10**m)*10**m).eq.0) then write(6,10)1.*i,sum/i,abs(3.14159-sum/i),sig
m=m+1elsecontinueend if
10 format(1x,f10.0,3x,f10.5,3x,f10.5,3x,f10.5)4 continue stop end
Now fix the number of trials and repeat with a different set of random numbers
Run n Fn F-Fn n 1 10000. 3.14968 0.00809 0.88455 2 10000. 3.13242 0.00917 0.88498 3 10000. 3.13669 0.00490 0.89087 4 10000. 3.15016 0.00857 0.88916 5 10000. 3.11856 0.02303 0.90492 6 10000. 3.14716 0.00557 0.88839 7 10000. 3.13996 0.00163 0.89368 8 10000. 3.13696 0.00463 0.89214 9 10000. 3.14782 0.00623 0.89010 10 10000. 3.14860 0.00701 0.89147
The mean value is <Fn> = 3.14080 The standard deviation of the means is = (<Fn
2> - <Fn>2 )1/2 = 9.46813E-03 n/n1/2
Hence, F = (b-a)< f > (b-a) (<f2> - <f>2 )1/2
n1/2
c program sample mean n=10000
h=1.a=0.b=1.rsum=0.rsum2=0.do 5 j=1,10sum=0.sum2=0.do 4 i=1,nx=a+b*r250(idum)f=4.*sqrt(1.-x*x)sum=sum+fsum2=sum2+f*fsig2=sum2/i -(sum/i)*(sum/i)sig=sqrt(sig2)if((i-(i/n)*n).eq.0) then
write(6,10)j, 1.*i,sum/i,abs(3.14159-sum/i),sigelsecontinueend if
10 format(i5,1x,f10.0,3x,f10.5,3x,f10.5,3x,f10.5)4 continue
rsum=rsum+sum/nrsum2=rsum2+(sum/n)**2
5 continuersum=rsum/10.rsum2=rsum2/10.rsig2=rsum2-rsum**2rsig=sqrt(rsig2)write(6,*) rsum,rsig,rsum+rsig,rsum-rsigstopend
In general dimension, F = V < f > V (<f2> - <f>2 )1/2
n1/2
Example:
Multidimensional Integrals
Consider a small atom such as magnesium with 12 electrons
To calculate electronic properties we need to integrate overall coordinates 3 x 12 = 36 dimensional integral!
With 64 points for each integration, this requires 6436 ~ 1065
evaluations of the integrand => impossible!
=155/6 = 25.83333integ10