Anda di halaman 1dari 12

Solving Numerical Problems using

Computational Methods in FORTRAN


Neeraj Tata
Kings College London
neeraj.tata@kcl.ac.uk

I.

Introduction

rom the time humankind started to ponder about questions that belong in the
scientific realm of philosophy, especially
Physics, the mathematics involved has always
been a challenge. As science advanced, the
language it was spoken in, namely Maths, got
more and more difficult to handle. Historically,
"scientific powerhouses", such as Harvard University or MIT, had to employ human calculators so that the burden of tiresome calculations
could be avoided by those who were on the
forefront of scientific innovation. But as technology caught on with the advancement of
science, especially with invention of the computer, much of this burden of crunching data
was awarded to computers. They were more
efficient in time, more accurate than humans
and most importantly, never got tired or bored.

In a time and age when one is supposed


to do millions of calculations in short span of
time, computers have proven themselves as
an invaluable resource to humankind, especially to the scientific community. This fact is
pretty evident when the stats are considered.
The Data Centre at CERN processes about one
petabyte of data every day - the equivalent of
around 210,000 DVDs.1 It would be impossible
to handle that large an amount of data without using computers. And since computers
have to be programmed, it is essential for any
aspiring scientist to be knowledgeable in programming.Not to mention, most simulations
in condensed matter physics require programming to produce the graphics.

II.

Problem 1: Logistic Map

In this section, an attempt has been made to


find the attractors of population evolution dynamical system using Markov chains.
While the problem at hand can be solved using recursion, iteration has been used instead.
This is achieved using do-loops in FORTRAN.
The program to write down a sequence of the
equation is as follows:

A flowchart showing the sequence of the


program
Since every next term in the sequence depends on the previous one, we start by setting
the value for the first one. We then use this

1 http://home.cern/about/computing

value to find the 2nd term, which is then used


to find the 3rd term and so on until we find the
nth (required) term in the sequence. We then
change the initial value of x (x0 ) and repeat
the process. Finally, we change the factor a,
and repeat the entire process, storing values
for each measurement.

I.

II.

Bifurcation Diagrams

For a = 1 to 4

Do-Loops

As mentioned above, iterations are achieved using do-loops in FORTRAN and since we have
to reiterate the whole process over three variables, we use three do-loops, each with its own
purpose. On a very general basis, the three
do-loops have the following function:
The first do-loop increments from x1 to
xn , therefore finds the terms in the sequence for a particular value of x0 and
a.
! Listing
do i = 0 ,
next =
prev =
end do

the sequence
99
a * prev *(1 - prev )
next

The second do-loop finds the different


sequences as x0 changes.

A bifurcation diagram showing the attractors


on the vertical axis and the values of a = 1 to 4
on the horizontal axis.

For a = 3 to 4

! Different initial values


do init = 0 , 49
.
.
.
end do
While the third do-loop helps us find the
different sequences formed by changing
the value of a.
! Different a values
do a = 0 , 49
.
.
.
end do
As seen above the do-loops iterate different
variables to produce the entire sequence.
2

A bifurcation diagram showing the attractors


on the vertical axis and the values of a = 3 to 4
on the horizontal axis.

For a = 3.5 to 3.8

A bifurcation diagram showing the attractors


on the vertical axis and the values of a = 3.5 to
3.8 on the horizontal axis.

III.

Fixed Points

The analogue of a steady solution of a differential equation of a difference equation. In


effect, X would be a fixed point of a difference
equation, xn+1 = F ( xn ), if: X = F ( X ) [1] To
understand them in current context, one has
to understand the concept of an attractor. An
attractor is a set of points, with all the points in
the basin of attraction, which itself is another
set of points, converging to the points of the
attractor. Intuitively, if a bowl is taken and
a ball is placed at the bottom of the bowl, it
would stay there without moving. But if a ball
is placed anywhere else it would roll down to
reach the bottom of the ball. Here the bottom
of the bowl is both a fixed point, because placing a ball at that does not move the ball, and
is an attractor too since the ball would eventually settle there if placed elsewhere. While
it is good to note that not all fixed points are
attractors, in the case of the logistic map we
have fixed points that are also attractors.
One can, in theory, calculate the fixed points
numerically, it is much easier to locate them
on the bifurcation diagram since they are quite
evident (to find?). As it happens, the map bifurcates approximately at a = 3. These functions
are periodic in nature, in the sense that values

