Sei sulla pagina 1di 105

Phys-562 Computational Physics

Course Lecture Notes

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

3 Integration of Ordinary Differential Equations 10


3.1 Runge-Kutta method . . . . . . . . . . . . . . . . . . . . . . . 10
3.1.1 Pendulum . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1.2 Projectile motion . . . . . . . . . . . . . . . . . . . . . 13
3.1.3 Assignments . . . . . . . . . . . . . . . . . . . . . . . . 16
3.1.4 Planetary motion and adaptive step size Runge-Kutta
method . . . . . . . . . . . . . . . . . . . . . . . . . . 17

4 Approximation and interpolation 24


4.1 Chebyshev approximation . . . . . . . . . . . . . . . . . . . . 24
4.1.1 Chebyshev Polynomials . . . . . . . . . . . . . . . . . . 24
4.1.2 Approximations by Chebyshev polynomials . . . . . . . 25
4.1.3 Zeros of Chebyshev approximated function . . . . . . . 26
4.1.4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 27

5 Methods in Linear Algebra 34


5.1 Fortran95 support . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.2 Solution of linear equations . . . . . . . . . . . . . . . . . . . 34
5.3 Eigenvalue problems . . . . . . . . . . . . . . . . . . . . . . . 36

1
5.4 The LAPACK library . . . . . . . . . . . . . . . . . . . . . . . 36
5.5 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

6 Zeros and minima of multivariable functions 41


6.1 Solving nonlinear equations by Newton method . . . . . . . . 41
6.1.1 Numerical example . . . . . . . . . . . . . . . . . . . . 42
6.2 Minimum search . . . . . . . . . . . . . . . . . . . . . . . . . 44
6.2.1 Fitting to experimental data . . . . . . . . . . . . . . . 48
6.2.2 Fitting of the γ-ray spectrum of 22 Na nuclei . . . . . . 49

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

8 Boundary Value Problems 65


8.1 Shooting method . . . . . . . . . . . . . . . . . . . . . . . . . 65

9 Schrödinger equation in one dimension 70


9.1 Quantization of the harmonic oscillator . . . . . . . . . . . . . 70
9.2 Solution of the one-dimensional harmonic oscillator . . . . . . 74
9.3 Scattering in one dimension . . . . . . . . . . . . . . . . . . . 80

10 Schrödinger equation in three spatial dimensions 88


10.1 Runge-Kutta solution for bound states . . . . . . . . . . . . . 89
10.2 α − α bound states . . . . . . . . . . . . . . . . . . . . . . . . 89
10.3 Scattering states . . . . . . . . . . . . . . . . . . . . . . . . . 95

2
Abstract

This tutorial is designed for use in the course PHYS-562 Computational


Physics class.
Chapter 1

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.

1.1 The first Fortran95 code


In our first Fortran95 code, let us consider the path of a ball thrown in the
air from height h with initial velocity �v0 in the direction which makes α angle
with the horizontal.
The Makefile in Fig. 1.2 describes the structure of the code. The codes
in OBJS1 are compiled together. The .o files were created from the .f95 file
by using the F95 command with flags F95FLAGS. In building the executable
code LDFLAGS were used, which contains the LIB library. These LDFLAGS
are optimal for the given processor architecture and uses the Apple’s vecLib
library from Xcode. By typing make we can produce the executable file.
Listing 1.2: Typical Makefile
1

2 OBJS1 = numtype . o ball . o


3

4 PROG1 = ball
5

6 F95 = gfortran - m64


7

8 F95FLAGS = - O3 - funroll - loops - march = native \


9 - fexternal - blas
10

11 LIBS = - framework vecLib


12

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

23 . SUFFIXES : $ ( SUFFIXES ) . f95


24

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

9 end module NumType

Listing 1.4: The setup module.


1

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

11 end module setup

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

25 end program ballpath

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

Figure 1.1: Results of the ballpath.f95 code.

Take the values E0 = 5 and Γ = 1, 0.5, 0.1, 0.01. Write a Fortran95


code for calculating T in the E = [0, 10] interval with appropriate step
size. Plot the results and write a short report in LATEX.

6
Chapter 2

Evaluation of functions

One of the main advantage of Fortran over other programming languages


that it has a lot of built-in functions. In fact all the elementary functions
are built in the Fortran and several non-elementary functions as well. These
functions are listed in any Fortran reference manual.
It may happen however, that the function we need is not provided by
Fortran. Then we have to calculate them somehow.

2.1 Functions defined by Taylor series


Sometimes the functions are defined by Taylor series or are given as a finite
rank polynomial
n

f (x) ≈ p(x) = ai xi = a0 + a1 x + a2 x2 + · · · + an xn . (2.1)
i=0

Probably the worst idea to calculate this polynomial term by term. Why?
In the Horner scheme the polynomial is written as

p(x) = a0 + x(a1 + x(a2 + · · · + x(an−1 + an x) · · · )), (2.2)

which can be evaluated from inside out.

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.

2.2 Recursion relations


Here are just a few examples of functions satisfying recursion relations:

(n + 1)Pn+1 (x) = (2n + 1)xPn (x) − nPn−1 (x), (2.3)


Tn+1 (x) = 2xTn (x) − Tn−1 (x), (2.4)
(n + 1)Lαn+1 (x) = (2n + α + 1 − x)Lαn (x) − (n + α)Lαn−1 (x), (2.5)
Hn+1 (x) = 2xHn (x) − 2nHn−1 (x), (2.6)

where P is the Legendre, T is the Chebyshev, L is the Laguerre, and H


is the Hermite polynomial if the 0-th term equals to 1 and the −1-th term
equals to 0. So, starting from these two initial values, we can evaluate these
orthogonal polynomials.

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.

2.3 Continued fractions


Continued fractions are defined by
b1
C = a0 + , (2.7)
b2
a1 +
b3
a2 +
.
a3 + . .
where {bi } and {ai } are series. The continued fractions may be finite or
infinite. They are extremely useful and efficient of representing various func-
tions. Most of the built-in functions in calculators and various computer
languages are evaluated by using continued fractions.

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.

2. Write a Fortran95 code for calculating the function


1+z
log . (2.11)
1−z
Test the result for various real and complex values. Compare the re-
sults with the results based on the Taylor expansion. Using this code
calculate the π.

9
Chapter 3

Integration of Ordinary
Differential Equations

A large class of problems in physics can be described via a relation between


an array of independent variables y and its derivative

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.

3.1 Runge-Kutta method


For solving the set of differential equation we discretize the variable t. Then
from value y(tn ) we calculate y(tn+1 ) by

y(tn+1 ) = y(tn ) + ∆, (3.2)

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,

Fθ = −mg sin θ − µlθ̇, (3.9)

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

