Documente Academic
Documente Profesional
Documente Cultură
Zoltán Papp
Department of Physics
California State University Long Beach
Version 0.5
Contents
1 Introduction 1
1.1 The first Fortran95 code . . . . . . . . . . . . . . . . . . . . . 3
1.1.1 Assignments . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Evaluation of functions 7
2.1 Functions defined by Taylor series . . . . . . . . . . . . . . . . 7
2.2 Recursion relations . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3 Continued fractions . . . . . . . . . . . . . . . . . . . . . . . . 8
1
5.4 The LAPACK library . . . . . . . . . . . . . . . . . . . . . . . 36
5.5 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7 Numerical integration 55
7.1 Trapezoid rule and Romberg integration . . . . . . . . . . . . 55
7.2 Gaussian integration . . . . . . . . . . . . . . . . . . . . . . . 57
7.3 Clenshaw-Curtis integration . . . . . . . . . . . . . . . . . . . 58
7.4 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2
Abstract
Introduction
In this course we are going to learn how to solve problems in physics using
computers. Use of computers can tremendously increase your efficiency. We
will learn some programing, some numerical methods, how to apply them in
solving problems in physics, and finally, how to present them in a professional
way.
We will use Mac computers with Mac OSX operating system. They
are very powerful and the operating system is based on Unix, which is the
dominant operating system for supercomputers. Students should acquire
some knowledge on basic Unix commands.
Before doing anything we need to polish our Mac a little bit. We should
add the following lines to the .profile or .bashrc files. These files are in
the root directory of the users and it define the Unix environment. Here we
define the available CPU cores, add the local directory to the PATH, with
ulimit commands we stretch out the local Unix variables appropriate for
programming, and redefine a few frequently used Unix command for our
convenience.
Listing 1.1: The .profile file.
1
3 OMP_NUM_THREADS =4
4 export OMP_NUM_THREADS
5
6 PATH = $PATH :.
7
8 ulimit -c unlimited
1
9 ulimit -d unlimited
10 ulimit - Ss unlimited
11 ulimit -u 500
12 ulimit -n 10000
13
14 alias m = ’ more ’
15 alias l = ’ ls � - FG ’
16 alias la = ’ ls � - aFG ’
17 alias ll = ’ ls � - alFG ’
18 alias h = ’ history ’
19 alias vi = ’ vim ’
The Apple provides the Xcode environment, which, among others con-
tains well tuned BLAS and Lapack routines (we will study them later).
We are not going to use any specific working environment. We will use
Makefile, which works on any Unix system. The Xcode is available from
http : //developer.apple.com.
As programing language we will use Fortran95. Fortran is the dominant
language in scientific/technical computing. Although Fortran is the oldest
programing language, it kept developing, and now it has all the important
features of other modern languages. Considering the huge legacy in available
scientific software, the computing power, the simplicity and ease of use, no
other language come even close to Fortran95. We will use gfortran, a free
Fortran95 compiler from the GNU Foundation. It is available practically for
any platform. This class is not about a programing language. Therefore it
is recommended that the students make a quick surface scan on Fortran95.
We will learn the necessary programing tools as we go, but a quick reading
of some tutorial may be helpful. Good tutorials can be found at
http : //www.dmoz.org/Computers/P rogramming/Languages/F ortran/.
As text editor we will use the free TextWrangler from Bare Bones Software.
We have floating license for Maple11 from Maplesoft. We are going to
use Maple occasionally to display or cross check our results. We will also use
the free Plot software (http : //plot.micw.eu).
The ’lingua fanca’ of scientific publishing is LATEX. We will learn how to
present our results in a professional way with LATEX. A lot of useful material
can be found at
http : //www.dmoz.org/Computers/Sof tware/T ypesetting/T eX/LaT eX/
or at http : //www.tug.org/tutorials/tuginda.
All the software we are going to use are free software, except for the
2
Maple. It is highly recommended that student build up a similar working
environment on their laptop or home computer. This may serve well them
along their carrier. It is also highly recommended that student get acquainted
with the Numerical Recipes book. This book contains a huge selection of
numerical techniques and codes. The codes may not be the best ones, but
this book is an excellent first take book. A quick scan of this book would
provide an overview of existing methods and software.
4 PROG1 = ball
5
13 LDFLAGS = $ ( LIBS )
14
15 all : $ ( PROG1 )
16
17 $ ( PROG1 ): $ ( OBJS1 )
3
18 $ ( F95 ) $ ( LDFLAGS ) -o $@ $ ( OBJS1 )
19
20 clean :
21 rm -f $ ( PROG1 ) *.{ o , mod }
22
25 . f95 . o :
26 $ ( F95 ) $ ( F95FLAGS ) -c $ <
Our first Fortran95 code is shown below. The precision is defined in the
Module NumType. The constants parameters are defined in Module setup.
The implicit none statement ensures that all the variables has to be de-
fined. We can use do - end do structure for the loop, and exit if the next
height would be negative. Or we can use the do while - end do structure
as well as well.
Listing 1.3: Module NumType
1
2 module NumType
3
4 save
5 i n t e g e r , parameter :: dp = kind (1. d0 )
6 r e a l ( dp ) , parameter :: pi = 4* atan (1. _dp )
7 complex( dp ) , parameter :: iic = (0. _dp ,1. _dp )
8
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 r e a l ( dp ) , parameter :: g = 10. _dp
7 r e a l ( dp ) , parameter :: h0 = 20. _dp
8 r e a l ( dp ) , parameter :: v0 = 15. _dp
9 r e a l ( dp ) , parameter :: alpha = 30*( pi /180)
10
4
Listing 1.5: The ballpath.f95
1
3 program ballpath
4
5 use setup
6 i m p l i c i t none
7 r e a l ( dp ) :: t , dt , x , y , vx , vy , v0x , v0y , x0 , y0
8
9 t = 0. _dp
10 x0 = 0. _dp
11 y0 = h0
12 v0x = v0 * cos ( alpha )
13 v0y = v0 * sin ( alpha )
14 dt = 0.01 _dp
15
16 do w h i l e ( y >= 0. _dp )
17 vx = v0x
18 vy = v0y - g * t
19 x = v0x * t
20 y = y0 + v0y * t - g /2* t **2
21 print *, x, y
22 t = t + dt
23 end do
24
We can run the code by typing ball. The results are printed on the
screen, or we can redirect to the file 1.r by typing ball > 1.r. The Plot
provides the picture in Fig. 1.1, which, with little work, can be put in a much
nicer form.
1.1.1 Assignments
1. The transmission coefficient of an one-dimensional scattering is given
by
1 2
4
Γ
T = .
(E − E0 )2 + 14 Γ2
5
20
15
10
0 10 20 30
6
Chapter 2
Evaluation of functions
Probably the worst idea to calculate this polynomial term by term. Why?
In the Horner scheme the polynomial is written as
Assignments
1. Evaluate the exponential function based on the first 10 terms of the
Taylor expansion by direct calculation of the terms and by Horner
7
scheme. Take the values x = 0.01, x = 0.1, x = 1, x = 10, and
x = 100, and x = −0.01, x = −0.1, x = −1, x = −10, and x = −100.
Explain the findings.
Assignments
1. Write a Fortran95 code for calculating the eigenvalues of one-dimensional
harmonic oscillator. Test the results by comparing to the results of a
Mathematica or a Maple code.
8
Continued fraction representation can be achieved in many different ways.
Here is the classic is Euler’s algorithm
a0
a0 + a0 a1 + a0 a1 a2 + · · · + a0 a1 a2 · · · an = . (2.8)
a1
1−
a2
1 + a1 −
.
1 + a2 + . .
Let us apply this algorithm to the exponential function. Starting from a
Taylor series � n �
�∞ n �∞ �z
z
ez = 1 + =1+ (2.9)
n=1
n! n=1 i=0
i
we get
1
ez = z
(2.10)
1
1− z
z 2
1+ 1
− z
z 3
1+ −
2 .
1+ z
3
− ..
Assignments
1. Write a Fortran95 code for calculating the exponential function. Test
the result for various real and complex values. Compare the results
with the results based on the Taylor expansion.
9
Chapter 3
Integration of Ordinary
Differential Equations
dy(t)
= f (t, y(t)), (3.1)
dt
where t is a continuous parameter, the time in most of the cases, and f is an
array of functions. Even higher order differential equations can be reduced
to set of first order differential equations. If y is know at a given value of t,
let say we know y 0 = y(t = 0), then we have initial value problem. Starting
form y 0 we should determine y(t) at later t.
10
where ∆ is estimated from f and the derivatives of f . The workhorse of
differential equation solvers is the 4-th order Runge-Kutta method:
h = tn+1 − tn (3.3)
k 1 = hf (tn , y n ) (3.4)
1 1
k 2 = hf (tn + h, y n + k 1 ) (3.5)
2 2
1 1
k 3 = hf (tn + h, y n + k 2 ) (3.6)
2 2
k 4 = hf (tn + h, y n + k 3 ) (3.7)
1
y(tn+1 ) = y(tn ) + (k 1 + 2k 2 + 2k 3 + k 4 ) + O(h5 ). (3.8)
6
3.1.1 Pendulum
As an example we consider damped pendulum. The force perpendicular to
the string is provided by the corresponding component of the weight and the
friction, which is opposite to the velocity,
where θ is the angle from the vertical, m is the mass, l is the length, and µ
is the friction coefficient. The differential equation for θ is given by
d2 θ dθ
ml 2
= −mg sin θ − µl . (3.10)
dt dt
By introducing ω = dθ/dt, we can cast this second order differential equation
into a set of two first order equations
dθ
= ω (3.11)
dt
dω g µ
= − sin θ − ω. (3.12)
dt l m
The Fortran95 codes for pendulum are shown below. The results for
µ = 0 and µ = 0.1 are given in Figure 3.1
Listing 3.1: pend.f95
1
11
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 2
7 r e a l ( dp ) , parameter :: g = 10. _dp , lenght = 10. _dp , &
8 mass = 1. _dp
9 r e a l ( dp ) :: mu
10
13 program pendulum
14
15 use setup
16 i m p l i c i t none
17 r e a l ( dp ) , dimension ( n_eq ) :: y
18 r e a l ( dp ) :: y0 , v0 , t , tmax , dt
19
30 do w h i l e ( t <= tmax )
31 w r i t e ( u n i t = 3 ,fmt= ’ (2 f20 .10) ’) t , y (1)
32 c a l l rk4step (t ,y , dt )
33 end do
34
4 use setup
12
5 i m p l i c i t none
6 r e a l ( dp ) , i n t e n t ( inout ) :: t
7 r e a l ( dp ) , i n t e n t ( in ) :: dt
8 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( inout ) :: y
9 r e a l ( dp ) , dimension ( n_eq ) :: k1 , k2 , k3 , k4 , dy
10
11 call deriv (t , y, k1 )
12 call deriv ( t + dt /2 , y + dt /2* k1 , k2 )
13 call deriv ( t + dt /2 , y + dt /2* k2 , k3 )
14 call deriv ( t + dt , y + dt * k3 , k4 )
15 dy = dt /6*( k1 +2* k2 +2* k3 + k4 ) ! increment
16
17 t = t + dt ! update
18 y = y + dy
19
20 contains
21
24 r e a l ( dp ) , i n t e n t ( in ) :: t
25 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( in ) :: y
26 r e a l ( dp ) , dimension ( n_eq ) :: f
27
28 f (1) = y (2)
29 f (2) = -g / lenght * sin ( y (1)) - mu / mass * y (2)
30
13
0.4
0.2
-0.2
-0.4
0 10 20 30 40 50 60
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 4
7 r e a l ( dp ) , parameter :: g = 10.0 _dp , mass = 1.0 _dp , &
8 drag = 0.1 _dp
9
12 program projectile
13
14 use setup
15 i m p l i c i t none
14
16 r e a l ( dp ) , dimension ( n_eq ) :: y
17 r e a l ( dp ) :: x0 , y0 , v0 , t , alpha , dt
18
27 y (1) = x0
28 y (2) = mass * v0 * cos ( alpha /180* pi )
29 y (3) = y0
30 y (4) = mass * v0 * sin ( alpha /180* pi )
31
11 c a l l deriv (t , y, k1 )
12 c a l l deriv ( t + dt /2 , y + dt /2* k1 , k2 )
13 c a l l deriv ( t + dt /2 , y + dt /2* k2 , k3 )
15
14 c a l l deriv ( t + dt , y + dt * k3 , k4 )
15 dy = dt /6*( k1 +2* k2 +2* k3 + k4 ) ! increment
16
17 t = t + dt ! update
18 y = y + dy
19
20 contains
21
31 vx = y (2)/ mass
32 vy = y (4)/ mass
33 v = sqrt ( vx **2+ vy **2)
34
35 f (1) = vx
36 f (2) = - drag * v * vx
37 f (3) = vy
38 f (4) = - mass * g - drag * v * vy
39
3.1.3 Assignments
1. Calculate the relativistic free fall, i.e. the motion of a relativistic particle
under the influence of a constant force.
16
1.5
0.5
0 1 2 3 4 5 6
Figure 3.2: Projectile motion for v0 = 10m/s, and α = 30◦ and α = 45◦ .
17
Typical applications of the adaptive step size Runge-Kutta method are
planetary motion problems and scattering problems. Here the force is big,
and the change of the momentum is big, when the particles are close to each
other and small when they are far apart. So, when the particles are close
one need smaller step size, and when they are far apart, larger step size is
sufficient. The adaptive code deal with this situation effectively. Irrespective
of the starting step size, it finds the optimal step size very quickly and adapts
when needed.
The equation of the planetary motion is given by Newton
d2�r Mm
m 2
= −γ 3 �r, (3.14)
dt r
where m and M are the masses of the planet and the Sun and γ is the
gravitational constant. The vector �r points from the Sun to the planet. For
simplicity we assume that the Sun is much heavier, and we stick the center of
coordinate system to the Sun. We rewrite (3.14) into cartesian components,
and in first order differential equation form
dx
= vx (3.15)
dt
dy
= vy (3.16)
dt
dz
= vz (3.17)
dt
dvx M
= −γ x (3.18)
dt r3
dvy M
= −γ 3 y (3.19)
dt r
dvz M
= −γ 3 z (3.20)
dt r
(3.21)
We orientate the coordinate system such that �r(0) = �x0 , 0, 0� and �v (0) =
�0, vy0 , 0�.
Listing 3.5: Adaptive Runge-Kutta steping routine
1
18
4 use setup , only : dp , n_eq
5 i m p l i c i t none
6 r e a l ( dp ) , i n t e n t ( inout ) :: t , tau
7 r e a l ( dp ) , dimension ( n_eq ) :: y , y1 , y2
8 r e a l ( dp ) :: tsave , halftau , erat , tau_old
9 r e a l ( dp ) , parameter :: safe1 = 0.9 _dp , &
10 safe2 = 4. _dp , eps = 1. e -16 _dp , er = 1. e -10 _dp
11 i n t e g e r :: maxtry =100 , itry
12
13 tsave = t
14 do itry = 1 , maxtry
15 halftau = tau /2
16 y2 = y
17 t = tsave
18 c a l l rk4step (t , y2 , halftau )
19 c a l l rk4step (t , y2 , halftau )
20 y1 = y
21 t = tsave
22 c a l l rk4step (t , y1 , tau )
23 erat = maxval ( abs ( y1 - y2 ) / &
24 ( er *( abs ( y1 )+ abs ( y2 ))/2+ eps ))
25 tau_old = tau
26 tau = safe1 * tau_old * erat **( -0.2 _dp )
27 tau = max ( tau , tau_old / safe2 )
28 tau = min ( tau , safe2 * tau_old )
29 i f ( erat > 1 ) c y c l e
30 y = y2
31 exit
32 end do
33
4 use setup
5 i m p l i c i t none
6 r e a l ( dp ) , i n t e n t ( inout ) :: t
7 r e a l ( dp ) , i n t e n t ( in ) :: dt
19
8 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( inout ) :: y
9 r e a l ( dp ) , dimension ( n_eq ) :: k1 , k2 , k3 , k4 , dy
10
11 c a l l deriv (t , y, k1 )
12 c a l l deriv ( t + dt /2 , y + dt /2* k1 , k2 )
13 c a l l deriv ( t + dt /2 , y + dt /2* k2 , k3 )
14 c a l l deriv ( t + dt , y + dt * k3 , k4 )
15 dy = dt /6*( k1 +2* k2 +2* k3 + k4 ) ! increment
16
17 t = t + dt ! update
18 y = y + dy
19
20 contains
21
24 r e a l ( dp ) , i n t e n t ( in ) :: t
25 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( in ) :: y
26 r e a l ( dp ) , dimension ( n_eq ) :: f
27 r e a l ( dp ) :: r
28
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 6
20
7 r e a l ( dp ) , parameter :: grav = 6.673 e -11 _dp , &
8 mass_sun = 1.9891 e +30 _dp , &
9 mass_earth = 5.9736 e +24 _dp
10
13 program planet
14
15 use setup
16 i m p l i c i t none
17 r e a l ( dp ) , dimension ( n_eq ) :: y
18 r e a l ( dp ) :: x0 , y0 , z0 , vx0 , vy0 , vz0 , t , tmax , dt
19
27 t = 0. _dp
28 tmax = 7*365*24*60*60
29 dt = 7*24*60*60
30 y (1) = x0 ; y (2) = y0 ; y (3) = z0
31 y (4) = vx0 ; y (5) = vy0 ; y (6) = vz0
32
36 do w h i l e ( t <= tmax )
37 w r i t e ( u n i t = 3 ,fmt= ’ (2 f30 .0) ’) y (1) , y (2)
38 p r i n t ’ (2 f30 .0) ’ , y (1) , y (2)
39 c a l l ark4 (t ,y , dt )
40 end do
41
21
Assignments
1. Perform a numerical study of the celestial three-body problem. Con-
sider two stars with equal mass rotating around each others and a much
smaller planet moving towards the center of the mass of the two-star
system.
22
2x1011
1x1011
-1x1011
-2x1011
Figure 3.3: Planetary motion. The red line corresponds to the Earth, the
blue line with 10% higher , the green line with 20% higher, and the magenta
is 50% lower initial velocity.
23
Chapter 4
Approximation and
interpolation
The first few polynomials are T0 (x) = 1, T1 (x) = x, T2 (x) = 2x2 − 1. They
satisfy the recursion relation
The Chebyshev polynomial Tn (x) has n zeros in the interval [−1, 1] at points
� �
(k − 1/2)π
xk = cos , k = 1, 2, . . . n., (4.4)
n
24
and it has n + 1 extrema at points
� �
� kπ
xk = cos , k = 0, 1, 2, . . . n., (4.5)
n
and |Tn (x�k )| = 1. The Chebyshev polynomials also satisfy the discrete or-
thogonality relation
�N
0 if i �= j, i, j < n
Ti (xk )Tj (xk ) = (N + 1)/2 if i = j �= 0, i < n (4.6)
k=0 N +1 if i = j = 0 .
25
then for ck we have
yN +2 = yN +1 = 0 (4.15)
yk = 2xyk+1 − yk+2 + ck (4.16)
and
f (x) ≈ c0 + y1 x − y2 . (4.17)
Another nice property of the Chebyshev expansion is that its derivative
can easily be calculated
N
� −1
�
f (x) ≈ fN� (x) = a�i Ti (x), (4.18)
i=0
where a� (N + 1) = a� (N ) = 0, and
26
If f is real, then its zeros are also real. They can easily be selected by the
conditions that the eigenvalues of (4.20) are real. The roots can be polished
as follows. We pick three points in the neighborhood of the estimated zero,
z1 , z2 and z3 , and the corresponding values of the function are f1 , f2 and f3 .
The location of the zero is estimated by
4.1.4 Example
As test case we consider f (x) = x cos(x) in the [−π/4, 9π/4] interval. The
code finds the exact zeros at π/2 and 3π/2.
Listing 4.1: Module for Chebyshev expansion
1
2 module chebyshev
3
4 use numtype
5 i m p l i c i t none
6 i n t e g e r , parameter :: maxch = 50
7 r e a l ( dp ) , dimension (0: maxch ) :: cheb , chder
8 r e a l ( dp ) , dimension ( maxch ) :: z0
9 i n t e g e r :: iz0
10
11 contains
12
16 r e a l ( dp ) , e x t e r n a l :: func
17 i n t e g e r :: n
18 r e a l ( dp ) , dimension (0: maxch ) :: f , a
19 r e a l ( dp ) :: ya , yb , aa , bb , x , ss
20 i n t e g e r :: i , j
27
21
42 i n t e g e r :: n
43 r e a l ( dp ) :: ya , yb , a (0: maxch ) , der (0: maxch )
44 i n t e g e r :: j
45
55 function cheby (y ,a ,n , ya , yb ) r e s u l t ( t )
56 ! func ( y ) = sum_ { i =0}^ n a_i T_i ( x )
57
58 i m p l i c i t none
59 i n t e g e r :: n
60 r e a l ( dp ) :: y , ya , yb
28
61 r e a l ( dp ) :: a (0: maxch )
62 r e a l ( dp ) :: aa , bb , x , t , y0 , y1
63 i n t e g e r :: k
64
65 aa = ( yb - ya )/2; bb = ( yb + ya )/2
66 x = (y - bb )/ aa
67 y1 = 0. _dp ; y0 = a ( n )
68 do k = n -1 , 0 , -1
69 t = y1 ; y1 = y0
70 y0 = a ( k )+2* x * y1 - t
71 end do
72 t = y0 - x * y1
73
78 i n t e g e r :: n , iz0
79 r e a l ( dp ) , e x t e r n a l :: func
80 r e a l ( dp ) , dimension (0: maxch ) :: a
81 i n t e g e r :: j
82 r e a l ( dp ) , dimension ( maxch ) :: wr0 , wi0 , z0
83 r e a l ( dp ) :: ww (3 , maxch ) , ya , yb
84
88 iz0 = 0
89 do j = 1 , n
90 i f ( wi0 ( j ) == 0. _dp . and . &
91 ya <= wr0 ( j ) . and . wr0 ( j ) <= yb ) then
92 iz0 = iz0 +1; z0 ( iz0 ) = wr0 ( j )
93 end i f
94 end do
95
96 contains
97
98 subroutine boyd (n ,a , wr , wi )
99
100 i n t e g e r :: n , j , ie
29
101 r e a l ( dp ) :: a (0: maxch )
102 r e a l ( dp ) :: wr ( maxch ) , wi ( maxch )
103 i n t e g e r , parameter :: lwork =4* maxch
104 r e a l ( dp ) :: aamat ( maxch , maxch ) , &
105 work ( lwork ) , rwork ( lwork ) , &
106 vl (1) , vr (1)
107
118 ie = 0
119 c a l l dgeev ( ’n ’ , ’n ’ ,n , aamat , maxch , wr ,&
120 wi , vl ,1 , vr ,1 , work , lwork , rwork , ie )
121 i f ( ie /= 0 ) stop ’� boyd : � ie � /= � 0 � ’
122
129 r e a l ( dp ) , e x t e r n a l :: func
130 r e a l ( dp ) :: zz , dz , eps , z1 , z2 , z3 , &
131 f1 , f2 , f3 , a12 , a23 , a31
132 i n t e g e r :: i , maxf
133
134 z1 = zz + dz ; f1 = func ( z1 )
135 z2 = zz - dz ; f2 = func ( z2 )
136 z3 = zz ; f3 = func ( z3 )
137
138 do i = 1 , maxf
139 a23 = ( z2 - z3 )* f2 * f3
140 a31 = ( z3 - z1 )* f1 * f3
30
141 a12 = ( z1 - z2 )* f1 * f2
142 zz = ( z1 * a23 + z2 * a31 + z3 * a12 )/( a23 + a31 + a12 )
143 i f ( abs ( zz - z3 ) < eps ) e x i t
144 z1 = z2 ; f1 = f2
145 z2 = z3 ; f2 = f3
146 z3 = zz ; f3 = func ( z3 )
147 end do
148
Listing 4.2: Main program for Chebyshev expansion, location of zeros and
extrema
1
2 program chebytest
3
4 use chebyshev
5 i m p l i c i t none
6 r e a l ( dp ) :: chder2 (0: maxch )
7 i n t e g e r :: n , i , np
8 r e a l ( dp ) , e x t e r n a l :: func , dfunc
9 r e a l ( dp ) :: x , dx , ya , yb , eps , zz , dz
10
11 n = 13
12 ya = - pi /4
13 yb = 9* pi /4
14 c a l l chebyex ( func ,n , cheb , ya , yb )
15
16 np = 50
17 dx = ( yb - ya )/ np
18 do i = 1 , np +1
19 x = ya +( i -1)* dx
20 w r i t e (1 ,*) x ,0. _dp , func ( x ) , cheby (x , cheb ,n , ya , yb )
21 end do
22
31
27 zz = z0 ( i )
28 c a l l root_polish ( func , zz , dz , eps ,10)
29 p r i n t * , ’� f ( x ) � = � 0 � ’ ,i , z0 ( i ) , zz
30 end do
31
32 p r i n t * , ’� derivative � ’
33
37 do i = 1 , np +1
38 x = ya +( i -1)* dx
39 w r i t e (2 ,*) x ,0 , dfunc ( x ) , cheby (x , chder ,n -1 , ya , yb )
40 end do
41
52 function func ( x ) r e s u l t ( f )
53
58 f = x * cos ( x )
59
62 function dfunc ( x ) r e s u l t ( df )
63
32
67
68 df = cos ( x ) - x * sin ( x )
69
-2
-4
-6
0 2 4 6
Figure 4.1: The function f (x) = x cos(x) (blue solid line), its derivative
(blue broken line) and their Chebyshev approximations with n = 6 (red
lines). Approximations with higher n are not distinguishable from the origi-
nal functions. Note, that the derivative approximated with n = 5.
Assignments
1. In the analytic solution of a quantum mechanical one-dimensional prob-
lem with finite rectangular potential well we face with a solution of
nonlinear equation. Outline this problem and solve the equations nu-
merically.
33
Chapter 5
34
b1
b2
b = .. , (5.3)
.
bn
and
x1
x2
x = .. . (5.4)
.
xn
The most efficient way of solving (5.1) goes through LU decomposition.
Here A is written as a product of two matrices
A = L · U, (5.5)
where L is a lower triangular
α11 0 0 ... 0
α11 α22 0 . . . 0
L=
. . . . . . . . . . . . . . . . . . . . . . . . , (5.6)
αn1 αn2 αn3 . . . αnn
and U is upper triangular
β11 β12 β13 . . . β1n
0 β22 β23 . . . β2n
U=
. . . . . . . . . . . . . . . . . . . . . . . . (5.7)
0 0 0 . . . βnn
This decomposition is not unique, it can be make unique by imposing the
condition that αii = 1, i.e. all diagonal elements of L are equal to 1. Then
Eq. (5.1) becomes
A · x = (L · U) · x = L · (U · x) = b, (5.8)
and the original problem is replaced by two simple equations
L·y =b (5.9)
and
U · x = y. (5.10)
These equations can easily be solved by substitution. The inverse and deter-
minant can also easily be calculated.
35
5.3 Eigenvalue problems
Another class of linear algebraic problems are the eigenvalue problems
A · x = λx, (5.11)
A · x = λ B · x. (5.12)
5.5 Example
N. Zettili: Quantum Mechanics Concepts and Applications, Second Edition,
p 141, Problem 2.9
Consider the matrices
7 0 0
A = 0 1 −i , (5.13)
0 i −1
and
1 0 3
B = 0 2i 0 . (5.14)
i 0 −5i
36
1. Are A and B Hermitian? Calculate AB and BA and verify that
T r(AB) = T r(BA); then calculate [A, B] and verify that T r([A, B]) =
0.
Listing 5.1: Fortran95 code for solving the problem 2.9 from the Zettili book
1
2 program mtest
3
4 use NumType
5 i m p l i c i t none
6 complex( dp ) , parameter :: &
7 one =(1. _dp ,0. _dp ) , nul =(0. _dp ,0. _dp )
8 i n t e g e r , parameter :: ndim = 5
9 complex( dp ) , dimension ( ndim , ndim ) :: a , b , c , d , e , f
10 i n t e g e r , parameter :: lwork =5* ndim
11 complex( dp ) :: ss1 , ss2 , work ( lwork )
12 r e a l ( dp ) :: rwork (3* ndim ) , w ( ndim )
13 i n t e g e r :: i , j , n , nn , info , ipiv ( ndim )
14
22 p r i n t * , ’� � problem � 2.9 ’
23 p r i n t * , ’� matrix � A � ’
24 do i =1 ,3
25 p r i n t ’ (10 f7 .2) ’ ,a (i ,1:3)
37
26 end do
27 print *,’ ----------------------’
28 p r i n t * , ’� matrix � B � ’
29 do i =1 ,3
30 p r i n t ’ (10 f7 .2) ’ ,b (i ,1:3)
31 end do
32
33
47
61
62 nn =3
63 info =0
64 e (1:3 ,1:3)= a (1:3 ,1:3)
65 c a l l ZHEEV ( ’v ’ , ’u ’ ,nn ,e , ndim ,W , WORK , lwork , RWORK , INFO )
38
66 p r i n t * , info
67 do i =1 ,3
68 p r i n t * , ’� eigenvalues � & � eigenvectors � of � A : � ’ ,w ( i )
69 p r i n t * , e (1:3 , i )
70 end do
71 p r i n t * , ’� orthogonality � ’
72 do i =1 ,3
73 do j =1 ,3
74 p r i n t * ,i ,j , dot_product ( e (1:3 , i ) , e (1:3 , j ))
75 end do
76 end do
77 p r i n t * , ’� completeness � & � spectral � decomposition � ’
78 f (1:3 ,1:3)= nul
79 do i =1 ,3
80 do j =1 ,3
81 ss1 =0. _dp
82 do n =1 ,3
83 ss1 = ss1 + conjg ( e (i , n ))* e (j , n ) * w ( n )
84 end do
85 f (i , j )= ss1
86 end do
87 end do
88 do i =1 ,3
89 p r i n t ’ (10 f7 .2) ’ ,f (i ,1:3)
90 end do
91
98 p r i n t * , ’� matrix � U ^+ � ’
99 c (1:3 ,1:3)= conjg ( transpose ( e (1:3 ,1:3)))
100 do i =1 ,3
101 p r i n t ’ (10 f7 .2) ’ ,c (i ,1:3)
102 end do
103 f (1:3 ,1:3)= matmul ( c (1:3 ,1:3) , e (1:3 ,1:3))
104 p r i n t * , ’� matrix � U ^+ � U � ’
105 do i =1 ,3
39
106 p r i n t ’ (10 f7 .2) ’ ,f (i ,1:3)
107 end do
108 f (1:3 ,1:3)= matmul ( e (1:3 ,1:3) , c (1:3 ,1:3))
109 p r i n t * , ’� matrix � U � U ^+ � ’
110 do i =1 ,3
111 p r i n t ’ (10 f7 .2) ’ ,f (i ,1:3)
112 end do
113
121 p r i n t * , ’� inverse � of � U ’
122 info =0
123 c a l l zgetrf ( nn , nn ,e , ndim , ipiv , info )
124 i f ( info /= 0) stop ’� zgetrf � info � /= � 0 ’
125 c a l l zgetri ( nn ,e , ndim , ipiv , work , lwork , info )
126 i f ( info /= 0) stop ’� zgetri � info � /= � 0 ’
127 do i =1 ,3
128 p r i n t ’ (10 f7 .2) ’ ,c (i ,1:3)
129 end do
130 print *,’ ------------------’
131 do i =1 ,3
132 p r i n t ’ (10 f7 .2) ’ ,e (i ,1:3)
133 end do
134
Assignments
1. Consider a quantum mechanical harmonic oscillator. For simplicity
let us take the values h̄ = 1, m = 1 and ω = 0.5. Consider this
Hamiltonian on the harmonic oscillator basis belonging to Hamiltonian
with h̄ = 1, m = 1 and ω = 1. Consider the problem on a 10 × 10
basis. Determine the eigenvalues and the eigenvectors. Compare them
to the exact eigenvalues and eigenvectors.
40
Chapter 6
F(x) = 0, (6.1)
and
xn+1 = xn + δx. (6.5)
41
6.1.1 Numerical example
As an example we consider the following set of differential equations, the
Lorenz equations,
dx
= σ(y − x) (6.6)
dt
dy
= x(τ − z) − y (6.7)
dt
dz
= xy − βz, (6.8)
dt
where σ, τ and β are constants. This set of equations were constructed to
model the flow of air when there is a temperature gradient. The variable x
is proportional to the intensity of convective motion, y is proportional to the
temperature gradient, and z is proportional to the distortion of the vertical
temperature profile from linearity.
This relatively simple set of differential equations has a very rich mathe-
matical structure. For various set of parameters the solution exhibits chaotic
behavior. Here we are interested in equilibrium points where
σ(y − x)
ẋ = Fx = x(τ − z) − y = 0. (6.9)
xy − βz
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: nv = 3 , np = 3
7 r e a l ( dp ) :: a ( np )
8
42
10
11 program newton
12
13 use setup
14 i m p l i c i t none
15
16 r e a l ( dp ) :: x ( nv ) , dx ( nv ) , f ( nv ) , jmat ( nv , nv ) , ff
17 i n t e g e r :: info , i , ipiv ( nv ) , maxstep
18
22 maxstep = 8
23 p r i n t ’ (6(7 x , a )) ’ , ’x ’ , ’� y ’ , ’� z ’ , ’ f_1 ’ , ’ f_2 ’ , ’ f_3 ’
24
25 do i =1 , maxstep
26
27 c a l l func ( nv , x , f , jmat , ff )
28 p r i n t ’ (6 f10 .4) ’ ,x (1: nv ) , f (1: nv )
29 dx (1: nv )= - f (1: nv )
30 info =0
31 c a l l dgetrf ( nv , nv , jmat , nv , ipiv , info )
32 c a l l dgetrs ( ’n ’ ,nv ,1 , jmat , nv , ipiv , dx , nv , info )
33 i f ( info /= 0 ) stop ’� error � in � dgetrs � ’
34
37 end do
38
43 use setup
44 i m p l i c i t none
45 i n t e g e r :: n
46 r e a l ( dp ) :: x ( n ) , f ( n ) , deriv (n , n ) , ff
47
43
50 f (3) = x (1)* x (2) - a (3)* x (3)
51 ff = f (1)**2+ f (2)**2+ f (3)**2
52
44
2. Calculate x0 , the center of gravity of all points except xn+1 .
3. Reflection.
Compute reflected point xr = x0 + α(x0 − xn+1 ).
If the reflected point is better than the second worst, but not better
than the best, i.e.: f (x1 ) ≤ f (xr ) ≤ f (xn ), then obtain a new simplex
by replacing the worst point xn+1 with the reflected point xr , and start
all over again.
4. Expansion.
If the reflected point is the best point so far f (xr ) < f (x1 ), then we
may in the right direction and make sense to compute the expanded
point xe = x0 + γ(x0 − xn+1 ). If the expanded point is better than the
reflected point f (xe ) < f (xr ), then obtain a new simplex by replacing
the worst point xn+1 with the expanded point xe , and go to step 1.
Else obtain a new simplex by replacing the worst point xn+1 with the
reflected point xr , and go to step 1. Else (i.e. reflected point is worse
than second worst) continue at step 5.
5. Contraction
Here, it is certain that f (xr ) ≥ f (xn ). Compute contracted point
xc = xn+1 +ρ(x0 −xn+1 ). If the contracted point is better than the worst
point, i.e. f (xc ) ≤ f (xn+1 ), then obtain a new simplex by replacing
the worst point xn+1 with the contracted point xc , and go to step 1.
Else go to step 6.
6. Reduction
For all but the best point, replace the point with xi = x1 + σ(xi − x1 )
for all i ∈ {2, . . . , n + 1}, and go to step 1.
45
Concerning the contraction: If f (xr ) > f (xn ) we can expect that a better
value will be inside the simplex formed by all the vertices xi . The initial
simplex is important, indeed, a too small initial simplex can lead to a local
search, consequently the method can get more easily stuck. So this simplex
should depend on the nature of the problem.
Also the way we terminate the search may depend on the nature of the
problem. The present code terminates if |f (x1 ) − f (xn )| < � × (f (x1 ) −
f (xn )/2. Another possible condition is when max |x0 −xi | < � for i = 1, . . . n.
Listing 6.3: Subroutine for minimum search with the downhill simplex
method
1
15 use NumType
16 i m p l i c i t none
17 i n t e g e r :: n , iter , itmin
18 r e a l ( dp ) , e x t e r n a l :: func
19 r e a l ( dp ) :: xstart (1: n ) , fstart , stepi , epsf
20 r e a l ( dp ) , parameter :: alph =1. _dp , gamm =2. _dp , &
21 rho =0.5 _dp , sig =0.5 _dp
22 r e a l ( dp ) :: xi (1: n ,1: n +1) , x (1: n ,1: n +1) , &
23 fi (1: n +1) , f (1: n +1) , &
24 x0 (1: n ) , xr (1: n ) , xe (1: n ) , xc (1: n ) , &
25 fxr , fxe , fxc , deltaf
26 i n t e g e r :: i , j , ii , it
27
46
30 xi (1: n , i )= xi (1: n ,1)
31 xi (i -1 , i )= xi (i -1 , i )*(1+ stepi )
32 fi ( i )= func ( xi (1: n , i ))
33 end do
34
35 do it = 1 , iter
36
37 do i = 1 , n +1 ! ordering
38 ii = minloc ( fi (1: n +1) , dim =1)
39 x (1: n , i ) = xi (1: n , ii ); f ( i ) = fi ( ii )
40 fi ( ii ) = huge (0. _dp )
41 end do
42 xi (1: n ,1: n +1) = x (1: n ,1: n +1)
43 fi (1: n +1) = f (1: n +1)
44
47
70
79 else ! reduction
80 do i = 2 , n +1
81 xi (1: n , i ) = x (1: n ,1)+ &
82 sig *( x (1: n , i ) - x (1: n ,1))
83 fi ( i ) = func ( xi )
84 end do
85 cycle
86
87 end i f
88
89 end do
90
48
by minimizing the χ2 function
N �
� �2
2 yi − f (si , x)
χ (x) = .
i=1
σi
22
6.2.2 Fitting of the γ-ray spectrum of Na nuclei
The picture 6.1 shows the γ spectrum of 22 Na nuclei. The channel number i
is associated with the γ-ray energy and the yi -s are the counted photons. The
whole physical process is rather complicated. Here we want to determine the
number of photons in those prominent peaks. Therefore we fit data in the
i = [250, 900] interval with two gaussians on a linear background
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: nsp =1025 , npar =8
7 i n t e g e r :: minsp , maxsp , ifcal , yy ( nsp ) , iprint
8
11
12 program gamma_spectrum
13
14 use setup
15 i m p l i c i t none
16 i n t e g e r :: i , ii , iter , itmin
17 r e a l ( dp ) :: xstart (1: npar ) , fstart , stepi , epsx , epsf
18 r e a l ( dp ) , e x t e r n a l :: least2
19
49
23 read (2 ,*) ii
24 w r i t e (3 , ’ (2 i8 ) ’) i , ii
25 yy ( i )= ii
26 end do
27 c l o s e ( u n i t =2)
28 c l o s e ( u n i t =3)
29
30 minsp = 250
31 maxsp = 900
32 xstart (1: npar ) = (/ -0.1 , 50. , 3000. , 320. , 10. , &
33 300. , 750. , 10. /)
34 ifcal = 0
35 iprint = 1
36 fstart = least2 ( xstart (1: npar ))
37
38 itmin = 200
39 iter = 2000
40 epsf = 0.001 _dp
41 stepi = 0.1 _dp
42 iprint = 0
43
47 iprint =2
48 fstart = least2 ( xstart (1: npar ))
49
52
55 use setup
56 i m p l i c i t none
57 r e a l ( dp ) :: par ( npar ) ,a ,b , y1 , x1 , sig1 , y2 , x2 , sig2 , ss , fi
58 i n t e g e r :: i
59
60 ifcal = ifcal +1
61 a = par (1); b = par (2)
62 y1 = par (3); x1 = par (4); sig1 = par (5)
50
63 y2 = par (6); x2 = par (7); sig2 = par (8)
64
65 ss = 0. _dp
66 do i = minsp , maxsp
67 fi = ( a * i + b + y1 * exp ( -(( i - x1 )/ sig1 )**2) &
68 + y2 * exp ( -(( i - x2 )/ sig2 )**2))
69 ss = ss +1/ sqrt ( yy ( i )+1. _dp ) * ( yy ( i ) - fi )**2
70 end do
71 ss = ss / abs ( maxsp - minsp )
72 p r i n t ’( i5 ,8 f12 .4 , � f20 .5) ’ , ifcal , par (1: npar ) , ss
73 w r i t e (8 ,*) ifcal , ss
74
75 i f ( iprint /= 0 ) then
76 do i = minsp , maxsp
77 fi = ( a * i + b + y1 * exp ( -(( i - x1 )/ sig1 )**2) &
78 + y2 * exp ( -(( i - x2 )/ sig2 )**2))
79 w r i t e ( u n i t = iprint ,fmt= ’( i4 , i10 , f15 .2) ’) &
80 i , yy ( i ) , fi
81 end do
82 end i f
83
Asignments
1. We ignored the first part of the γ spectrum. Modify the fitting functions
such that it may incorporate the first part of the spectrum as well.
51
3000
2500
2000
1500
1000
500
22
Figure 6.1: γ-ray spectrum of Na.
52
3000
2500
2000
1500
1000
500
53
3000
2500
2000
1500
1000
500
54
Chapter 7
Numerical integration
Here
∆Ii = hi (f (xi ) + f (xi+1 ))/2, (7.2)
and hi is the length of the interval. This is a rather simple and not so
accurate method. However, sometimes mercury can be turned to gold, as it
has been showed by Romberg. In Romberg integration the interval is halved
repeatedly times. So we have a sequence I1/2 , I1/4 , I1/8 , I1/16 , and so on,
which we can approximate to I1/∞ . The Romberg integration is easy to use,
provide accurate results, and if the function evaluation is fast, this should be
the first choice.
Listing 7.1: Subroutine for Romberg integration
1
4 ! res = int_a ^ b f ( x ) dx
5 ! eps required accuracy
55
6 ! n n_max in approx ( input )
7 ! n accuaracy reached after n steps
8
16 h = b - a
17 np = 1
18 r (1 ,1) = h /2 * ( func ( a ) + func ( b ) )
19 res = r (1 ,1)
20
21 do i =2 , n
22 h = h /2
23 np = 2* np
24 sumt = 0.0 _dp
25 do k =1 ,( np -1) ,2
26 sumt = sumt + func ( a + k * h )
27 end do
28 r (i ,1) = 0.5 _dp * r (i -1 ,1) + h * sumt
29 m = 1
30 do j =2 , i
31 m = 4* m
32 r (i , j ) = r (i ,j -1)+( r (i ,j -1) - r (i -1 ,j -1))/( m -1)
33 end do
34 i f ( abs ( res - r (i , i )) < eps ) then
35 n = i
36 res = r (i , i )
37 return
38 end i f
39 res = r (i , i )
40 end do
41 p r i n t * , ’� romint � : ’ ,eps , r (i -1 ,i -1) , res
42
56
7.2 Gaussian integration
The most efficient, and theoretically the most sophisticated, method of nu-
merical integration is the Gauss method. We approximate the integral by
� b n
�
I= W (x)f (x)dx ≈ wi f (xi ). (7.3)
a i=1
Here W is the weight function, xi ’s are the abscissas and wi ’s are the weights.
The integration is exact for f (x) = P2n−1 (x), i.e. it is exact for polynomials
of rank less than 2n − 1.
The Gaussian quadrature is related to polynomials which are orthogonal
with respect to the weight function W
� b
Pi (x)W (x)Pj (x)dx = �Pi |Pj � = δij Nj . (7.4)
a
The xi abscissas are the ith roots of the polynomial Pn . The weights are
calculated by the formula
−an Nn
wi = . (7.5)
Pn� (xi )Pn+1 (xi )
were
Pn+1 (x)
an = lim . (7.6)
x→∞ xPn (x)
57
• Gauss-Jacobi: W (x) = (1 − x)α (1 + x)β , x ∈ (−1, 1)
(α,β) (α,β) (α,β)
cj Pj+1 = (dj + ej x)Pj − fj Pj−1 ,
(α,β) (α,β)
P0 = 1, P1 (x) = [2(α + 1) + (α + β + 2)(x − 1)]/2,
(α,β)
Pj is the Jacobi polynomial, and
We use d01bcf from the NAG library (look in the internet how to use).
The Gaussian integration methods are really very effective. But, the user
need to separate out a weight function W (x) and the hope that the remain-
ing f can be well approximated by polynomial. The abscissas and weights
are given to several important cases associated with famous orthogonal poly-
nomials. However, if the integrand is not one of those well-known cases,
the calculation of weights are rather complicated. Also the integral does
not provide error estimation. If we need higher accuracy we simply need to
recalculate everything.
58
• x ∈ [a, b], |ab| < ∞, interval :
y = cos(t) (7.11)
� b � � �
b−a 1 b−a b+a
I = f (x)dx = f y+ dy (7.12)
a 2 −1 2 2
� � �
b−a π b−a b+a
= f cos(t) + sin(t)dt (7.13)
2 0 2 2
• x ∈ [0, ∞) interval :
• x ∈ (−∞, ∞) interval :
59
The approximation means
N
�
I ≈ IN = wj f (L cot(tj )) (7.24)
j=1
tj = πj/(N + 1), j = 0, 1, . . . , N + 1 (7.25)
4L
wj = 2 (7.26)
sin (tj )(N + 1)(1 + δ0j )(1 + δN +1j )
8 use integr
9 i m p l i c i t none
10 i n t e g e r :: n , j , m
11 r e a l ( dp ) :: a , b , tj , ss
12 r e a l ( dp ) , dimension ( maxint ) :: x , w
13
14 do j =1 , n
15 tj =( j * pi )/( n +1)
16 x ( j )=( b - a )/2* cos ( tj )+( b + a )/2
17 ss =0. _dp
18 do m =1 , n
19 ss = ss + sin ( m * tj )*(1 - cos ( m * pi ))/ m
20 end do
21 w ( j )=( b - a )* sin ( tj )/( n +1)* ss
22 end do
23
26
60
31 ! x abscissas
32 ! w weights
33
34 use integr
35 i m p l i c i t none
36 i n t e g e r :: n , j , m
37 r e a l ( dp ) :: tj , ss , scale
38 r e a l ( dp ) , dimension ( maxint ) :: x , w
39
40 do j =1 , n
41 tj =( j * pi )/( n +1)
42 x ( j )= scale *( cot ( tj /2))**2
43 ss =0. _dp
44 do m =1 , n
45 ss = ss + sin ( m * tj )*(1 - cos ( m * pi ))/ m
46 end do
47 w ( j )=4* scale *&
48 sin ( tj )/((1 - cos ( tj ))**2*( n +1))* ss
49 end do
50
51 contains
52
53 function cot ( x )
54 use NumType
55 r e a l ( dp ) :: x , cot
56
7.4 Exercise
As an exercise we calculate
� 5
1
dx = 2 arctan(5) (7.27)
−5 1 + x2
61
by using Gauss-Legendre, Clenshaw-Curtis and Romberg integration. We
also calculate the integral
� ∞ 2
x exp(−x)
√ dx = 1.0684041171059840796 (7.28)
0 1+x
We determined the value by using Maple.
We can see that the Romberg procedure is able to provide accurate results,
but need way much more evaluations of the function than the Gauss or
Chebyshev integration. The Chebyshev is almost as good as the Gaussian
integration. But the Chebyshev is more general; it does not need a weight
function present in the integrand.
Listing 7.3: Test of integration routines
1
2 module integr
3
4 use NumType
5 i n t e g e r , parameter :: maxint = 300
6
9 program inttest
10
11 use integr
12 i m p l i c i t none
13 r e a l ( dp ) :: a , b , res , c , d , eps , scale
14 r e a l ( dp ) , dimension ( maxint ) :: x , w
15 i n t e g e r :: nint , ifail , i
16 r e a l ( dp ) , e x t e r n a l :: runge , fun
17
20 a = -5
21 b =5
22 nint =100
23 c =0
24 d =0
25 c a l l d01bcf (1 ,a ,b ,c ,d , nint ,w ,x , ifail )
26 i f ( ifail /= 0 ) stop ’� ifail � /= � 0 � ’
62
27
28 res = 0. _dp
29 do i =1 , nint
30 res = res + w ( i )* runge ( x ( i ))
31 end do
32 p r i n t * , ’� Gauss � ’ ,res ,2* atan (5. _dp ) , nint
33
34 nint =150
35 c a l l cc11 ( nint ,a ,b ,x , w )
36
52 nint =100
53 a =0. _dp
54 b =1. _dp
55 c =1. _dp
56 d =0. _dp
57 c a l l d01bcf ( -3 ,a ,b ,c ,d , nint ,w ,x , ifail )
58 i f ( ifail /= 0 ) stop ’� ifail � /= � 0 � ’
59 res = 0. _dp
60 do i =1 , nint
61 res = res + w ( i )* fun ( x ( i ))
62 end do
63 p r i n t * , ’� Gauss � � � � � � � � � � � ’ ,res , nint
64
65 nint =100
66 scale =1. _dp
63
67 c a l l cc0inf ( nint , scale ,x , w )
68
77 function runge ( x )
78
87 function fun ( x ) r e s u l t ( f )
88
Assignment
1. Represent the Hamiltonian
p2 1 2 2
H= + x + 2e−x (7.29)
2 2
on an appropriate basis and determine the first two eigenvalues up to
three digits accuracy.
64
Chapter 8
65
proj. We find two zeros, α1 = 16.162◦ and α2 = 68.806◦ . The corresponding
paths of the ball are shown in Fig. 8.1.
Listing 8.1: Runge-Kutta method for projectile motion with air drag
1
11 c a l l deriv (t , y, k1 )
12 c a l l deriv ( t + dt /2 , y + dt /2* k1 , k2 )
13 c a l l deriv ( t + dt /2 , y + dt /2* k2 , k3 )
14 c a l l deriv ( t + dt , y + dt * k3 , k4 )
15 dy = dt /6*( k1 +2* k2 +2* k3 + k4 ) ! increment
16
17 t = t + dt ! update
18 y = y + dy
19
20 contains
21
31 vx = y (2)/ mass
32 vy = y (4)/ mass
33 v = sqrt ( vx **2+ vy **2)
34
35 f (1) = vx
36 f (2) = - drag * v * vx
66
37 f (3) = vy
38 f (4) = - mass * g - drag * v * vy
39
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 4
7 r e a l ( dp ) , parameter :: g = 10.0 _dp , mass = 1.0 _dp , &
8 drag = 0.1 _dp , distance = 4. _dp
9 r e a l ( dp ) , parameter :: x0 = 0. _dp , y0 = 0. _dp , &
10 v0 = 10. _dp
11 i n t e g e r :: iw
12
15 program shoot
16
17 use setup
18 use chebyshev
19 i m p l i c i t none
20 r e a l ( dp ) , e x t e r n a l :: proj
21 r e a l ( dp ) :: alpha , yx , aa , bb , da , eps
22 i n t e g e r :: n , iz , maxf
23
24 aa = 0
25 bb = 90
26 iw = 0
27 n = 5
28 call chebyex ( proj , n , cheb , aa , bb )
29 call chebyzero (n , cheb , aa , bb , z0 , iz0 )
30
67
33 da = 0.1 _dp
34 do iz =1 , iz0
35 alpha = z0 ( iz )
36 iw =0
37 c a l l root_polish ( proj , alpha , da , eps , maxf )
38 iw = iz
39 yx = proj ( alpha )
40 p r i n t * , iz , alpha , yx
41 end do
42
45
48 use setup
49 i m p l i c i t none
50 r e a l ( dp ) :: alpha , proj
51 r e a l ( dp ) :: t , dt
52 r e a l ( dp ) , dimension ( n_eq ) :: y
53
54 t = 0. _dp
55 dt = 0.001 _dp
56 y (1) = x0
57 y (2) = mass * v0 * cos ( alpha /180* pi )
58 y (3) = y0
59 y (4) = mass * v0 * sin ( alpha /180* pi )
60
68
3
2.5
1.5
0.5
0 1 2 3 4
Figure 8.1: Path of the ball thrown with initial angles α1 = 16.162◦ and
α2 = 68.806◦ .
Assignment
1. Determine the shooting angle if the target is at �4, 2, 0�.
69
Chapter 9
70
In the example we take h̄ = 1, m = 1, and ω = 1. Fig. 9.1 shows an even
parity solution with energies E = 0.48, E = 0.49, E = 0.5, E = 0.51, and
E = 0.52. We get square integrable solution only at E = 0.5. So, this subtle
square integrability condition quantize the energy.
Listing 9.1: Runge-Kutta method for the one-dimensional harmonic oscillator
1
11 call deriv (x , y, k1 )
12 call deriv ( x + dx /2 , y + dx /2* k1 , k2 )
13 call deriv ( x + dx /2 , y + dx /2* k2 , k3 )
14 call deriv ( x + dx , y + dx * k3 , k4 )
15 dy = dx /6*( k1 +2* k2 +2* k3 + k4 ) ! increment
16
17 x = x + dx ! update
18 y = y + dy
19
20 contains
21
31 f (1) = y (2)
32 f (2) = -2* mass / hbar2 * &
33 ( energy - potential ( x ))* y (1)
34
71
35 end subroutine deriv
36
Listing 9.2: Code for quantizing the one dimensional harmonic oscilltor
1
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 2
7 r e a l ( dp ) , parameter :: hbar = 1. _dp , hbar2 = hbar **2 ,&
8 mass = 1. _dp , omega = 1. _dp , &
9 x0 = sqrt ( hbar /( mass * omega ))
10 r e a l ( dp ) :: energy
11
14 program hoout
15
16 use setup
17 i m p l i c i t none
18 r e a l ( dp ) :: x , dx , xmax , y ( n_eq )
19
24 x =0. _dp
72
25 y (1) = 1
26 y (2) = 0
27
28 do w h i l e ( x < xmax )
29
33 end do
34
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
0 0.5 1 1.5 2 2.5 3
73
9.2 Solution of the one-dimensional harmonic
oscillator
The Schrödinger equation of the one-dimensional harmonic oscillator has two
singular points. One at zero, the other one at infinity. At x = 0 the solution
should be regular. From parity requirement follows that for even parity states
ψ � (0) = 0 and for odd parity states ψ(0) = 0. At infinity the wave function
has a Gaussian falloff
� �
x2
lim ψ(x) ∼ exp − 2 . (9.3)
x→∞ 2x0
end � 2 �
� xmax x
ψ (xmax ) = − 2 exp − max . (9.5)
x0 2x20
Then we integrate the differential equation inward and vary the energy until
we meet the conditions at x = 0. In order that we can normalize the wave
function, we also integrate the |ψ|2 . The modified Runge-Kutta subroutine
is given in Table 9.3, the solution in Table 9.4. We calculated the first
three eigenstates end recovered the En = h̄ω(n + 1/2) exact result up to five
significant digits. The corresponding wave functions are in shown in Fig. 9.2.
Listing 9.3: Runge-Kutta method for the one-dimensional harmonic oscillator
with normalization
1
74
9 r e a l ( dp ) , dimension ( n_eq ) :: k1 , k2 , k3 , k4 , dy
10
11 call deriv (x , y, k1 )
12 call deriv ( x + dx /2 , y + dx /2* k1 , k2 )
13 call deriv ( x + dx /2 , y + dx /2* k2 , k3 )
14 call deriv ( x + dx , y + dx * k3 , k4 )
15 dy = dx /6*( k1 +2* k2 +2* k3 + k4 ) ! increment
16
17 x = x + dx ! update
18 y = y + dy
19
20 contains
21
31 f (1) = y (2)
32 f (2) = -2* mass / hbar2 * &
33 ( energy - potential ( x ))* y (1)
34 f (3) = -y (1)**2 ! normalization
35
75
49 end subroutine rk4step
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 3
7 r e a l ( dp ) , parameter :: hbar = 1. _dp , hbar2 = hbar **2 , &
8 mass = 1. _dp , omega = 1. _dp , &
9 x0 = sqrt ( hbar /( mass * omega ))
10 r e a l ( dp ) :: energy , xmax , dx , eps
11 r e a l ( dp ) , a l l o c a t a b l e , dimension (: ,:) :: wf
12 i n t e g e r :: imax
13
16 program ho1d
17
18 use setup
19 use chebyshev
20 i m p l i c i t none
21 r e a l ( dp ) :: eminx , emaxx , emin , emax , de , e0 , de0
22 r e a l ( dp ) , e x t e r n a l :: psi0
23 i n t e g e r :: nch , iz , i , maxf , izz
24
76
38 emin = eminx
39 emax = emin + de
40
47 do iz =1 , iz0
48 e0 = z0 ( iz )
49 c a l l root_polish ( psi0 , e0 , de0 , eps , maxf )
50 izz = izz +1
51 p r i n t * , izz , e0
52 c a l l wavef ( e0 , izz )
53 end do
54
55 emin = emax
56 emax = emin + de
57
58 end do
59
64 use setup
65 i m p l i c i t none
66 r e a l ( dp ) , i n t e n t ( in ) :: e
67 r e a l ( dp ) :: x , psi
68 r e a l ( dp ) , dimension ( n_eq ) :: y
69
70 energy = e
71 x = xmax
72 y (1) = exp ( -0.5 _dp *( x / x0 )**2)
73 y (2) = -( x / x0 **2)* y (1)
74 y (3) = 0. _dp
75 do w h i l e ( x > 0. _dp )
76 c a l l rk4step (x ,y , - dx )
77 end do
77
78 psi = y (1)* y (2)
79 p r i n t * ,e , psi
80
83 subroutine wavef (e , iz )
84
85 use setup
86 i m p l i c i t none
87 r e a l ( dp ) , i n t e n t ( in ) :: e
88 r e a l ( dp ) :: x , parity
89 r e a l ( dp ) , dimension ( n_eq ) :: y
90 i n t e g e r :: iz , i , imin
91
92 energy = e
93 x = xmax
94 y (1) = exp ( -0.5 _dp *( x / x0 )**2)
95 y (2) = -( x / x0 **2)* y (1)
96 y (3) = 0. _dp
97 do w h i l e ( x > 0. _dp )
98 c a l l rk4step (x ,y , - dx )
99 end do
100
101 x = xmax
102 i = imax +1
103 y (1) = exp ( -0.5 _dp *( x / x0 )**2) / sqrt (2* y (3))
104 y (2) = -( x / x0 **2)* y (1)
105 y (3) = 0. _dp
106 do w h i l e ( x > 0. _dp )
107 i =i -1
108 wf (i ,1)= x
109 wf (i ,2)= y (1)
110 w r i t e ( u n i t =20+ iz ,fmt= ’ (2 f15 .5) ’) x , y (1)
111 c a l l rk4step (x ,y , - dx )
112 end do
113 imin = i
114 i f ( abs ( y (1)) > abs ( y (2)) ) then
115 parity =1
116 else
117 parity = -1
78
118 end i f
119
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-6 -4 -2 0 2 4 6
Assignment
1. Determine the first five eigenvalues and eigenvectors of the Hamiltonian
p2 1 2 2
H= + x + 2e−x . (9.6)
2 2
79
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-6 -4 -2 0 2 4 6
80
The discontinuity of ψ and ψ � at x = 0 is a non-linear function of A and
B. We can solve the set of nonlinear equation by using Newton-Raphson
method.
We use units such that h̄ = 1 and m = 1. We consider the potential
Figure 9.4: .
81
11 call deriv (x , y, k1 )
12 call deriv ( x + dx /2 , y + dx /2* k1 , k2 )
13 call deriv ( x + dx /2 , y + dx /2* k2 , k3 )
14 call deriv ( x + dx , y + dx * k3 , k4 )
15 dy = dx /6*( k1 +2* k2 +2* k3 + k4 ) ! increment
16
17 x = x + dx ! update
18 y = y + dy
19
20 contains
21
32 f (1) = y (2)
33 f (2) = -2* mass / hbar2 * &
34 ( energy - potential ( x ))* y (1)
35
47 v = 1. _dp + v0 * tanh ( x )
48
82
Listing 9.6: Solution of the one-dimensional scattering problem
1
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 2
7 r e a l ( dp ) , parameter :: hbar = 1. _dp , hbar2 = hbar **2 , &
8 mass = 1. _dp , v0 = 1. _dp
9 r e a l ( dp ) :: energy , xmax , dx , eps
10
13 program st1d
14
15 use setup
16 i m p l i c i t none
17 complex( dp ) , dimension ( n_eq ) :: aa , aa1 , aa2 , &
18 ff0 , ff1 , ff2 , delta
19 complex( dp ) , dimension ( n_eq , n_eq ) :: jmat
20 complex( dp ) :: da
21 i n t e g e r :: maxstep , i , ipiv ( n_eq ) , info
22 r e a l ( dp ) :: k1 , k2 , r , t
23 r e a l ( dp ) , e x t e r n a l :: potential
24
34 maxstep = 10
35 do i =1 , maxstep
36
37 c a l l match0 ( aa , ff0 )
38 p r i n t ’ (8 f7 .3) ’ ,aa , ff0
39 aa1 (1)= aa (1)+ da ; aa1 (2)= aa (2)
83
40 c a l l match0 ( aa1 , ff1 )
41 aa2 (1)= aa (1); aa2 (2)= aa (2)+ da
42 c a l l match0 ( aa2 , ff2 )
43 jmat (: ,1)=( ff1 - ff0 )/ da ! derivative
44 jmat (: ,2)=( ff2 - ff0 )/ da
45
46 delta = - ff0
47 info =0
48 c a l l zgetrf ( n_eq , n_eq , jmat , n_eq , ipiv , info )
49 c a l l zgetrs ( ’n ’ , n_eq ,1 , jmat , n_eq , ipiv ,&
50 delta , n_eq , info )
51
57 end do
58
59 c a l l match0 ( aa , ff0 )
60 p r i n t ’ (8 f7 .3) ’ ,aa , ff0
61 c a l l wavef ( aa )
62
70 subroutine match0 ( aa , ff )
71
72 use setup
73 i m p l i c i t none
74 complex( dp ) , dimension ( n_eq ) :: aa , ff , y
75 complex( dp ) :: k , psi0m , psi0p , dpsi0m , dpsi0p
76 r e a l ( dp ) :: x
77 r e a l ( dp ) , e x t e r n a l :: potential
78
79 x = - xmax
84
80 k = sqrt (2* mass / hbar2 *( energy - potential ( x )))
81 y (1) = exp ( iic * k * x )+ aa (1)* exp ( - iic * k * x )
82 y (2) = iic * k * exp ( iic * k * x ) - iic * k * aa (1)* exp ( - iic * k * x )
83 do w h i l e ( x < 0. _dp )
84 c a l l rk4step (x ,y , dx )
85 end do
86 psi0m = y (1)
87 dpsi0m = y (2)
88
89 x = xmax
90 k = sqrt (2* mass / hbar2 *( energy - potential ( x )))
91 y (1) = aa (2)* exp ( iic * k * x )
92 y (2) = iic * k * aa (2)* exp ( iic * k * x )
93 do w h i l e ( x > 0. _dp )
94 c a l l rk4step (x ,y , - dx )
95 end do
96 psi0p = y (1)
97 dpsi0p = y (2)
98
113 x = - xmax
114 k = sqrt (2* mass / hbar2 *( energy - potential ( x )))
115 y (1) = exp ( iic * k * x )+ aa (1)* exp ( - iic * k * x )
116 y (2) = iic * k * exp ( iic * k * x ) - iic * k * aa (1)* exp ( - iic * k * x )
117 do w h i l e ( x < xmax )
118 w r i t e (1 ,*) x , dble ( y (1)) , dimag ( y (1))
119 c a l l rk4step (x ,y , dx )
85
120 end do
121
122 x = xmax
123 k = sqrt (2* mass / hbar2 *( energy - potential ( x )))
124 y (1) = aa (2)* exp ( iic * k * x )
125 y (2) = iic * k * aa (2)* exp ( iic * k * x )
126 do w h i l e ( x > - xmax )
127 w r i t e (2 ,*) x , dble ( y (1)) , dimag ( y (1))
128 c a l l rk4step (x ,y , - dx )
129 end do
130
2 A B psi_0 psi ‘ _0
3 0.000 0.000 1.000 0.000 -0.418 -0.020 0.080 -0.759
4 0.007 0.042 1.456 0.334 0.000 -0.000 -0.000 -0.000
5 0.007 0.042 1.456 0.334 -0.000 0.000 -0.000 -0.000
6 r = 0.00179584 t = 0.99820416 r + t = 1.00000000
Assignments
1. Calculate the transmission coefficients trough a potential barrier as a
function of energy. Determine the energy of resonances.
86
1.5
0.5
-0.5
-1
-1.5
-10 -5 0 5 10
87
Chapter 10
88
10.1 Runge-Kutta solution for bound states
We can reorganize (10.2) to the form
� �
d2 u 2µ h̄2 l(l + 1)
=− 2 E− − V (r) ul (r). (10.5)
dr2 h̄ 2µ r2
and
f 1 = y2 (10.7)
� 2 �
2µ h̄ l(l + 1)
f2 = − 2 E − − V (r) y1 . (10.8)
h̄ 2µ r2
We start from rmax distance, which is well outside the range of the potential.
We impose the boundary condition y1 (rmax ) = exp(−κrmax ) and y2 (rmax ) =
−κ exp(−κrmax ), and from rmax we integrate the differential equation inward
to zero. If the energy is a bond-state energy, the radial wave function u should
vanish at zero, u0 = y1 (0) = 0. So, the solutions are determined from the
condition y1 (0)(E) = 0, i.e. y1 (0), as a function of energy should be zero.
The mass of the α particle is mα = 4m, where m is the mass of the nu-
cleon. Therefore the reduced mass, in nucleon-mass units, µ = 2. The alpha
particles are spinless particles, therefore in the relative coordinate only even
partial waves are possible.
Below we show the Runge-Kutta stepping subroutine and the main code.
For zero search, we adopted the Chebyshev code, which we do not show here.
In the rk4step.f95 routine (Table 10.1), where we calculate the cen-
trifugal term, the 1/r2 term may become singular. to avoid this, we add a
89
small tiny value, which do not effect the physics, but save us from numerical
trouble.
Table 10.2 show the main routine for finding the eigenvalues and eigen-
states. In l = 0 angular momentum we found three eigenstates at E1 = −76.9
MeV, E1 = −29.0 MeV, and E1 = −1.62 MeV energies. The corresponding
eigenstates are shown in Fig. 10.1.
Listing 10.1: Runge-Kutta step for α − α bound states
1
11 call deriv (x , y, k1 )
12 call deriv ( x + dx /2 , y + dx /2* k1 , k2 )
13 call deriv ( x + dx /2 , y + dx /2* k2 , k3 )
14 call deriv ( x + dx , y + dx * k3 , k4 )
15 dy = dx /6*( k1 +2* k2 +2* k3 + k4 ) ! increment
16
17 x = x + dx ! update
18 y = y + dy
19
20 contains
21
31 f (1) = y (2)
32 f (2) = -2* xm / hbar2 *( energy &
90
33 - hbar2 /(2* xm )* l *( l +1)/ x **2 &
34 - potential ( x ) )* y (1)
35 f (3) = y (1)**2 ! normalization
36
2 module setup
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 3
7 r e a l ( dp ) , parameter :: hbar2 = 41.47 _dp , &
8 mass = 4.0 _dp , xm = mass * mass /( mass + mass )
9 i n t e g e r :: l
10
17 program bound
18
19 use setup
20 use chebyshev
91
21 i m p l i c i t none
22 r e a l ( dp ) :: eminx , emin , emaxx , emax , deltae , &
23 de , e0 , psi
24 r e a l ( dp ) , e x t e r n a l :: psi0
25 i n t e g e r :: nch , izz , i , maxf , n , nstep
26
27 l =0
28
42 e0 = eminx
43 do
44 psi = psi0 ( e0 )
45 ! print * , e0 , psi
46 i f ( psi /= xmax . or . e0 > emaxx ) e x i t
47 e0 = e0 + de
48 end do
49
50 eminx = e0
51 p r i n t * , eminx
52 deltae =( emaxx - eminx )/ nstep
53 p r i n t * , deltae
54
55 izz =0
56 do n = 1 , nstep
57
92
61 c a l l chebyex ( psi0 , nch , cheb , emin , emax )
62 c a l l chebyzero ( nch , cheb , emin , emax , z0 , iz0 )
63 p r i n t * , z0 (1: iz0 )
64
65 de =0.1 _dp
66 do i =1 , iz0
67 e0 = z0 ( i )
68 c a l l root_polish ( psi0 , e0 , de , eps , maxf )
69 psi = psi0 ( e0 )
70 izz = izz +1
71 p r i n t * , izz , e0
72 c a l l wavef ( e0 , izz )
73 end do
74
75 end do
76
81 use setup
82 i m p l i c i t none
83 r e a l ( dp ) , i n t e n t ( in ) :: e
84 r e a l ( dp ) :: x , psi , kappa
85 r e a l ( dp ) , dimension ( n_eq ) :: y
86
87 energy = e
88 kappa = sqrt (2* xm / hbar2 *( - energy ))
89
90 x = dx
91 y (1)= x **( l +1)
92 y (2)=( l +1)* x ** l
93 y (3)=0. _dp
94 do w h i l e ( x <= xmax . and . y (2) > 0. _dp )
95 c a l l rk4step (x ,y , dx )
96 end do
97 xmid = x
98 i f ( xmid >= xmax ) then
99 psi = xmax
100 return
93
101 end i f
102
103 x = xmax
104 y (1) = exp ( - kappa * x )
105 y (2) = - kappa * y (1)
106 y (3) = 0. _dp
107 do w h i l e ( x > xmid )
108 c a l l rk4step (x ,y , - dx )
109 end do
110 psi = y (2)
111
112 p r i n t * ,e , psi
113
116
126 energy = e
127 kappa = sqrt (2* xm / hbar2 *( - energy ))
128
129 x = xmax
130 y (1) = exp ( - kappa * x )
131 y (2) = - kappa * y (1)
132 y (3) = 0. _dp
133 do w h i l e ( x > xmid )
134 n = nint ( x / dx )
135 wf (n ,1)= x
136 wf (n ,2)= y (1)
137 c a l l rk4step (x ,y , - dx )
138 end do
139 y12 = y (1)
140 y32 = - y (3)
94
141
166
Assignments
1. Determine the bound states of the α − α system in l = 1, 2, 3, and4
angular momenta. The α particles are charged. Add a charge to the
system and repeat the analysis.
95
1
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
0 2 4 6 8 10
the phase shift. The asymptotic form of the wave function is given by
The scattering wave function is not square integrable. Yet, the probability
current cannot be singular, and therefore the wave function should be regular
at the origin ul (0) = 0. These condition determine the scattering phase shift
and wave function uniquely. The code is given in the Listing 10.3. We
calculated the l = 0 phase shifts in the previously considered α − α system
at E = 1MeV, E = 5MeV and E = 10MeV. We got phase shift values 1.608,
0.256 and 2.661 radians, respectively. The corresponding wave functions are
given in Fig. 10.2.
Listing 10.3: Scattering code
1
2 module setup
96
3
4 use NumType
5 i m p l i c i t none
6 i n t e g e r , parameter :: n_eq = 2
7 r e a l ( dp ) , parameter :: hbar2 = 41.47 _dp , &
8 mass = 4.0 _dp , xm = mass * mass /( mass + mass )
9 i n t e g e r :: l
10
17 program scattering
18
19 use setup
20 use chebyshev
21 i m p l i c i t none
22 r e a l ( dp ) :: dmin , dmax , de , delta0 , psi
23 r e a l ( dp ) , e x t e r n a l :: psi0
24 i n t e g e r :: nch , iz , i , maxf
25
26 l =0
27 energy = 5. _dp
28
36
40 nch =6
41 c a l l chebyex ( psi0 , nch , cheb , dmin , dmax )
42 c a l l chebyzero ( nch , cheb , dmin , dmax , z0 , iz0 )
97
43 p r i n t * , z0 (1: iz0 )
44
45 de =0.01 _dp
46 do iz =1 , iz0
47 delta0 = z0 ( iz )
48 c a l l root_polish ( psi0 , delta0 , de , eps , maxf )
49 psi = psi0 ( delta0 )
50 p r i n t * , ’� Delta = ’ , delta0
51 c a l l wavefunction ( delta0 )
52 end do
53
54
59 use setup
60 i m p l i c i t none
61 r e a l ( dp ) , i n t e n t ( in ) :: delta
62 r e a l ( dp ) :: x , psi , k
63 r e a l ( dp ) , dimension ( n_eq ) :: y
64
67 x = dx
68 y (1) = x **( l +1)
69 y (2) = ( l +1)* x ** l
70 do w h i l e ( x <= xmax . and . y (2) > 0. _dp )
71 c a l l rk4step (x ,y , dx )
72 end do
73 xmid = x
74 i f ( xmid >= xmax ) then
75 stop ’� xmax � is � too � small � ’
76 end i f
77
78 x = xmax
79 y (1) = sin ( k * x -l * pi /2 + delta )
80 y (2) = k * cos ( k * x -l * pi /2 + delta )
81 do w h i l e ( x > xmid )
82 c a l l rk4step (x ,y , - dx )
98
83 end do
84 psi = y (2)
85
86 p r i n t * , delta , psi
87
90
93 use setup
94 i m p l i c i t none
95 r e a l ( dp ) , i n t e n t ( in ) :: delta
96 r e a l ( dp ) :: x , k , y12 , y11
97 r e a l ( dp ) , dimension ( n_eq ) :: y
98 i n t e g e r :: n , i
99
102 x = xmax
103 y (1) = sin ( k * x -l * pi /2 + delta )
104 y (2) = k * cos ( k * x -l * pi /2 + delta )
105 do w h i l e ( x > xmid )
106 n = nint ( x / dx )
107 wf (n ,1) = x ; wf (n ,2) = y (1)
108 c a l l rk4step (x ,y , - dx )
109 end do
110 y12 = y (1)
111
99
123 wf (0: n ,2) = y12 / y11 * wf (0: n ,2)
124 n = nint ( xmax / dx )
125 do i = 0 , n
126 w r i t e (10 ,*) wf (i ,1) , wf (i ,2)
127 end do
128
0.5
-0.5
-1
0 10 20 30 40 50
Assignments
1. By examining the phase shift as a function of energy, determine the
resonance energies. The time delay is defined as
dδ(E)
τ= . (10.11)
dE
100
Plot the τ as a function of E up to 20 MeV and for various angular
momenta. At resonance energy the time delay peaks.
101