tend to repeat themselves after a few iterations.


The number of such iterations required for a
number to repeat itself is called the periodicity.
As a increases and exceeds 3.54409 in our logistic map, the curve tends to become a period
doubling cascade, that is, a very small change
in a would increase the periodicity of the map
by a factor of 2, so it takes twice as many as
iterations for a number to repeat itself.
So in our context, if we start with an random initial population, it would eventually end
up the value of the attractor. If the value of
a < 3, then we would just have one point in
the attractor, the convergence point of the population where it is stable. So the "killing factor"
such as shortage of resources, predators, etc.
and the "sustenance factor" namely reproduction, food sources, etc. Balance out each other
to produce a dynamic state of equilibrium. But
as we increase a, the number of points in the
attractors increases, which means that the population can now tend to any of those values.
So now the distribution of the killing and the
sustenance factors are more intertwined, with
their relationship being more complex. So too
much of predators could result in a shortage
of prey and eventually starve the predators
themselves killing them, while the remaining
prey now reproduce and increase in number,
thereby increasing food shortages and eventual
death of the population until some sort of an
equilibrium, which would arguably be a stable
one, would be reached. So the accumulation
of the fix points tell us that the relationship
between those factors is much more complex
and hence, much more "unpredictable" (not in
the truest sense, though), deterministic nevertheless.

IV.

Self-Similarity

It is very curious to see that the upper bifurcation is a smaller scale map of the original one. This is called Scale invariance or
Self-Similarity, wherein a pattern repeats itself
but on a smaller scale. The structure formed
thereby is called Fractals. These patterns are observed in probability distributions of random
3

processes as well as in the bifurcation maps


of chaotic processes.[2] It is important to note
that Chaotic processes are very different from
random processes. Chaotic systems are essentially deterministic, i.e. if one knew the initial
state he could predict the final state accurately,
while random systems are those in which any
and every piece of information of the initial
would be useless to accurately predict the final state.[3] Given a value of x0 and a, we can,
with perfect accuracy, predict some later state
xn . Hence our system is not random. But what
makes a chaotic system chaotic is that a very
small perturbation in the initial state would
be carried on to the later state, amplifying itself at each step and becoming large enough
to produce a very different final result. So the
defining feature of a chaotic system is that the
disturbance amplifies to change the final state
hugely. It is also called the "butterfly effect", a
name coined by Edward Lorenz. The name is
derived from the idea that a butterfly flapping
its wings can cause very small changes in the
air around thereby triggering a chain-reaction
that ultimately results in a tornados path being altered. It is also good to note that the
attractor is called strange[3] if it has a fractal
shape. 2

III.

Problem 2: Monte Carlo


Algorithm

Monte Carlo Algorithm is an randomized algorithm that relies on repeated sampling to


extrapolate the behaviour of a system from
information of a random sample. Since we
have uniformly distributed random numbers
(pseudo-random numbers 3 ), the number of
those lying inside the area we are concerned
with is proportional to the area itself. That is
the randomly generated number has a probability of lying in a particular patch proportional
to the area of the patch itself. So to calculate
the area under a curve, i.e. its integral, we
generate a set of random numbers and count
the number of those random lying inside our
2 Coincidentally,
3 for

Lorenzs attractor looks like a butterfly.


more on Pseudo-random numbers, see Appendix A

integral. We then extrapolate the total number


to the total area of consideration and the number of hits to the required area. This method is
more or less probabilistic in its approach and
will have errors. Since we are extrapolating a
finite data set to an infinite one, we are adding
data we do not have. In any case, the accuracy of this method would increase as increase
the number of random numbers considered.
That is we increase the number of hits.number
has a probability of lying in a particular patch
proportional to the area of the patch itself. So
to calculate the area under a curve, i.e. its integral, we generate a set of random numbers
and count the number of those random lying
inside our integral. We then extrapolate the
total number to the total area of consideration
and the number of hits to the required area.
This method is more or less probabilistic in its
approach and will have errors. Since we are
extrapolating a finite data set to an infinite one,
we are adding data we do not have. This addition of data is what causes errors since it is not
qualified data. In any case, the accuracy of this
method would increase as increase the number
of random numbers considered, that is as we
increase the number of hits.