= ω (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

11 end module setup


12

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

20 mu = 0.1 _dp ! friction coeff .


21

22 t = 0. _dp ! time to start


23 tmax = 60. _dp ! time to exit
24 dt = 0.001 _dp ! time step
25 y (1) = 30*( pi /180) ! initial angle
26 y (2) = 0. _dp ! initial angular speed
27 open( u n i t = 3 , f i l e = ’1 pend . dat ’ , &
28 action = ’ write ’ , s t a t u s = ’ replace ’)
29

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

35 end program pendulum

Listing 3.2: Runge-Kutta step routine for pendulum


1

2 subroutine rk4step (t ,y , dt ) ! 4 - th order Runge - Kutta step


3

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

22 subroutine deriv (t ,y , f ) ! derivative


23

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

31 end subroutine deriv


32

33 end subroutine rk4step

3.1.2 Projectile motion


In the projectile motion the force is given by

F� = −mg ŷ − ρv 2 v̂, (3.13)

where ρ is the coefficient of the drag force.


The Fortran95 codes for the projectile motion are shown below. The
results for ρ = 30◦ and µ = 45◦ are given in Fig. 3.2

13
0.4

0.2

-0.2

-0.4

0 10 20 30 40 50 60

Figure 3.1: Pendulum motion.

Listing 3.3: proj.f95


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 = 4
7 r e a l ( dp ) , parameter :: g = 10.0 _dp , mass = 1.0 _dp , &
8 drag = 0.1 _dp
9

10 end module setup


11

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

19 alpha = 30. _dp


20

21 t = 0. _dp ! time to start


22 x0 = 0. _dp
23 y0 = 0. _dp
24 v0 = 10. _dp
25 dt = 0.01 _dp
26

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

32 open( u n i t = 3 , f i l e = ’1 proj . dat ’ , &


33 action = ’ write ’ , s t a t u s = ’ replace ’)
34

35 do w h i l e ( y (3) >= 0. _dp )


36 w r i t e ( u n i t = 3 ,fmt= ’ (2 f20 .10) ’) y (1) , y (3)
37 c a l l rk4step (t ,y , dt )
38 end do
39

40 end program projectile

Listing 3.4: Runge-Kutta step routine for projectile motion


1

2 subroutine rk4step (t ,y , dt ) ! 4 - th order Runge - Kutta step


3

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
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 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

22 subroutine deriv (t ,y , f ) ! derivative


23

24 use setup , only : dp , n_eq , g , mass , drag


25 i m p l i c i t none
26 r e a l ( dp ) , i n t e n t ( in ) :: t
27 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( in ) :: y
28 r e a l ( dp ) , dimension ( n_eq ) :: f
29 r e a l ( dp ) :: vx , vy , v
30

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

40 end subroutine deriv


41

42 end subroutine rk4step

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.

2. Study the problem of a periodically driven pendulum. Add a periodic


external force to the equation for pendulum.

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◦ .

3.1.4 Planetary motion and adaptive step size Runge-


Kutta method
How we know if the 4-th order Runge-Kutta method provides the right solu-
tion? We don’t know. What we can do is that we try different time steps, and
if the result does not depend on the time step, then we are on safe ground.
Or we may try some technique which adapts the time step to the specific
problem. We may compare the outcome of a time step with two half steps.
If the difference is small, then we keep the result of the two half steps, and
continue with the same time step, or even larger step if the difference is really
small. If the difference is not small enough we reduce the time step and try
again. After a few try the method adapts itself to the problem and finds the
optimal time step. This method also works very well if f has some violent
change in some regions. Then the time step becomes small, while in other
regions it can go with large steps.

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

2 subroutine ark4 (t ,y , tau )


3

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

34 end subroutine ark4

Listing 3.6: Runge-Kutta steping routine for planetary motion


1

2 subroutine rk4step (t ,y , dt ) ! 4 - th order Runge - Kutta step


3

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

22 subroutine deriv (t ,y , f ) ! derivative


23

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

29 r = sqrt ( y (1)**2+ y (2)**2+ y (3)**2)


30 f (1) = y (4)
31 f (2) = y (5)
32 f (3) = y (6)
33 f (4) = - grav * mass_sun / r **3 * y (1)
34 f (5) = - grav * mass_sun / r **3 * y (2)
35 f (6) = - grav * mass_sun / r **3 * y (3)
36

37 end subroutine deriv


38

39 end subroutine rk4step

Listing 3.7: Main routine for planetary motion


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 = 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

11 end module setup


12

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

20 x0 = 1.496 e +11 _dp


21 y0 = 0. _dp
22 z0 = 0. _dp
23 vx0 = 0. _dp
24 vy0 = 0.5 _dp * 29.783 e +3 _dp
25 vz0 = 0. _dp
26

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

33 open( u n i t = 3 , f i l e = ’ earth . dat ’ , &


34 action = ’ write ’ , s t a t u s = ’ replace ’)
35

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

42 end program planet

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.

2. Study the classical helium atom.

22
2x1011

1x1011

-1x1011

-2x1011

-4x1011 -3x1011 -2x1011 -1x1011 0 1x1011

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

4.1 Chebyshev approximation


4.1.1 Chebyshev Polynomials
The Chebyshev polynomials are given by

Tn (x) = cos(n arccos x). (4.1)

The first few polynomials are T0 (x) = 1, T1 (x) = x, T2 (x) = 2x2 − 1. They
satisfy the recursion relation

Tn+1 (x) = 2xTn (x) − Tn−1 (x). (4.2)

They are orthonormal in the interval [−1, 1]



� 1 0 if i �= j
Ti (x)Tj (x) 
dx √ = π/2 if i = j = � 0 (4.3)
−1 1 − x2 

π if i = j = 0 .

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 .

4.1.2 Approximations by Chebyshev polynomials


If f (x) is an arbitrary function in the [−1, 1] interval, then the approximation
formula
�N
f (x) ≈ fN (x) = ai Ti (x) (4.7)
i=0

is exact at the zeros of TN (x), and coefficients are given by


N
2 − δ0i �
ai = f (xk )Ti (xk )
N + 1 k=0
N � � �� � �
2 − δ0i � (k + 1/2)π iπ(k + 1/2)
= f cos cos . (4.8)
N + 1 k=0 N +1 N +1

The Chebyshev approximation (4.7) can be evaluated by using Clenshaw


recurrence formula. If
N

f (x) ≈ fN (x) = ck Fk (x), (4.9)
k=0

and Fk satisfies a three-term recursion relation


Fn+1 (x) = α(n, x)Fn (x) + β(n, x)Fn−1 (x), (4.10)
then the approximation to f can be evaluated by a recursion. Define
yN +2 = yN +1 = 0 (4.11)
yk = α(k, x)yk+1 + β(k + 1, x)yk+2 + ck , (4.12)

25
then for ck we have

ck = yk − α(k, x)yk+1 − β(k + 1, x)yk+2 . (4.13)

Putting this relation back to (4.9), and reorganizing, we get

f (x) ≈ c0 F0 (x) + y1 F1 (x) + y2 β(1, x)F0 (x). (4.14)

In the case of Chebyshev expansion, T0 = 1, T1 = x, α(k, x) = 2x, and


β(k, x) = −1. Therefore

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

a�k−1 = (2kak + a�k+1 )/(1 + δ0k ). (4.19)

4.1.3 Zeros of Chebyshev approximated function


All roots of fN (x), both on and off the interval [−1, 1], are the eigenvalues
of the N × N A matrix whose elements are such that there is 1/2 in the off
diagonals, plus an extra 1/2 in A12 and plus −aj /aN , j = 0...N − 1, in the
last row. For N = 5 the matrix is given by
 
0 1 0 0 0
 1/2 0 1/2 0 0 
 
A=  0 1/2 0 1/2 0  . (4.20)

 0 0 1/2 0 1/2 
−a0 /2a5 −a1 /2a5 −a2 /2a5 −a3 /2a5 + 1/2 −a4 /2a5

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

z1 (z2 − z3 )f2 f3 + z2 (z3 − z1 )f3 f1 + z3 (z1 − z2 )f1 f2


z4 = . (4.21)
(z2 − z3 )f2 f3 + (z3 − z1 )f3 f1 + (z1 − z2 )f1 f2

Then we make a replacement z4 → z3 , z3 → z2 and z2 → z1 , and repeat until


|z4 − z3 | < � with some small �. You can see if, let say, f1 = 0, then z4 = z1 .
If the initial estimation for the zero is good, this procedure converges very
fast.

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

13 subroutine chebyex ( func ,n ,a , ya , yb )


14 ! func ([ ya , yb ]) = sum_ { i =0}^ n a_i T_i
15

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

22 i f ( n > maxch ) stop ’� � n � >� maxch � ’


23 aa = ( yb - ya )/2; bb = ( yb + ya )/2
24 do i = 0 , n
25 x = cos ( pi /( n +1)*( i +0.5 _dp ))
26 f ( i ) = func ( aa * x + bb )
27 end do
28 do j = 0 , n
29 ss = 0. _dp
30 do i = 0 , n
31 ss = ss + &
32 f ( i )* cos (( pi /( n +1))* j *( i +0.5 _dp ))
33 end do
34 a ( j ) = 2. _dp * ss /( n +1)
35 end do
36 a (0) = 0.5 _dp * a (0)
37

38 end subroutine chebyex


39

40 subroutine chebyderiv (a ,n , der , ya , yb ) !


41

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

46 der ( n ) = 0. _dp ; der (n -1) = 2* n * a ( n )


47 do j = n -1 , 1 , -1
48 der (j -1) = der ( j +1)+2* j * a ( j )
49 end do
50 der (0) = der (0)/2
51 der (0: n -1) = der (0: n -1)*2/( yb - ya )
52

53 end subroutine chebyderiv


54

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

74 end function cheby


75

76 subroutine chebyzero (n ,a , ya , yb , z0 , iz0 )


77

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

85 c a l l boyd (n ,a , wr0 , wi0 )


86 wr0 (1: n ) = wr0 (1: n )*( yb - ya )/2+( yb + ya )/2
87

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

108 i f ( abs ( a ( n )) == 0. _dp ) stop ’a ( n )=0 ’


109 aamat (1: n ,1: n ) = 0. _dp
110 aamat (1 ,2) = 1. _dp
111 do j = 2 , n -1
112 aamat (j ,j -1) = 0.5 _dp
113 aamat (j , j +1) = 0.5 _dp
114 end do
115 aamat (n ,1: n ) = -a (0: n -1)/(2* a ( n ))
116 aamat (n ,n -1) = aamat (n ,n -1) + 0.5 _dp
117

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

123 end subroutine boyd


124

125 end subroutine chebyzero


126

127 subroutine root_polish ( func , zz , dz , eps , maxf )


128

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

149 end subroutine root_polish


150

151 end module chebyshev

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

23 c a l l chebyzero (n , cheb , ya , yb , z0 , iz0 )


24 eps = 1. e -15 _dp
25 dz = 0.01 _dp
26 do i = 1 , iz0

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

34 c a l l chebyderiv ( cheb ,n , chder , ya , yb )


35 c a l l chebyderiv ( chder ,n -1 , chder2 , ya , yb )
36

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

42 c a l l chebyzero (n -1 , chder , ya , yb , z0 , iz0 )


43 do i = 1 , iz0
44 zz = z0 ( i )
45 c a l l root_polish ( dfunc , zz , dz , eps ,10)
46 p r i n t * , ’� df ( x )/ dx � = � 0 � ’ ,i , z0 ( i ) , zz ,&
47 cheby ( z0 ( i ) , chder2 ,n -2 , ya , yb )
48 end do
49

50 end program chebytest


51

52 function func ( x ) r e s u l t ( f )
53

54 use numtype , only : dp


55 i m p l i c i t none
56 r e a l ( dp ) :: x , f
57

58 f = x * cos ( x )
59

60 end function func


61

62 function dfunc ( x ) r e s u l t ( df )
63

64 use numtype , only : dp


65 i m p l i c i t none
66 r e a l ( dp ) :: x , df

32
67

68 df = cos ( x ) - x * sin ( x )
69

70 end function dfunc

-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

Methods in Linear Algebra

5.1 Fortran95 support


• definition: dimension(N,M) :: A, B
• basis operations: C = A + B, C = a · A,
element by element multiply: C = A · B
• matrix-vector, matrix-matrix functions:

– dot product(a(1 : n), b(1 : n)) = ni ai bi

– matmul(A, B) = k Aik Bkj
– minval/maxval: minimal/maximal value of an array
– minloc/maxloc: location of the minimal/maximal value
– transpose, conjg

5.2 Solution of linear equations


The basic numerical problem is solving the linear equation
A · x = b, (5.1)
where  
a11 a12 . . . a1n
 a11 a12 . . . a1n 
A= 
. . . . . . . . . . . . . . . . . . , (5.2)
an1 an2 . . . ann

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)

