You are on page 1of 38

Manual of Numeri

al Te hniques and Simulation


for M.S Physi s Students
V.N.Purushothaman
Department of Physi s
Sree Kerala Varma College, Tri hur
November 3, 2011

Contents
1 Numeri al Methods in PYTHON
1.1 Lagrange's Interpolation . . . . . . . . . .
1.2 Polynomial Evaluation: Bessel's Fun tion .
1.3 Polynomial Evaluation: Legendre Fun tion
1.4 Least Square Fitting . . . . . . . . . . . .
1.5 Monte Carlo Method . . . . . . . . . . . .
1.6 Newton-Raphson Iterative Method . . . .
1.7 Runge-Kutta Method . . . . . . . . . . .
1.8 Numeri al Integration . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

2 Simulation of Physi al Phenomena


2.1 Lennard-Jones potential . . . . . . . . . . . . . . . . .
2.2 Two-slit photon interferen e . . . . . . . . . . . . . . .
2.3 Random Walk in One-Dimension . . . . . . . . . . . .
2.4 Random Walk in Two-Dimensions . . . . . . . . . . . .
2.5 Motion in a Central For e Field:- Rutherford S attering
2.6 Logisti equation . . . . . . . . . . . . . . . . . . . . .
2.7 Simulation of Kepler's First and se ond Laws . . . . .
2.8 Simulation of Kepler's Third Law . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

1
1
3
4
5
7
9
11
12

.
.
.
.
.
.
.
.

15
15
16
18
20
23
25
27
30

Prefa e

This book originated from my experien e as an instru tor and examiner of Final M.S
Physi s students for the past few years. The subje t matter is arranged in the order
of in reasing algorithmi omplexity rather than the depth of prin iples involved.
Most of the variables are given the same name in program and algorithm to fa ilitate
readability. The names are also similar to those in onventional physi s textbooks.
I

1
The algorithm for ea h problem is hosen to be as short as possible. The output of
the omputer programs obtained is also given.
In a ordan e with the purpose of this book, it is assumed that the student is
well versed in the programming language PYTHON, though the number of in-built
fun tions in the sour e ode is kept a minimum. Most of the library fun tions used
are supported by a majority of python interpreters.
To make the presented programs more omplete and useful, readers are kindly
invited to send their omments, suggestions or remarks to the author by post or email in the following address
V.N.Purushothaman,
Department of Physi s,
Sree Kerala Varma College, Tri hur -680 011
e-mail: vadakkedamredimail. om

1
1.1

Numeri al Methods in PYTHON


Lagrange's Interpolation

Aim

To interpolate the value of a fun tion using Lagrange interpolating polynomial.


Prin iple

The te hnique for interpolating y = y(x) is to t an approximate polynomial y f (x)


through the points (xi , yi ) in the neighborhood of the point x where the value of y
is required. Let there be n points xi where the values yi are given. A polynomial of
degree n 1 ould be used for interpolating. In indLagrange method the polynomial
is hosen in the following form
y f (x) =

n
X
i=1

yi

(x xj )
(xi xj )
j=1,j6=i

This is exa t at given data points x = xj .


Algorithm
1: Read x, n
2: for i = 1 to n step 1 do
3:
Read (xi , yi )
4: end for
5: sum 0
6: for i = 1 to n step 1 do
product 1
7:
8:
for j = 1 to n step 1
9:
if i 6= j then

do

n
Y

(1)

1 NUMERICAL METHODS IN PYTHON

10:
11:
12:
13:
14:
15:
16:

product product

end if
end for

(x xj )
(xi xj )

sum sum + yi product

end for
print sum
End