I.

Calculating the integral of sin2 ( x )

A plot showing random points and the


sin2 ( x ) f unction

Integral
Monte Carlo

Actual

0.786497

0.785398

Error
Expected

Actual

0.071

0.001099

Table 1: Errors: Integral of sin2 ( x )

As shown above the error calculated, 0.001099,


is within the error expected, 0.071.
A flowchart showing the sequence of the
program to calculate the integral of sin2 ( x ) as
x goes from 0 to 2

The code from the lectures has been modified


in the following way:
The ratio of the probabilities in the problem given in the lecture gives the ratio of
the area of the circle to the ratio of the
square, whereas in our problem it gives
the ratio of the integral (area under the
curve) to the area of the square and so
appropriate modification was made to
reflect so.
The condition for the required area was
that the point had to have distance less
than 1 from the origin. This was changed
to the condition that y sin2 ( x )

II. Calculating the volume of a spherical shell


A similar strategy as above was used to compute the volume of the spherical shell. Since it
is three dimensional, three coordinates were
used, instead of just two, and the condition
imposed was that a hit was defined to be a
point with a distance between 0.65 and 1 from
the origin.

While a method using a regular mesh


would have the same level of difficulty as implementing Monte Carlo would have; it is a
lot more accurate than Monte Carlo, since the
number of points are larger. Monte Carlo is particularly more useful than regular mesh when
the dimension of the problem is more than six.
So, in principle, it would be better to use a
regular mesh rather than Monte Carlo for the
current problem.

IV.
A plot showing random points and the sin2 ( x )

Volume

Error

Monte Carlo

Actual

3.0696

3.03844

Expected

Actual

0.57735

0.03116

Table 2: Errors: Volume of the spherical shell

The value obtained from the program is 3.0696,


while the actual value is 3.03844, and so the
error is 0.03116. The error calculated using the
standard deviation gives us a value of 0.57735.
So it is safe to conclude that the error is well
within the error expected.

III.

Errors and Efficiency

As mentioned earlier, Monte Carlo method


comes with an error, which can be estimated
using the variance or standard deviation. The
standard deviation is the same irrespective of
the dimension of the problem. So the error is
same on a measurement of area, volume or any
n-dimensional measurement. That is the error
on both the area of the integral and the volume
of the spherical shell must be the same. The
relationship between the number of measurements and the error is given by
x n1 ,
where n is the number of measurements
6

Conclusions

In problem 1, we have seen how programming


is helpful in handling large amounts of data.
We have also seen how we can solve statistical, numerical or analytic problems using programming, more specifically, we saw how we
can model population and predict its growth
using the logistic map. An introduction to
chaotic non linear dynamic systems was included along with a brief explanation of fractals.
In problem 2, the Monte Carlo method was
tested for its efficiency by applying it to problems one can easily solve. It was compared
with the regular mesh way of calculating areas
and volumes, and it can be concluded, based
upon the data, that for lower dimensions using regular mesh is more accurate than Monte
Carlo.
In conclusion, I have learnt the syntax and
implementation of various programs in FORTRAN, as well as a few computational methods
and algorithms along with their applications
in solving various problems.

References
[1] Drazin P. G., Nonlinear Systems, (1992)
[2] Guckenheimer John, Holmes Philips, Nonlinear Oscillations, Dynamical Systems, and
Bifurcations of Vector Fields, Applied Mathematical Sciences; vol.42 (1983)
[3] West J. Bruce, An Essay on the Importance of
Being Nonlinear in Journal of Mathematical
Biology; vol.62 (1985)

V.
I.

Appendices

Appendix A: Pseudo-Random Number

A Pseudo-random number is not a random number in the truest sense, since given the initial
conditions, namely the seed, it can be completely determined. But it appears to be random since
we do not have any information about the initial conditions. So it appears to be random but is
actually not. John Von Neumann once joked that "Anyone who considers arithmetical methods of
producing random digits is, of course, in a state of sin." 4