or the generalized eigenvalue problems

A · x = λ B · x. (5.12)

5.4 The LAPACK library


There are couple of things people dealing with computational physics should
not do. They should not develop new processors, operating systems, new
compilers, and they should not fabricate their own code for solving linear
equations and eigenvalue problems. At least not for not too big matrices.
They are all done by professionals who created the LAPACK library. LA-
PACK (Linear Algebra PACKage) is the successor of LINPACK and EIS-
PACK. It is based on BLAS (Basic Linear Algebra Subroutines), it is a free
software and available for all platforms. Tutorials and user guides are avail-
able everywhere. The LAPACK and BLAS is fully contained in Apple’s
vecLib, availble on every Mac. LAPACK also provides sample codes. If you
need something linear algebra, just consult with the LAPACK website.

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.

2. Find the eigenvalues and the normalized eigenvectors of A. Verify that


the sum of the eigenvalues of A is equal to the value of T r(A) and the
eigenvectors form a basis.

3. Verify that U † AU is diagonal and U −1 = U † , where U is the matrix


formed by the normalized eigenvectors of A.

4. Calculate the inverse of A� = U † AU and verify that A�−1 is a diagonal


matrix whose eigenvalues are the inverse of those of A� .

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

15 a (1:3 ,1:3) = reshape ( (/ 7* one , nul , nul , &


16 nul , one , iic , &
17 nul , -iic , - one /) , (/ 3 , 3/))
18 b (1:3 ,1:3) = reshape ( (/ one , nul , iic , &
19 nul , 2* iic , nul , &
20 3* one , nul , -5* iic /) , (/ 3 , 3/))
21

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

34 print *,’ ----------------------’


35 p r i n t * , ’� � A ^+ � -� A � ’
36 c (1:3 ,1:3)= conjg ( transpose ( a (1:3 ,1:3)))
37 do i =1 ,3
38 p r i n t ’ (10 f7 .2) ’ ,c (i ,1:3) - a (i ,1:3)
39 end do
40 print *,’ ----------------------’
41 d (1:3 ,1:3)= conjg ( transpose ( b (1:3 ,1:3)))
42 p r i n t * , ’� B ^+ � -� B � ’
43 do i =1 ,3
44 p r i n t ’ (10 f7 .2) ’ ,d (i ,1:3) - b (i ,1:3)
45 end do
46