Program
x,y,produ t=[,[,[
n=input('How Many Data points n? ')
for i in range(n):
x.append(input('value of x'))
y.append(input(' orresponding value of y'))
ux=input('What is the value of x at whi h y is required?')
for i in range(n):
p=1
for j in range(n):
if i!=j: p*=(ux-x[j)/(x[i-x[j)
produ t.append(p*y[i)
print 'The value of y at given x is', sum(produ t)

Result

Values of y for dierent x is al ulated using dierent sets of [xi , yi]


Observations

Label Data (xi , yi )n


x
y
x
y
x
y
1.2

Polynomial Evaluation: Bessel's Fun tion

Aim

To evaluate the Bessel Fun tion of any order n for any value of x.

UX

1.2 Polynomial Evaluation: Bessel's Fun tion

Prin iple

The Bessel fun tion of order n for a variable x is given by the series
Jn (x) =

X
s=0

(1)s  x 2s+n
s!(n + s)! 2

(2)

1  x n
. The ratio of the pth and
n!
2
1  x 2
term of the expansion is
. using these results Bessel fun tion
p(n + p) 2

The zeroth term (s = 0) of the expansion is


(p 1)th

of any order an be al ulated for any desired a ura y.

Algorithm
1: Read order n, variable x
2: f act 1
3: for i=2 to n step 1 do
4:
f act = f act.i
5: end for
 

and desired a ura y e

f act represents n!

x n
1
f act 2
7: sum term
8: p 1
9: while |term| > e do
1  x 2
term term.
10:
p(n + p) 2
11:
sum sum + term
12:
pp+1
6: term

13:
14:

end while
Print sum

sum gives Jn (x)

Program
term=[
n,x,e=input('Give Order n, value x and required a ura y e of Bessel fun tion')
x/=2.0
fa torial=1
for i in range(1,n+1):fa torial*=i
term.append(x**n/fa torial)
for i in range(1,1000):
term.append(-term[i-1*x*x/(i*(i+n)))
if abs(term[i)< e:break
print 'J%d(%f)=%f'%(n,x,sum(term))

1 NUMERICAL METHODS IN PYTHON

Result

The value of Bessel fun tion for dierent orders and dierent values of variable are
found and tabulated.
Observations
Order n
Value of x

1.3

Value of Bessel fun tion

Polynomial Evaluation: Legendre Fun tion

Aim

To evaluate the Legendre Fun tion of any order n for any value of x.
Prin iple

Legendre fun tion Pn (x) are dened through a generating fun tion as

X
1

=
Pn (x)tn
1 2xt + t2
n=0

(3)

where |t| 1 The zeroth term (n = 0) of the expansion is 1 = P0 (x)t0 Hen e P0 (x) = 1
for all x. Expanding left side as a binomial series and equating oe ient of t on both
sides, xt = P1 (x)t or P1 (x) = x for all x. Dierentiating and equating oe ients of
tn on both sides one gets
Pn+1 (x) =

(2n + 1)xPn (x) nPn1 (x)


n+1

(4)

This is a re urren e relation whi h may be used for al ulating Legendre polynomials
of any order.
Algorithm
1: Read n, x
2: P0 (x) 1
3: P1 (x) x
4: if n=0 then
5:
Print P0 (X) = 1
6: end if
7: if n=1 then
8:
Print P1 (X) = x
9: end if
10: if n > 1 then

1.4 Least Square Fitting


for i = 1 to n 1 do

11:
12:
13:
14:
15:
16:
17:
18:

(2i + 1)xPi (x) iPi1 (x)


i+1
pi1 (x) pi (x)
pi (x) pi+1 (x)
Pi+1 (x)

end for
end if
Print Pn (x)
End

Program
n,x,e=input('Give Order n, value x and error e of Legendre fun tion')
p0,p1=1,x
if n==0:print 'p0(x)=1.0'
if n==1:print 'p1(%0.5f)=%0.5f'%(x,x)
if n>1:
for i in range(1,n):
p=((2*i+1)*x*p1-i*p0)/(i+1)
p0,p1=p1,p
print 'p%d(%0.5f)=%0.5f'%(n,x,p)

Result
Observations
Order n
Value of x

1.4

Value of Legendre fun tion

Least Square Fitting

Aim

To nd the slope and inter ept of the linear least square t of the given data. Also
to al ulate the most probable value of y for given x
Prin iple

When the data obtained is a urate, Interpolation by polynomial tting gives good
results. If data has errors, as in the ase of experimentally generated data, a urve an
be tted only approximately. This must be statisti ally a best t. It an be shown
that the best t is obtained if the sum of squares of deviations from the approximate
urve is minimum. This is alled least squares approximation. If the urve tted is a
straight line, it is alled linear least squares approximation.

1 NUMERICAL METHODS IN PYTHON

Let yi = mxi + c be the a tual straight line to be tted. Let (xi , yi )n be the n data
points to be tted. Then deviation Di = yP
i . ForP
best t it an be shown that
i y

Di2
Di2
2
2
D
=
= 0 is the ondition for
must
be
a
minimum.
As
D

0
,
i
i
i
m
c
minimum. Substituting Di = yi mxi c and simplifying one gets
X
X
X
m
x2i + c
xi =
xi yi
(5)
X
X
m
xi + nc =
yi
(6)
P

Put

xi = p,

yi = q ,

x2i = r and

xi yi = s. Then

mr + cp = s and mp + nc = q

(7)

pq ns
p2 nr
ps qr
c= 2
p nr

(8)

Solving
m=

The value uy for any ux is then given by uy = m.ux + c


Algorithm
1: Read number of data points n
2: Sum of xvalues p 0
3: Sum of yvalues q 0
4: Sum of x2 values r 0
5: Sum of xyvalues s 0
6: for i=1 to n do
7:
Read pair (xi , yi )
8:
p p + xi
9:
q q + yi
10:
r r + x2i
11:
s s + xi yi
12: end for
pq ns
13: m 2

p nr
ps qr
14: c 2
p nr
15: Print m and c

16:
17: Read ux where y
18: uy mx + c
19: print uy
20: End

is required

(9)

1.5 Monte Carlo Method

Program
from numpy import*
n=input('Number of data points n')
x=array([input('X-values for fitting') for i in range(n))
y=array([input('Corresponding Y-values') for i in range(n))
p,q,r,s=sum(x),sum(y),sum(x*x),sum(x*y)
m=(n*s-p*q)/(n*r-p**2)
=(q*r-p*s)/(n*r-p**2)
print 'y=%0.6f x+%0.6f'%(m, )
ux=input('x-value for whi h y is required')
print 'y(%0.5f)=%0.5f'%(ux,m*ux+ )

Result

The slope and inter ept for dierent sets of data are found. Using this slope and
inter ept values of y orresponding to dierent values x are al ulated
Observations
var.
data (Xi , Yi )

xi
yi
xi
yi
xi
yi
1.5

Monte Carlo Method

Aim

To nd the value of using random numbers.


Prin iple

(0,1)

......................
.................
............
...........
..........
........
........
.......
.......
......
......
......
.....
.....
.....
....
....
....
....
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
.

(0,0)

(1,1)

(1,0)

UX

UY

1 NUMERICAL METHODS IN PYTHON

Points in the rst quadrant of a unit ir le entered at origin satises the following
inequalities.
1. 0 x +1
2. 0 y +1
3. x2 + y 2 1 .
.The rst two onditions are satised by all points in a unit square in the rst quadrant
as shown in gure. The ratio of area of the se tor(= /4) to the area of the square(=
12 ) is /4. Four times this ratio gives the value of .
To determine this ratio in Monte Carlo Method, pairs of pseudo-random numbers
in the range (0, 1) are generated for oordinates (x, y). All these
p pairs fall within
the square, but only those points belong to the ir le for whi h x2 + y 2 1. The
number of su h points N are ounted. The ratio of number N to the total number of
points gives the ratio of their areas.
In PYTHON there is one in-built random number generator using the multipli ative ongruential re ursive method developed by D.Lehmer. The ith and (i + 1)th
random numbers are related as
xi+1 = (axi + c)mod m

(10)

where the multiplier a = 75 1 = 16, 806, the in rement c = 0 and the modulus
m = 231 1 = 2, 14, 74, 83, 647 are alled magi numbers. The re urren e relation
suggests that the random numbers will repeat with a period less than m. The magi
numbers for the set (a, m, c) are hosen so that period is m and every number
between 0 and m 1 o ur at some point. The role of initial seed x0 has only little
ee t.
Algorithm

To generate random numbers


fun tion rand()
1: if xi = 0 then
2:
3:
4:
5:
6:
7:
8:
9:
10:

xi 1

end if

a 16806
c0
m 2147483647
xi+1 (axi + c)mod m
Return xi+1 /m
xi xi+1

End
To al ulate value of
1: Read N ,the ount of random numbers to be generated

1.6 Newton-Raphson Iterative Method


2: j 0
3: for i = 1 to N do
4:
x rand()
5:
y rand()
6:
if (x2 + y 2 ) 1
7:
j j+1
8:
end if
9: end for
10: 4.j/i
11: Print
12: End

then

Program
from random import random
j=0
for i in range(1000000):
if (random()**2+random()**2)<=1:j+=1
print "Value of pi = ",4.0*j/i

Result

The value of is al ulated using various large sets of random numbers and tabulated.
1.6

Newton-Raphson Iterative Method

Aim

To nd the roots of polynomial and trans endental equations.


Prin iple

The Taylor series expansion of a fun tion f (x) is given by


f (x) =

X
(x x0 )n f (n) (x0 )
n=0

n!

(11)

where f (n) (x0 ) is the nth derivative of f (x) at x = x0 . Hen e the value of f (x) at
x = xi+1 about x = xi is given by
f (xi+1 =

X
(xi+1 xi )n f (n) (xi )
n=0

n!

(12)

Negle ting terms of order 2 and above (n 2)


f (xi+1 ) = f (xi ) + (xi+1 xi )n f (1) (xi )

(13)

10

1 NUMERICAL METHODS IN PYTHON

If xi+1 is a root of f (x) in this approximate form, then f (xi+1 ) = 0. Substituting and
rearranging
xi+1 = xi

f (xi )
f (1) (xi )

(14)

To get the a tual root within a pres ribed error limit, this expression may be iterated
with the urrent xi+1 as the new xi . If xi happens to be near the a tual root, a small
number of iterations will give the a tual root. Hen e the initial guess of the root is
important.
Algorithm
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Read initial guess x0 , error e,number of iterations n, and minimum slope s


for i = 1 to n do
f f (x0 )
g f (1) (x0 )
if g < s then

'Slope too small to onverge to a root'


Goto line:16
end if

x x0 (f /g)

if |(x x0 )/x| < e then


Print x
Goto line:16
end if
end for
Print 'Do not onverge to a root within n-iterations.
End

Program
from math import *
def f(x):return x**3-3.0*x-5.0
def g(x):return 3.0*x**2-3.0
i,x1=0,0.0
x=input('Initial guess of the root')
while abs(g(x))>0.001 and abs(f(x))>1.0e-6:
x1=x-f(x)/g(x)
x=x1
i+=1
print "Iterations= %d \t,x1= %5.3f,\t f(x1)= %10.3e"%(i,x1,f(x1))

Result

THE ZEROS OF A FEW FUNCTIONS ARE FOUND AND ARE TABULATED

1.7 Runge-Kutta Method

11

Observations
F (X)
X 2 + 5X + 6
X 2 + 5X + 6
5X 5 2 cos X
10eX + 16 sin2 (X)
10eX + 16 sin2 (X)
1.7

X0

E
0.001
0.0001
.0001
0.001
0.001

-5
2
0.1
10
15

5
2
16
6
6

S
0.0001
0.0001
0.0001
0.001
0.001

Root
-3.00000
-2.00000
0.7779492
9.430085
15.69952

F(ROOT)
0
0
7 107
1.2 103
1.14 103

Runge-Kutta Method

Aim

To solve a rst order dierential equation by Runge-Kutta fourth order method.


Prin iple

Consider a dierential equation

dy
= f (x, y) with the initial ondition y(x0 ) = y0 .
dx


dy
If s =
is the average slope of the fun tion between x0 and x0 + x, then
dx mean
y(x0 + x) = y(x0 ) + s.x In Runge-Kutta fourth order method, s is found as the
weighted average of slopes at dierent points between x0 and x0 + x as follows.


Cal ulate slope s1 at x0 ; s1 =

dy
= f (x0 , y0).
dx

Cal ulate y at (x0 + x/2) using s1 ; y1 = y(x0 + x/2) = y0 + s1 .x/2


Cal ulate the slope s2 at (x0 + x/2) using y1 ; s2 = f (x0 + x/2, y1 ).
Cal ulate the value of y at (x0 + x/2) using s2 ; .y2 = y0 + s2 .x/2
Cal ulate on e again the slope s3 at (x0 + x/2) using y2 ; s3 = f (x0 + x/2, y2).
Cal ulate the value of y at x0 + x) using s3 ; y3 = y0 + s3 .x
Cal ulate the slope s4 at (x0 + x) using y3 ; s4 = f (x0 + x, y3 ).
Then y(x0 + x) = y0 +

(s1 + 2s2 + 2s3 + s4 )


x
6

Algorithm
1: Read x0 , y0 , n, x1
2: x (x1 x0 )/n
3: for i = 1 to n step 1 do
s1 f (x0 , y0 )
4:
5:
s2 f (x0 + x/2, y0 + s1 x/2)
6:
s3 f (x0 + x/2, y0 + s2 x/2)

12
7:
8:
9:
10:
11:
12:

1 NUMERICAL METHODS IN PYTHON


s4 f (x0 + x, y0 + s3 x)
y0 y0 + x(s1 + 2s2 + 2s3 + s4 )/6
x0 x0 + x

end for
Print y0
End

Program

Finds y(ux) using (x0 , y0 ) and the equation dy/dx = (y x)/(y + x)


def f(x,y): return (y-x)/(y+x)
def rk4(x, y, fxy, h,n):
for i in range(1,n+1):
k1 = h * fxy(x, y)
k2 = h * fxy(x + h/2.0, y+k1/2.0)
k3 = h * fxy(x + h/2.0, y+k2/2.0)
k4 = h * fxy(x + h, y+k3)
y += ( k1 + 2*k2 + 2*k3 + k4 )/6.0
x+=h
return y
x0,y0,ux,n = input('Give initial values of x0,y0,ux and number of steps n')
h=(ux-x0)/(1.0*n)
print 'y(%0.5f)=%0.6f'%(ux,rk4(x0,y0,f,h,n) )

Result
Observations
dy/dx =
2x2
x+y
xy
yex
1.8

x0

0
1
1
2

y0

1
1
1
1

x1

2
3
4
3

10
15
20
40

y(x1 )

6.3333
18.1670700
1804.628000
1.089314

Numeri al Integration

Aim

To evaluate denite integrals of tabulated fun tions using Newton-Cotes Integration


formulae.
Prin iple

The basi strategy is to repla e a ompli ated fun tion f (x) or tabulated data by
a ollo ation polynomial p(x) so that integration be omes easy. Trapezoidal rule

1.8 Numeri al Integration

13

for integration uses straight lines (rst order polynomial) to onne t data points
equispa ed in x. If f (x1 ) and f (x2 ) are given, then a line from (x1 , f (x1 )) to (x2 , f (x2 ))
is given by
f (x2 ) f (x1 )
(x x1 )
f (x) = f (x1 ) +
x2 x1

Z x2 
Z x2
f (x2 ) f (x1 )
(x x1 ) dx
f (x1 ) +
f (x)dx
I =
x2 x1
x1
x1
f (x2 ) + f (x1 )
= (x2 x1 )
2

(15)
(16)
(17)

The integration is thus repla ed by summation. This is alled Trapezoidal rule as


eq.17 represents area of a Trapezoid bounded by parallel sides f (x1 ), f (x2 ), the
straight line (x1 , f (x1 )) to (x2 , f (x2 )) and the x-axis. If data is available for n + 1
equispa ed points (xi ) between x1 and xn+1 ,
n
X
f (x1 ) + f (xn+1 )
+h
f (xj )
I = h
2
j=2

(18)

where h = xj xj1 .
In Simpson's method, higher order ollo ation polynomials are employed. If a
quadrati fun tion is tted to represent a set of three data points (x1 , y1), (x2 , y2) and
(x3 , y3 ), the integral may be approximated by Legrange polynomial to get
I =

x3

x
Z 1x3
x1

f (x)dx
(19)


(x x1 )(x x3 )
(x x1 )(x x2 )
(x x2 )(x x3 )
f (x1 ) +
f (x2 ) +
f (x3 ) dx
(x1 x2 )(x1 x3 )
(x2 x1 )(x2 x3 )
(x3 x1 )(x3 x2 )

Integrating and rearranging, one gets


i=

h
[f (x0 ) + 4f (x1 ) + f (x2 )]
3

(20)

where h = x1 x0 = x2 x1 . This is known as Simpson's 1/3 rule. For n segments


0
between xn+1 and x1 , h = xn x
.
n
I=

h
[f (x1 ) + 4f (x2 ) + 2f (x3 ) + 4f (x4 ) + 2f (x5 ) + .... + f (xn+1 )]
3

(21)

Algorithm

To ompute the denite integral of a tabulated fun tion


1: Read number n of data points (xi , yi ) for tabulated fun tion or the limits of
integration if fun tional form is given.
2: Find or assign a value for interval h
3: sum 0

14

1 NUMERICAL METHODS IN PYTHON

4: For trapezoidal law


5: for i = 1 to n step 1 do
6:
sum sum + yi h
7: end for
8: sum sum (y1 + yn ) h
9: For Simpson's law, if n is odd
10: for i = 2 to n 1 step 2 do
11:
sum sum + 4 yi h/3
12: end for
13: for i = 3 to n 1 step 2 do
14:
sum sum + 2 yi h/3
15: end for
16: sum sum + (y1 + yn ) h/3
17: For Simpson's law, if n is even
18: for i = 3 to n 1 step 2 do
19:
sum sum + 4 yi h/3
20: end for
21: for i = 4 to n 1 step 2 do
22:
sum sum + 2 yi h/3
23: end for
24: sum sum + (y2 + yn ) h/3 + (y1 + y2 )h/2
25:
26: Print sum whi h is the value of the integral.
27: End

Program

To nd

Rb
a

(1 + x)1 dx using Trapezoidal rule and Simpson's 1/3 rule

def f(z):return 1/(1+z)


a,b,n=input('Give the lower limit a,upper limit b and the no: of steps n')
h=(b-a)/(1.0*n)
x=[a+h*i for i in range(n)
y=[f(x[i) for i in range(n)
trapintegral=(y[0+y[-1+2*sum(y[1:-1:1))*h/2.0
print 'trapezoidal=',trapintegral
simpintegral=(h/3.0)*(y[0+y[-1+4*sum(y[1:-1:2)+2*sum(y[2:-1:2))
print "simpsons= ",simpintegral

Result

A few fun tions and a few tabulated values were substituted and the integrals were
found to be nearly a urate.

15
Observations
Integral

R 3 2x
e dx
R14 2
3x dx
R24
x sin(x/2)dx
R22
dx/(2 + cos x
0

2
2.1

data points
9999
9999
9999
9999

Trapezoidal
197.939
55.99
0.162489
3.6274

Simpson's
197.939
55.99
0.162489
3.62734

Analyti al
198.02
56.00
3.6275987

Simulation of Physi al Phenomena


Lennard-Jones potential

Aim

To plot the attra tive and repulsive parts of the Lennard-Jone's potential for various
values of parameters.
Prin iple

Two distin t for es a t on neutral atoms and mole ules. An attra tive van der Waal's
for e at a large distan e and a for e of repulsion at a short distan e whi h is a
onsequen e of the overlapping ele tron orbitals (Pauli repulsion). The LennardJone's potential is a simple mathemati al model that des ribes this behavior. It is of
the form
V (r) =

a2
a1
6
12
r
r

(22)

Obviously, V (r) has a minimum, say alled well depth. Let be the distan e at
whi h the potential vanishes alled hard sphere radius. Expressing a1 and a2 in terms
of and ,one gets
V (r) = 4

 
12
r

 6 
r

(23)

These parameters an be tted to reprodu e experimental data or dedu ed from


results of a urate quantum al ulations. The 1/r12 term des ribes the repulsive
for e and the 1/r6 term des ribes the attra tive for e.
The Lennard-Jone's potential, being a mathemati al model, is approximate. Espe ially the form of the repulsion term has no theoreti al justi ation as the for e of
repulsion depends exponentially on the distan e. The attra tive long-range potential
is derived from dispersion intera tions. Still, it is a relatively good approximation and
simple in form. It is often used to des ribe the properties of gases and to investigate
dispersion and overlap intera tions in mole ular models. It is parti ularly a urate for
noble gas atoms and is a good approximation at long and short distan es for neutral
atoms and mole ules.

16

2 SIMULATION OF PHYSICAL PHENOMENA

Algorithm
1:
2:
3:
4:
5:
6:
7:
8:

Read Well depth and hard sphere radius and distan e in rement dr
for r = 0 to 3 step dr do
va 4(/r)6
vr 4(/r)12
V va + vr

plot va,vr,V
end for
End

Program
from pylab import*
epsilon,sigma=input('Give the values of potential Well depth and hard sphere
r=linspa e(sigma,3*sigma,1000)
vrepulsive=4*epsilon*(sigma/r)**12
vattra tive=-4*epsilon*(sigma/r)**6
vtotal=vrepulsive+vattra tive
plot(r,vattra tive)
plot(r,vrepulsive)
plot(r,vtotal)
show()

Result

The following graph is obtained.


Lennard-Jones potential
200

150

100

potential

50

50

100

150

200
0.20

0.25

0.30

0.35

0.40

0.45

Distance

0.50

0.55

0.60

0.65

2.2 Two-slit photon interferen e


Standard Values for and
ATOM/ION (in nm)
(in K)
O
3.166
78.24
H
NA
NA
Z6.000
251.58
Na+
2.275
58.01
K+
3.023
58.01
2.2

17

Charge
-0.8476
0.4238
-1.0000
1.0000
1.0000

Two-slit photon interferen e

Aim

To plot the probability density for arrival of photons s attered by two slits at ea h
point on a s reen kept at a xed distan e from the line of slits and parallel to it.
slits
o

d/2
sour e

o
d/2
D

S reen
Prin iple

Consider a set of photons of identi al in all respe ts moving toward the line of slits.
At the slits they get s attered in dierent dire tions with dierent probabilities. The
onsequent state of the photons an be des ribed by a spheri al wave fun tion

eik.~r12
(~r12 ) =
r12

(24)

where ~r12 = ~r1 ~r2 , k = 2/ is the wave ve tor and , the wavelength of the
photon. Equation 24 is a reasonably good estimate of the probability amplitude for
a free parti le to move from position ~r1 to position ~r2 in empty spa e. In gure, the
ontribution to probability amplitude from left path is given by
eikrAC
rAC
eikrBC
R (x) =
rBC
L (x) =

1 Ref:Chapter-3

, Volume-III,'Feynmann's Le tures on Physi s'

(25)
(26)

18

2 SIMULATION OF PHYSICAL PHENOMENA

where rAC = d2 + (x b/2)2 and rBC = d2 + (x + b/2)2 . The probability density


P (x)of nding the photon at various points on the s reen is plotted against x for four
ases.
1. When only left slit is open,
p

PL (x) = |L (x)|2 =

(27)

2
rAC

2. When only right slit is open,


PR (x) = |R (x)|2 =

(28)

2
rBC

.
3. When both slits are open and the photon is observed at one of the slits ,
PLR (x) = |L (x)|2 + |R (x)|2 =

1
2
rAC

1
2
rBC

(29)

.
4. When both slits are open and no observation is made,
PLRI (x) = |L (x) + R (x)|2 =

1
2
rAC

1
2
rBC

2 cos k(rAC rBC )


rAC .rBC

(30)

.
Among these ases, only the 4th one ould show interferen e pattern.
Algorithm
1:
2:
3:
4:
5:
6:
7:
8:

De lare data type of all variables.


Read d and b as multiples of wavelength of the photon
for x = 100
p to 100 step 0.001 do
rAC pd2 + (x b/2)2
rBC d2 + (x + b/2)2
1
1
2 cos k(rAC rBC )
yLRI (x) 2 + 2 +
rAC rBC
rAC .rBC
Plot yLRI (x)

end for

Program

from pylab import*


b=input("Distan e between the slits as multiple of wavelength(10 to 20) = ")
d=input("Distan e between line of slits and s reen as multiple of wavelength(
x=linspa e(-1000,1000,1000)
p=1/sqrt(d*d+(x-b/2)*(x-b/2))
q=1/sqrt(d*d+(x+b/2)*(x+b/2))
y=p*p+q*q+2*p*q* os(2*pi*(q-p)/(q*p))

2.3 Random Walk in One-Dimension

19

title('Intensity(probability density)Distribution on the s reen')


xlabel('Distan e along the s reen')
ylabel("Intensity")
plot(x,y)
show()
Intensity(probability density)Distribution on the screen
0.0000014

0.0000012

Intensity

0.0000010

0.0000008

0.0000006

0.0000004

0.0000002

0.0000000
1000

2.3

500

500

1000

Distance along the screen

Random Walk in One-Dimension

Aim:

To establish the relation between the number of steps a Random walker takes and
his mean square displa ement.
Prin iple:

Random walk refers to a statisti al pro ess in whi h an obje t is assumed to move in
dire tions hosen at random through a xed distan e in xed intervals of time. It an
be used to explain physi al phenomena like diusion, Brownian motion, transport of
photons down the temperature gradient in stars et . Let us assume that ea h ollision
makes the parti le move either one step forward or one step ba kward and of length
unity (mean free path). As ollision is random, the probabilities for forward and
ba kward movements are equal and ex lusive (independent of ea h other).
At the beginning let the parti le be at x = 0. After one step of length unity let it
rea h some point x1 = 1. Either the same parti le is brought ba k to x = 0 or a new
parti le starts from x = 0 and the above pro ess is repeated. As there is no preferred
dire tion, the mean value of x1 from all trials will be zero. That is, < x1 >= 0. But

20

2 SIMULATION OF PHYSICAL PHENOMENA

its mean square position is


(31)

< x21 >=< (0 1)2 >= 1

At the time of se ond steps, the parti le has position


(32)

x2 = x1 1

Its mean position < x2 > is still zero, but its mean square position
< x22 >=< (x1 1)2 >=< x21 2x1 + 1 >=< x21 > +1 = 2.

(33)

Continuing this pro ess for N steps, we nd < xN >= 0, and
< x2N >=< x2N 1 > +1 =< x2N 2 > +2 = .... =< x21 > +N 1 = N

(34)

The root mean square position after N steps is < x2N > = N . So, after 100 steps
of unit length, the parti le has typi ally moved a distan e of 10 units from the origin,
and after 106 steps, typi ally a distan e 1000 units provided
the number of trials are
p
2
large. In ontrast, if there is no randomness, then < xN > = N . This is a very
important result.
p

Algorithm
1:

De lare oating point variables position x, sumX , sumX 2 , mean square deviation
msd and the probability required for forward step(+1 step) f having range 0 to
1. Integer variables are number of trials t and number of steps s
Set x 0, msd 0
Read t, s, f
Initialize random number generator fun tion rand if required.
sumX 0, sumX2 0
for m = 1 to t do
n0,x0
for n = 1 to s do

2:
3:
4:
5:
6:
7:
8:
9:
p rand
10:
if p f then
11:
x x+1
12:
else
13:
x x1
14:
end if
15:
end for
16:
sumX sumX + x
17:
sumX2 sumX2 + x2
18: end for
19: Mean displacement sumX/t
20: Mean square displacement sumX2/t

2.3 Random Walk in One-Dimension

21

Program
from pylab import*
s,n=input("Number of steps(<50) and number of walks(>100000)")
tot,msd,thousands=0,[,[
for i in range(1,n):
x=0
for j in range(s):
if random()>0.5:x+=1
else:x-=1
tot+=x*x
if i%1000==0:
msd.append(1.0*tot/i)
thousands.append(i)
grid(True)
plot(thousands,msd,'.')
show()

Result

For 100,000 trials ea h of 45 steps, the following graph is obtained.

1-D random walk

45.4

Mean square displacement

45.2
45.0
44.8
44.6
44.4
44.2
44.0
43.80

20000

40000
60000
no:of trials

80000

100000

22

2 SIMULATION OF PHYSICAL PHENOMENA

2.4

Random Walk in Two-Dimensions

Aim:

To establish the relation between the number of steps a Random walk takes and his
mean square displa ement.
Prin iple:

Random walk refers to a statisti al pro ess in whi h an obje t is assumed to move in
dire tions hosen at random through a xed distan e in xed intervals of time. It an
be used to explain physi al phenomena like diusion , Brownian motion, transport
of photons down the temperature gradient in stars et . Let us assume that ea h
ollision makes the parti le move either one step forward or one step ba kward and
of length unity mean free path). As ollision is random, the probabilities for motion
in all dire tions are equal and ex lusive (independent of ea h other).
After nth step of length unity in some dire tion n , let it rea h some point (xn , yn)
so that (xn xn1 )2 + (yn yn1 )2 = 1, xn = xn1 + cos n , yn = yn1 + sin n .
The dire tion n taken by the parti le is ompletely random. The value of n in
ea h step is obtained by a random number generator fun tion. At the beginning let
the parti le be at (x0 , y0 ) = (0, 0). After the parti le ompletes the spe ied number
of steps, n, let d2 = x2n + yn2 be the square of net displa ement. Either the same
parti le is brought ba k to the origin or a new parti le starts from the origin and the
above pro ess is repeated. d2 is al ulated in ea h ase. Mean d2 =< d2 > over all
trials is al ulated in ea h ase. < x >=< y > must be zero as motion is isotropi .
But its mean square displa ement
< d2 > =
=
=
+
=
=
=
.
=
=

< x2n + yn2 >


< (xn1 + cos n )2 + (yn1 + sin n )2 >
< x2n1 > + < cos n 2 > + < 2xn1 cos n >
2
< yn1
> + < sin n 2 > + < 2yn1 sin n >
1
1
2
>+ +0
< x2n1 > + + 0+ < yn1
2
2
2
< x2n1 > + < yn1
> +1
2
< x2n2 > + < yn2
> +2
........................................................
< x20 > + < y02 > +n
n

Mean square displa ement= number of steps.


2 The

(35)

2 3

random motion under the a tion of a for e in some xed dire tion an be explored by
spe ifying a proportionately high probability for motion along the for e.
3 Sin e the two dire tions are equally likely, after a number of trial walks , the mean square
displa ement < x21 >=< y12 >

2.4 Random Walk in Two-Dimensions


Algorithm
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:

De lare variables x, y, d, as oating point and m, n, t, s as integers


Set total distan e square tds to zero
Read t and s
Initialize random number generator
Initialize Graphi s
for m = 1 to t step 1 do
x 0, y 0
for n = 1 to s step 1 do
= random()
x x + cos
y y + sin

end for

tds x2 + y 2
msd tds/m
Plot msd against m

end for
Print msd
End

Program
from pylab import*
s,n=input("Number of steps(<50) and number of walks(>10000)")
tot,msd,thousands=0,[,[
for i in range(1,n):
x,y=0,0
for j in range(s):
theta=2*pi*random()
x+= os(theta)
y+=sin(theta)
tot+=(x*x+y*y)
if i%100==0:
msd.append(1.0*tot/i)
thousands.append(i)
grid(True)
title('2-D random walk')
xlabel('no:of trials')
ylabel('Mean square displa ement')
plot(thousands,msd,'.')
show()

23

24

2 SIMULATION OF PHYSICAL PHENOMENA

Result

For 20,000 trials ea h of 23 steps, the following graph is obtained.

2-D random walk

24.5

Mean square displacement

24.0
23.5
23.0
22.5
22.0
21.5
21.00

2.5

5000

10000
no:of trials

15000

20000

Motion in a Central For e Field:- Rutherford S attering

.......
.....
......
......
.......
......
.
.
.
.
.
.
.......
.......
.......
.......
.......
.......
.
.
.
.
.
.
..
.......
.......
........
.......
........
........
.
.
.
.
.
.
...
........
........
........
........
.........
........
.
.
.
.
.
.
.
.
.....
.........
.........
..........
..........
..........
...........
.
.
.
.
.
.
.
.
.
.
........
............
.............
............
...............
...............
.................
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.....................
................................................................................................................................................

A(x1 , y1)

B(xn , yn )

Aim

To plot the traje tory of a parti le moving in a Coulomb eld and to determine angle
of dee tion as a fun tion of impa t parameter.

2.5 Motion in a Central For e Field:- Rutherford S attering

25

Prin iple

Rutherford's experiment is to measure the dee tion of a beam of parti les by


gold nu lii due to Coulomb repulsion. The ele trostati for e is given by
Ze.2e
r
F~ =
40 r 2

(36)

Resolving and putting r2 = x2 + y 2 , the omponents of a eleration are


2ze2 x
4m0 r 3
2ze2 y
ay =
4m0 r 3

ax =

Putting c =

(37)
(38)

2ze2
d2 x
d2 y
, ax = 2 and ay = 2 one gets
4m0
dt
dt
d2 x
cx
=
dt2
(x2 + y 2)3/2
cy
d2 y
= 2
2
dt
(x + y 2)3/2

(39)
(40)

The velo ity and position of every parti le at dierent instants of time are then
determined by solving the above dierential equations numeri ally. From gure, if
(x1 , y1 ) are asymptoti points , ABC is isos eles. If is the angle of dee tion,
cot (/2) =

xn x0
yn y0

(41)

Algorithm
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Read the initial values of velo ities and positions of parti les in the beam, the
impa t parameter and the time step dt.
for b=0 to 20 step 1 do
while kyk is below a xed value yn do
Cal ulate ax and ay using formulae 40 and 2.5
Cal ulate x and y using 4th -order Runge-Kutta Method.
plot (x, y )
end while
Cal ulate cot (/2) and b/ cot (/2)
plot cot (/2) against b
end for
End program

26

2 SIMULATION OF PHYSICAL PHENOMENA

Program
from numpy import*
from pylab import*
=21.82743562;
dt=0.0001
x=zeros(10001,'float')
y=zeros(10001,'float')
b=linspa e(0,1,10)
otthetaby2=zeros(10,'float')
for j in range(10):
x[0,y[0,t,vx,vy=-5,b[j,0,10,0
for i in range(10000):
vx+=x[i* *dt/(x[i*x[i+y[i*y[i)**1.5
vy+=y[i* *dt/(x[i*x[i+y[i*y[i)**1.5
x[i+1=x[i+vx*dt
y[i+1=y[i+vy*dt
figure(1)
xlabel('x')
ylabel('y')
title("Path of alpha parti le")
plot(x,y)
otthetaby2[j=(x[i-x[0)/(y[i-y[0)
figure(2)
xlabel('impa t parameter b')
ylabel(' ot(theta/2)')
title("Relation between b and theta")
grid(True)
plot(b, otthetaby2)
show()

Result
2.6

Logisti equation

Logisti map

A map is a fun tion whi h relates the oordinates of a point Pn+1 in terms of those
of the previous point Pn . A map is always dis ret as it uses the previous value of
the dependent variable as the present value of independent variable. There is thus no
question of dierentiability for a map.
The logisti map is developed by Robert May in 1976 as a mathemati al model of
population growth whose generations do not overlap with a xed environment. It is
given by
xn+1 = cxn (1 xn )

(42)

2.6 Logisti equation

27

where 0 < x < 1 and c > 1 A ontinuous form of the logisti map is the logisti
equation rst developed by P.F.Verllhurst in 1845.
f (x) = cx(1 x)

(43)

Mathemati al properties of
logisti map and equation
1. The roots of logisti equation 43 are obtained by setting f (x) = 0. They are
x=0 and x=1.
2.
df (x)
= c(1 2x)
dx

(44)

Extremum o urs at df(x)/dx=0 whi h is, at x = 1/2. This is a maximum


be ause d2 f (x)/dx2 = c whi h is negative. This point x=1/2 is alled the
riti al point of the fun tion possessing only a single maximum in a given intervel ( 0 < x < 1) in this ase.
3. After some iterations of the map 42, it often onverges to some xed value alled
an 'attra tor'. Any further iteration of 42 will yield the same value. If xn is
su h a value,
xn = cxn (1 xn )
xn = 1 1/c

(45)
(46)

Condition for stability of attra tor


In equation 42 if xn < 0, then iterations will lead xn+1 to . If xn = 0 then xn+1 is
zero always. For x = 1/c, xn+1 = 1 1/c = xn . The range 0 < xn 1/c is alled the
'basin of attra tion' of xn . A value xn approa hes xn if su essive iterations bring it
loser to xn .

In the limit f (xn1 ) xn 0



xn+1 xn


xn x < 1
n


f (xn ) xn


f (xn1 ) x < 1
n


df (xn )


< 1
dxn
xn =xn

|c(1 2xn )| = |2 c| < 1

This is possible only if 1 < c < 3.

(47)

(48)

28

2 SIMULATION OF PHYSICAL PHENOMENA

When c = 3 the attra tor bifur ates to two xed points x1 and x2 in su h a way
that
x2 = f (x1 )
x1 = f (x2 )
x2 = f [f (x2 )]
= c2 x2 (1 x2 )[1 cx2 (1 x2 )]

Ea h x2 is said to be a xed point of period 2. In general, if xp is a xed point of


period p , xp repeats after a set of p iterations of f. That is
f (p) (xp ) = f (f (f...p times...(xp ) = xp

(49)

This bifur ation of the attra tor at c = 3 is alled pit hfork bifur ation due to its
shape.
|f (f (xn ))| 1

(50)

This requires c 1 + 6 = 3.449489743. Then ea h bran h of xed points bifur ates


into two seperate bran hes. The points on these bran hes will be of period 4.
If c is further in reased, further bran hing o urs. Fixed points of period p give rise
to 2p bran hes. It is found that for c = 3.5699....., an innite number of bifur ations
o ur. In logisti map, xed points never repeat. The band of xed points forms a
ontinuum. Complete haos begins from this point. Thus bifur ation is the route to
haos for logisti equation.
The values of c at whi h bifur ations o ur be ome loser as period in reases. It
is found that the ratio of su essive spa ings of c onverges to a xed value
cp cp1
= 4.66920161........
p cp+1 cp

= lim

(51)

This dimensionless number, alled Feigenbaum number, is a universal onstant for all
maps whi h takes the route of bifur ation to haos.
Program
from pylab import*
def f( , x): return * x * (1 - x)
i, f,x0,n,g =2.9,3.655, 0.1,50,1000
i, f=input("range of ontrol parameter = ")
s=( f- i)/1000.0
L ,Lx = [,[
for in arange( i, f, s):
x = x0
for i in range(g): x = f( , x)
p = 0
while p < n:

2.7 Simulation of Kepler's First and se ond Laws

29

x = f( , x)
L .append( )
Lx.append(x)
p += 1
plot(L , Lx, ".")
xlabel("Control Parameter")
ylabel("Population")
show()

Logistic Map

1.0

Population

0.8
0.6
0.4
0.2
0.01.0

2.7

1.5

2.0

2.5
3.0
Control Parameter

3.5

4.0

Simulation of Kepler's First and se ond Laws

Aim

To write a omputer program in C language to simulate Kepler's First and se ond


Laws and exe ute it.

30

2 SIMULATION OF PHYSICAL PHENOMENA

Prin iple

Kepler's laws are a onsequen e of Newton's law of Gravitation.


GMm
F~ =
r
r2

(52)

r 2 = x2 + y 2
GM
r
~a =
r2
GMx
ax =
r3
GMy
ay =
r3
vx = vx0 + ax dt
vy = vy0 + ay dt
x = x0 + vx dt
y = y0 + vy dt

(53)

A eleration and its omponents are found as follows

(54)
(55)
(56)
(57)
(58)
(59)
(60)

We an express GM in terms of mean earth to sun distan e r = 1.496 1011 m=1


astronomi al unit(AU) and time in tropi al year 1y = 3.15569259747 107 s. As
earth's orbit is nearly ir ular (e entri ity=0.016), gravitational for e is ompletely
entripetal.
mr 2 =
GM =
=
=
=

GMm
r2
2 3
r
4 2 r 3
T2
4 2 (AU)3 /y 2
39.47841760(AU)3/y 2

(61)
(62)
(63)
(64)
(65)

First law states that the orbit is an ellipse with sun at one of the fo ii. An x-y plot
gives the shape of the orbit whi h an be seen to be an ellipse.
Se ond law states that areal velo ity is a onstant. If dA is the area swept by
radius ve tor ~r in time dt with an angular displa ement d, then
dA
1 2 d
=
r
dt
2 dt
1 2
r
=
2
1
rv
=
2
1q 2
=
(x + y 2 )(vx2 + vy2 )
2

A graph of t dA/dt an be seen to be a straight line parallel to time axis.

(66)

(67)
(68)

2.7 Simulation of Kepler's First and se ond Laws

31

Algorithm
(x0 , y0 ) is a point on the orbit at time t
(x, , y) is point on the orbit at time t + dt
(vx , vy ) is velo ity at time t
(ax , ay ) is a eleration at time t.
GM = 39.47841760, a onstant
1: Read x0 and vy
2: y0 0 and t 0
3:
4: vx0 0
5:
6: for i=0 p
to k do
r q(x20 + y02)
7:
8:

9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Choose x-axis as the line joining sun and planet at t=0

(vx2 + vy2 )

ax GMx0 /r 3
ay GMy0 /r 3
vx ax dt + vx
vy ay dt + vy
x vx dt + x0
y vy dt + y0
This plot is the path of the planet around sun
dA/dt 21 rv
t t + dt
if (y0 < 0 and y > 0) then break.

end if
end for
Plot y against x
Plot dA/dt against t
Stop

Program
from pylab import*
gm,k=39.4784176,10000
#gm=Gravitational onstant G.mass of sun M, a areal velo ity
x,y=zeros(k,'float'),zeros(k,'float')
t,a=zeros(k,'float'),zeros(k,'float')
#Choose x axis along the initial position of the planet and sun
# and velo ity along y-axis.
y[0,vx=0.0,0.0
x[0=input('Give initial distan e as multiple of mean sun-earth distan e Ro')
# Choose velo ity to be near to ir ular motion GMm/r^2=mv^2/r
print 'and velo ity of planet as multiple of Ro/year (<%f)'%(1.1*sqrt(gm/x[0))
vy=input(' ')

32

2 SIMULATION OF PHYSICAL PHENOMENA

dt=4*pi*x[0/(k*vy)
for i in range(k-1):
r=sqrt(x[i**2+y[i**2)
v=sqrt(vx**2+vy**2)
vx-=gm*x[i*dt/r**3
vy-=gm*y[i*dt/r**3
x[i+1=x[i+vx*dt
y[i+1=y[i+vy*dt
a[i=r*v/2
t[i+1=i*dt
if y[i<0 and y[i+1>0: break
figure(1)
title("Path of planet")
xlabel('x')
ylabel('y')
plot(x,y,'.')
figure(2)
title("Areal velo ity-time graph")
xlabel('Time')
ylabel('Areal velo ity')
plot(t,a,'.')
show()
Path of planet

Areal velocity-time graph

4.5

2.0

4.0

1.5

3.5

1.0

3.0
Areal velocity

0.5

0.0






2.5
2.0

0.5

1.5

1.0

1.0
0.5

1.5

2.0
2.5

2.8

2.0

1.5

1.0

0.5

0.0
x

0.5

1.0

1.5

2.0

0.00.0

0.5

1.0

1.5
Time

2.0

2.5

3.0

Simulation of Kepler's Third Law

Aim

To write a omputer program in C language to simulate Kepler's Third Law and


exe ute it.
Prin iple

Kepler's laws are a onsequen e of Newton's law of Gravitation.


GMm
F~ =
r
r2

(69)

2.8 Simulation of Kepler's Third Law

33

A eleration and its omponents are found as follows


r 2 = x2 + y 2
GM
~a =
r
r2
GMx
ax =
r3
GMy
ay =
r3
vx = vx0 + ax dt
vy = vy0 + ay dt
x = x0 + vx dt
y = y0 + vy dt

(70)
(71)
(72)
(73)
(74)
(75)
(76)
(77)

Expressing GM in terms of mean earth to sun distan e r = 1.4961011 m=1 astronomi al unit(AU) and time in tropi al year 1y = 3.15569259747107s, GM 39.4784176.
Also as earth's orbit is nearly ir ular (e entri ity=0.016), gravitational for e is ompletely entripetal.
mr 2 =
GM =
=
=
=

GMm
r2
2 3
r
4 2 r 3
T2
4 2 (AU)3 /y 2
39.47841760(AU)3/y 2

(78)
(79)
(80)
(81)
(82)

Third law states that period p and semi major axis a of the orbit are related as
p2 a3 .log p log a graph is a straight line with positive slope.
Algorithm
(x0 , y0 ) Point on the orbit at time t
(x, , y) Point on the orbit at time t + dt
(vx0 , vy0 ) velo ity of the planet at time t
(vx , vy ) velo ity at time t + dt
(ax , ay ) a eleration at time t
p orbital period
r distan e between sun and planet
gm = 39.47841760, a onstant
1:
2:

fun tion period(r0, v0 )


GM 39.47841760

34

2 SIMULATION OF PHYSICAL PHENOMENA

3:
4:
5:

(x0 , y0) (r0 , 0)


p
(vx0 , vy0 ) (0, 2GM/x0 )
The velo ity is hosen like this be ause for a stable losed orbit the total
energy must be negative.mv 2 /2 GMm/x0 < 0 or v 2 < 2GM/x0

6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:

for i=0 p
to k do

r (x2i + yi2 )
ax GM xi /r 3
ay GM yi /r 3
vx ax dt + vx
vy ay dt + vy
xi+1 vx dt + xi
yi+1 vy dt + yi
t t + dt
if (yi < 0 and yi+1 > 0) then

Break

pt

end if
end for
if ((xmax xmin ) > (ymax ymin )) then
a (xmax xmin )/2

else

a (ymax ymin )/2

end if
return p,a
end fun tion
for i=1 to 15 do
pi , ai =period(ri , vi )
end for
Plot p3 against a2
Stop

Program
from pylab import*
def period(r0,v0):
gm,k=39.4784176,10000
x,y=zeros(k,'float'),zeros(k,'float')
t=0.0
y[0,vx=0.0,0.0
x[0,vy=r0,v0
dt=4*pi*x[0/(k*vy)
for i in range(k-1):
r=sqrt(x[i**2+y[i**2)
v=sqrt(vx**2+vy**2)
vx-=gm*x[i*dt/r**3

2.8 Simulation of Kepler's Third Law

35

vy-=gm*y[i*dt/r**3
x[i+1=x[i+vx*dt
y[i+1=y[i+vy*dt
t+=dt
if y[i<0 and y[i+1>0:break
if (max(y)-min(y))>(max(x)-min(x)):a=(max(y)-min(y))/2
else:a=(max(x)-min(x))/2
return t,a
z=linspa e(0.5,20,20)
w=sqrt(2*39.4784176/z)
p,m=zeros(20,'float'),zeros(20,'float')
for j in range(20): p[j,m[j,=period(z[j,w[j)
lm,lp=3*log(m),2*log(p)
title("Semimajor axis-period graph")
xlabel('3 log(r)')
ylabel('2 log(T)')
plot(lm,lp)
show()

Semimajor axis-period graph

10

2 log(T)

2
4

2

2
3 log(r)

10

36

2 SIMULATION OF PHYSICAL PHENOMENA

TABLE OF SPECIAL FUNCTIONS


Table of Bessel Fun tions Jn (x) ( E n 10n )
n/x 0.50
1.00
1.50
2.00
1 2.42268E-01 4.40050E-01 5.57911E-01 5.76725E-01
2 3.06040E-02 1.14903E-01 2.32084E-01 3.52836E-01
3 2.56373E-03 1.95638E-02 6.09810E-02 1.28943E-01
4 1.60726E-04 2.47667E-03 1.17700E-02 3.39947E-02
5 8.05325E-06 2.49566E-04 1.79214E-03 7.04365E-03
6 3.36057E-07 2.09263E-05 2.27329E-04 1.20288E-03
7 1.20155E-08 1.50166E-06 2.46227E-05 1.73611E-04
8 3.75814E-10 9.41901E-08 2.32777E-06 2.20459E-05
9 1.04466E-11 5.24773E-09 1.95274E-07 2.48016E-06
10 2.61314E-13 2.62998E-10 1.47249E-08 2.50521E-07
Table of Legendre
n/x 0.10
2
-0.48500
3
-0.14750
4
0.337938
5
0.178829
6
-0.24882
7
-0.19949
8
0.180321
9
0.211388
10
-0.12212

2.50
4.97094E-01
4.46058E-01
2.16604E-01
7.37824E-02
1.94996E-02
4.22437E-03
7.77360E-04
1.22164E-04
1.73237E-05
2.20192E-06

Fun tions Pn (x) (E n 10n )


0.30
0.50
0.70
-0.36500
-0.12500
0.235000
-0.38250
-0.43750
-0.19250
0.072937
-0.28906
-0.41206
0.345386
0.089843
-0.36519
0.129181
0.323242
-0.12528
-0.22407
0.223145
0.150155
-0.23907
-0.07363
0.306704
0.063700
-0.26789
0.272060
0.251476
-0.18822
0.085805

3.00
3.39061E-01
4.86082E-01
3.09101E-01
1.32041E-01
4.30071E-02
1.14503E-02
2.43661E-03
4.76728E-04
8.21032E-05
1.26405E-05

0.90
0.715000
0.472500
0.207938
-0.04114
-0.24116
-0.36782
-0.409686
-0.36951
-0.263146

Index
areal velo ity, 30
astronomi al unit, 30
Bessel Fun tion, 3
ollo ation polynomial, 12, 13
Coulomb, 24
D.Lehmer, 8
e entri ity, 30, 33
ellipse, 30
hard sphere radius, 15

Runge-Kutta fourth order method, 11


semi major axis, 33
Simpson's 1/3 rule, 13
Taylor series, 9
trans endental equations, 9
Trapezoidal rule, 12
tropi al year, 30
van der Waal, 15
well depth, 15

interpolating polynomial, 1
Interpolation, 5
Kepler, 30, 32
Kepler's First and se ond Laws, 29
Lagrange, 1
Legendre Fun tion, 4
Lennard-Jone's potential, 15
magi numbers, 8
mean free path, 19, 22
Monte Carlo, 8
multipli ative ongruential re ursive method,
8
neutral atoms, 15
Newton's law of Gravitation, 30
Newton-Cotes Integration formulae, 12
orbit, 30, 34
Pauli repulsion, 15
probability, 22
probability density, 17
pseudo-random numbers, 8
quadrati , 13
random number generator, 22
Random walk, 19, 21
37

You might also like