Professional Documents
Culture Documents
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 . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
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
Aim
n
X
i=1
yi
(x xj )
(xi xj )
j=1,j6=i
do
n
Y
(1)
10:
11:
12:
13:
14:
15:
16:
product product
end if
end for
(x xj )
(xi xj )
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
Aim
To evaluate the Bessel Fun tion of any order n for any value of x.
UX
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
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
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
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))
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
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) =
(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
11:
12:
13:
14:
15:
16:
17:
18:
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
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.
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=
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)
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
Aim
(0,1)
......................
.................
............
...........
..........
........
........
.......
.......
......
......
......
.....
.....
.....
....
....
....
....
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
.
(0,0)
(1,1)
(1,0)
UX
UY
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
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
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
Aim
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)
(13)
10
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:
x x0 (f /g)
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
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
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.
dy
= f (x0 , y0).
dx
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:
end for
Print y0
End
Program
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
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
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)
(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 )
h
[f (x0 ) + 4f (x1 ) + f (x2 )]
3
(20)
h
[f (x1 ) + 4f (x2 ) + 2f (x3 ) + 4f (x4 ) + 2f (x5 ) + .... + f (xn+1 )]
3
(21)
Algorithm
14
Program
To nd
Rb
a
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
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)
16
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
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
17
Charge
-0.8476
0.4238
-1.0000
1.0000
1.0000
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
(25)
(26)
18
PL (x) = |L (x)|2 =
(27)
2
rAC
(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
(30)
.
Among these
ases, only the 4th one
ould show interferen
e pattern.
Algorithm
1:
2:
3:
4:
5:
6:
7:
8:
end for
Program
19
0.0000012
Intensity
0.0000010
0.0000008
0.0000006
0.0000004
0.0000002
0.0000000
1000
2.3
500
500
1000
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
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
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
45.4
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.4
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 > =
=
=
+
=
=
=
.
=
=
(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 >
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
Result
24.5
24.0
23.5
23.0
22.5
22.0
21.5
21.00
2.5
5000
10000
no:of trials
15000
20000
.......
.....
......
......
.......
......
.
.
.
.
.
.
.......
.......
.......
.......
.......
.......
.
.
.
.
.
.
..
.......
.......
........
.......
........
........
.
.
.
.
.
.
...
........
........
........
........
.........
........
.
.
.
.
.
.
.
.
.....
.........
.........
..........
..........
..........
...........
.
.
.
.
.
.
.
.
.
.
........
............
.............
............
...............
...............
.................
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.....................
................................................................................................................................................
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.
25
Prin iple
(36)
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
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)
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)
(45)
(46)
xn+1 xn
xn x < 1
n
f (xn ) xn
f (xn1 ) x < 1
n
df (xn )
< 1
dxn
xn =xn
(47)
(48)
28
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 )]
(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)
= 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:
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
Aim
30
Prin iple
(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)
(54)
(55)
(56)
(57)
(58)
(59)
(60)
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
(66)
(67)
(68)
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:
(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
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
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
Aim
(69)
33
(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:
34
3:
4:
5:
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
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
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()
10
2 log(T)
2
4
2
2
3 log(r)
10
36
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
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
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