47

48 print *,’ ----------------------’


49 p r i n t * , ’� � [A , B ] � ’
50 c (1:3 ,1:3)= matmul ( a (1:3 ,1:3) , b (1:3 ,1:3))
51 d (1:3 ,1:3)= matmul ( b (1:3 ,1:3) , a (1:3 ,1:3))
52 do i =1 ,3
53 p r i n t ’ (10 f7 .2) ’ ,c (i ,1:3) - d (i ,1:3)
54 end do
55 ss1 = nul ; ss2 = nul
56 do i =1 ,3
57 ss1 = ss1 + c (i , i ); ss2 = ss2 + d (i , i )
58 end do
59 p r i n t * , ’� tr ( AB ) � & � tr ( BA ) � ’ ,ss1 , ss2
60

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

92 print *,’ ----------------------’


93 p r i n t * , ’� matrix � U � ’
94 do i =1 ,3
95 p r i n t ’ (10 f7 .2) ’ ,e (i ,1:3)
96 end do
97

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

114 f (1:3 ,1:3)= matmul ( a (1:3 ,1:3) , e (1:3 ,1:3))


115 d (1:3 ,1:3)= matmul ( c (1:3 ,1:3) , f (1:3 ,1:3))
116 p r i n t * , ’� matrix � U ^+ � A � U � ’
117 do i =1 ,3
118 p r i n t ’ (10 f7 .2) ’ ,d (i ,1:3)
119 end do
120

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

135 end program mtest

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

Zeros and minima of


multivariable functions

6.1 Solving nonlinear equations by Newton


method
We consider a set of, not necessarily linear, equation

F(x) = 0, (6.1)

where x is the vector of independent variables. We approximate F(xn+1 ) by


a Taylor series around F(xn )

F(xn+1 ) = F(xn ) + J(xn )δxn + O(δx2 ), (6.2)

where δxn = xn+1 − xn and


� �
∂Fi
(J(x))ij = (x). (6.3)
∂xj

In Newton, or Newton-Raphson, method we neglect the higher terms and


assume that F(xn+1 ) = 0. Then, by rearranging, we get

J(xn )δxn = −F(xn ), (6.4)

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

For the parameters we take a(1) = σ = 28, a(2) = τ = 10 and a(3) =


β = 8/3. The Fortran code is given in Fig. 6.1, the results are shown in Fig.
6.2. We cans see that the convergence is very fast. This is a nice property of
the Newton-raphson method. However, the method often fails to converge.
Listing 6.1: Fortran code for finding the stationary point of the Lorenz model
by using Newton-Raphson method
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 :: nv = 3 , np = 3
7 r e a l ( dp ) :: a ( np )
8

9 end module setup

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

19 a (1: np ) = (/ 28. _dp , 10. _dp , 8. _dp /3 /)


20 x (1: nv ) = (/ 40. _dp , 60. _dp , 50. _dp /)
21

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

35 x (1: nv )= x (1: nv )+ dx (1: nv )


36

37 end do
38

39 end program newton


40

41 subroutine func ( n , x , f , deriv , ff )


42

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

48 f (1) = a (2)*( x (2) - x (1))


49 f (2) = a (1)* x (1) - x (2) - x (1)* x (3)

43
50 f (3) = x (1)* x (2) - a (3)* x (3)
51 ff = f (1)**2+ f (2)**2+ f (3)**2
52

53 deriv (1 ,1) = -a (2) ! df_1 / dx_1


54 deriv (2 ,1) = a (1) - x (3) ! df_1 / dx_2
55 deriv (3 ,1) = x (2) ! df_1 / dx_3
56 deriv (1 ,2) = a (2) ! df_2 / dx_1
57 deriv (2 ,2) = -1 ! df_2 / dx_2
58 deriv (3 ,2) = x (1) ! df_2 / dx_3
59 deriv (1 ,3) = 0 ! df_3 / dx_1
60 deriv (2 ,3) = -x (1) ! df_3 / dx_2
61 deriv (3 ,3) = -a (3) ! df_3 / dx_3
62

63 end subroutine func

Listing 6.2: Convergence of the Newton-Raphson method for the Lorenz


model
1 x y z f_1 f_2 f_3
2 40.0000 60.0000 50.0000 200.0000 -940.0000 2266.6667
3 24.9508 24.9508 35.6533 -0.0000 -215.9068 527.4647
4 14.1190 14.1190 30.7566 0.0000 -53.0398 117.3276
5 9.7198 9.7198 28.1705 0.0000 -11.3769 19.3528
6 8.5825 8.5825 27.1370 0.0000 -1.1755 1.2935
7 8.4861 8.4861 27.0015 0.0000 -0.0131 0.0093
8 8.4853 8.4853 27.0000 0.0000 -0.0000 0.0000
9 8.4853 8.4853 27.0000 0.0000 0.0000 0.0000

6.2 Minimum search


Another important numerical problem is finding the minima of a multivari-
able function. Some methods need derivatives, some get along without them.
A particular robust method is the Nelder-Mead or downhill simplex method.
We are looking for the minima of an n variable function. We start from a
simplex, a set of n + 1 points, where no three points are on a line. Then we
perform the following steps.
1. Order according to the values at the vertices:
f (x1 ) ≤ f (x2 ) . . . ≤ f (xn+1 )

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.

Note: α, γ, ρ and σ are respectively the reflection, the expansion, the


contraction and the shrink coefficient. Standard values are α = 1, γ = 2,
ρ = 1/2 and σ = 1/2.
For the reflection, since xn+1 is the vertex with the higher associated value
among the vertices, we can expect to find a lower value at the reflection of
xn+1 in the opposite face formed by all vertices point xi except xn+1 .
For the expansion: if the reflection point xr is the new minimum along the
vertices we can expect to find interesting values along the direction from x0
to xr .

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

2 subroutine downhill (n , func , xstart , fstart , &


3 stepi , epsf , itmin , iter )
4 !
5 ! n dimension of the problem
6 ! func function
7 ! xstart starting values
8 ! fstart conrespoding function value
9 ! stepi relative stepsize for initial simplex
10 ! epsf esilon for termination
11 ! itmin termination istested if itmin < it
12 ! iter maximum number of iterations
13 !
14

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

28 xi (1: n ,1) = xstart (1: n ); fi (1) = fstart


29 do i = 2 , n +1

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

45 x0 (1: n ) = sum ( x (1: n ,1: n ) , dim =2)/ n ! central


46

47 i f ( itmin < it ) then ! exit


48 deltaf = ( f ( n ) - f (1))
49 w r i t e (7 ,*) it , deltaf
50 i f ( deltaf < epsf ) e x i t
51 end i f
52

53 xr (1: n ) = x0 (1: n )+ alph *( x0 (1: n ) - x (1: n , n +1))


54 fxr = func ( xr )
55 i f ( fxr < f ( n ) . and . & ! reflection
56 f (1) <= fxr ) then
57 xi (1: n , n +1) = xr (1: n ); fi ( n +1) = fxr
58 cycle
59