4 more on "Various techniques used in connection with random digits" by John von Neumann(1951) https:
//dornsifecms.usc.edu/assets/sites/520/docs/VonNeumann-ams12p36-38.pdf

II.

Appendix B: Source Code

II.1

Logistic Map

include InitRandSeed . f90


Program PopMod
real c , p , a
integer i , j , k
! This is for a = 3.5 to 3.8.
a = 3.5 ! Set this to 3 to start from 3.
do k = 0 , 1999
do j = 0 , 49
CALL init_random_seed ()
CALL RANDOM_NUMBER ( c )
do i = 1 , 100
c = a * c * (1 - c )
end do
write (1 ,*) a , c
end do
a = a + 0.3/2000 ! Set 0.3 to the size of the interval .
end do
end Program PopMod

II.2

Monte Carlo for integral of sin2 ( x )

include InitRandSeed . f90


Program MonteCarlo
IMPLICIT NONE
integer :: hit , tot , i , k
real :: x , integral , y
REAL , PARAMETER :: Pi = 3.1415927
hit = 0
tot = 0
CALL init_random_seed ()
do i = 0 , 9999
call RANDOM_NUMBER ( x )
call RANDOM_NUMBER ( y )
x = x * Pi /2
if ( y <= (( sin ( x ))**2)) then
hit = hit +1
end if
tot = tot + 1
end do
integral = dble ( hit )/ dble ( tot )* Pi /2
write (* ,*) integral , i
end Program MonteCarlo

II.3

Monte Carlo for volume of spherical shell

include InitRandSeed . f90


Program Volume
IMPLICIT NONE
integer :: hit , tot , i
real :: x , y , z , r , integral
hit = 0
tot = 0
CALL init_random_seed ()
do i = 0 , 9999
call RANDOM_NUMBER ( y )
call RANDOM_NUMBER ( x )
call RANDOM_NUMBER ( z )
x = 2* x -1
y = 2* y -1
z = 2* z -1
r = sqrt ( x **2 + y **2 + z **2)
if (0.65 <= r . AND . r <= 1) then
hit = hit +1
end if
tot = tot + 1
end do
integral = 8*(( dble ( hit )/ dble ( tot )))
write (102 ,*) integral
end Program Volume

10

II.4

Initialising Random Seed

subroutine init_random_seed ()
use iso_fortran_env , only : int64
implicit none
integer , allocatable :: seed (:)
integer :: i , n , un , istat , dt (8) , pid
integer ( int64 ) :: t
call random_seed ( size = n )
allocate ( seed ( n ))
! First try if the OS provides a random number generator
open ( newunit = un , file = " / dev / urandom " , access = " stream " , &
form = " unformatted " , action = " read " , status = " old " , iostat = istat )
if ( istat == 0) then
read ( un ) seed
close ( un )
else
! Fallback to XOR : ing the current time and pid . The PID is
! useful in case one launches multiple instances of the same
! program in parallel .
call system_clock ( t )
if ( t == 0) then
call date_and_time ( values = dt )
t = ( dt (1) - 1970) * 365 _int64 * 24 * 60 * 60 * 1000 &
+ dt (2) * 31 _int64 * 24 * 60 * 60 * 1000 &
+ dt (3) * 24 _int64 * 60 * 60 * 1000 &
+ dt (5) * 60 * 60 * 1000 &
+ dt (6) * 60 * 1000 + dt (7) * 1000 &
+ dt (8)
end if
pid = getpid ()
t = ieor (t , int ( pid , kind ( t )))
do i = 1 , n
seed ( i ) = lcg ( t )
end do
end if
call random_seed ( put = seed )
contains
! This simple PRNG might not be good enough for real work , but is
! sufficient for seeding a better PRNG .
function lcg ( s )
integer :: lcg
integer ( int64 ) :: s
if ( s == 0) then
s = 104729
else
s = mod (s , 4294967296 _int64 )

11

end if
s = mod ( s * 279470273 _int64 , 4294967291 _int64 )
lcg = int ( mod (s , int ( huge (0) , int64 )) , kind (0))
end function lcg
end subroutine init_random_seed
5

5 The

12

above code can be found at https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html

Anda mungkin juga menyukai