60 e l s e i f ( fxr < f (1) ) then ! expansion


61 xe (1: n ) = x0 (1: n )+ gamm *( x0 (1: n ) - x (1: n , n +1))
62 fxe = func ( xe )
63 i f ( fxe < fxr ) then
64 xi (1: n , n +1) = xe (1: n ); fi ( n +1) = fxe
65 cycle
66 else
67 xi (1: n , n +1) = xr (1: n ); fi ( n +1) = fxr
68 cycle
69 end i f

47
70

71 e l s e i f ( fxr >= f ( n ) ) then ! contraction


72 xc (1: n ) = x (1: n , n +1)+ rho *( x0 (1: n ) - x (1: n , n +1))
73 fxc = func ( xc )
74 i f ( fxc <= f ( n +1) ) then
75 xi (1: n , n +1) = xc (1: n ); fi ( n +1) = fxc
76 cycle
77 end i f
78

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

91 xstart (1: n )= xi (1: n ,1); fstart = fi (1)


92

93 end subroutine downhill

6.2.1 Fitting to experimental data


In physics we collect experimental data. Then, to explain them, we construct
some theory. Theory always rely on some assumptions and often contain
some parameters which has to be determined from experiment. In other
word, we have to fit our theory to the experiment.
We denote our experimental data by yi , i = 1, . . . N , and the correspond-
ing errors or accuracy limits by σi . If the experimental data follow random

distribution σi = σ � + yi is the usual choice, where σ � is the experimental
accuracy. We assume that the data depend on parameter s. Our theory
besides s, has further unknown parameters xj , j = 1, . . ., and predict the
physical values as f (s, x). Here x denotes the parameter vector. We try to
determine those unknown parameters by fitting f to the experimental data

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

f (s) = as + b + y1 e−(s−x1 )/σ1 + y2 e−(s−x2 )/σ2 .

The code is given below.


Listing 6.4: The main code for the fitting of the γ spectrum.
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 :: nsp =1025 , npar =8
7 i n t e g e r :: minsp , maxsp , ifcal , yy ( nsp ) , iprint
8

9 end module setup


10

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

20 open( u n i t =2 , f i l e = ’ na22 . dat ’)


21 open( u n i t =3 , f i l e = ’ na22 . d ’)
22 do i = 1 , 1025

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

44 c a l l downhill ( npar , least2 , xstart , fstart , &


45 stepi , epsf , itmin , iter )
46

47 iprint =2
48 fstart = least2 ( xstart (1: npar ))
49

50 end program gamma_spectrum


51

52

53 function least2 ( par ) r e s u l t ( ss )


54

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

84 end function least2

We may start with initial parameters a = −1, b = 50, y1 = 3000, x1 =


320, σ1 = 10, y2 = 300, x2 = 750, and σ2 = 10. Picture 6.2 shows the
goodness in the initial estimation of the parameters. We can see that this
estimation is rather bad. Picture 6.3 shows the result of the fitting procedure.
The fitting resulted in the following parameters a = −0.0077, b = 74.6574,
y1 = 2917.9178, x1 = 312.4101, σ1 = 14.5863, y2 = 246.7541, x2 = 756.4810,
and σ2 = 19.9311.

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

0 200 400 600 800 1000

22
Figure 6.1: γ-ray spectrum of Na.

52
3000

2500

2000

1500

1000

500

300 400 500 600 700 800 900

Figure 6.2: Initial estimation for the spectrum.

53
3000

2500

2000

1500

1000

500

300 400 500 600 700 800 900

Figure 6.3: Result of the fitting procedure.

54
Chapter 7

Numerical integration

7.1 Trapezoid rule and Romberg integration


The most straightforward method for numerical integration is the trapezoid
rule. We divide in interval into subintervals, and approximate the integral in
the subinterval by the area of a trapezoid
� b �
I= f (x)dx ≈ ∆Ii . (7.1)
a i

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

2 subroutine rombint ( a , b , func , res , n , eps )


3

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

9 use integr , only : dp , maxint


10 i n t e g e r :: n
11 r e a l ( dp ) :: a , b , eps , res
12 r e a l ( dp ) , e x t e r n a l :: func
13 i n t e g e r :: np , i , j , k , m
14 r e a l ( dp ) :: h , sumt , r ( maxint , maxint )
15

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

43 end subroutine rombint

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)

Gaussian-type integration methods are particularly effective when after


separating a W from the integrand, the rest, the function f is well approx-
imated by a polynomial. Below are some famous weight functions and the
corresponding orthogonal polynomials.
• Gauss-Legendre: W (x) = 1, x ∈ (−1, 1)
(j + 1)Pj+1 = (2j + 1)xPj − jPj−1 , P0 (x) = 1, P1 (x) = x, Pj is the
Legendre polynomial.

• Gauss-Chebyshev: W (x) = 1/ 1 − x2 , x ∈ (−1, 1)
Tj+1 = 2xTj − Tj−1 , T0 (x) = 1, T1 (x) = x, where Tj is the Chebyshev
polynomial of the first kind.

• Gauss-Chebyshev: W (x) = 1 − x2 , x ∈ (−1, 1)
Uj+1 = 2xUj − Uj−1 , U0 (x) = 1, U1 (x) = 2x, where Uj is the Chebyshev
polynomial of the second kind.

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

cj = 2(j + 1)(j + α + β + 1)(2j + α + β) (7.7)


dj = (2j + α + β + 1)(α + β)(α − β) (7.8)
ej = (2j + α + β)(2j + α + β + 1)(2j + α + β + 2) (7.9)
fj = 2(j + α)(j + β)(2j + α + β + 2) (7.10)

• Gauss-Laguerre: W (x) = xα e−x , x ∈ (0, ∞)


(j +1)Lαj+1 = (2j +α+1−x)Lαj −(j +α)Lαj−1 , Lα0 (x) = 1, Lα1 = α+1−x,
Lαj is the associated Laguerre polynomial.
2
• Gauss-Hermite: W (x) = e−x , x ∈ (−∞, ∞)
Hj+1 = 2xHj − 2jHj−1 , H0 (x) = 1, H1 (x) = 2x, Hj is the Hermite
polynomial.

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.

7.3 Clenshaw-Curtis integration


The Clenshaw-Curtis integration methods are almost as efficient as the Gaus-
sian methods but they are much simple. They are related to the Chebyshev
approximation. We present the following cases:

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

The approximation means


N
� � �
b−a b+a
I ≈ IN = wj f cos(tj ) + (7.14)
j=1
2 2
tj = πj/(N + 1), j = 1, 2, . . . , N (7.15)
N

(b − a) sin(tj ) [1 − cos(mπ)]
wj = sin(mtj ) (7.16)
N +1 m=1
m

• x ∈ [0, ∞) interval :

x = L cot2 (t/2), L is an adjustable constant (7.17)


� ∞ � π
2L sin(t)
I = f (x)dx = f (L cot2 (t/2)) dt (7.18)
0 0 [1 − cos(t)]2

The approximation means


N

I ≈ IN = wj f (L cot2 (tj /2)) (7.19)
j=1
tj = πj/(N + 1), j = 1, 2, . . . , N (7.20)
N

4L sin(tj ) [1 − cos(mπ)]
wj = 2
sin(mtj ) (7.21)
(N + 1)[1 − cos(tj )] m=1 m

• x ∈ (−∞, ∞) interval :

x = L cot(t), L is an adjustable constant (7.22)


� ∞ � π
2L
I = f (x)dx = f (L cot(t)) dt (7.23)
−∞ 0 sin(t)

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 )

Listing 7.2: Gauss-Legendre integration


1

2 subroutine cc11 (n ,a ,b ,x , w ) ! Clenshaw - Curtis


3 ! n number of points
4 ! a , b interval
5 ! x abscissas
6 ! w weights
7

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

24 end subroutine cc11


25

26

27 subroutine cc0inf (n , scale ,x , w ) ! Clenshaw - Curtis


28 ! 0 to infinity
29 ! n number of points
30 ! scale to scale the function

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

57 cot =1/ tan ( x )


58

59 end function cot


60

61 end subroutine cc0inf

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

7 end module integr


8

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

18 p r i n t * , ’� int_ -1^1 � 1/(1+ x ^2) � dx � ’


19

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

37 res =0. _dp


38 do i =1 , nint
39 res = res + w ( i )* runge ( x ( i ))
40 end do
41 p r i n t * , ’�C - C � � � ’ ,res ,2* atan (5. _dp ) , nint
42

43 eps =1. e -10 _dp


44 nint =20
45

46 c a l l rombint (a ,b , runge , res , nint , eps )


47

48 p r i n t * , ’� Romberg ’ ,res ,2* atan (5. _dp ) , nint ,2** nint


49

50 p r i n t * , ’� int_0 ^ infinity � x * exp ( - x )/(1+ x ^2) � dx � ’


51

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

69 res =0. _dp


70 do i =1 , nint
71 res = res + w ( i )* fun ( x ( i ))
72 end do
73 p r i n t * , ’� Clenshaw - Curtis � ’ ,res , nint
74

75 end program inttest


76

77 function runge ( x )
78

79 use numtype , only : dp


80 i m p l i c i t none
81 r e a l ( dp ) :: runge , x
82

83 runge = 1. _dp /(1. _dp + x **2)


84

85 end function runge


86

87 function fun ( x ) r e s u l t ( f )
88

89 use numtype , only : dp


90 i m p l i c i t none
91 r e a l ( dp ) :: f , x
92

93 f = x **2* exp ( - x )/ sqrt (1. _dp + x )


94

95 end function fun

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

Boundary Value Problems

So far in differential equations we studied the conditions were provided at


one end of the boundaries. Therefore they are called initial value problems.
We provide some initial value, and the systems evolves. Another class of
differential equations is when the conditions are provided at both ends of the
parameter. They are called boundary value problems.
A typical boundary value problem is shooting. In shooting problem we
know the solution at t = 0, when we fire the bullet. At some later time,
which is not known, we know that it should be at a certain location. So, the
conditions are set at the boundaries, at the beginning and at the end.

8.1 Shooting method


The most straightforward solution of boundary value problems is the shooting
method. We supplement the conditions at one end such that we solve an
initial value differential equation. We evaluate the solution until we reach
the other boundary. Then we compare the solution with the condition there,
and repeat the procedure with different initial condition until they match.
As an example we consider a real shooting problem. We throw a ball
with initial velocity v0 = 10m/s and want to hit the target which is 4 meters
away. What should be the initial angle?
We shoot with some arbitrary initial angle, which is between 0◦ and 90◦ .
The ball lands somewhere. Function proj calculates the difference of the
landing point and the target. If this value is zero, we hit the target.
Now we can use our Chebyshev zero search procedure to locate the zeros of

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

2 subroutine rk4step (t ,y , dt ) ! 4 - th order Runge - Kutta step


3

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
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 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

22 subroutine deriv (t ,y , f ) ! derivative


23

24 use setup , only : dp , n_eq , g , mass , drag


25 i m p l i c i t none
26 r e a l ( dp ) , i n t e n t ( in ) :: t
27 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( in ) :: y
28 r e a l ( dp ) , dimension ( n_eq ) :: f
29 r e a l ( dp ) :: vx , vy , v
30

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

40 end subroutine deriv


41

42 end subroutine rk4step

Listing 8.2: Code for shooting


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 = 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

13 end module setup


14

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

31 eps =0.01 _dp


32 maxf =10

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

43 end program shoot


44

45

46 function proj ( alpha )


47

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

61 do w h i l e ( y (3) >= 0. _dp )


62 i f ( iw /= 0) w r i t e ( u n i t = iw ,fmt= ’ (2 f15 .5) ’) &
63 y (1) , y (3)
64 c a l l rk4step (t ,y , dt )
65 end do
66

67 proj = y (1) - distance


68 p r i n t * , alpha , proj
69

70 end function proj

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

Schrödinger equation in one


dimension

We consider first the quantum mechanical Schrödinger equation in one di-


mension. We will show how the requirement of square integrability quantize
the system.

9.1 Quantization of the harmonic oscillator


The Schrödinger equation of the one-dimensional harmonic oscillator problem
is given by
h̄2 d2 ψ(x) 1
− + mω 2 x2 ψ(x) = Eψ(x), (9.1)
2m dx2 2
which can be rewritten as
� �
d2 ψ(x) 2mE x2
=− − 2 ψ(x), (9.2)
dx2 h̄2 x0

where x0 = h̄/(mω) is a constant that has a dimension of length. The
Hamiltonian of the one-dimensional harmonic oscillator commute with the
parity operator and therefore eigenstates can be labeled by parity. The even
parity states are symmetric, the odd parity states are antisymmetric with
respect to x → −x exchange. It is straightforward to solve this differential
equation by using the Runge-Kutta method. For unnormalized even parity
states we start with ψ(0) = 1 and ψ � (0) = 0, and for odd parity with ψ(0) = 0
and ψ � (0) = 1.

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

2 subroutine rk4step (x ,y , dx ) ! 4 - th order Runge - Kutta step


3

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 ) :: x
7 r e a l ( dp ) , i n t e n t ( in ) :: dx
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 (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

22 subroutine deriv (x ,y , f ) ! Schrodinger eq .


23

24 use setup , only : dp , n_eq , &


25 hbar2 , mass , energy
26 i m p l i c i t none
27 r e a l ( dp ) , i n t e n t ( in ) :: x
28 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( in ) :: y
29 r e a l ( dp ) , dimension ( n_eq ) :: f
30

31 f (1) = y (2)
32 f (2) = -2* mass / hbar2 * &
33 ( energy - potential ( x ))* y (1)
34

71
35 end subroutine deriv
36

37 function potential ( x ) r e s u l t ( v ) ! potential


38

39 use setup , only : dp , mass , omega


40 i m p l i c i t none
41 r e a l ( dp ) , i n t e n t ( in ) :: x
42 r e a l ( dp ) :: v
43

44 v = 0.5 _dp * mass *( omega * x )**2


45

46 end function potential


47

48 end subroutine rk4step

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

12 end module setup


13

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

20 energy = 0.52 _dp


21 xmax =3.0 _dp
22 dx = 0.0001 _dp
23

24 x =0. _dp

72
25 y (1) = 1
26 y (2) = 0
27

28 do w h i l e ( x < xmax )
29

30 w r i t e (1 ,*) x ,0. _dp , y (1)


31 c a l l rk4step (x ,y , dx )
32

33 end do
34

35 end program hoout

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6
0 0.5 1 1.5 2 2.5 3

Figure 9.1: Solutions of the one dimensional harmonic oscillator problem at


various energies..

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

It is hard to match with the Gaussian falloff if we start from x = 0. There-


fore we start at large xmax distance where the wave function has supposedly
reached its asymptotic form. We set up the conditions
� 2 �
x
ψ(xmax ) = exp − max (9.4)
2x20

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

2 subroutine rk4step (x ,y , dx ) ! 4 - th order Runge - Kutta step


3

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 ) :: x
7 r e a l ( dp ) , i n t e n t ( in ) :: dx
8 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( inout ) :: y

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

22 subroutine deriv (x ,y , f ) ! Schrodinger eq .


23

24 use setup , only : dp , n_eq , &


25 hbar2 , mass , energy
26 i m p l i c i t none
27 r e a l ( dp ) , i n t e n t ( in ) :: x
28 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( in ) :: y
29 r e a l ( dp ) , dimension ( n_eq ) :: f
30

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

36 end subroutine deriv


37

38 function potential ( x ) r e s u l t ( v ) ! potential


39

40 use setup , only : dp , mass , omega


41 i m p l i c i t none
42 r e a l ( dp ) , i n t e n t ( in ) :: x
43 r e a l ( dp ) :: v
44

45 v = 0.5 _dp * mass *( omega * x )**2 ! HO potential


46

47 end function potential


48

75
49 end subroutine rk4step

Listing 9.4: solution of the one-dimensional harmonic oscillator 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 = 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

14 end module setup


15

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

25 eminx =0. _dp


26 emaxx =10. _dp
27

28 xmax =6.0 _dp


29 dx = 0.0001 _dp
30 eps = 0.000001 _dp
31 de0 = 0.01 _dp
32 maxf = 10
33 imax = abs ( xmax / dx )
34 a l l o c a t e ( wf (0: imax ,2))
35 de =1. _dp
36 izz =0
37 nch =5

76
38 emin = eminx
39 emax = emin + de
40

41 do w h i l e ( emin < emaxx )


42

43 c a l l chebyex ( psi0 , nch , cheb , emin , emax )


44 c a l l chebyzero ( nch , cheb , emin , emax , z0 , iz0 )
45 p r i n t * , z0 (1: iz0 )
46

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

60 end program ho1d


61

62 function psi0 ( e ) r e s u l t ( psi )


63

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

81 end function psi0


82

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

120 do i = imin , imax


121 w r i t e ( u n i t =20+ iz ,fmt= ’ (2 f15 .5) ’) - wf (i ,1) ,&
122 parity * wf (i ,2)
123 end do
124

125 end subroutine wavef

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-6 -4 -2 0 2 4 6

Figure 9.2: The first three harmonic oscillator eigenstates.

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

Figure 9.3: The first ten harmonic oscillator eigenstates.

9.3 Scattering in one dimension


We consider a one dimensional scattering problem. We assume that the
potential is such that V (−∞) = 0 and V (∞) = V0 . In these asymptotic
regions the wave function is given by

ψ− = exp(ik1 x) + A exp(−ik1 x) if x < xmin
ψ(x) = (9.7)
ψ+ = B exp(ik2 x) if x > xmax ,
� �
where k1 = 2m/h̄ E and k2 = 2m/h̄2 (E − V0 ). Otherwise, ψ and ψ � are
2

continuous everywhere. So, the numerical procedure is quite straightforward.


We choose xmin and xmax such that the potential is already asymptotic. Then
starting from xmin with the boundary condition ψ− and with arbitrary A, we
solve the Schrödinger equation up to x = 0. Then from xmax with boundary
condition ψ+ with arbitrary B, we solve the equation backwards to x = 0.

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

V (x) = 1 + v0 tanh(x), (9.8)

where v0 = 1 (Fig. 9.4), and take E = 2.5. The Runge-Kutta stepping


routine is very much the same as before, only the potential is changed (Listing
9.5). The main program contains the Newton-Raphson solution and the
calculation of the reflection a transmission coefficients (Listing 9.6). The
result of the calculation is shown in Listing 9.7 and the wave function is
given in Fig. 9.5.

Figure 9.4: .

Listing 9.5: Runge-Kutta method for the one-dimensional scattering


1

2 subroutine rk4step (x ,y , dx ) ! 4 - th order Runge - Kutta step


3

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 ) :: x
7 r e a l ( dp ) , i n t e n t ( in ) :: dx
8 complex( dp ) , dimension ( n_eq ) , i n t e n t ( inout ) :: y
9 complex( dp ) , dimension ( n_eq ) :: k1 , k2 , k3 , k4 , dy
10

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

22 subroutine deriv (x ,y , f ) ! Schrodinger eq .


23

24 use setup , only : dp , n_eq , &


25 hbar2 , mass , energy
26 i m p l i c i t none
27 r e a l ( dp ) , i n t e n t ( in ) :: x
28 complex( dp ) , dimension ( n_eq ) , i n t e n t ( in ) :: y
29 complex( dp ) , dimension ( n_eq ) :: f
30 r e a l ( dp ) , e x t e r n a l :: potential
31

32 f (1) = y (2)
33 f (2) = -2* mass / hbar2 * &
34 ( energy - potential ( x ))* y (1)
35

36 end subroutine deriv


37

38 end subroutine rk4step


39

40 function potential ( x ) r e s u l t ( v ) ! potential


41

42 use setup , only : dp , v0


43 i m p l i c i t none
44 r e a l ( dp ) , i n t e n t ( in ) :: x
45 r e a l ( dp ) :: v
46

47 v = 1. _dp + v0 * tanh ( x )
48

49 end function potential

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

11 end module setup


12

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

25 energy = 2.5 _dp


26 xmax =10.0 _dp
27 dx = 0.001 _dp
28 aa (1) = ( 0.0 _dp , 0.0 _dp )
29 aa (2) = ( 1. _dp , 0.0 _dp )
30 da = ( 0.000001 _dp , 0. _dp )
31 eps = 0.0001 _dp
32 p r i n t ’ (4(9 x , a )) ’ , ’� A ’ , ’� � B ’ , ’� psi_0 ’ , ’� psi ‘ _0 ’
33

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

52 aa = aa + delta ! Newton step


53

54 i f ( maxval ( abs ( delta )) < &


55 eps * maxval ( abs ( aa )) ) e x i t
56

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

63 k1 = sqrt (2* mass / hbar2 *( energy - potential ( - xmax )))


64 k2 = sqrt (2* mass / hbar2 *( energy - potential ( xmax )))
65 r =( abs ( aa (1)))**2; t = k2 / k1 *( abs ( aa (2)))**2
66 p r i n t ’ (3( a , f12 .8)) ’ , ’� r = ’ ,r , ’� t = ’ ,t , ’� r + t = ’ ,r + t
67

68 end program st1d


69

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

99 ff (1)= psi0p - psi0m


100 ff (2)= dpsi0p - dpsi0m
101

102 end subroutine match0


103

104 subroutine wavef ( aa )


105

106 use setup


107 i m p l i c i t none
108 complex( dp ) , dimension ( n_eq ) :: aa , y
109 complex( dp ) :: k
110 r e a l ( dp ) :: x
111 r e a l ( dp ) , e x t e r n a l :: potential
112

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

131 end subroutine wavef

Listing 9.7: Solution of the one-dimensional scattering problem


1

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

Figure 9.5: Wave function of one-dimensional scattering.

87
Chapter 10

Schrödinger equation in three


spatial dimensions

We assume that the potential is spherically symmetric, or central, V (�r) =


V (r). This potential is rotationally invariant. Then the Hamilton operator
commute with the angular momentum operator, and these two operator have
common eigenstates. We seek the solution of the problem as common eigen-
states of operators H, L and Lz , where H is the Hamilton operator, L is the
the orbital angular momentum operators, and Lz is its z component. In this
case, the wave function can be decomposed as
ul (r)
Ψ(�r) = Ylm (θ, φ), (10.1)
r
where Ylm (θ, φ) is the eigenvector of the angular momentum operator with
eigenvalues l and m. The radial wave function ul (r) satisfies the radial
Schrödinger equation
� 2� 2 � �
h̄ d l(l + 1)
− − + V (r) ul (r) = Eul (r), (10.2)
2µ dr2 r2
where µ is the reduced mass. The equation is singular at r = 0 and at r = ∞.
From the requirement of square integrability, for E < 0, one can easily derive
the necessary boundary conditions that ul (r) behaves like
ul (r) ∼ rl+1 , if r → 0, and (10.3)
ul (r) ∼ exp(−κr), if r → ∞, (10.4)

where κ = 2m/h̄2 |E|.

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

By introducing y1 = u and y2 = du/dr, we can cast (10.5) into the form


where the Runge-Kutta method is readily applicable:

y = f (r, y), (10.6)

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.

10.2 α − α bound states


We consider two alpha particles interacting with potential

V (r) = −122.694 exp(−0.22r2 ) MeV. (10.9)

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

2 subroutine rk4step (x ,y , dx ) ! 4 - th order Runge - Kutta step


3

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 ) :: x
7 r e a l ( dp ) , i n t e n t ( in ) :: dx
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 (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

22 subroutine deriv (x ,y , f ) ! Schrodinger eq .


23

24 use setup , only : dp , n_eq , hbar2 , &


25 xm , energy , l
26 i m p l i c i t none
27 r e a l ( dp ) , i n t e n t ( in ) :: x
28 r e a l ( dp ) , dimension ( n_eq ) , i n t e n t ( in ) :: y
29 r e a l ( dp ) , dimension ( n_eq ) :: f
30

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

37 end subroutine deriv


38

39 function potential ( x ) r e s u l t ( v ) ! potential


40

41 use setup , only : dp


42 i m p l i c i t none
43 r e a l ( dp ) , i n t e n t ( in ) :: x
44 r e a l ( dp ) :: v
45

46 v = -122.694 _dp * exp ( -0.22 _dp * x **2)


47

48 end function potential


49

50 end subroutine rk4step

Listing 10.2: Code for finding the eigenstates


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 = 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

11 r e a l ( dp ) :: energy , xmax , dx , eps , xmid


12 r e a l ( dp ) , a l l o c a t a b l e , dimension (: ,:) :: wf
13 i n t e g e r :: imax
14

15 end module setup


16

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

29 xmax =10.0 _dp


30 dx = 0.0001 _dp
31 eps = 0.00001 _dp
32 maxf = 20
33 eminx = -100. _dp
34 emaxx =0. _dp
35 de =2. _dp
36 nstep =5
37 nch =5
38

39 imax = nint ( xmax / dx )+1


40 a l l o c a t e ( wf (0: imax ,2))
41

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

58 emin = eminx +( n -1)* deltae


59 emax = eminx + n * deltae
60 p r i n t * , emin , emax

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

77 end program bound


78

79 function psi0 ( e ) r e s u l t ( psi )


80

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

114 end function psi0


115

116

117 subroutine wavef (e , iz )


118

119 use setup


120 i m p l i c i t none
121 r e a l ( dp ) , i n t e n t ( in ) :: e
122 r e a l ( dp ) :: x , psi , kappa , y12 , y32 , y11 , y31 , yy
123 r e a l ( dp ) , dimension ( n_eq ) :: y
124 i n t e g e r :: iz , n , i
125

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

142 wf (0 ,1)=0. _dp


143 wf (0 ,2)=0. _dp
144 x = dx
145 y (1)= x **( l +1)
146 y (2)=( l +1)* x ** l
147 y (3)=0. _dp
148 do w h i l e ( x <= xmid )
149 n = nint ( x / dx )
150 wf (n ,1)= x
151 wf (n ,2)= y (1)
152 c a l l rk4step (x ,y , dx )
153 end do
154 y11 = y (1)
155 y31 = y (3)
156

157 wf (0: n ,2)= y12 / y11 * wf (0: n ,2)


158 y31 =( y12 / y11 )**2* y31
159 yy = y31 + y32
160 n = nint ( xmax / dx )
161 wf (0: n ,2)= wf (0: n ,2)/ sqrt ( yy )
162 do i =0 , n
163 w r i t e (10*( l +1)+ iz ,*) wf (i ,1) , wf (i ,2)
164 end do
165

166

167 end subroutine wavef

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.

10.3 Scattering states


A we saw before for bound states, the physical information is in the asymp-
totic behavior of the wave function. For scattering states this information is

95
1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

0 2 4 6 8 10

Figure 10.1: l = 0 and l = 2 eigenstates.

the phase shift. The asymptotic form of the wave function is given by

lim ul (r) ∼ sin(kr − lπ/2 + δl ). (10.10)


r→∞

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

11 r e a l ( dp ) :: energy , xmax , dx , eps , xmid


12 r e a l ( dp ) , a l l o c a t a b l e , dimension (: ,:) :: wf
13 i n t e g e r :: imax
14

15 end module setup


16

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

29 xmax =20.0 _dp


30 dx = 0.001 _dp
31 eps = 0.0000001 _dp
32 maxf = 20
33 dmin =0. _dp
34 dmax = pi
35

36

37 imax = nint ( xmax / dx )+1


38 a l l o c a t e ( wf (0: imax ,2))
39

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

55 end program scattering


56

57 function psi0 ( delta ) r e s u l t ( psi )


58

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

65 k = sqrt (2* xm / hbar2 * energy )


66

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

88 end function psi0


89

90

91 subroutine wavefunction ( delta )


92

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

100 k = sqrt (2* xm / hbar2 * energy )


101

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

112 wf (0 ,1) = 0. _dp ; wf (0 ,2) = 0. _dp


113 x = dx
114 y (1) = x **( l +1)
115 y (2) = ( l +1)* x ** l
116 do w h i l e ( x <= xmid )
117 n = nint ( x / dx )
118 wf (n ,1) = x ; wf (n ,2) = y (1)
119 c a l l rk4step (x ,y , dx )
120 end do
121 y11 = y (1)
122

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

129 end subroutine wavefunction

0.5

-0.5

-1

0 10 20 30 40 50

Figure 10.2: Scattering wave function at energies E = 1MeV (green), E =


5MeV (blue) and E = 10MeV (red).

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

Potrebbero piacerti anche