Computational Physics
A Practical Introduction to Computational Physics and Scientific Computing

Konstantinos N. Anagnostopoulos
National Technical University

COMPUTATIONAL PHYSICS
A Practical Introduction to Computational Physics and Scientific Computing

AUTHORED BY KONSTANTINOS N. ANAGNOSTOPOULOS
Physics Department, National Technical University of Athens, Zografou Campus, 15780 Zografou, Greece
konstant@mail.ntua.gr, www.physics.ntua.gr/~konstant/

PUBLISHED BY KONSTANTINOS N. ANAGNOSTOPOULOS
and the
NATIONAL TECHNICAL UNIVERSITY OF ATHENS

Book Website:
www.physics.ntua.gr/~konstant/ComputationalPhysics

©Konstantinos N. Anagnostopoulos 2014, 2016

First Published 2014
Version1 1.1.20161207100600

Cover: Design by K.N. Anagnostopoulos. The front cover picture is a snapshot taken during Monte Carlo simulations of hexatic membranes. Work done with Mark J. Bowick. Relevant video at youtu.be/Erc7Q6YXfLk

C○C This book and its cover(s) are subject to copyright. They are licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit
creativecommons.org/licenses/by-sa/4.0/

The book is accompanied by software available at the book’s website. All the software, unless the copyright does not belong to the author, is open source, covered by the GNU public license, see www.gnu.org/licenses/. This is explicitly mentioned at the end of the respective source files.

ISBN 978-1-312-46441-4 (lulu.com, vol. I)
ISBN 978-1-312-46488-9 (lulu.com, vol. II)

Contents

Foreword
1 The Computer
 1.1 The Operating System
  1.1.1 Filesystem
  1.1.2 Commands
  1.1.3 Looking for Help
 1.2 Text Processing Tools – Filters
 1.3 Programming with Emacs
  1.3.1 Calling Emacs
  1.3.2 Interacting with Emacs
  1.3.3 Basic Editing
  1.3.4 Cut and Paste
  1.3.5 Windows
  1.3.6 Files and Buffers
  1.3.7 Modes
  1.3.8 Emacs Help
  1.3.9 Emacs Customization
 1.4 The Fortran Programming Language
  1.4.1 The Foundation
  1.4.2 Details
  1.4.3 Arrays
 1.5 Gnuplot
 1.6 Shell Scripting
2 Kinematics
 2.1 Motion on the Plane
  2.1.1 Plotting Data
  2.1.2 More Examples
 2.2 Motion in Space
 2.3 Trapped in a Box
  2.3.1 The One Dimensional Box
  2.3.2 Errors
  2.3.3 The Two Dimensional Box
 2.4 Applications
 2.5 Problems
3 Logistic Map
 3.1 Introduction
 3.2 Fixed Points and  n
2  Cycles
 3.3 Bifurcation Diagrams
 3.4 The Newton-Raphson Method
 3.5 Calculation of the Bifurcation Points
 3.6 Liapunov Exponents
 3.7 Problems
4 Motion of a Particle
 4.1 Numerical Integration of Newton’s Equations
 4.2 Prelude: Euler Methods
 4.3 Runge–Kutta Methods
  4.3.1 A Program for the 4th Order Runge–Kutta
 4.4 Comparison of the Methods
 4.5 The Forced Damped Oscillator
 4.6 The Forced Damped Pendulum
 4.7 Appendix: On the Euler–Verlet Method
 4.8 Appendix: 2nd order Runge–Kutta Method
 4.9 Problems
5 Planar Motion
 5.1 Runge–Kutta for Planar Motion
 5.2 Projectile Motion
 5.3 Planetary Motion
 5.4 Scattering
  5.4.1 Rutherford Scattering
  5.4.2 More Scattering Potentials
 5.5 More Particles
 5.6 Problems
6 Motion in Space
 6.1 Adaptive Stepsize Control for Runge–Kutta Methods
 6.2 Motion of a Particle in an EM Field
 6.3 Relativistic Motion
 6.4 Problems
7 Electrostatics
 7.1 Electrostatic Field of Point Charges
 7.2 The Program – Appetizer and ... Desert
 7.3 The Program – Main Dish
 7.4 The Program - Conclusion
 7.5 Electrostatic Field in the Vacuum
 7.6 Results
 7.7 Poisson Equation
 7.8 Problems
8 Diffusion Equation
 8.1 Introduction
 8.2 Heat Conduction in a Thin Rod
 8.3 Discretization
 8.4 The Program
 8.5 Results
 8.6 Diffusion on the Circle
 8.7 Analysis
 8.8 Problems
9 The Anharmonic Oscillator
 9.1 Introduction
 9.2 Calculation of the Eigenvalues of Hnm  (λ)
 9.3 Results
 9.4 The Double Well Potential
 9.5 Problems
10 Time Independent Schrödinger Equation
 10.1 Introduction
 10.2 The Infinite Potential Well
 10.3 Bound States
 10.4 Measurements
 10.5 The Anharmonic Oscillator - Again...
 10.6 The Lennard–Jones Potential
 10.7 Problems
11 The Random Walker
 11.1 (Pseudo)Random Numbers
 11.2 Using Pseudorandom Number Generators
 11.3 Random Walks
 11.4 Problems
12 Monte Carlo Simulations
 12.1 Statistical Physics
 12.2 Entropy
 12.3 Fluctuations
 12.4 Correlation Functions
 12.5 Sampling
  12.5.1 Simple Sampling
  12.5.2 Importance Sampling
 12.6 Markov Processes
 12.7 Detailed Balance Condition
 12.8 Problems
13 Simulation of the d = 2  Ising Model
 13.1 The Ising Model
 13.2 Metropolis
 13.3 Implementation
  13.3.1 The Program
  13.3.2 Towards a Convenient User Interface
 13.4 Thermalization
 13.5 Autocorrelations
 13.6 Statistical Errors
  13.6.1 Errors of Independent Measurements
  13.6.2 Jackknife
  13.6.3 Bootstrap
 13.7 Appendix: Autocorrelation Function
 13.8 Appendix: Error Analysis
  13.8.1 The Jackknife Method
  13.8.2 The Bootstrap Method
  13.8.3 Comparing the Methods
 13.9 Problems
14 Critical Exponents
 14.1 Critical Slowing Down
 14.2 Wolff Cluster Algorithm
 14.3 Implementation
  14.3.1 The Program
 14.4 Production
 14.5 Data Analysis
 14.6 Autocorrelation Times
 14.7 Temperature Scaling
 14.8 Finite Size Scaling
 14.9 Calculation of βc
 14.10 Studying Scaling with Collapse
 14.11 Binder Cumulant
 14.12 Appendix: Scaling
  14.12.1 Binder Cumulant
  14.12.2 Scaling
  14.12.3 Finite Size Scaling
 14.13 Appendix: Critical Exponents
  14.13.1 Definitions
  14.13.2 Hyperscaling Relations
 14.14 Problems
Bibliography
Index
This book has been written assuming that the reader executes all the commands presented in the text and follows all the instructions at the same time. If this advice is neglected, then the book will be of little help and some parts of the text may seem incomprehensible.

The book’s website is at
http://www.physics.ntua.gr/~konstant/ComputationalPhysics/
From there, you can can download the accompanying software, which contains, among other things, all the programs presented in the book.

Some conventions: Text using the font shown below refers to commands given using a shell (the “command line”), input and output of programs, code written in Fortran (or any other programming language), as well as to names of files and programs:

> echo Hello world  
Hello world

When a line starts with the prompt

>

then the text that follows is a command, which can be given from the command line of a terminal. The second line, Hello World, is the output of the command.

The contents of a file with Fortran code is listed below:

program add  
 z = 1.0  
 y = 2.0  
 x = z + y  
 print *, x  
end program add

What you need in order to work on your PC:

If you have installed a GNU/Linux distribution on your computer, all of the above can be installed easily. For example, in a Debian like distribution (Ubuntu, ...) the commands

> sudo apt-get install tcsh emacs gnuplot-x11 gnuplot-doc  
> sudo apt-get install gfortran gawk gawk-doc binutils  
> sudo apt-get install manpages-dev coreutils liblapack3

install all the necessary tools.

If you don’t wish to install GNU/Linux on your computer, you can try the following:

Foreword

This book is the culmination of my ten years’ experience in teaching three introductory, undergraduate level, scientific computing/computational physics classes at the National Technical University of Athens. It is suitable mostly for junior or senior level science courses, but I am currently teaching its first chapters to sophomores without a problem. A two semester course can easily cover all the material in the book, including lab sessions for practicing.

Why another book in computational physics? Well, when I started teaching those classes there was no bibliography available in Greek, so I was compelled to write lecture notes for my students. Soon, I realized that my students, majoring in physics or applied mathematics, were having a hard time with the technical details of programming and computing, rather than with the physics concepts. I had to take them slowly by the hand through the “howto” of computing, something that is reflected in the philosophy of this book. Hoping that this could be useful to a wider audience, I decided to translate these notes in English and put them in an order and structure that would turn them into “a book”.

I also decided to make the book freely available on the web. I was partly motivated by my anger caused by the increase of academic (e)book prices to ridiculous levels during times of plummeting publishing costs. Publishers play a diminishing role in academic publishing. They get an almost ready-made manuscript in electronic form by the author. They need to take no serious investment risk on an edition, thanks to print-on-demand capabilities. They have virtually zero cost ebook publishing. Moreover, online bookstores have decreased costs quite a lot. Academic books need no advertisement budget, their success is due to their academic reputation. I don’t see all of these reflected on reduced book prices, quite the contrary, I’m afraid.

My main motivation, however, is the freedom that independent publishing would give me in improving, expanding and changing the book in the future. It is great to have no length restrictions for the presentation of the material, as well as not having to report to a publisher. The reader/instructor that finds the book long, can read/print the portion of the book that she finds useful for her.

This is not a reference book. It uses some interesting, I hope, physics problems in order to introduce the student to the fundamentals of solving a scientific problem numerically. At the same time, it keeps an eye in the direction of advanced and high performance scientific computing. The reader should follow the instructions given in each chapter, since the book teaches by example. Several skills are taught through the solution of a particular problem. My lectures take place in a (large) computer lab, where the students are simultaneously doing what I am doing (and more). The program that I am editing and the commands that I am executing are shown on a large screen, displaying my computer monitor and actions live. The book provides no systematic teaching of a programming language or a particular tool. A very basic introduction is given in the first chapter and then the reader learns whatever is necessary for the solution of her problem. There is more than one way to do it8 and the problems can be solved by following a basic or a fancy way, depending on the student’s computational literacy. The book provides the necessary tools for both. A bibliography is provided at the end of the book, so that the missing pieces of a puzzle can be sought in the literature.

This is also not a computational physics playground. Of course I hope that the reader will have fun doing what is in the book, but my goal is to provide an experience that will set the solid foundation for her becoming a high performance computing, number crunching, heavy duty data analysis expert in the future. This is why the programming language of the core numerical algorithms has been chosen to be Fortran, a highly optimized, scientifically oriented, programming language. The computer environment is set in a Unix family operating system, enriched by all the powerful GNU tools provided by the FSF9 . These tools are indispensable in the complicated data manipulation needed in scientific research, which requires flexibility and imagination. Of course, Fortran is not the best choice for heavy duty object oriented programming, and is not optimal for interacting with the operating system. The philosophy10 is to let Fortran do what is best for, number crunching, and leave data manipulation and file administration to external, powerful tools. Tools, like awk, shell scripting, gnuplot, Perl and others, are quite powerful and complement all the weaknesses of Fortran mentioned before. The plotting program is chosen to be gnuplot, which provides very powerful tools to manipulate the data and create massive and complicated plots. It can also create publication quality plots and contribute to the “fun part” of the learning experience by creating animations, interactive 3d plots etc. All the tools used in the book are open source software and they are accessible to everyone for free. They can be used in a Linux environment, but they can also be installed and used in Microsoft Windows and Mac OS X.

The other hard part in teaching computational physics to scientists and engineers is to explain that the approach of solving a problem numerically is quite different from solving it analytically. Usually, students of this level are coming with a background in analysis and fundamental physics. It is hard to put them into the mode of thinking about solving a problem using only additions, multiplications and some logical operations. The hardest part is to explain the discretization of a model defined analytically, which can be done in many ways, depending on the accuracy of the approximation. Then, one has to extrapolate the numerical solution, in order to obtain a good approximation of the analytic one. This is done step by step in the book, starting with problems in simple motion and ending with discussing finite size scaling in statistical physics models in the vicinity of a continuous phase transition.

The book comes together with additional material which can be found at the web page of the book11 . The accompanying software contains all the computer programs presented in the book, together with useful tools and programs solving some of the exercises of each chapter. Each chapter has problems complementing the material covered in the text. The student needs to solve them in order to obtain hands on experience in scientific computing. I hope that I have already stressed enough that, in order for this book to be useful, it is not enough to be read in a café or in a living room, but one needs to do what it says.

Hoping that this book will be useful to you as a student or as an instructor, I would like to ask you to take some time to send me feedback for improving and/or correcting it. I would also appreciate fan mail or, if you are an expert, a review of the book. If you use the book in a class, as a main textbook or as supplementary material, I would also be thrilled to know about it. Send me email at konstantmail.ntua.gr and let me know if I can publish, anonymously or not, (part of) what you say on the web page (otherwise I will only use it privately for my personal ego-boost). Well, nothing is given for free: As one of my friends says, some people are payed in dollars and some others in ego-dollars!

Have fun computing scientifically!

Athens, 2014.

Chapter 1
The Computer

The aim of this chapter is to lay the grounds for the development of the computational skills which are necessary in the following chapters. It is not an in depth exposition but a practical training by example. For a more systematic study of the topics discussed, we refer to the bibliography. Many of the references are freely available in the web.

The are many choices that one has to make when designing a computer project. These depend on the needs for numerical efficiency, on available programming hours, on the needs for extensibility and upgradability and so on. In this book we will get the flavor of a project that is mostly scientifically and number crunching oriented. One has to make the best of the available computing resources and have powerful tools available for a productive analysis of the data. Such an environment, found in most of today’s supercomputers, that offers flexibility, dependability, simplicity, powerful tools for data analysis and effective compilers is provided by the family of the Unix operating systems. The GNU/Linux operating system is a Unix variant that is freely available and most of its utilities are open source software. The voluntary work of millions of excellent programmers worldwide has built the most stable, fastest and highest quality software available for scientific computing today. Thanks to the idea of the open source software pioneered by Richard Stallman1 this giant collaboration has been made possible.

Another choice that we have to make is the programming language, and this is going to be Fortran. Fortran has been built mainly for numerical applications and it has been used by many scientists and engineers because of its efficiency in high performance computing. The language is simple and compilers are able to optimize, parallelize and vectorize the code very efficiently. There is a lot of scientific and engineering software available in libraries written in Fortran, which has been used and tested extensively for many years. This is a crucial factor for scientific software, so that it can be trusted to be efficient and free of errors. Fortran is not the best choice for interacting with the operating system or for text processing. This shortcoming can be easily overcome by the use of external tools and Fortran can be left to do what she has been designed for: number crunching. Its structure is simple and can be used both for procedural and object oriented programming, in such a way that, it will not make the life of an inexperienced programmer difficult, and at the same time provide high level, abstract and powerful tools for high performance, modular, object oriented, programming needed in a large and complicated project.

Fortran, as well as other languages like C, C++ and Java, is a language that needs to be compiled by a compiler. Other languages, like perl, awk, shell scripting, Macsyma, Mathematica, Octave, Matlab, ...  , are interpreted line by line. These languages can be simple in their use, but they can be prohibitively slow when it comes to a numerically demanding program. A compiler is a tool that analyzes the whole program and optimizes the computer instructions executed by the computer. But if programming time is more valuable, then a simple, interpreted language can lead to faster results.

Another choice that we make in this book, and we mention it because it is not the default in most Linux distributions, is the choice of shell. The shell is a program that “connects” the user to the operating system. In this book, we will teach how to use a shell2 to “send” commands to the operating system, which is the most effective way to perform complicated tasks. We will use the shell tcsh, although most of the commands can be interpreted by most popular shells. Shell scripting is simpler in this shell, although shells like bash provide more powerful tools, mostly needed for complicated system administration tasks. That may cause a small inconvenience to some readers, since tcsh is not preinstalled in Linux distributions3 .

1.1 The Operating System

The Unix family of operating systems offer an environment where complicated tasks can be accomplished by combining many different tools, each of which performs a distinct task. This way, one can use the power of each tool, so that trivial but complicated parts of a calculation don’t have to be programmed. This makes the life of a researcher much easier and much more productive, since research requires from us to try many things before we understand how to compute what we are looking for.

In the Unix operating system everything is a file, and files are organized in a unique and unified filesystem. Documents, pictures, music, movies, executable programs are files. But also directories or devices, like hard disks, monitors, mice, sound cards etc, are, from the point of view of the operating system, files. In order for a music file to be played by your computer, the music data needs to be written to a device file, connected by the operating system to the sound card. The characters you type in a terminal are read from a file “the keyboard”, and written to a file “the monitor” in order to be displayed. Therefore, the first thing that we need to understand is the structure of the Unix filesystem.

1.1.1 Filesystem

There is at least one path in the filesystem associated with each file. There are two types of paths, relative paths and absolute paths. These are two examples:

bin/RungeKutta/rk.exe  
/home/george/bin/RungeKutta/rk.exe

The paths shown above may refer to the same or a different file. This depends on “where we are”. If “we are” in the directory /home/george, then both paths refer to the same file. If on the other way “we are” in a directory /home/john or /home/george/CompPhys, then the paths refer4 to two different files. In the last two cases, the paths refer to the files

/home/john/bin/RungeKutta/rk.exe  
/home/george/CompPhys/bin/RungeKutta/rk.exe

respectively. How can we tell the difference? An absolute path always begins with the / character, whereas a relative path does not. When we say that “we are in a directory”, we refer to a position in the filesystem called the current directory, or working directory. Every process in the operating system has a unique current directory associated with it.


pict

Figure 1.1: The Unix filesystem. It looks like a tree, with the root directory / at the top and branches that connect directories with their parents. Every directory contains files, among them other directories called its subdirectories. Every directory has a unique parent directory, noted by .. (double dots). The parent of the root directory is itself.

The filesystem is built on its root and looks like a tree positioned upside down. The symbol of the root is the character / The root is a directory. Every directory is a file that contains a list of files, and it is connected to a unique directory, its parent directory . Its list of files contains other directories, called its subdirectories, which all have it as their parent directory. All these files are the contents of the directory. Therefore, the filesystem is a tree of directories with the root directory at its top which branch to its subdirectories, which in their turn branch into other subdirectories and so on. There is practically no limit to how large this tree can become, even for a quite demanding environment5 .

A path consists of a string of characters, with the characters / separating its components, and refers to a unique location in the filesystem. Every component refers to a file. All, but the last one, must be directories in a hierarchy, from parent directory to subdirectory. The only exception is a possible / in the beginning, which refers to the root directory. Such an example can be seen in figure 1.1.

In a Unix filesystem there is complete freedom in the choice of the location of the files6 . Fortunately, there are some universally accepted conventions respected by almost everyone. One expects to find home directories in the directory /home, configuration files in the directory /etc, application executables in directories with names such as /bin, /usr/bin, /usr/local/bin, software libraries in directories with names such as /lib, /usr/lib etc.

There are some important conventions in the naming of the paths. A single dot “.” refers to the current directory and a double dot “..” to the parent directory. Similarly, a tilde “~” refers to the home directory of the user. Assume, e.g., that we are the user george running a process with a current directory /home/george/Music/Rock (see figure 1.1). Then, the following paths refer to the same file /home/george/Doc/lyrics.doc:

../../Doc/lyrics.doc  
~/Doc/lyrics.doc  
~george/Doc/lyrics.doc  
./../../Doc/lyrics.doc

Notice that ~ and ~george refer to the home directory of the user george (ourselves), whereas ~mary refer to the home directory of another user, mary.

We are now going to introduce the basic commands for filesystem navigation and manipulation7 . The command cd (=change directory) changes the current directory, whereas the command pwd (=print working directory) prints the current directory:

> cd /usr/bin  
> pwd  
/usr/bin  
> cd /usr/local/lib  
> pwd  
/usr/local/lib  
> cd  
> pwd  
/home/george  
> cd -  
> pwd  
/usr/local/lib  
> cd ../../  
> pwd  
/usr

The argument of the command cd is an absolute or a relative path. If the path is correct and we have the necessary permissions, the command changes the current directory to this path. If no path is given, then the current directory changes to the home directory of the user. If the character - is given instead of a path, then the command changes the current directory to the previous current directory.

The command mkdir creates new directories, whereas the command rmdir removes empty directories. Try:

> mkdir new  
> mkdir new/01  
> mkdir new/01/02/03  
mkdir: cannot create directory ‘new/01/02/03’: No such file or  
       directory  
> mkdir -p new/01/02/03  
> rmdir new  
rmdir: ‘new’: Directory not empty  
> rmdir new/01/02/03  
> rmdir new/01/02  
> rmdir new/01  
> rmdir new

Note that the command mkdir cannot create directories more than one level down the filesystem, whereas the command mkdir -p can. The “switch” -p makes the behavior of the command different than the default one.

In order to list the contents of a directory, we use the command ls (=list):

> ls  
BE.eps  Byz.eps  Programs      srBE_xyz.eps  srB_xyz.eps  
B.eps   Bzy.eps  srBd_xyz.eps  srB_xy.eps  
> ls Programs  
Backup            rk3_Byz.f90  rk3.f90  
plot-commands     rk3_Bz.f90   rk3_g.f90

The first command is given without an argument and it lists the contents of the current directory. The second one, lists the contents of the subdirectory of the current directory Programs. If the argument is a list of paths pointing to regular files, then the command prints the names of the paths. Another way of giving the command is

[literate={-}{{\texttt{-}}}1]%[basicstyle=\ttfamily]  
> ls -l  
total 252  
-rw-r--r--  1 george users 24284 May  1 12:08 BE.eps  
-rw-r--r--  1 george users 22024 May  1 11:53 B.eps  
-rw-r--r--  1 george users 29935 May  1 13:02 Byz.eps  
-rw-r--r--  1 george users 48708 May  1 12:41 Bzy.eps  
drwxr-xr-x  4 george users  4096 May  1 23:38 Programs  
-rw-r--r--  1 george users 41224 May  1 22:56 srBd_xyz.eps  
-rw-r--r--  1 george users 23187 May  1 21:13 srBE_xyz.eps  
-rw-r--r--  1 george users 24610 May  1 20:29 srB_xy.eps  
-rw-r--r--  1 george users 23763 May  1 20:29 srB_xyz.eps

The switch -l makes ls to list the contents of the current directory together with useful information on the files in 9 columns. The first column lists the permissions of the files (see below). The second one lists the number of links of the files8 . The third one lists the user who is the owner of each file. The fourth one lists the group that is assigned to the files. The fifth one lists the size of the file in bytes (=8 bits). The next three ones list the modification time of the file and the last one the paths of the files.

File permissions9 are separated in three classes: owner permissions, group permissions and other permissions. Each class is given three specific permissions, r=read, w=write and x=execute. For regular files, read permission effectively means access to the file for reading/copying, write permission means permission to modify the contents of the file and execute permission means permission to execute the file as a command10 . For directories, read permission means that one is able to read the names of the files in the directory (but not make it as current directory with the cd command), write permission means to be able to modify its contents (i.e. create, delete, and rename files) and execute permission grants permission to access/modify the contents of the files (but not list the names of the files, this is granted by the read permission).

The command ls -l lists permissions in three groups. The owner (positions 2-4), the group (positions 5-7) and the rest of the world (others - positions 8-10). For example

[literate={-}{{\texttt{-}}}1]  
-rw-r--r--  
-rwxr-----  
drwx--x--x

In the first case, the owner has read and write but not execute permissions and the group+others have only read permissions. In the second case, the user has read, write and execute permissions, the group has read permissions and others have no permissions at all. In the last case, the user has read, write and execute permissions, whereas the group and the world have only execute permissions. The first character d indicates a special file, which in this case is a directory. All special files have this position set to a character, while regular files have it set to -.

File permissions can be modified by using the command chmod:

> chmod u+x  file  
> chmod og-w file1 file2  
> chmod a+r  file

Using the first command, the owner (u≡ user) obtains (+) permission to execute (x) the file named file. Using the second one, the rest of the world (o≡ others) and the group (g≡ group) loose (-) the write (w) permission to the files named file1 and file2. Using the third one, everyone (a≡ all) obtain read (r) permission on the file named file.

We will close this section by discussing some commands which are used for administering files in the filesystem. The command cp (copy) copies the contents of files into other files:

> cp file1.f90 file2.f90  
> cp file1.f90 file2.f90 file3.f90 Programs

If the file file2.f90 does not exist, the first command copies the contents of file1.f90 to a new file file2.f90. If it already exists, it replaces its contents by the contents of the file file2.f90. In order for the second command to be executed, Programs needs to be a directory. Then, the contents of the files file1.f90, file2.f90, file3.f90 are copied to indentical files in the directory Programs. Of course, we assume that the user has the appropriate privileges for the command to be executed successfully.

The command mv “moves”, or renames, files:

> mv file1.f90 file2.f90  
> mv file1.f90 file2.f90 file3.f90 Programs

The first command renames the file file1.f90 to file2.f90. The second one moves files file1.f90, file2.f90, file3.f90 into the directory Programs.

The command rm (remove) deletes files11 . Beware, the command is unforgiving: after deletion, a file cannot be restored into the filesystem12 . Therefore, after executing successfully the following commands

> ls  
file1.f90  file2.f90  file3.f90  file4.csh  
> rm file1.f90 file2.f90 file3.f90  
> ls  
file4.csh

the files file1.f90, file2.f90, file3.f90 do not exist in the filesystem anymore. A more prudent use of the command demands the flag -i. Then, before deletion we are asked for confirmation:

> rm -i *  
rm: remove regular file ‘file1.f90’? y  
rm: remove regular file ‘file2.f90’? y  
rm: remove regular file ‘file3.f90’? y  
rm: remove regular file ‘file4.csh’? n  
> ls  
file4.csh

When we type y, the file is deleted, when we type n, the file is not deleted.

We cannot remove directories the same way. It is possible to use the command rmdir in order to remove empty directories. In order to delete directories together with their contents (including subdirectories and their contents) use the command13 rm -r. For example, assume that the contents of the directories dir1 and dir1/dir2 are the files:

./dir1  
./dir1/file2.f90  
./dir1/file1.f90  
./dir1/dir2  
./dir1/dir2/file3.f90

Then the results of the following commands are:

> rm dir1  
rm: cannot remove ‘dir1’: Is a directory  
> rm dir1/dir2  
rm: cannot remove ‘dir1/dir2’: Is a directory  
> rmdir dir1  
rmdir: dir1: Directory not empty  
> rmdir dir1/dir2  
rmdir: dir1/dir2: Directory not empty  
> rm -r dir1

The last command removes all files (assuming that we have write permissions for all directories and subdirectories). Alternatively, we can empty the contents of all directories first, and then remove them with the command rmdir:

> cd dir1/dir2; rm file3.f90  
> cd .. ; rmdir dir2  
> rm file1.f90 file2.f90  
> cd .. ; rmdir dir1

Note that by using a semicolon, we can execute two or more commands on the same line.

1.1.2 Commands

Commands in a Unix operating system are files with execute permission. When we write a sentence on the command line, like

> ls -l test.f90 test.dat

the shell reads its and interprets it. The shell is a program that creates a interface between a user and the operating system. The first word (ls) of the sentence is interpreted as a command. The rest of the words are the arguments of the command and the program can use them (or not) at the discretion of its programmer. There is a special convention for arguments that begin with a - (e.g. -l, --help, --version, -O3). They are called options or switches, and they act as virtual switches that make the program act in a particular way. We have already seen that the program ls gives a different output with the switch -l.

In order for a command to be executed, the shell looks for a file that has the same name as the command (here a file named ls). In order to understand where the shell looks for such a file, we should digress a little bit and explain the use of shell variables and environment variables. These have a name, which is a string of permissible characters, and their values are obtained by preceding their name with the $ character. For example the variable PATH has value $PATH. The values of the environment variables can be set with the command14 setenv and of the shell variables with the command set:

> setenv MYVAR test-env  
> set myvar = test-shell  
> echo $MYVAR $myvar  
test-env test-shell

Two special variables are the variables PATH and path:

>echo $PATH  
/usr/local/bin:/usr/bin:/bin:/usr/X11/bin  
>echo $path  
/usr/local/bin /usr/bin /bin /usr/X11/bin

The first one is an environment variable and the second one is a shell variable. Their values are set by the shell, and we don’t need to worry about them, unless we want to change them. Their value is a string of characters whose components should be valid paths to directories. In the first case, the components are separated by a :, while in the second case, by one or more spaces. In the example shown above, the shell searches each component of the path or PATH variables (in this order) until it finds a file ls in their contents. If it succeeds and the file has execute permissions, then the program in this file is executed. If it fails, then it prints an error message. Try the commands:

> which ls  
/bin/ls  
> ls -l /bin/ls  
-rwxr-xr-x 1 root root 93560 Sep 28  2006 /bin/ls

We see that the program that the ls command executes the program in the file /bin/ls.

The arguments of a command are passed on to the program that the command executes for possible interpretation. For example:

> ls -l test.f90 test.dat

The argument -l is the switch that results in a long listing of the files. The arguments test.f90 and test.dat are interpreted by the program ls as paths that it will look up for file information.

You can use the * (wildcard) character as a shorthand notation for a group of files. For example, in the command shown below

> ls -l *.f90 *.dat

the shell will expand *.f90 and *.dat to a list of all files whose names end with .f90 or .dat. Therefore, if the current directory contains the files test.f90, test1.f90, myprog.f90, test.dat, hello.dat, the arguments that will be passed on to the command ls are

> ls -l myprog.f90 test1.f90 test.f90 hello.dat test.dat

For each command there are three special files associated with it. The first one is the standard input (stdin), the second one is the standard output (stdout) and the third one the standard error (stderr). These are files where the program can print or read data from. By default, these files are the terminal that the user uses to execute the command. In this case, when the program reads data from the stdin, then it reads the data that we type to the terminal using the keyboard. When the program writes data to the stdout or to the stderr, then the data is written to the terminal.

The advantage of using these special files in order to read/write data is that the user can redirect the input/output to these files to any file she wants. Using the character > at the end of a command redirects the stdout to the file whose name is written after >. For example:

> ls  
file1.f90  file2.f90  file3.f90  file4.csh  
> ls > results  
> ls  
file1.f90  file2.f90  file3.f90  file4.csh  results

The first of the above commands, prints the contents of the current working directory to the terminal. The second command redirects data written to the stdout to the file results. After executing the command, the file results is created and its contents are the names of the files file1.f90 file2.f90 file3.f90 file4.csh. If the file results does not exist (as in the above example), the file is created. If it already exists, it is truncated and its contents replaced by the data written to the stdout of the command. If we want to append data without erasing the existing contents, then we should use the string of characters >>. Therefore, if we give the command

> ls >> results

after executing the previous commands, then the contents of the file results will be

file1.f90  file2.f90  file3.f90  file4.csh  
file1.f90  file2.f90  file3.f90  file4.csh  results

The redirection of the stdin is accomplished by the use of the character < while that of the stderr by the use of the string of characters15 >&. We will see more examples in section 1.2.

It is possible to redirect the stdout of a command to be the stdin of another command. This is very useful for creating filters. A filter is a command that creates a flow of data between two or more programs. This process is called piping. Pipes are creating by using the character |

> cmd1 | cmd2 | cmd3 | ... | cmdN

Using the syntax shown above, the stdout of the command cmd1 is redirected to the stdin of the command cmd2, the stdout of the command cmd2 is redirected to the stdin of the command cmd3 etc. More examples will be presented in section 1.2.

1.1.3 Looking for Help

Unix got itself a reputation for not being user friendly. This is far from the truth. Although there is a steep learning curve, detailed documentation for almost everything is available online.

The key for a comfortable ride is to learn how to use the help system available on your computer and on the internet. Most of the commands are self documented. A simple test, like the one shown below, will help you with the basic usage of most of the commands:

[literate={-}{{\texttt{-}}}1]  
> cmd --help  
> cmd -h  
> cmd -help  
> cmd -\?

For example, try the command ls --help. For a window application, start from the menu “Help”. You should not be afraid and/or lazy and you should proceed with careful searching and reading.

For example, let’s assume that you have heard about a command that sounds like printf, or something like that. The first level of online help is the man (=manual) command that searches the “man pages”. Read the output of the command

> man printf

The command info usually provides more detailed and user friendly documentation. It has basic browsing capabilities like the browsers you use to read pages from the internet. Try the command

> info printf

Furthermore, the commands

> man -k printf  
> whatis printf

will inform you that there are other, possibly related, commands with names like fprintf, fwprintf, wprintf, sprintf...:

> whatis printf  
printf               (1)   - format and print data  
printf               (1p)  - write formatted output  
printf               (3)   - formatted output conversion  
printf               (3p)  - print formatted output  
printf [builtins]    (1)   - bash built-in commands, see bash(1)

The second column printed by the whatis command is the “section” of the man pages. In order to gain access to the information in a particular section, you have to give it as an argument to the man command:

> man 1  printf  
> man 1p printf  
> man 3  printf  
> man 3p printf  
> man bash

Section 1 of the man pages contains information of ordinary command line commands, section 3 contains information on functions in libraries of the C language. Section 2 contains information on commands used for system administration. You may browse the directory /usr/share/man, or read the man page of the man command (use the command man man for that!).

By using the command

[literate={-}{{\texttt{-}}}1]  
> printf --help

we obtain plenty of memory refreshing information. The command

> locate printf

shows us many files related to the command printf. The commands

> which printf  
> where printf

give information on the location of the executable(s) of the command printf.

Another useful feature of the shell is the command or it filename completion. This means that we can write only the first characters of the name of a command or filename and then press simultaneously the keys [Ctrl-d]16 (i.e. press the key Ctrl and the key of the letter d at the same time). Then the shell will complete the name of the command up to the point that is is unique with the given string of characters17 :

> pri[Ctrl-d]  
printafm    printf  printenv   printnodetest

Try to type an x on the command line and then type [Ctrl-d]. You will learn all the commands that are available and whose name begins with an x: xterm, xeyes, xclock, xcalc, ...

Finally, the internet contains a wealth of information. Google your blues... and you will be rewarded!

1.2 Text Processing Tools – Filters

For doing data analysis, we will need powerful tools for manipulating data in text files. These are files that consist solely of printable characters. Some tools that can be used in order to construct complicated and powerful filters are the programs cat, less, head, tail, grep, sort and awk.

Suppose that we have data in a file named data18 which contains information on the contents of a food warehouse and their prices:

bananas  100 pieces 1.45  
apples   325 boxes  1.18  
pears     34 kilos  2.46  
bread     62 kilos  0.60  
ham       85 kilos  3.56

The command

> cat data

prints the contents of the file data to the stdout. In general, this command prints the contents of all files given in its arguments or the stdin if none is given. Since the stdin and the stdout can be redirected, the command

> cat <  data > data1

takes the contents of the file data from the stdin and prints them to the stdout, which in this case is the file data1. This command has the same result as the command:

> cp data data1

The command

> cat data data1 > data2

prints the contents of the file data and then the contents of the file data1 to the stdout. Since the stdout is redirected to the file data2, data2 contains the data of both files.

By giving the command

> less gfortran.txt

you can browse the data contained in the file gfortran.txt one page at a time. Press [space] in order to “turn” a page, [b] to turn back a page. Press the up and down arrows to move one line backwards/forward. Press [g] in order to jump to the beginning of the file and press [G] in order to jump to the end. Press [h] in order to get a help message and press [q] in order to quit.

The commands

> head -n 1 data  
bananas  100 pieces 1.45  
> tail -n 2 data  
bread     62 kilos  0.60  
ham       85 kilos  3.56  
> tail -n 2 data | head -n 1  
bread     62 kilos  0.60

print the first line, the last two lines and the second to the last line of the file data to the stdout respectively. Note that, by piping the stdout of the command tail to the stdin of the command head, we are able to construct the filter “print the line before the last one”.

The command sort sorts the contents of a file by comparing each line of its text with all others. The sorting is alphabetical, unless otherwise set by using options. For example

> sort data  
apples   325 boxes  1.18  
bananas  100 pieces 1.45  
bread     62 kilos  0.60  
ham       85 kilos  3.56  
pears     34 kilos  2.46

For reverse sorting, try sort -r data. We can also sort by comparing specific fields of each line. By default, fields are words separated by one or more spaces. For example, in order to sort w.r.t. the second column of the file data, we can use the switch -k 2 (=second field). Furthermore, we can use the switch -n for numerical sorting:

> sort -k 2 -n data  
pears     34 kilos  2.46  
bread     62 kilos  0.60  
ham       85 kilos  3.56  
bananas  100 pieces 1.45  
apples   325 boxes  1.18

If we omit the switch -n, the comparison of the lines is performed based on character sorting of the second field and the result is

> sort -k 2 data  
bananas  100 pieces 1.45  
apples   325 boxes  1.18  
pears     34 kilos  2.46  
bread     62 kilos  0.60  
ham       85 kilos  3.56

The last column contains floating point numbers (not integers). In order to sort by the values of such numbers we should use the switch -g:

> sort -k 4 -g data  
bread     62 kilos  0.60  
apples   325 boxes  1.18  
bananas  100 pieces 1.45  
pears     34 kilos  2.46  
ham       85 kilos  3.56

The command grep processes a text file line by line, searching for a given string of characters. When this string is found anywhere in a line, this line is printed to the stdout. The command

> grep kilos data  
pears     34 kilos  2.46  
bread     62 kilos  0.60  
ham       85 kilos  3.56

prints each line containing the string “kilos”. If we want to search for all line not containing the string “kilos”, then we add the switch -v:

> grep -v kilos data  
bananas  100 pieces 1.45  
apples   325 boxes  1.18

We can use a regular expression for searching a whole family of strings of characters. These monsters need a full book for discussing them in detail! But it is not hard to learn how to use some simple forms of regular expressions. Here are some examples:

> grep ^b data  
bananas  100 pieces 1.45  
bread     62 kilos  0.60  
> grep ’0$’ data  
bread     62 kilos  0.60  
> grep ’3[24]’ data  
apples   325 boxes  1.18  
pears     34 kilos  2.46

The first one, prints each line whose first character is a b. The second one, prints each line that ends with a 0. The third one, prints each line contaning the strings 32 or 34.

By far, the strongest tool in our toolbox is the awk program. By default, awk analyzes a text file line by line. Each word (or field in the awk jargon) of these lines is stored in a set of variables with names $1, $2, .... The variable $0 contains the full line currently processed, whereas the variable NF counts the number of fields in the current line. The variable NR counts the number of lines of the file processed so far by awk.

An awk program can be written in the command line. A set of commands within { ... } is executed for each line of input. The constructs BEGIN{ ... } and END{ ... } contain commands executed, only once, before and after the processing of the file respectively. For example, the command

> awk ’{print $1,"total value= ",$2*$4}’ data  
bananas total value=  145  
apples  total value=  383.5  
pears   total value=  83.64  
bread   total value=  37.2  
ham     total value=  302.6

prints the name of the product (1st column = $1) and the total value stored in the warehouse (2nd column = $2) × (4th column = $4). More examples are given below:

> awk ’{value += $2*$4}END{print "Total= ",value}’ data  
Total=  951.94  
> awk ’{av += $4}END{print "Average Price= ",av/NR}’ data  
Average Price=  1.85  
> awk ’{print $2^2 * sin($4) + exp($4)}’ data

The first one calculates the total value of all products: The processing of each line results in the increment (+=) of the variable value by the product of the second and fourth fields. In the end (END{ ... }), the string Total= is printed, together with the final value of the variable value. This is an easy way for computing the sum of the values calculated for each line. The second command, calculates and prints an average. The sum is calculated in each line and stored in the variable av. In the end, we print the quotient of the sum of all values by the number of lines that have been processed (NR). The last command shows a (crazy) mathematical expression based on numerical values found in each line of the file data: It computes the square of the second field times the sine of the fourth field plus the exponential of the fourth field.

There is much more potential in the commands presented above. Reading the documentation and getting experience by using them will provide you with very strong tools in order to accomplish complicated tasks.

1.3 Programming with Emacs

For a programmer that spends many hours programming every day, the environment and the tools available for editing the commands of a large and complicated program determine, to a large extent, the quality of her life! An editor edits the contents of a text file, that consists solely of printable characters. Such editors, available in most Linux environments, are the programs gedit, vim, pico, nano, zile... They provide basic functionality such as adding, removing or changing text within a file as well as more complicated functions, such as copying, pasting, searching and replacing text etc. There are many functions that are particularly useful to a programmer, such as detecting and formatting keywords of a particular programming language, pretty printing, closing scopes etc, which can be very useful for comfortable programming and for spotting errors. A very powerful and “knowledgeable” editor, offering many such functions for several programming languages, is the GNU Emacs editor19 . Emacs is open source software, it is available for free and can be used in most available operating systems. It is programmable20 and the user can automate most of her everyday repeated tasks and configure it to her liking. There is a full interaction with the operating system, in fact Emacs has been built with the ambition of becoming an operating system. For example, a programmer can edit a Fortran file, compile it, debug it and run it, everything done with Emacs commands.

1.3.1 Calling Emacs

In the command line type

> emacs &

Note the character & at the end of the line. This makes the particular command to run in the background. Without it, the shell waits until a command exits in order to return the prompt.

In a desktop environment, Emacs starts in its own window. For a quick and dirty editing session, or in the case that a windows environment is not available21 , we can run Emacs in a terminal mode. Then, we omit the & at the end of the line and we run the command

> emacs -nw

The switch -nw forces Emacs to run in terminal mode.


pict

Figure 1.2: The Emacs window in a windows environment. The buttons of very basic functions found on its toolbar are shown and explained.


pict

Figure 1.3: Emacs in a non-window mode running on the console. In this figure, we have typed the command save-buffers-kill-emacs in the minibuffer, a command that exits Emacs after saving edited data from all buffers. The same command can be given using the keyboard shortcut C-x C-c. We can see the mode line and the name of the buffer toy.f written on it, the percentage of the buffer (6%) shown in the window, the line and columns (33,0) where the point lies and the editing mode which is active on the buffer (Fortran mode (Fortran), Abbreviation mode (Abbrev), Auto Fill mode (Fill)).

1.3.2 Interacting with Emacs

We can interact with Emacs in various ways. Newbies will prefer buttons and menus that offer a simple and intuitive interface. For advanced usage, however, we recommend that you make an effort to learn the keyboard shortcuts. There are also thousands of functions available to be used interactively. They are called from a “command line”, called the minibuffer in the Emacs jargon.

Keyboard shortcuts are usually combinations of keystrokes that consist of the simultaneous pressing of the Ctrl or Alt keys together with other keys. Our convention is that a key sequence starting with a C- means that the characters that follow are keys simultaneously pressed with the Ctrl key. A key sequance starting with a M- means that the characters that follow are keys simultaneously pressed with the Alt key22 . Some commands have shortcuts consisting of two or more composite keystrokes. For example by C-x C-c we mean that we have to press simultaneously the Ctrl key together with x and then press simultaneously the Ctrl key together with c. This sequence is a shortcut to the command that exits Emacs. Another example is C-x 2 which means to press the Ctrl key together with x and then press only the key 2. This is a shortcut to the command that splits a window horizontally to two equal parts.

The most useful shortcuts are M-x (press the Alt key siumutaneously with the x key) and C-g. The first command takes us to the minibuffer where we can give a command by typing its name. For example, type M-x and then type save-buffers-kill-emacs in the minibuffer (this will terminate Emacs). The second one is an “SOS button” that interrupts anything Emacs does and returns control to the working buffer. This can be pretty handy when a command hangs or destroys our work and we need to interrupt it.


pict pict pict pict

Figure 1.4: The basic menus found in Emacs when run in a desktop environment. We can see the basic commands and the keyboard shortcut reminders in the parentheses. E.g. the command File → Visit New File can be given by typing C-x C-f. Note the commands File → Visit New File (open a file), File→ Save (write contents of a buffer to a file), File→ Exit Emacs, File → Split Window (split window in two), File→ New Frame (open a new Emacs desktop window) and of course the well known commands Cut, Copy, Paste, Undo from the Edit menu. We can choose different buffers from the menu Buffers, which contain the contents of other files that we have opened for editing. We recommend trying the Emacs Tutorial and Read Emacs Manual in the Help menu.

The conventions for the mouse events are as follows: With Mouse-1, Mouse-2 and Mouse-3 we denote a simple click with the left, middle and right buttons of the mouse respectively. With Drag-Mouse-1 we mean to press the left button of the mouse and at the same time drag the mouse.

We summarize the possible ways of giving a command in Emacs with the following examples that have the same effect: Open a file and put its contents in a buffer for editing.

The number of available commands increases from the top to the bottom of the above list.

1.3.3 Basic Editing

In order to edit a file, Emacs places the contents of a file in a buffer. Such a buffer is a chunk of computer memory where the contents of the file are copied and it is not the file itself. When we make changes to the contents of a buffer, the file remains intact. For our changes to take effect and be written to the file, we have to save the buffer. Then, the contents of the buffer are written back to the file. It is important to understand the following cycle of events:

Emacs may have more than one buffers open for editing simultaneously. By default, the name of the buffer is the same as the name of the file that is edited, although this is not necessary23 . The name of a buffer is written in the modeline of the window of the buffer, as can be seen in figure 1.3.

If Emacs crashes or exits before we save our edits, it is possible to recover (part of) them. There is a command M-x recover-file that will guide us through the necessary recovery steps, or we can look for a file that has the same name as the buffer we were editing surrounded by two #. For example, if we were editing the file file.f90, the automatically saved changes can be found in the file #file.f90#. Auto saving is done periodically by Emacs and its frequency can be controlled by the user.

The point where we insert text while editing is called “the point”. This is right before the blinking cursor24 . Each buffer has another position marked by “the mark”. A point and the mark define a “region” in the buffer. This is a part of the text in the buffer where the functions of Emacs can act (e.g. copy, cut, change case, spelling etc.). We can set the region by setting a point and then press C-SPC25 or give the command M-x set-mark-command. This defines the current point to be the mark. Then we can move the cursor to another point which will define a region together with the mark that we set. Alternatively we can use Drag-Mouse-1 (hold the left mouse button and drag the mouse) and mark a region. The mark can be set with Mouse-3, i.e. with a simple click of the right button of the mouse. Therefore by Mouse-1 at a point and then Mouse-3 at a different point will set the region between the two points.

We can open a file in a buffer with the command C-x C-f, and then by typing its path. If the file already exists, its contents are copied to a buffer, otherwise a new buffer is created. Then:

When we finish editing (or frequently enough so that we don’t loose our work due to an unfortunate event), we save the changes in the buffer, either by pressing the save icon on the toolbar, or by pressing the keys C-s, or by giving the command M-x save-buffer.

1.3.4 Cut and Paste

Use the instructions below for slightly more advanced editing:

We note that cutting and pasting can be made between different windows of the same or different buffers.

1.3.5 Windows

Sometimes it is very convenient to edit one or more different buffers in two or more windows. The term “windows” in Emacs refers to regions of the same Emacs desktop window. In fact, a desktop window running an Emacs session is referred to as a frame in the Emacs jargon. Emacs can split a frame in two or more windows, horizontally or/and vertically. Study figure 1.5 on page 81 for details. We can also open a new frame and edit several buffers simultaneously27 . We can manipulate windows and frames as follows:


pict

Figure 1.5: In this figure, the Emacs window has been split in three windows. The splitting was done horizontally first (C-x 2), and then vertically (C-x 3). By dragging the mouse (Drag-Mouse-1) on the horizontal mode lines and vertical lines that separate the windows, we can change window sizes. Notice the useful information diplayed on the mode lines. Each window has one point and the cursor is on the active window (in this case the window of the buffer named ELines.f). A buffer with no active changes in its contents is marked by a --, an edited buffer is marked by ** and a buffer in read only mode with (%%). With a mouse click on a %%, we can change them to -- (so that we can edit) and vice versa. With Mouse-3 on the name of a mode we can activate a choice of minor modes. With Mouse-1 on the name of a mode we ca have access to commands relevant to the mode. The numbers (17,31), (16,6) and (10,15) on the mode lines show the (line,column) of the point location on the respective windows.

You can have many windows in a dumb terminal. This is a blessing when a dekstop environment is not available. Of course, in that case you cannot have many frames.

1.3.6 Files and Buffers

1.3.7 Modes

Each buffer can be in different modes. Each mode may activate different commands or editing environment. For example each mode can color keywords relevant to the mode and/or bind keys to different commands. There exist major modes, and a buffer can be in only one of them. There are also minor modes, and a buffer can be in one or more of them. Emacs activates major and minor modes by default for each file. This usually depends on the filename but there are also other ways to control this. The user can change both major and minor modes at will, using appropriate commands.

Active modes are shown in a parenthesis on the mode line (see figures 1.3 and 1.5.

Some interesting minor modes are:

In a desktop environment, we can choose modes from the menu of the mode line. By clicking with Mouse-3 on the name of a mode we are offered options for (de)activating minor modes. With a Mouse-1 we can (de)activate the read-only mode with a click on :%% or :-- respectively. See figure 1.5.

1.3.8 Emacs Help

Emacs’ documentation is impressive. For newbies, we recommend to follow the mini course offered by the Emacs tutorial. You can start the tutorial by typing C-h t or select Help → Emacs Tutorial from the menu. Enjoy... The Emacs man page (give the man emacs command in the command line) will give you a summary of the basic options when calling Emacs from the command line.

A quite detailed manual can be found in the Emacs info pages28 . Using info needs some training, but using the Emacs interface is quite intuitive and similar to using a web browser. Type the command C-h r (or choose Help→ Emacs Tutorial from the menu) and you will open the front page of the emacs manual in a new window. By using the keys SPC and Backspace we can read the documentation page by page. When you find a link (similar to web page hyperlinks), you can click on it in order to open to read the topic it refers to. Using the navigation icons on the toolbar, you can go to the previous or to the next pages, go up one level etc. There are commands that can be given by typing single characters. For example, type d in order to jump to the main info directory. There you can find all the available manuals in the info system installed on your computer. Type g (emacs) and go to the top page of the Emacs manual. Type g (info) and read the info manual.

Emacs is structured in an intuitive and user friendly way. You will learn a lot from the names of the commands: Almost all names of Emacs commands consist of whole words, separated by a hyphen “-”, which almost form a full sentence. These make them quite long sometimes, but by using auto completion of their names this does not pose a grave problem.

1.3.9 Emacs Customization

You can customize everything in Emacs. From key bindings to programming your own functions in the Elisp language. The most common way for a user to customize her Emacs sessions, is to put all her customization commands in the file ∼ /.emacs in her home directory. Emacs reads and executes all these commands just before starting a session. Such a .emacs file is given below:

; Define F1 key to save the buffer  
(global-set-key [f1]    ’save-buffer)  
; Define Control-c s to save the buffer  
(global-set-key "\C-cs" ’save-some-buffers)  
; Define Meta-s (Alt-s) to interactively search forward  
(global-set-key "\M-s"  ’isearch-forward)  
; Define M-x is         to interactively search forward  
(defalias       ’is     ’isearch-forward)  
; Define M-x fm         to set fortran-mode for the buffer  
(defun fm()    (interactive) (f90-mode))  
; Define M-x sign       to sign my name  
(defun sign()  (interactive) (insert "K. N. Anagnostopoulos"))

Everything after a ; is a comment. Functions/commands are enclosed in parentheses. The first three ones bind the keys F1, C-c s and M-s to the commands save-buffer, save-some-buffers and isearch-forward respectively. The next one defines an alias of a command. This means that, when we give the command M-x is in the minibuffer, then the command isearch-forward will be executed. The last two commands are the definitions of the functions (fm) and (sign), which can be called interactively from the minibuffer.

For more complicated examples google “emacs .emacs file” and you will see other users’ .emacs files. You may also customize Emacs from the menu commands Options→ Customize Emacs. For learning the Elisp language, you can read the manual “Emacs Lisp Reference Manual” found at the address
www.gnu.org/software/emacs/manual/elisp.html

1.4 The Fortran Programming Language

In this section, we give a very basic introduction to the Fortran programming language. This is not a systematic exposition and you are expected to learn what is needed in this book by example. So, please, if you have not already done it, get in front of a computer and do what you read. You can find many good tutorials and books introducing Fortran in a more complete way in the bibliography.

1.4.1 The Foundation

The first program that one writes when learning a new programming language is the “Hello World!” program. This is the program that prints “Hello World!” on your screen:

program hello  
 
!print a message to the world:  
 print *, ’Hello World!’  !this is a comment  
 
end program hello

Commands, or statements, in Fortran are strings of characters separated by blanks (“words”) that we are allowed to write from the 1st to the 132nd column of a file. Each line starts a new command29 . We can put more than one command on each line by separating them with a semicolon (;). Everything after an exclamation mark (!) is a comment. Proliferation of comments is necessary for documenting our code. Good documentation of our code is an integral part of programming. If the code is planned to be read by others, or by us at a later time, make sure to explain in detail what each line is supposed to do. You and your collaborators will save a lot of time in the process of debugging, improving and extending your code.

The main entry to the program is defined by the command program name, where name can be any string of alphanumeric characters and an underscore. When the program runs, it starts executing commands at this point. The end of the program, as well as of any other program unit (functions, subroutines, modules), is defined by the line end program name.

The first (and only) command given in the above program is the print command. It prints the string of characters “Hello World!” to the stdout. The “*,” is part of the syntax and it is not printed, of course. Fortran does not distinguish capital from small letters, so we could have written PRINT, Print, prINt, ... A string of characters in Fortran is enclosed in single or double quotes (’Hello World!’ or "Hello World!" is equivalent).

In order to execute the commands in a program, it is necessary to compile it. This is a job done by a program called the compiler that translates the human language programming statements into binary commands that can be loaded to the computer memory for execution. There are many Fortran compilers available, and you should learn which compilers are available for use in your computing environment. Typical names for Fortran compilers are gfortran, f90, ifort, g95, .... You should find out which compiler is best suited for your program and spend time reading its documentation carefully. It is important to learn how to use a new compiler so that you can finely tune it to optimize the performance of your program.

We are going to use the open source and freely available compiler gfortran, which can be installed on most popular operating systems30 . The compilation command is:

> gfortran hello.f90 -o hello

The switch -o defines the name of the executable file, which in our case is hello. If the compilation is successful, the program runs with the command:

> ./hello  
 Hello world!

Now, we will try a simple calculation. Given the radius of a circle we will compute its length and area. The program can be found in the file area_01.f90:

program circle_area  
 
 PI = 3.141593  
 R  = 4.0  
 print *,’Perimeter= ’,2.0*PI*R  
 print *,’Area=      ’,PI*R**2  
 
end program circle_area

The first two commands define the values of the variables PI and R. These variables are of type REAL, which are floating point numbers. Fortran has implicit rules that can be used to define the type of variables. By default, variables whose name starts with i, j, k, l, m and n are of INTEGER type. These are exact whole numbers. All other variables are of type REAL31 . We can override these implicit rules by explicitly declaring the type of a variable or by changing the implicit rules with the use of the implicit statement. The following two commands have two effects: Computing the length 2πR  and the area    2
πR   of the circle and printing the results. The expressions 2.0*PI*R and PI*R**2 are evaluated before being printed by the print command. The multiplication and raising to a power operators are * and **, respectively. Note the explicit decimal points at the constants 2.0 and 4.0. If we write 2 or 4 instead, then these are going to be constants of the INTEGER type and by using them the wrong way we may obtain surprising results32 . We compile and run the program with the commands:

> gfortran area_01.f90 -o area  
> ./area  
 Perimeter=    25.13274  
 Area=         50.26548

We will now try a process that repeats itself for many times. We will calculate the length and area of 10 circles of different radii Ri = 1.28 + i  , i = 1,2,...,10  . We will store the values of the radii in an array R(10) of the REAL type. The code can be found in the file area_02.f90:

program circle_area  
 
 dimension R(10)  
 
 PI = 3.141593  
 R(1) = 2.28  
 do i=2,10  
  R(i) = R(i-1) + 1.0  
 enddo  
 
 do i = 1,10  
  perimeter = 2*PI*R(i)  
  area      = PI*R(i)**2  
  print *,i,’) R= ’,R(i),’ perimeter= ’,perimeter  
  print *,i,’) R= ’,R(i),’ area     = ’,area  
 enddo  
 
end program circle_area

The command dimension R(10) defines an array of length 10. This way, the elements of the array are referred by an index that takes value from 1 to 10. For example R(4) is the fourth element of the array.

Between the lines

 do  i = 2, 10  
  ...  
 enddo

we can write commands that are repeatedly executed while the INTEGER variable i takes values from 2 to 10 with increasing step33 equal to 1. The command:

 R(i) = R(i-1) + 1.0

defines the i-th radius to have a value which is larger by the (i-1)-th by 1. For the loop to work correctly, we must define the initial value of R(1), otherwise the final result is undefined34 . The second loop uses the defined R-values in order to do the computation and printing of the results.

Now, we will write an interactive version of the program. Instead of hard coding the values of the radii, we will interact with the user asking her to give her own values. The program will read the 10 values of the radii from the standard input (stdin). The program can be found in the file area_03.f90:

program circle_area  
 
 implicit none  
 
 integer,parameter    :: N=10  
 real   ,parameter    :: PI=3.141593  
 real   ,dimension(N) :: R  
 real                 :: area,perimeter  
 integer              :: i  
 
 do i=1,N  
  print*,’Enter radius of circle: ’  
  read *, R(i)  
  print*,’i= ’,i,’ R(i)= ’,R(i)  
 enddo  
 
 open(UNIT=13,FILE=’AREA.DAT’)  
 
 do i = 1,N  
  perimeter = 2*PI*R(i)  
  area      = PI*R(i)**2  
  write(13,*)i,’) R= ’,R(i),’ area= ’,area,&  
       ’ perimeter= ’,perimeter  
 enddo  
 
 close(13)  
 
end program circle_area

The first statement in the above program is implicit none! This statement deactivates the implicit rules of Fortran, and the programmer is obliged to declare all variables in a program unit. It is highly recommendable that you always use this option... You might spend a little more time typing the declarations, but this effort cannot be compared to the pain looking for bugs due to typos in the names of variables35 ! We will follow this practice throughout the book.

The declarations of the variables follow this statement. The variables N and i are declared to be of the INTEGER type, whereas the variables PI, area, perimeter and R(N) are declared to be of the REAL type. The variables PI and N are specified to be parameters. Parameters are given specific values which cannot be changed during the execution of the program.

The array elements R(i) are read using the command read:

  read *, R(i)

The command read reads from the stdin. The user types the values at the terminal and then presses [Enter]. We can read more than one variables with one read command.

In order to print data to a file, we have to connect it to a unit. Each unit is represented by any number between 0 and 99. Some numbers are reserved for special units36 . The connection of a unit to a file is done with the open command. When this is done, we can write to the file with the command37 write(n,*), where n is the unit number. When we are done writing to a file we should use the command close(n). Then the unit number is available to be used for a different file. The flow of commands is like

 open(UNIT=13,FILE=’AREA.DAT’)  
 ...  
 write(13,*) ....  
 ...  
 close(13)

The name of the file is determined by the option FILE=’AREA.DAT’ of the open statement. Uppercase or lowercase characters in the filename make a difference. The option FILE=’path’ can use any valid path in the filesystem, provided that we have the necessary permissions.

The line

  write(13,*)i,’) R= ’,R(i),’ area= ’,area,&  
       ’ perimeter= ’,perimeter

shows us how to continue a line containing a long statement to the next one. We place a & at the end of the line and then continue writing the statement to the next. This can happen up to 39 times.

The next step will be to learn how to define and use functions and subroutines. The program below shows how to define a subroutine area_of_circle, which computes the length and area of a circle of given radius. The following program can be found in the file area_04.f90:

program circle_area  
 
 implicit none  
 
 integer,parameter   :: N=10  
 real   ,parameter   :: P=3.141593  
 real   ,dimension(N):: R  
 real                :: area,perimeter  
 integer             :: i  
 
 do i=1,N  
  print*,’Enter radius of circle: ’  
  read *, R(i)  
  print*,’i= ’,i,’ R(i)= ’,R(i)  
 enddo  
 
 open(UNIT=13,FILE=’AREA.DAT’)  
 
 do i = 1,N  
  call area_of_circle(R(i),perimeter,area)  
  write(13,*)i,’) R= ’,R(i),’ area= ’,area,&  
         ’ perimeter= ’,perimeter  
 enddo  
 
 close(13)  
 
end program circle_area  
 
subroutine area_of_circle(R,L,A)  
 implicit none  
 real           :: R,L,A  
 real,parameter :: PI = 3.141593 , PI2 = 2.0*PI  
 
 L= PI2*R  
 A= PI*R*R  
 
 return  
 
end subroutine area_of_circle

The calculation of the length and the area of the circle is performed by the call to the subroutine:

  call area_of_circle(R(i),perimeter,area)

The command call calls a subroutine and transfers the control of the program within the subroutine. The above subroutine has the arguments (R(i),perimeter,area). The argument R(i) is an input variable. It provides the necessary data to the subroutine in order to perform its computation. The arguments perimeter and area are intended for output. Upon return of the subroutine to the main program, they store the result of the computation. The user of a subroutine must learn how to use its arguments in order to be able to call it in her program. These must be documented carefully by the programmer of the subroutine.

The actual program executed by the subroutine is between the lines:

subroutine area_of_circle(R,L,A)  
 ...  
end subroutine area_of_circle

The arguments (R,L,A) must be declared in the subroutine and need not have the same names as the ones that we use when we call it. A change of their values within the subroutine will change the values of the corresponding variables in the calling program38 . Therefore, the statements L=PI2*R and A=PI*R*R change the values of the variables perimeter and area to the desired values. The command return returns the control to the calling program. The parameters PI and PI2 are “private” to the subroutine. Their names and values are invisible outside the subroutine. Similarly, the variables i, N, ..., defined in the main program, are invisible within the subroutine.

We summarize all of the above in a program trionymo.f90, which computes the roots of a second degree polynomial:

! =============================================================  
! Program to compute roots of a 2nd order polynomial  
! Tasks: Input from user,logical statements,  
!        use of functions,stop  
!        Accuracy in floating point arithmetic  
!        e.g. IF(x.eq.0.0)  
!  
! Tests: a,b,c= 1  2  3 D=   -8  
!        a,b,c= 1 -8 16 D=    0  x1=   4  
!        a,b,c= 1 -1 -2 D=    9. x1=   2. x2=  -1.  
!        a,b,c= 2.3 -2.99 -16.422 x1=   3.4 x2=  -2.1  
! But:   6.8(x-4.3)**2 = 6.8 x**2 -58.48*x+125.732  
!        a,b,c= 6.8 -58.48 125.73199  
!        D= 0.000204147349  x1=   4.30105066 x2=   4.29894924  
!        a,b,c= 6.8 -58.48 125.732, D=   -0.000210891725 < 0!!  
! =============================================================  
program trionymo  
 implicit none  
 real :: a,b,c,D  
 real :: x1,x2  
 real :: Discriminant  
 
 print*,’Enter a,b,c:’  
 read *,a,b,c  
 
! Test if we have a well defined polynomial of 2nd degree:  
 if( a .eq. 0.0) stop ’trionymo: a=0’  
 
! Compute the discriminant (= diakrinousa)  
 D = Discriminant(a,b,c)  
 print *, ’Discriminant: D=  ’,D  
 
 
! Compute the roots in each case: D>0, D=0, D<0 (no roots)  
 if(D .gt. 0.0 )then  
  call roots(a,b,c,x1,x2)  
  print *,’Roots:        x1= ’,x1,’ x2= ’,x2  
 else if (D .eq. 0.0) then  
  call roots(a,b,c,x1,x2)  
  print *,’Double Root:  x1= ’,x1  
 else  
  print *,’No real roots’  
 endif  
 
end program trionymo  
! =============================================================  
! This is the function that computes the discriminant  
! A function returns a value. This value is assigned with the  
! statement:  
! Discriminant = <value>  
! i.e. we simply assign anywhere in the program a variable with  
! the name  of the function.  
! =============================================================  
real function Discriminant(a,b,c)  
 implicit none  
 real :: a,b,c  
 
 Discriminant = b**2 - 4.0 * a * c  
 
end function Discriminant  
! =============================================================  
! The subroutine that computes the roots.  
! =============================================================  
subroutine roots(a,b,c,x1,x2)  
 implicit none  
 real :: a,b,c  
 real :: x1,x2  
 real :: D, Discriminant  
 
 if(a .eq. 0.0) stop ’roots: a=0’  
 
 D = Discriminant(a,b,c)  
 if(D.ge.0.0)then  
  D = sqrt(D)  
 else  
  print *,’roots: Sorry, cannot compute roots, D<0=’,D  
  stop  
 endif  
 
 x1 = (-b + D)/(2.0*a)  
 x2 = (-b - D)/(2.0*a)  
 
end subroutine roots  

The program reads the coefficients of the polynomial ax2 + bx + c  . After a check whether a ⁄= 0  , it computes the discriminant D =  b2 − 4ac  by calling the function Discriminant(a,b,c). The only difference between a function and a subroutine is that the first one returns a value of a given type. We don’t need to use the command call in order to run the commands of a function, this is done by computing its value in an expression. The type of the value returned must be declared both in the program that uses the function (real :: Discriminant) and at the entry point of its program unit (real function Discriminant(a,b,c)). The value returned to the calling program is the one assigned to the variable that has the same name as the function:

real function Discriminant(a,b,c)  
 ...  
 Discriminant = b**2 - 4.0 * a * c  
 ...  
end function Discriminant

Notice the use of the comparison operators .gt. (strictly greater than) and .eq. (equal to)39 :

 if(D .gt. 0.0 )then  
  ...  
 else if (D .eq. 0.0) then  
  ...  
 else  
  ...  
 endif

1.4.2 Details

You may skip this paragraph during a first reading of the book. It is intended mainly to be a reference when reading the later chapters.

There are more types of variables built in Fortran. In the program listed below, we show how to use CHARACTER variables, floating point numbers of double precision REAL(8) and complex numbers of single and double precision, COMPLEX and COMPLEX(8) respectively:

program f90_vars  
 implicit none  
 
 character(100) :: string  
 
 real(4)    ::   x !single precision, same as real :: x  
 real(8)    ::  x8 !equivalent to: double precision x8  
!real(16)   :: x16 !may not be supported by all compilers  
!Complex Numbers:  
 complex(4) :: z   !single precision, same as complex :: z  
 complex(8) :: z8  !double precision  
 
!A string: a character array:  
 string = ’Hello World!’ !string smaller size, leaves blanks  
                                         !TRIM: trim blanks  
 print *,’A string ::’,      string, ’::’,TRIM(string),’::’  
 print *,’join them::’,      string   //       string ,’::’  
 print *,’join them::’, TRIM(string)  //  TRIM(string),’::’  
!Reals with increasing accuracy: Determine PI=3.14159...  
 x   = 4.0  *atan(1.0  )  
!Use D for double    precision exponent  
 x8  = 4.0D0*atan(1.0D0)  
!Use Q for quadriple precision exponent  
!x16 = 4.0Q0*atan(1.0Q0)  
 print *,’x4= ’,x,’ x8= ’,x8 !,’ x16= ’,x16  
 print *,’x4: ’,range(x ),precision(x ),EPSILON(x ),&  
          TINY(x ),HUGE(x )  
 print *,’x8: ’,range(x8),precision(x8),EPSILON(x8),&  
          TINY(x8),HUGE(x8)  
 
!Complex numbers: single precision  
 z = (2.0,1.0)*cexp((3.0,-1.0))  
 print *,’z= ’,z,’ Re(z)= ’,REAL(z),’ Im(z)= ’,IMAG(z),&  
      ’ |z|= ’,ABS(z),’ z*= ’,CONJG(z)  
 
!Complex numbers: double precision  
 z8 = (2.0D0,1.0D0)*cdexp((3.0D0,-1.0D0))  
 print *,’z= ’,z8,’ Re(z)= ’,DBLE(z8),’ Im(z)= ’,DIMAG(z8),&  
      ’ |z|= ’,CDABS(z8),’ z*= ’,DCONJG(z8)  
 print *,’z4: ’,range(z ),precision(z )  
 print *,’z8: ’,range(z8),precision(z8)  
 
end program f90_vars

Some interesting points of the program above are:

Another important point to discuss is how to be able to access the same variables from different program units. So far, we simply mentioned that variables have a scope within each function and subroutine. If we wish to have access to the same location of memory40 from different program units, then we use the COMMON statement which defines a common block. See the following example:

! ---------------------------------------  
program f90_common  
 implicit none  
 real :: k1=1.0,k2=1.0,k3=1.0  
 common /CONSTANTS/k1,k2  
 
 print *,’main: k1= ’,k1,’ k2= ’,k2,’ k3= ’,k3  
 call s1 !prints k1 and k2 but not k3  
 call s2 !changes the value of k2 but not k3  
 print *,’main: k1= ’,k1,’ k2= ’,k2,’ k3= ’,k3  
 
end program f90_common  
! ---------------------------------------  
subroutine s1()  
 implicit none  
 real k1,k2,k3  
 common /CONSTANTS/k1,k2  
 
 print *,’s1: k1= ’,k1,’ k2= ’,k2,’ k3= ’,k3  
end subroutine s1  
! ---------------------------------------  
subroutine s2()  
 implicit none  
 real k1,k2,k3  
 common /CONSTANTS/k1,k2  
 
 k2 = 2.0  
 k3 = 2.0  
end subroutine s2

The common block has the name CONSTANTS and we can refer to it from any program unit. Each program unit that uses this common block must use the same declaration, although the names of variables are allowed to be different. The common block CONSTANTS points to the same location in the computer memory, where we expect to find the values of two real variables. These variables (k1 and k2) are used and have their values changed in the subroutines s1 and s2. The variable k3, is a different variable in each program unit. The program prints

 main: k1=    1.000000   k2=    1.000000   k3=    1.000000  
 s1:   k1=    1.000000   k2=    1.000000   k3=  -2.8117745E-05  
 main: k1=    1.000000   k2=    2.000000   k3=    1.000000

One of the weaknesses of Fortran is that it does not have a convenient control for Input/Output (I/O). For complicated I/O and text manipulation we will use other programs that can do a better job, like awk, perl, shell scripting, or programs written in C/C++. It is important to know some details about I/O commands in Fortran, mainly the specifications that control the accuracy of printed floating point numbers. So far, I/O commands, like print, write, read, used a * in order to control the printing of numbers. But we can replace the * with explicit format directives as follows:

program f90_format1  
 implicit none  
 integer             :: i  
 real                :: x  
 real, dimension(10) :: a  
 real(8)             :: x8  
 
 i  = 123456  
 x  = 2.0  *atan2(1.0,0.0)  
 print ’(A5,I6,F12.7)’,’x,i= ’,i,x  
 x8 = 2.0D0*atan2(1.0D0,0.0D0)  
 write(6,’(F18.16,E24.17,G24.17,G24.17)’) x8,x8,&  
      1.0D15*x8,1.0D18*x8  
 write(6,’(3F20.16)’) x8,x8/2.0,cos(x8)  
 write(6,’(200F12.6)’)(a(i), i=1,10)  
end program f90_format1

Note the parentheses within the single quotes: (A5,I6,F12.7) is a format directive for the print statement. The A is for printing a CHARACTER, the I for printing an INTEGER and the F for printing a floating point number. The numbers after the letter declare the number of spaces used for printing each one. Beware! If the printing space is not enough, Fortran will not print and you will find a series of * in place of the value of your result! Bummer... In order to estimate the number of spaces needed for a floating point number, you have to include the space taken by the decimal point, the sign, the exponent character, the sign of the exponent and the digits needed for the exponent. Plus a space to separate the numbers in between... So, be generous and give plenty of printing space. In the example shown above, A5 denotes a character of 5 spaces, I6 and integer of 6 spaces and F12 a floating point number of 12 spaces. The decimal point in F12.7 means that we want a floating point with the accuracy of 7 significant digits.

The format directive (F18.16,E24.17,G24.17,G24.17) shows how to print double precision variables. These provide an accuracy of 16-17 significant digits and there is no need for keeping more digits. The command E prints a number in scientific form with an exponent. The command G prints the exponent when it is needed. The numbers before the letters denote multiplicity. Therefore 3F20.16 instructs the printing of 3 floating point numbers by reserving 20 spaces and using 16 significant digits for each one of them.

The command write(6,’(200F12.6)’)(a(i), i=1,10) shows how to print a large array using an implicit loop. We used many more spaces than actually needed (200F12.16) which is OK. If the array gets larger by increasing the range of i, then we will have enough room for printing in the same line. The program prints (we have folded the long line in order to make it visible):

x,i= 123456   3.1415927  
3.1415926535897931 0.31415926535897931E+01  3141592653589793.0  
                                        0.31415926535897933E+19  
  3.1415926535897931  1.5707963267948966 -1.0000000000000000  
    0.000000    0.000000    0.000000 ....

We can organize the format commands by using the FORMAT statement. Then, we use labeled statements in order to refer to them. Labels are numbers put in the beginning of a line which should be unique to a program unit and are within the range 1-99999. We can transfer the control of the program to such a line with a goto command or by using the label in the I/O statements print, write and read as in the example shown below:

program f90_format2  
 implicit none  
 integer i  
 real    x, a(10)  
 real*8  x8  
 
 i  = 123456  
 x  = 2.0  *atan2(1.0,0.0)  
 print 100,’x,i= ’,i,x  
 x8 = 2.0D0*atan2(1.0D0,0.0D0)  
 write(6,123) x8,x8,&  
      1.0D15*x8,1.0D18*x8  
 write(6,4444) x8,x8/2.0,cos(x8)  
 write(6,9999)(a(i), i=1,10)  
100  FORMAT(A5,I6,F12.7)  
123  FORMAT(F18.16,E24.17,G24.17,G24.17)  
4444 FORMAT(3F20.16)  
9999 FORMAT(200F12.6)  
end program f90_format2

The reader should also study the Fortran intrinsic functions shown in table 1.2, page 181.

1.4.3 Arrays

You may skip this section during the first reading of this book. It will be useful to come back here later.

Arrays are related data of the same type which can be accessed by using one or more indices. For example, after a declaration real, dimension(10) :: A, the expressions

A(1), A(2), ... , A(10)

refer to its 10 real values. The indices can be integer expressions, for example

A(i), B(2*i+3), C(INT(x+y(j)))

where in the last case we used the integer value of the intrinsic function INT(x), which returns the integer part of x. Note that, arrays and functions enclose indices and arguments between parentheses (...) which are of the same style, and the compiler must look at their declarations in order to tell the difference. Examples of array declarations are

 real, dimension(10) :: a,b  
 real, dimension(20) :: c,d

which declare the arrays a, b, c, d, which are of the real kind, with elements a(1) ... a(10), b(1) ... b(10), c(1) ... c(20) and d(1) ... d(20). An equivalent declaration is

 real :: a(10), b(10), c(20), d(20)

or

 integer, parameter     :: n1 = 10, n2 = 20  
 real,    dimension(n1) :: a, c(n2)  
 real                   :: b(n1), d(n2)

In the last form, we show how to use constant parameters for declaring the size of arrays. For the declarations shown above, the lower bound of all arrays is 1 and the upper bound for a and b is 10 and for c and d is 20. The upper and lower bound of arrays can be explicitly determined. The declarations

 integer, parameter          :: n1 = 10, n2 = 20  
 real,    dimension(0:n1)    :: a  
 real,    dimension(-n1:n2)  :: c

define the real array a with 11 values a(0) ... a(10) and the array c with 31 values c(-10) c(-9) ... c(-1) c(0) c(1) ... c(20).

The arrays shown above have dimension 1 and they are like vectors. We can declare arrays of more than one dimension. This means that we need more than one indices in order to determine an array element41 . Therefore, the declaration

integer, dimension(2,2)     :: a

defines an integer array with values a(1,1), a(1,2), a(2,1) and a(2,2). The following declarations define two three dimensional real arrays a and b:

 integer, parameter :: n1 = 10, n2 = 20, n3 = 2*n1+n2  
 real, dimension(n1,n2,n3)          :: a  
 real, dimension(-n1:n1,0:n2,13:n3) :: b

Some important definitions used in the bibliography are:

The values of arrays can be set the same way as scalars:

 integer :: i  
 real    :: a(4), b(2,2)  
 
 b(1,1) = 2.0 ; b(1,2) = 4.0  
 b(2,1) = 3.4 ; b(2,2) = 7.8  
 do i=1,4  
  a(i) = 1.0  
 enddo

Alternatively we can use the name of the array as one object:

 a = (/ 1.0, 2.0, 3.0, 4.0 /)  
 b = 0.0

The first line defines the values of an array by using an array constructor. The second line defines all elements of the array b to be equal to 0. This is an example of a very convenient feature of the Fortran language. If all the arrays in an expression are conformable, then we can use the intrinsic Fortran operations to act on whole arrays. Two arrays are conformable if they have the same shape or if one of them is a scalar. Therefore the program

 integer :: i,j  
 real    :: x,y,a(10),b(10),c(4,4),d(4,4)  
 
 do i=1,10  
  a(i) = b(i)  
 enddo  
 
 do j=1,4  
  do i=1,4  
   c(i,j) = x*d(i,j)+y  
  enddo  
 enddo

is equivalent to

 integer :: i,j  
 real    :: x,y,a(10),b(10),c(4,4),d(4,4)  
 
 a = b  
 c = x*d+y  

Many Fortran intrinsic functions are elemental. This means that their arguments can be arrays, in which case the function acts on each array element separately. For example, the commands

 integer :: i,j  
 real    :: x,y,a(10),b(10),c(4,4),d(4,4)  
 
 c = sin(d) + x*exp(-2.0*d)  
 call random_number(a)  

set c(i,j) = sin(d(i,j))+x*exp(-2.0*d(i,j)) for all i and j, and the elements of a(i) equal to a random number uniformly distributed in the interval [0,1)  . We should stress that in order for two arrays to be conformable, it is not necessary that they have the same lower and upper bounds. For example, the command b=c*d in the following program has the same effect as the do loop:

 integer :: i  
 real    :: b(0:19), c(10:29), d(-9:10)  
 
 b = c*d  
 
 do i=1,20  
  b(i-1) = c(i+9) * d(i-10)  
 enddo

In the following, we mention some useful functions that act on arrays. Assume that

 real    :: a(-10:10), b(-10:10), c(10,10), d(10,10), e(10,10)

then

You can find more functions and documentation in the bibliography  [1110]. In the following, we provide some information related to the Input/Output (I/O) of arrays. Input (“reading”) and output (“writing”) of array values can be done by reading and writing their elements in any order we want. In the example below, we read the array a and write the array b in two different ways:

 integer :: i,j  
 real    :: a(4), b(2,2)  
 
 do i=1,4  
  read *,a(i)  
 enddo  
 read *, (a(i), i=1,4)  
 
 do j=1,2  
  do i=1,2  
   print *,b(i,j)  
  enddo  
 enddo  
 print *,( (b(i,j), i=1,2), j=1,2)

Inside the do loops, input and output is done one element per line from/to standard input/output. The commands (a(i), i=1,4) and ( (b(i,j) i=1,2), j=1,2) are implied do loops and read/write from/to the same line. During input, if the number of values for a are exhausted, then the program tries to read values from the following line(s). Similarly, if the output of b exhausts the maximum number of characters per line, then the output continues in the next line42 . Try it...

We can also preform I/O of arrays without explicit reference to their elements. In this case, the arrays are read/written in a specified order. For example, the program

 real  :: a(4), b(2,2)  
 
 read  *, a  
 read  *, b  
 
 print *, a,b

reads the values a(1) a(2) a(3) a(4) from the stdin. Then, it continues reading b(1,1), b(2,1), b(1,2), b(2,2) from the next line (record). Notice that the array b is read in a column major way. Printing a and b, will print a(1) a(2) a(3) a(4) and b(1,1), b(2,1), b(1,2), b(2,2) in two different records (also in column major mode).

Finally, we summarize some of the Fortran capabilities in array manipulation. More details can be found in the bibliography. Read the comments in the program for a partial explanation of each command:

program arrays  
 implicit none  
 integer :: i,j,n,m  
 real    :: a(3), b(3,3), c(3,3)=-99.0, d(3,3)=-99.0, s  
 integer :: semester(1000),grade(1000)  
 logical :: pass(1000)  
 !construct the matrix: use the RESHAPE function  
 !|1.1 -1.2 -1.3|  
 !|2.1  2.2 -2.3|  
 !|3.1  3.2  3.3|  
 b = RESHAPE((/  1.1,  2.1,  3.1, & !(notice rows<->columns)  
                -1.2,  2.2,  3.2, &  
                -1.3, -2.3,  3.3  /),(/3,3/))  
 !same matrix, now exchange rows and columns: ORDER=(/2,1/)  
 b = RESHAPE((/  1.2, -1.2, -1.3, &  
                 2.1,  2.2, -2.3, &  
                 3.1,  3.2,  3.3  /),(/3,3/),ORDER=(/2,1/))  
 a = b(:,2) !a assigned the second column of b: a(i)=b(i,2)  
 a = b(1,:) !a assigned the first  row    of b: a(i)=b(1,i)  
 a = 2.0*b(:,3)+sin(b(2,:))!a(i)= 2*b(i,3)+sin(b(2,i))  
 a = 1.0+2.0*exp(-a)+b(:,3)!a(i)= 1+2*exp(-a(i))+b(i,3)  
 s = SUM(b)              !returns sum of all      elements of b  
 s = SUM(b,MASK=(b.gt.0))!returns sum of positive elements of b  
 a = SUM(b,DIM=1)     !each a(i) is the sum of the columns of b  
 a = SUM(b,DIM=2)     !each a(i) is the sum of the rows    of b  
 !repeat all the above using PRODUCT!  
 !all instructions may be executed in parallel at any order!  
 FORALL(i=1:3) c(i,i) = a(i) !set the diagonal of c  
 !compute upper bounds of indices in b:  
 n=UBOUND(b,DIM=1);m=UBOUND(b,DIM=2)  
 !log needs positive argument, add a restriction ("mask")  
 FORALL(i=1:n,j=1:m, b(i,j).gt.0.0 ) c(i,j) = log(b(i,j))  
 !upper triangular part of matrix:  
 !careful, j=i+1:m NOT permitted  
 FORALL(i=1:n,j=1:m, i     .lt.  j ) c(i,j) = b(i,j)  
 !each statement executed BEFORE the next one!  
 FORALL(i=2:n-1,j=2:n-1)  
  !all right hand side evaluated BEFORE the assignment  
  !i.e., the OLD values of b averaged and then assigned to b  
  b(i,j)=(b(i+1,j)+b(i-1,j)+b(i,j+1)+b(i,j-1))/4.0  
  c(i,j)=1.0/b(i+1,j+1) !the NEW values of b are assigned  
 END FORALL  
 ! assignment but only for elements  b(i,j) which are not  0  
 WHERE (b     .ne. 0.0)              c      = 1.0/b  
 !MATMUL(b,c) is evaluated, then d is assigned the result only  
 !at positions where b>0.  
 WHERE (b     .gt. 0.0)              d      = MATMUL(b,c)  
 WHERE (grade .ge. 5  )  
  semester = semester + 1 !student’s semester increases by 1  
  pass     = .true.  
 ELSEWHERE  
  pass     = .false.  
 END WHERE  
end program arrays

The code shown above can be found in the file f90_arrays.f90 of the accompanying software.

1.5 Gnuplot

Plotting data is an indispensable tool for their qualitative, but also quantitative, analysis. Gnuplot is a high quality, open source, plotting program that can be used for generating publication quality plots, as well as for heavy duty analysis of a large amount of scientific data. Its great advantage is the possibility to use it from the command line, as well as from shell scripts and other programs. Gnuplot is programmable and it is possible to call external programs in order manipulate data and create complicated plots. There are many mathematical functions built in gnuplot and a fit command for non linear fitting of data. There exist interactive terminals where the user can transform a plot by using the mouse and keyboard commands.

This section is brief and only the features, necessary for the following chapters, are discussed. For more information visit the official page of gnuplot http://gnuplot.info. Try the rich demo gallery at http://gnuplot.info/screenshots/, where you can find the type of graph that you want to create and obtain an easy to use recipe for it. The book  [14] is an excellent place to look for many of gnuplot’s secrets43 .

You can start a gnuplot session with the gnuplot command:

> gnuplot  
 
  G N U P L O T  
  Version X.XX  
  ....  
  The gnuplot FAQ is available from www.gnuplot.info/faq/  
  ....  
Terminal type set to ’wxt’  
gnuplot>

There is a welcome message and then a prompt gnuplot> is issued waiting for your command. Type a command an press [Enter]. Type quit in order to quit the program. In the following, when we show a prompt gnuplot>, it is assumed that the command after the prompt is executed from within gnuplot.

Plotting a function is extremely easy. Use the command plot and x as the independent variable of the function44 . The command

gnuplot> plot x

plots the function y = f(x ) = x  which is a straight line with slope 1. In order to plot many functions simultaneously, you can write all of them in one line:

gnuplot> plot [-5:5][-2:4] x, x**2, sin(x),besj0(x)

The above command plots the functions x  , x2   , sinx  and J0(x )  . Within the square brackets [:], we set the limits of the x  and y  axes, respectively. The bracket [-5:5] sets −  5 ≤ x ≤ 5  and the bracket [-2:4] sets − 2 ≤  y ≤ 4  . You may leave the job of setting such limits to gnuplot, by omitting some, or all of them, from the respective positions in the brackets. For example, typing [1:][:5] changes the lower and upper limits of x  and y  and leaves the upper and lower limits unchanged45 .

In order to plot data points (xi,yi)  , we can read their values from files. Assume that a file data has the following numbers recorded in it:

# x  y1   y2  
0.5 1.0 0.779  
1.0 2.0 0.607  
1.5 3.0 0.472  
2.0 4.0 0.368  
2.5 5.0 0.287  
3.0 6.0 0.223

The first line is taken by gnuplot as a comment line, since it begins with a #. In fact, gnuplot ignores everything after a #. In order to plot the second column as a function of the first, type the command:

gnuplot> plot "data" using 1:2 with points

The name of the file is within double quotes. After the keyword using, we instruct gnuplot which columns to use as the x  and y  coordinates, respectively. The keywords with points instructs gnuplot to add each pair (xi,yi)  to the plot with points.

The command

gnuplot> plot "data" using 1:3 with lines

plots the third column as a function of the first, and the keywords with lines instruct gnuplot to connect each pair (x ,y )
  i  i  with a straight line segment.

We can combine several plots together in one plot:

gnuplot> plot   "data" using 1:3 with points, exp(-0.5*x)  
gnuplot> replot "data" using 1:2  
gnuplot> replot 2*x

The first line plots the 1st and 3rd columns in the file data together with the function e−x∕2   . The second line adds the plot of the 1st and 2nd columns in the file data and the third line adds the plot of the function 2x  .

There are many powerful ways to use the keyword using. Instead of column numbers, we can put mathematical expressions enclosed inside brackets, like using (...):(...). Gnuplot evaluates each expression within the brackets and plots the result. In these expressions, the values of each column in the file data are represented as in the awk language. $i are variables that expand to the number read from columns i=1,2,3,.... Here are some examples:

gnuplot> plot "data" using 1:($2*sin($1)*$3) with points  
gnuplot> replot 2*x*sin(x)*exp(-x/2)

The first line plots the 1st column of the file data together with the value yisin(xi)zi  , where yi  , xi  and zi  are the numbers in the 2nd, 1st and 3rd columns respectively. The second line adds the plot of the function 2x sin(x)e−x∕2   .

gnuplot> plot "data" using (log($1)):(log($2**2))  
gnuplot> replot 2*x+log(4)

The first line plots the logarithm of the 1st column together with the logarithm of the square of the 2nd column.

We can plot the data written to the standard output of any command. Assume that there is a program called area that prints the perimeter and area of a circle to the stdout in the form shown below:

> ./area  
R=    3.280000      area=    33.79851  
R=    6.280000      area=    123.8994  
R=    5.280000      area=    87.58257  
R=    4.280000      area=    57.54895

The interesting data is at the second and fourth columns. These can be plotted directly with the gnuplot command:

gnuplot> plot "< ./area" using 2:4

All we have to do is to type the full command after the < within the double quotes. We can create complicated filters using pipes as in the following example:

gnuplot> plot \  
 "< ./area|sort -g -k 2|awk ’{print log($2),log($4)}’" \  
 using 1:2

The filter produces data to the stdout, by combining the action of the commands area, sort and awk. The data printed by the last program is in two columns and we plot the results using 1:2.

In order to save plots in files, we have to change the terminal that gnuplot outputs the plots. Gnuplot can produce plots in several languages (e.g. PDF, postscript, SVG, LATEX, jpeg, png, gif, etc), which can be interpreted and rendered by external programs. By redirecting the output to a file, we can save the plot to the hard disk. For example:

gnuplot> plot "data" using 1:3  
gnuplot> set terminal jpeg  
gnuplot> set output "data.jpg"  
gnuplot> replot  
gnuplot> set output  
gnuplot> set terminal wxt

The first line makes the plot as usual. The second one sets the output to be in the JPEG format and the third one sets the name of the file to which the plot will be saved. The fourth lines repeats all the previous plotting commands and the fifth one closes the file data.jpg. The last line chooses the interactive terminal wxt to be the output of the next plot. High quality images are usually saved in the PDF, encapsulated postcript or SVG format. Use set terminal pdf,postscript eps or svg, respectively.

And now a few words for 3-dimensional (3d) plotting. The next example uses the command splot in order to make a 3d plot of the function f (x, y) = e−x2−y2   . After you make the plot, you can use the mouse in order to rotate it and view it from a different perspective:

gnuplot> set pm3d  
gnuplot> set hidden3d  
gnuplot> set size ratio 1  
gnuplot> set isosamples 50  
gnuplot> splot [-2:2][-2:2] exp(-x**2-y**2)

If you have data in the form (xi,yi,zi)  and you want to create a plot of zi = f(xi,yi)  , write the data in a file, like in the following example:

-1 -1 2.000  
-1  0 1.000  
-1  1 2.000  
 
 0 -1 1.000  
 0  0 0.000  
 0  1 1.000  
 
 1 -1 2.000  
 1  0 1.000  
 1  1 2.000

Note the empty line that follows the change of the value of the first column. If the name of the file is data3, then you can plot the data with the commands:

gnuplot> set pm3d  
gnuplot> set hidden3d  
gnuplot> set size ratio 1  
gnuplot> splot "data3" with lines

We close this section with a few words on parametric plots. A parametric plot on the plane (2-dimensions) is a curve (x(t),y(t))  , where t  is a parameter. A parametric plot in space (3-dimensions) is a surface (x(u, v)  ,y(u, v),  z (u, v))  , where (u,v)  are parameters. The following commands plot the circle (sin t,cost)  and the sphere (cos ucos v,  cosu sinv,  sin u)  :

gnuplot> set parametric  
gnuplot> plot sin(t),cos(t)  
gnuplot> splot cos(u)*cos(v),cos(u)*sin(v),sin(u)

1.6 Shell Scripting

Complicated system administration tasks are not among the strengths of the Fortran programming language. But in a typical GNU/Linux environment, there exist many powerful tools that can be used very effectively for this purpose. This way, one can use Fortran for the high performance and scientific computing part of the project and leave the administration and trivial data analysis tasks to other, external, programs.

One can avoid repeating the same sequence of commands by coding them in a file. An example can be found in the file script01.csh:

#!/bin/tcsh -f  
gfortran area_01.f90 -o area  
./area  
gfortran area_02.f90 -o area  
./area  
gfortran area_03.f90 -o area  
./area  
gfortran area_04.f90 -o area  
./area

This is a very simple shell script. The first line instructs the operating system that the lines that follow are to be interpreted by the program /bin/tcsh46 . This can be any program in the system, which in our case is the tcsh shell. The following lines are valid commands for the shell, one in each line. They compile the Fortran programs found in the files that we created in section 1.4 with gfortran, and then they run the executable ./area. In order to execute the commands in the file, we have to make sure that the file has the appropriate execute permissions. If not, we have to give the command:

> chmod u+x script01.csh

Then we simply type the path to the file script01.csh

> ./script01.csh

and the above commands are run the one after the other. Some of the versions of the programs that we wrote are asking for input from the stdin, which, normally, you have to type on the terminal. Instead of interacting directly with the program, we can write the input data to a file Input, and run the command

./area < Input

A more convenient solution is to use the, so called, “Here Document”. A “Here Document” is a section of the script that is treated as if it were a separate file. As such, it can be used as input to programs by sending its “contents” to the stdin of the command that runs the program47 . The “Here Document” does not appear in the filesystem and we don’t need to administer it as a regular file. An example of using a “Here Document” can be found in the file script02.csh:

#!/bin/tcsh -f  
gfortran area_04.f90 -o area  
./area <<EOF  
1.0  
2.0  
3.0  
4.0  
5.0  
6.0  
7.0  
8.0  
9.0  
10.0  
EOF

The stdin of the command ./area is redirected to the contents between the lines

./area <<EOF  
...  
EOF

The string EOF marks the beginning and the end of the “Here Document”, and can be any string you like. The last EOF has to be placed exactly in the beginning of the line.

The power of shell scripting lies in its programming capabilities: Variables, arrays, loops and conditionals can be used in order to create a complicated program. Shell variables can be used as discussed in section 1.1.2: The value of a variable name is $name and it can be set with the command set name = value. An array is defined, for example, by the command

set R = (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)

and its data can be accessed using the syntax $R[1] ... $R[10].

Lets take a look at the following script:

#!/bin/tcsh -f  
 
set files = (area_01.f90 area_02.f90 area_03.f90 area_04.f90)  
set R     = (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)  
 
echo "Hello $USER Today is " ‘date‘  
foreach file ($files)  
 echo "# ----------- Working on file $file "  
 gfortran $file -o area  
 ./area <<EOF  
$R[1]  
$R[2]  
$R[3]  
$R[4]  
$R[5]  
$R[6]  
$R[7]  
$R[8]  
$R[9]  
$R[10]  
EOF  
 echo "# ----------- Done "  
 if( -f AREA.DAT ) cat AREA.DAT  
end

The first two lines of the script define the values of the arrays files (4 values) and R (10 values). The command echo echoes its argument to the stdin. $USER is the name of the user running the script. ‘date‘ is an example of command substitution: When a command is enclosed between backquotes and is part of a string, then the command is executed and its stdout is pasted back to the string. In the example shown above, ‘date‘ is replaced by the current date and time in the format produced by the date command.

The foreach loop

foreach file ($files)  
 ...  
end

is executed once for each of the 4 values of the array files. Each time the value of the variable file is set equal to one of the values area_01.f90, area_02.f90, area_03.f90, area_04.f90. These values can be used by the commands in the loop. Therefore, the command gfortran $file -o area compiles a different file each time that it is executed by the loop.

The last line in the loop

 if( -f AREA.DAT ) cat AREA.DAT

is a conditional. It executes the command cat AREA.DAT if the condition -f AREA.DAT is true. In this case, -f constructs a logical expression which is true when the file AREA.DAT exists.

We close this section by presenting a more complicated and advanced script. It only serves as a demonstration of the shell scripting capabilities. For more information, the reader is referred to the bibliography  [1617181920]. Read carefully the commands, as well as the comments which follow the # mark. Then, write the commands to a file script04.csh48 , make it an executable file with the command chmod u+x script04.csh and give the command

> ./script04.csh This is my first serious tcsh script

The script will run with the words “This is my first serious tcsh script” as its arguments. Notice how these arguments are manipulated by the script. Then, the script asks for the values of the radii of ten or more circles interactively, so that it will compute their perimeter and area. Type them on the terminal and then observe the script’s output, so that you understand the function of each command. You will not regret the time investment!

#!/bin/tcsh -f  
# Run this script as:  
# ./script04.csh Hello this is a tcsh script  
#----------------------------------------------------------------------  
# ‘command‘ is command substitution: it is replaced by stdout of command  
set now = ‘date‘ ; set mypc = ‘uname -a‘  
# Print information: variables are expanded within double quotes  
echo "I am user $user working on the computer $HOST" #HOST is predefined  
echo "Today the date is      :  $now"                #now  is defined above  
echo "My home directory is   :  $home"               #home is predefined  
echo "My current directory is:  $cwd"                #cwd changes with cd  
echo "My computer runs       :  $mypc"               #mypc is defined above  
echo "My process id is       :  $$   "               #$$   is predefined  
# Manipulate the command line: ($#argv is number of elements in array argv)  
echo "The command line has $#argv arguments"  
echo "The name of the command I am running is: $0"  
echo "Arguments 3rd to last of the command   : $argv[3-]"    #third to last  
echo "The last argument is                   : $argv[$#argv]" #last element  
echo "All arguments                          : $argv"  
 
# Ask user for input: enter radii of circles  
echo -n "Enter radii of circles: " # variable $< stores one line of input  
set  Rs = ($<)  #Rs is now an array with all words entered by user  
if($#Rs < 10 )then #make a test, need at least 10 of them  
 echo "Need more than 10 radii. Exiting...."  
 exit(1)  
endif  
echo "You entered $#Rs radii, the first is $Rs[1] and the last $Rs[$#Rs]"  
echo "Rs= $Rs"  
# Now, compute the perimeter of each circle:  
foreach R ($Rs)  
 # -v rad=$R set the awk variable rad equal to $R. pi=atan2(0,-1)=3.14...  
 set l = ‘awk -v rad=$R ’BEGIN{print 2*atan2(0,-1)*rad}’‘  
 echo "Circle with R= $R has perimeter $l"  
end  
# alias defines a command to do what you want: use awk as a calculator  
alias acalc  ’awk "BEGIN{print \!* }"’ # \!* substitutes args of acalc  
echo "Using acalc to compute       2+3=" ‘acalc 2+3‘  
echo "Using acalc to compute cos(2*pi)=" ‘acalc cos(2*atan2(0,-1))‘  
# Now do the same loop over radii as above in a different way  
# while( expression ) is executed as long as "expression" is true  
while($#Rs > 0) #executed as long as $Rs contains radii  
 set R = $Rs[1] #take first element of $Rs  
 shift Rs       #now $Rs has one less element:old $Rs[1] has vanished  
 set a = ‘acalc atan2(0,-1)*${R}*${R}‘ # =pi*R*R calculated by acalc  
 # construct a filename to save the result from the value of R:  
 set file = area${R}.dat  
 echo "Circle with R= $R has area $a" > $file #save result in a file  
end             #end while  
# Now look for our files: save their names in an array files:  
set files = (‘ls -1 area*.dat‘)  
if( $#files == 0) echo "Sorry, no area files found"  
echo "--------------------------------------------"  
echo "files: $files"  
ls -l $files  
echo "--------------------------------------------"  
echo "And the results for the area are:"  
foreach f ($files)  
 echo -n "file ${f}: "  
 cat $f  
end  
# now play a little bit with file names:  
echo "--------------------------------------------"  
set f = $files[1] # test permissions on first file  
# -f, -r, -w, -x, -d test existence of file, rwxd permissions  
# the ! negates the expression (true -> false, false -> true)  
echo "testing permissions on files:"  
if(  -f $f     ) echo "$file exists"  
if(  -r $f     ) echo "$file is readable by me"  
if(  -w $f     ) echo "$file is writable by be"  
if(! -w /bin/ls) echo "/bin/ls is NOT writable by me"  
if(! -x $f     ) echo "$file is NOT an executable"  
if(  -x /bin/ls) echo "/bin/ls is executable by me"  
if(! -d $f     ) echo "$file is NOT a directory"  
if(  -d /bin   ) echo "/bin is a directory"  
echo "--------------------------------------------"  
# transform the name of a file  
set f = $cwd/$f       # add the full path in $f  
set filename  = $f:r  # removes extension .dat  
set extension = $f:e  # gets    extension .dat  
set fdir      = $f:h  # gets    directory of $f  
set base      = ‘basename $f‘ # removes directory name  
echo "file      is: $f"  
echo "filename  is: $filename"  
echo "extension is: $extension"  
echo "directory is: $fdir"  
echo "basename  is: $base"  
# now transform the name to one with different extension:  
set newfile = ${filename}.jpg  
echo "jpeg name is: $newfile"  
echo "jpeg base is:" ‘basename $newfile‘  
if($newfile:e == jpg)echo ‘basename $newfile‘ " is a picture"  
echo "--------------------------------------------"  
# Now save all data in a file using a "here document"  
# A here document starts with <<EOF and ends with a line  
# starting exactly with EOF (EOF can be any string as below)  
# In a "here document" we can use variables and command  
# substitution:  
cat <<AREAS >> areas.dat  
# This file contains the areas of circle of given radii  
# Computation done by ${user} on ${HOST}. Today is ‘date‘  
‘cat $files‘  
AREAS  
# now see what we got:  
if( -f areas.dat) cat areas.dat  
# You can use a "here document" as standard input to any command:  
# use gnuplot to save a plot: gnuplot does the job and exits...  
gnuplot <<GNU  
set terminal jpeg  
set output   "areas.jpg"  
plot "areas.dat" using 4:7 title "areas.dat",\  
     pi*x*x                title "pi*R^2"  
set output  
GNU  
# check our results: display the jpeg file using eog  
if( -f areas.jpg) eog areas.jpg &




awk search for and process patterns in a file,
cat display, or join, files
cd change working directory
chmod change the access mode of a file
cp copy files
date display current time and date
df display the amount of available disk space
diff display the differences between two files
du display information on disk usage
echo echo a text string to output
find find files
grep search for a pattern in files
gzip compress files in the gzip (.gz) format (gunzip to uncompress)
head display the first few lines of a file
kill send a signal (like KILL) to a process
locate search for files stored on the system (faster than find)
less display a file one screen at a time
ln create a link to a file
lpr print files
ls list information about files
man search information about command in man pages
mkdir create a directory
mv move and/or rename a file
ps report information on the processes run on the system
pwd print the working directory
rm remove (delete) files
rmdir remove (delete) a directory
sort sort and/or merge files
tail display the last few lines of a file
tar store or retrieve files from an archive file
top dynamic real-time view of processes
wc counts lines, words and characters in a file
whatis list man page entries for a command
where show where a command is located in the path (alternatively: whereis)
which locate an executable program using ”path”
zip create compressed archive in the zip format (.zip)
unzip get/list contents of zip archive



Table 1.1: Basic Unix commands.

Table 1.2: Some intrinsic functions in Fortran.


Function

Description



ABS

modulus of a complex number, absolute value of number

ACOS

arccosine of a number

ADJUSTL

moves non blank characters of a string to the left

ADJUSTR

moves non blank characters of a string to the right

AIMAG

imaginary part of a complex number

AINT

truncates fractional part but preserves data type

ANINT

rounds to nearest whole number but preserves data type

ASIN

arcsine of a number

ATAN

arctangent of a number

ATAN2

arctangent of arg1 divided by arg2 resolved into the correct quadrant

CMPLX

converts to the COMPLEX data type arg1 + i arg2

CONJG

complex conjugate of a complex number

COS

cosine of an angle in radians

COSH

hyperbolic cosine

DATE_AND_TIME

returns current date and time

DBLE

converts to the real(8) data type

DIM

if arg1 >  arg2, then returns arg1 - arg2; otherwise 0

DPROD

double precision product of two single precision numbers

EXP

exponential

EPSILON

Returns a positive number that is negligible compared to 1.0

HUGE

Returns the largest number of the same kind as the argument

INT

converts to the INTEGER data type by truncation

KIND

Returns the KIND value of argument

LEN

Returns the length of a string

LEN_TRIM

returns the length of a string without trailing blanks

LGE,LGT,LLE,LLT

string comparison functions

LOG

natural logarithm

LOG10

common logarithm

MAX

maximum value of arguments

MAXEXPONENT

returns the maximum exponent of the same kind as the argument

MIN

minimum value of arguments

MINEXPONENT

returns the minimum exponent of the same kind as the argument

MOD

arg1 modulo arg2

NINT

converts to the INTEGER data type by rounding

RANDOM_NUMBER

returns pseudo-random numbers 0 ≤ r < 1

RANDOM_SEED

starts random number generator or returns generator parameters

PRECISION

returns the decimal precision of the same kind as the argument

REAL

real part of a complex number

REAL

converts to the REAL data type

SIGN

if arg2 <  0, then returns -arg1; else +arg1

SIN

sine of an angle in radians

SINH

hyperbolic sine

SQRT

square root

TAN

tangent of an angle in radians

TANH

hyperbolic tangent

TINY

returns the smallest positive number of the same kind as the argument

TRIM

returns string with trailing blanks removed



Array functions


ALL

true if all values are true

ALLOCATED

array allocation status

ANY

true if any values are true

COUNT

number of elements in an array

DOT_PRODUCT

dot product of two rank-one arrays

LBOUND

lower dimension bounds of an array

MATMUL

matrix multiplication

MAXLOC

location of a maximum value in an array

MAXVAL

maximum value in an array

MERGE

merge arrays under mask

MINLOC

location of a minimum value in an array

MINVAL

minimum value in an array

PACK

pack an array into an array of rank one under a mask

PRODUCT

product of array elements

RESHAPE

reshape an array

SHAPE

shape of an array or scalar

SIZE

size of an array

SPREAD

replicate an array by adding a dimension

SUM

sum of array elements

TRANSPOSE

transpose an array of rank two

UBOUND

upper dimension bounds of an array

UNPACK

unpack an array of rank one into an array under a mask



Table 1.3: Basic Emacs commands.






Leaving Emacs



suspend Emacs (or iconify it under X) C-z
exit Emacs permanently C-x C-c



Files



read a file into Emacs C-x C-f
save a file back to disk C-x C-s
save all files C-x s
insert contents of another file into this bufferC-x i
toggle read-only status of buffer C-x C-q



Getting Help



The help system is simple. Type C-h (or F1) and follow the directions. If you are a first-time user, type C-h t for a tutorial.
remove help window C-x 1
apropos: show commands matching a string C-h a
describe the function a key runs C-h k
describe a function C-h f
get mode-specific information C-h m



Error Recovery



abort partially typed or executing command C-g
recover files lost by a system crash M-x recover-session
undo an unwanted change C-x u, C-_ or C-/
restore a buffer to its original contentsM-x revert-buffer
redraw garbaged screen C-l



Incremental Search



search forward C-s
search backward C-r
regular expression search C-M-s
abort current search C-g
Use C-s or C-r again to repeat the search in either direction. If Emacs is still searching, C-g cancels only the part not matched.



Motion



entity to move over backward forward
character C-b C-f
word M-b M-f
line C-p C-n
go to line beginning (or end) C-a C-e
go to buffer beginning (or end) M-< M->
scroll to next screen C-v
scroll to previous screen M-v
scroll left C-x <
scroll right C-x >
scroll current line to center of screen C-u C-l



Killing and Deleting



entity to kill backward forward
character (delete, not kill) DEL C-d
word M-DEL M-d
line (to end of) M-0 C-k C-k
kill region C-w
copy region to kill ring M-w
yank back last thing killed C-y
replace last yank with previous killM-y



Marking



set mark here C-@ or C-SPC
exchange point and mark C-x C-x
mark paragraph M-h
mark entire buffer C-x h



Query Replace



interactively replace a text string M-% or M-x query-replace
using regular expressions M-x query-replace-regexp



Buffers



select another buffer C-x b
list all buffers C-x C-b
kill a buffer C-x k



Multiple Windows



When two commands are shown, the second is a similar command for a frame instead of a window.
delete all other windows C-x 1    C-x 5 1
split window, above and below C-x 2    C-x 5 2
delete this window C-x 0    C-x 5 0
split window, side by side C-x 3
switch cursor to another window C-x o C-x 5 o
grow window taller C-x ^
shrink window narrower C-x {
grow window wider C-x }



Formatting



indent current line (indent code etc)TAB
insert newline after point C-o
fill paragraph M-q



Case Change



uppercase word M-u
lowercase word M-l
capitalize word M-c
uppercase region C-x C-u
lowercase region C-x C-l



The Minibuffer



The following keys are defined in the minibuffer.
complete as much as possible TAB
complete up to one word SPC
complete and execute RET
abort command C-g
Type C-x ESC ESC to edit and repeat the last command that used the minibuffer. Type F10 to activate menu bar items on text terminals.



Spelling Check



check spelling of current word M-$
check spelling of all words in region M-x ispell-region
check spelling of entire buffer M-x ispell-buffer
On the fly spell checking M-x flyspell-mode



Info – Getting Help Within Emacs



enter the Info documentation readerC-h i
scroll forward SPC
scroll reverse DEL
next node n
previous node p
move up u
select menu item by name m
return to last node you sawl
return to directory node d
go to top node of Info file t
go to any node by name g
quit Info q



Chapter 2
Kinematics

In this chapter we show how to program simple kinematic equations of motion of a particle and how to do basic analysis of numerical results. We use simple methods for plotting and animating trajectories on the two dimensional plane and three dimensional space. In section 2.3 we study numerical errors in the calculation of trajectories of freely moving particles bouncing off hard walls and obstacles. This will be a prelude to the study of the integration of the dynamical equations of motion that we will introduce in the following chapters.

2.1 Motion on the Plane

When a particle moves on the plane, its position can be given in Cartesian coordinates (x(t),y(t))  . These, as a function of time, describe the particle’s trajectory. The position vector is ⃗r(t) = x(t)xˆ+  y(y)ˆy  , where ˆx  and ˆy  are the unit vectors on the x  and y  axes respectively. The velocity vector is ⃗v(t) = vx(t)ˆx + vy(t)ˆy  where

          ⃗v(t)  =   d⃗r(t)
                     dt
        dx (t)              dy (t)
vx (t) = -----       vy(t) = ----- ,                (2.1)
          dt                 dt
The acceleration ⃗a(t) = ax(t)ˆx + ay(t)ˆy  is given by
                   d⃗v(t)      d2⃗r(t)
            ⃗a(t) = -----  =   ------
                    dt         dt2
        dvx-(t)    d2x(t)              dvy(t)   d2y(t)
ax (t) =   dt   =   dt2        ay(t) =   dt  =   dt2  .      (2.2)

pict

Figure 2.1: The trajectory of a particle moving in the plane. The figure shows its position vector ⃗r  , velocity ⃗v  and acceleration ⃗a  and their Cartesian components in the chosen coordinate system at a point of the trajectory.

In this section we study the kinematics of a particle trajectory, therefore we assume that the functions (x(t),y(t))  are known. By taking their derivatives, we can compute the velocity and the acceleration of the particle in motion. We will write simple programs that compute the values of these functions in a time interval [t0,tf]  , where t0   is the initial and tf  is the final time. The continuous functions x(t),y(t),vx (t),vy(t)  are approximated by a discrete sequence of their values at the times t0,t0 + δt,t0 + 2δt,t0 + 3δt,...  such that t0 + nδt ≤ tf  .


pict

Figure 2.2: The flowchart of a typical program computing the trajectory of a particle from its (kinematic) equations of motion.

We will start the design of our program by forming a generic template to be used in all of the problems of interest. Then we can study each problem of particle motion by programming only the equations of motion without worrying about the less important tasks, like input/output, user interface etc. Figure 2.2 shows a flowchart of the basic steps in the algorithm. The first part of the program declares variables and defines the values of the fixed parameters (like π =  3.1459 ...  , g = 9.81  , etc). The program starts by interacting with the user (“user interface”) and asks for the values of the variables x0   , y
 0   , t
 0   , t
 f  , δt...  . The program prints these values to the stdout so that the user can check them for correctness and store them in her data.

The main calculation is performed in a loop executed while t ≤ tf  . The values of the positions and the velocities x(t),y(t),vx(t),vy(t)  are calculated and printed in a file together with the time t  . At this point we fix the format of the program output, something that is very important to do it in a consistent and convenient way for easing data analysis. We choose to print the values t, x, y, vx, vy in five columns in each line of the output file.

The specific problem that we are going to solve is the computation of the trajectory of the circular motion of a particle on a circle with center (x0,y0)  and radius R  with constant angular velocity ω  . The position on the circle can be defined by the angle 𝜃  , as can be seen in figure 2.3. We define the initial position of the particle at time t
 0   to be 𝜃(t ) = 0
   0  .


pict

Figure 2.3: The trajectory of a particle moving on a circle with constant angular velocity calculated by the program Circle.f90.

The equations giving the position of the particle at time t  are

x(t) =   x0 + R cos(ω (t − t0))
y(t) =   y0 + R sin (ω(t − t0)) .                 (2.3)
Taking the derivative w.r.t. t  we obtain the velocity
vx(t)  =   − ωR sin (ω(t − t0))
v (t)  =   ωR cos(ω (t − t )),                  (2.4)
 y                       0
and the acceleration
ax(t)  =  − ω2R  cos(ω(t − t0)) = − ω2(x (t) − x0)
              2                     2
ay(t)  =  − ω R  sin (ω(t − t0)) = − ω (y(t) − y0).         (2.5)
We note that the above equations imply that ⃗
R ⋅⃗v =  0  (⃗
R ≡  ⃗r − ⃗r0   ,      ⃗
⃗v ⊥ R  , ⃗v tangent to the trajectory) and ⃗a = − ω2 ⃗R  (R⃗  and ⃗a  anti-parallel, ⃗a ⊥ ⃗v  ).

The data structure is quite simple. The constant angular velocity ω  is stored in the REAL variable omega. The center of the circle (x ,y )
  0  0  , the radius R  of the circle and the angle 𝜃  are stored in the REAL variables x0, y0, R, theta. The times at which we calculate the particle’s position and velocity are defined by the parameters t0,tf ,δt  and are stored in the REAL variables t0, tf, dt. The current position (x(t),y (t))  is calculated and stored in the REAL variables x, y and the velocity (vx(t),vy(t))  in the REAL variables vx, vy. The declarations of the variables are put in the beginning of the program:

 real :: x0,y0,R,x,y,vx,vy,t,t0,tf,dt  
 real :: theta,omega  
 real, parameter :: PI=3.1415927

were we defined the value1 of π = 3.1415927  by using the parameter specification.

The user interface of the program is the interaction of the program with the user and, in our case, it is the part of the program where the user enters the parameters omega, x0, y0, R, t0, tf, dt. The program issues a prompt with the names the variables expected to be read. This is done using simple print statements. The variables are read from the stdin by simple read statements and the values entered by the user are printed to the stdout2 :

 print *,’# Enter omega:’  
 read  *,omega  
 print *,’# Enter center of circle (x0,y0) and radius R:’  
 read  *,x0,y0,R  
 print *,’# Enter t0,tf,dt:’  
 read  *,t0,tf,dt  
 print *,’# omega= ’,omega  
 print *,’# x0= ’,x0,’ y0= ’,y0,’ R= ’,R  
 print *,’# t0= ’,t0,’ tf= ’,tf,’ dt= ’,dt

Next, the program initializes the state of the computation. This includes checking the validity of the parameters entered by the user, so that the computation will be possible. For example, the program computes the expression 2.0*PI/omega, where it is assumed that omega has a non zero value. We will also demand that R  > 0  and ω > 0  . An if statement will make those checks and if the parameters have illegal values, the stop statement will stop the program execution3 . The program opens the file Circle.dat for writing the calculated values of the position and the velocity of the particle.

 if(R     .le. 0.0) stop ’Illegal value of R’  
 if(omega .le. 0.0) stop ’Illegal value of omega’  
 print *,’# T= ’,2.0*PI/omega  
 open(unit=11,file=’Circle.dat’)

If R  ≤ 0  or ω ≤ 0  the corresponding stop statements are executed which end the program execution. The optional error messages are included after the stop statements which are printed to the stdout. The value of the period T = 2 π∕ω  is also calculated and printed for reference.

The open statement uses unit 11 for writing to the file Circle.dat. The choice of the unit number is free for the programmer to choose. We recommend using the units 10 to 99 for input/output to files4 .

The main calculation is performed within the loop

 t   =  t0  
 do while(t .le. tf)  
  .........  
  t  =  t + dt  
 enddo

The first statement sets the initial value of the time. The statements between the do while(condition) and enddo are executed as long as condition has a .TRUE. value. The statement t=t+dt increments the time and this is necessary in order not to enter into an infinite loop. he commands put in place of the dots ......... calculate the position and the velocity and print them to the file Circle.dat:

  theta = omega * (t-t0)  
  x  =  x0+R*cos(theta)  
  y  =  y0+R*sin(theta)  
  vx =  -omega*R*sin(theta)  
  vy =   omega*R*cos(theta)  
  write(11,*)t,x,y,vx,vy

Notice the use of the intrinsic functions sin and cos that calculate the sine and cosine of an angle expressed in radians. We use the intermediate variable theta in order to store the phase 𝜃(t) = ω(t − t0)  . The command write(11,*) writes the variables t,x,y,vx,vy to the unit 11, which has been associated to the file Circle.dat with the open statement shown above.

The program is stored in the file Circle.f90 and can be found in the accompanied software. The extension .f90 is used by the compiler in order to denote source code written in free format Fortran language. Compilation and running can be done using the commands:

> gfortran Circle.f90 -o cl  
> ./cl

The switch -o cl forces the compiler gfortran to write the binary commands executed by the program to the file5 cl. The command ./cl loads the program instructions to the computer memory for execution. When the programs starts execution, it first asks for the parameter data and then performs the calculation. A typical session looks like:

> gfortran Circle.f90 -o cl  
> ./cl  
 # Enter omega:  
1.0  
 # Enter center of circle (x0,y0) and radius R:  
1.0 1.0 0.5  
 # Enter t0,tf,dt:  
0.0 20.0 0.01  
 # omega=   1.  
 # x0=   1. y0=   1. R=   0.5  
 # t0=   0. tf=   20. dt=   0.00999999978  
 # T=   6.28318548

The lines shown above that start with a # character are printed by the program and lines without # are the values of the parameters entered interactively by the user. The user types in the parameters and then presses the Enter key in order for the program to read them. Here we have ω = 1.0  , x0 = y0 = 1.0  , R =  0.5  , t  = 0.0
 0  , t =  20.0
 f  and δt = 0.01  .

You can execute the above program many times for different values of the parameter by writing the parameter values in a file using an editor. For example, in the file Circle.in type the following data:

1.0            omega  
1.0  1.0 0.5   (x0, y0) , R  
0.0 20.0 0.01  t0 tf dt

Each line has the parameters read by the program with a read statement (a record). The rest of the line is ignored by the program and the user can write anything she likes as a comment on how to use the parameters. The program can read the above values of the parameters with the command:

> ./cl < Circle.in > Circle.out

The command ./cl runs the commands found in the executable file ./cl. The < Circle.in redirects the contents of the file Circle.in to the standard input (stdin) of the command ./cl. This way the program reads in the values of the parameters from the contents of the file Circle.in. The > Circle.out redirects the standard output (stdout) of the command ./cl to the file Circle.out. Its contents can be inspected after the execution of the program with the command cat:

> cat Circle.out  
 # Enter omega:  
 # Enter center of circle (x0,y0) and radius R:  
 # Enter t0,tf,dt:  
 # omega=   1.  
 # x0=   1. y0=   1. R=   0.5  
 # t0=   0. tf=   20. dt=   0.00999999978  
 # T=   6.28318548

We list the full program in Circle.f90 below:

!============================================================  
!File Circle.f90  
!Constant angular velocity circular motion  
!Set (x0,y0) center of circle, its radius R and omega.  
!At t=t0, the particle is at theta=0  
!------------------------------------------------------------  
program Circle  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real :: x0,y0,R,x,y,vx,vy,t,t0,tf,dt  
 real :: theta,omega  
 real, parameter :: PI=3.1415927  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter omega:’  
 read  *,omega  
 print *,’# Enter center of circle (x0,y0) and radius R:’  
 read  *,x0,y0,R  
 print *,’# Enter t0,tf,dt:’  
 read  *,t0,tf,dt  
 print *,’# omega= ’,omega  
 print *,’# x0= ’,x0,’ y0= ’,y0,’ R= ’,R  
 print *,’# t0= ’,t0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 if(R     .le. 0.0) stop ’Illegal value of R’  
 if(omega .le. 0.0) stop ’Illegal value of omega’  
 print *,’# T= ’,2.0*PI/omega  
 open(unit=11,file=’Circle.dat’)  
!------------------------------------------------------------  
!Compute:  
 t   =  t0  
 do while(t .le. tf)  
  theta = omega * (t-t0)  
  x  =  x0+R*cos(theta)  
  y  =  y0+R*sin(theta)  
  vx =  -omega*R*sin(theta)  
  vy =   omega*R*cos(theta)  
  write(11,*)t,x,y,vx,vy  
  t  =  t + dt  
 enddo  
 close(11)  
end program Circle

2.1.1 Plotting Data

We use gnuplot for plotting the data produced by our programs. The file Circle.dat has the time t and the components x, y, vx, vy in five columns. Therefore we can plot the functions x(t)  and y (t)  by using the gnuplot commands:

gnuplot> plot   "Circle.dat" using 1:2 with lines title "x(t)"  
gnuplot> replot "Circle.dat" using 1:3 with lines title "y(t)"


pict pict

Figure 2.4: The plots (x(t),y(t))  (left) and 𝜃(t)  (right) from the data in Circle.dat for ω = 1.0  , x0 = y0 = 1.0  , R = 0.5  , t0 = 0.0  , tf = 20.0  and δt = 0.01  .

The second line puts the second plot together with the first one. The results can be seen in figure 2.4.

Let’s see now how we can make the plot of the function 𝜃(t)  . We can do that using the raw data from the file Circle.dat within gnuplot, without having to write a new program. Note that           −1
𝜃(t) = tan   ((y − y0)∕(x − x0))  . The function atan2 is available in gnuplot6 as well as in Fortran. Use the online help system in gnuplot in order to see its usage:

gnuplot> help atan2  
 The ‘atan2(y,x)‘ function returns the arc tangent (inverse  
 tangent) of the ratio of the real parts of its arguments.  
 ‘atan2‘ returns its argument in radians or degrees, as  
 selected by ‘set angles‘, in the correct quadrant.

Therefore, the right way to call the function is atan2(y-y0,x-x0). In our case x0=y0=1 and x, y are in the 2nd and 3rd columns of the file Circle.dat. We can construct an expression after the using command as in page 153, where $2 is the value of the second and $3 the value of the third column:

gnuplot> x0 = 1 ; y0 = 1  
gnuplot> plot "Circle.dat" using 1:(atan2($3-y0,$2-x0)) \  
                           with lines title "theta(t)",pi,-pi

The second command is broken in two lines by using the character so that it fits conveniently in the text7 . Note how we defined the values of the variables x0, y0 and how we used them in the expression atan2($3-x0,$2-y0). We also plot the lines which graph the constant functions f1(t) = π  and f2(t) = − π  which mark the limit values of 𝜃(t)  . The gnuplot variable8 pi is predefined and can be used in formed expressions. The result can be seen in the left plot of figure 2.4.

The velocity components (vx(t),vy(t))  as function of time as well as the trajectory ⃗r(t)  can be plotted with the commands:

gnuplot> plot   "Circle.dat" using 1:4 title "v_x(t)" \  
                             with lines  
gnuplot> replot "Circle.dat" using 1:5 title "v_y(t)" \  
                             with lines  
gnuplot> plot   "Circle.dat" using 2:3 title "x-y"  
                             with lines


pict

Figure 2.5: The particle trajectory plotted by the gnuplot program in the file animate2D.gnu of the accompanied software. The position vector is shown at a given time t, which is marked on the title of the plot together with the coordinates (x,y). The data is produced by the program Circle.f90 described in the text.

We close this section by showing how to do a simple animation of the particle trajectory using gnuplot. There is a file animate2D.gnu in the accompanied software which you can copy in the directory where you have the data file Circle.dat. We are not going to explain how it works9 but how to use it in order to make your own animations. The final result is shown in figure 2.5. All that you need to do is to define the data file10 , the initial time t0, the final time tf and the time step dt. These times can be different from the ones we used to create the data in Circle.dat. A full animation session can be launched using the commands:

gnuplot> file = "Circle.dat"  
gnuplot> set xrange [0:1.6]; set yrange [0:1.6]  
gnuplot> t0   = 0; tf = 20 ; dt = 0.1  
gnuplot> load "animate2D.gnu"

The first line defines the data file that animate2D.gnu reads data from. The second line sets the range of the plots and the third line defines the time parameters used in the animation. The final line launches the animation. If you want to rerun the animation, you can repeat the last two commands as many times as you want using the same or different parameters. E.g. if you wish to run the animation at “half the speed” you should simply redefine dt=0.05 and set the initial time to t0=0:

gnuplot> t0   = 0; dt = 0.05  
gnuplot> load "animate2D.gnu"

2.1.2 More Examples

We are now going to apply the steps described in the previous section to other examples of motion on the plane. The first problem that we are going to discuss is that of the small oscillations of a simple pendulum. Figure 2.6 shows the single oscillating degree of freedom 𝜃(t)  , which is the small angle that the pendulum forms with the vertical direction.


pict

Figure 2.6: The simple pendulum whose motion for 𝜃 ≪ 1  is described by the program SimplePendulum.f90.

The motion is periodic with angular frequency      ∘ ---
ω =    g∕l  and period T  = 2π∕ω  . The angular velocity is computed from ˙𝜃 ≡ d𝜃∕dt  which gives

𝜃(t) =   𝜃0cos (ω(t − t0))

˙𝜃(t) =   − ω𝜃0 sin (ω(t − t0))                  (2.6)
We have chosen the initial conditions 𝜃 (t0) = 𝜃0   and 𝜃˙(t0) = 0  . In order to write the equations of motion in the Cartesian coordinate system shown in figure 2.6 we use the relations
x (t)  =   lsin (𝜃(t))
 y(t)  =   − lcos(𝜃(t))

vx(t)  =   dx(t) = l˙𝜃(t)cos(𝜃(t))
            dt
          dy(t)    ˙
vy(t)  =    dt   = l𝜃(t)sin (𝜃(t)).                (2.7)
These are similar to the equations (2.3) and (2.4) that we used in the case of the circular motion of the previous section. Therefore the structure of the program is quite similar. Its final form, which can be found in the file SimplePendulum.f90, is:
!==============================================================  
!File SimplePendulum.f90  
!Set pendulum original position at theta0 with no initial speed  
!--------------------------------------------------------------  
program SimplePendulum  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real :: l,x,y,vx,vy,t,t0,tf,dt  
 real :: theta,theta0,dtheta_dt,omega  
 real, parameter ::PI=3.1415927,g=9.81  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter l: ’  
 read  *,l  
 print *,’# Enter theta0:’  
 read  *,theta0  
 print *,’# Enter t0,tf,dt:’  
 read  *,t0,tf,dt  
 print *,’# l=  ’,l ,’ theta0= ’,theta0  
 print *,’# t0= ’,t0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 omega = sqrt(g/l)  
 print *,’# omega= ’,omega,’ T= ’,2.0*PI/omega  
 open(unit=11,file=’SimplePendulum.dat’)  
!------------------------------------------------------------  
!Compute:  
 t   =  t0  
 do while(t .le. tf)  
  theta     =        theta0*cos(omega*(t-t0))  
  dtheta_dt = -omega*theta0*sin(omega*(t-t0))  
  x  =  l*sin(theta)  
  y  = -l*cos(theta)  
  vx =  l*dtheta_dt*cos(theta)  
  vy =  l*dtheta_dt*sin(theta)  
  write(11,100)t,x,y,vx,vy,theta,dtheta_dt  
  t  =  t + dt  
 enddo  
 close(11)  
100 FORMAT(7G15.7)  
end program SimplePendulum

We note that the acceleration of gravity g  is hard coded in the program and that the user can only set the length l  of the pendulum. The data file SimplePendulum.dat produced by the program, contains two extra columns with the current values of 𝜃(t)  and the angular velocity ˙𝜃(t)  . The statement write(11,100) writes to the unit 11 according to the format set by the FORMAT statement, found in the line labeled by the label 100. This is done so that we can be sure that the data is printed in one line for each value of t  (see the discussion on page 119).

A simple session for the study of the above problem is shown below11 :

> gfortran SimplePendulum.f90 -o sp  
> ./sp  
 # Enter l:  
1.0  
 # Enter theta0:  
0.314  
 # Enter t0,tf,dt:  
0 20 0.01  
 # l=       1.       theta0=   0.31400001  
 # t0=      0.       tf=       20. dt=   0.00999999978  
 # omega=   3.132092 T=        2.0060668  
> gnuplot  
gnuplot> plot   "SimplePendulum.dat" u 1:2 w l t "x(t)"  
gnuplot> plot   "SimplePendulum.dat" u 1:3 w l t "y(t)"  
gnuplot> plot   "SimplePendulum.dat" u 1:4 w l t "v_x(t)"  
gnuplot> replot "SimplePendulum.dat" u 1:5 w l t "v_y(t)"  
gnuplot> plot   "SimplePendulum.dat" u 1:6 w l t "theta(t)"  
gnuplot> replot "SimplePendulum.dat" u 1:7 w l t "theta’(t)"  
gnuplot> plot   [-0.6:0.6][-1.1:0.1] "SimplePendulum.dat" \  
                                     u 2:3 w l t "x-y"  
gnuplot> file = "SimplePendulum.dat"  
gnuplot> t0=0;tf=20.0;dt=0.1  
gnuplot> set xrange  [-0.6:0.6];set yrange [-1.1:0.1]  
gnuplot> load "animate2D.gnu"

The next example is the study of the trajectory of a particle shot near the earth’s surface12 when we consider the effect of air resistance to be negligible. Then, the equations describing the trajectory of the particle and its velocity are given by the parametric equations

 x (t)  =   v0xt
                 1
 y (t)  =   v0yt −--gt2
                 2
vx (t)  =   v0x
 vy(t)  =   v0y − gt,                        (2.8)
where t  is the parameter. The initial conditions are x(0) = y(0) = 0  , vx(0) = v0x = v0cos 𝜃  and vy(0 ) = v0y = v0 sin 𝜃  , as shown in figure 2.7.

pict

Figure 2.7: The trajectory of a particle moving under the influence of a constant gravitational field. The initial conditions are set to x(0) = y(0) = 0  , vx(0) = v0x = v0cos𝜃  and vy(0) = v0y = v0sin 𝜃  .

The structure of the program is similar to the previous ones. The user enters the magnitude of the particle’s initial velocity and the shooting angle 𝜃  in degrees. The initial time is taken to be t0 = 0  . The program calculates v0x  and v
 0y  and prints them to the stdout. The data is written to the file Projectile.dat. The full program is listed below and it can be found in the file Projectile.f90 in the accompanied software:

!============================================================  
!File Projectile.f90  
!Shooting a projectile near the earth surface.  
!No air resistance.  
!Starts at (0,0), set (v0,theta).  
!------------------------------------------------------------  
program Projectile  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real :: x0,y0,R,x,y,vx,vy,t,tf,dt  
 real :: theta,v0x,v0y,v0  
 real, parameter :: PI=3.1415927,g=9.81  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter v0,theta (in degrees):’  
 read  *,v0,theta  
 print *,’# Enter tf,dt:’  
 read  *, tf,dt  
 print *,’# v0= ’,v0,’ theta= ’,theta,’o (degrees)’  
 print *,’# t0= ’,0.0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 if( v0    .le. 0.0) stop ’Illegal value of v0<=0’  
 if( theta .le. 0.0 .or. theta .ge. 90.0) &  
      stop ’Illegal value of theta’  
 theta  = (PI/180.0)*theta !convert to radians  
 v0x    = v0*cos(theta)  
 v0y    = v0*sin(theta)  
 print *,’# v0x = ’,v0x,’ v0y= ’,v0y  
 open(unit=11,file=’Projectile.dat’)  
!------------------------------------------------------------  
!Compute:  
 t   =  0.0  
 do while(t .le. tf)  
  x  =  v0x * t  
  y  =  v0y * t - 0.5*g*t*t  
  vx =  v0x  
  vy =  v0y     -     g*t  
  write(11,*)t,x,y,vx,vy  
  t  =  t + dt  
 enddo  
 close(11)  
end program Projectile

A typical session for the study of this problem is shown below:

> gfortran Projectile.f90 -o pj  
> ./pj  
 # Enter v0,theta (in degrees):  
10 45  
 # Enter tf,dt:  
1.4416 0.001  
 # v0=    10.0000000   theta= 45.000000  o (degrees)  
 # t0=     0.0000000   tf=     1.4416000 dt=   1.00000005E-03  
 # v0x =   7.0710678   v0y=    7.0710678  
> gnuplot  
gnuplot> plot   "Projectile.dat" using 1:2 w l t "x(t)"  
gnuplot> replot "Projectile.dat" using 1:3 w l t "y(t)"  
gnuplot> plot   "Projectile.dat" using 1:4 w l t "v_x(t)"  
gnuplot> replot "Projectile.dat" using 1:5 w l t "v_y(t)"  
gnuplot> plot   "Projectile.dat" using 2:3 w l t "x-y"  
gnuplot> file = "Projectile.dat"  
gnuplot> set xrange [0:10.3];set yrange [0:10.3]  
gnuplot> t0=0;tf=1.4416;dt=0.05  
gnuplot> load "animate2D.gnu"

Next, we will study the effect of air resistance of the form ⃗F = − mk ⃗v  . The solutions to the equations of motion


pict

Figure 2.8: The forces that act on the particle of figure 2.7 when we assume air resistance of the form ⃗
F = − mk⃗v  .

a   =   dvx-=  − kv
 x       dt        x
        dvy
ay  =   ----=  − kvy − g                     (2.9)
         dt
with initial conditions x(0) = y (0 ) = 0  , vx(0) = v0x = v0 cos𝜃  and vy(0) = v0y = v0 sin 𝜃  are13
              −kt
vx(t)  =  v(0xe     )
v (t)  =    v  + -g  e−kt − g-
 y           0y  k          k
           v0x-(     −kt)
 x(t)  =   k   1 − e
           1(       g) (        )   g
 y(t)  =   -- v0y + --  1 − e−kt −  -t            (2.10)
           k        k               k

Programming the above equations is as easy as before, the only difference being that the user needs to provide the value of the constant k  . The full program can be found in the file ProjectileAirResistance.f90 and it is listed below:

!============================================================  
!File ProjectileAirResistance.f90  
!Shooting a projectile near the earth surface  
!with air resistance  
!Starts at (0,0), set k, (v0,theta).  
!------------------------------------------------------------  
program ProjectileAirResistance  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real :: x0,y0,R,x,y,vx,vy,t,tf,dt,k  
 real :: theta,v0x,v0y,v0  
 real, parameter :: PI=3.1415927,g=9.81  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter k, v0,theta (in degrees):’  
 read  *,k, v0,theta  
 print *,’# Enter tf,dt:’  
 read  *, tf,dt  
 print *,’# k = ’,k  
 print *,’# v0= ’,v0,’ theta= ’,theta,’o (degrees)’  
 print *,’# t0= ’,0.0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 if( v0    .le. 0.0) stop ’Illegal value of v0<=0’  
 if( k     .le. 0.0) stop ’Illegal value of k <=0’  
 if( theta .le. 0.0 .or. theta .ge. 90.0) &  
      stop ’Illegal value of theta’  
 theta  = (PI/180.0)*theta !convert to radians  
 v0x    = v0*cos(theta)  
 v0y    = v0*sin(theta)  
 print *,’# v0x = ’,v0x,’ v0y= ’,v0y  
 open(unit=11,file=’ProjectileAirResistance.dat’)  
!------------------------------------------------------------  
!Compute:  
 t   =  0.0  
 do while(t .le. tf)  
  x  =  (v0x/k)*(1.0-exp(-k*t))  
  y  =  (1.0/k)*(v0y+(g/k))*(1.0-exp(-k*t))-(g/k)*t  
  vx =  v0x*exp(-k*t)  
  vy =  (v0y+(g/k))*exp(-k*t)-(g/k)  
  write(11,*)t,x,y,vx,vy  
  t  =  t + dt  
 enddo  
 close(11)  
end program ProjectileAirResistance


pict pict

Figure 2.9: The plots of x(t)  ,y(t)  (left) and v (t)
 x  ,v (t)
 y  (right) from the data produced by the program ProjectileAirResistance.f90 for k = 5.0  , v0 = 10.0  , 𝜃 = π∕4  , tf = 0.91  and δt = 0.001  . We also plot the asymptotes of these functions as t → ∞ .


pict

Figure 2.10: Trajectories of the particles shot with v = 10.0
 0  , 𝜃 = π∕4  in the absence of air resistance and when the air resistance is present in the form  ⃗
F = − mk ⃗v  with k = 5.0  .

We also list the commands of a typical session of the study of the problem:

> gfortran ProjectileAirResistance.f90 -o pja  
> ./pja  
 # Enter k, v0,theta (in degrees):  
5.0 10.0 45  
 # Enter tf,dt:  
0.91 0.001  
 # k =   5.  
 # v0=   10. theta=   45.o (degrees)  
 # t0=   0. tf=   0.910000026 dt=   0.00100000005  
 # v0x =   7.07106781 v0y=   7.07106781  
> gnuplot  
gnuplot> v0x = 10*cos(pi/4) ; v0y = 10*sin(pi/4)  
gnuplot> g = 9.81 ; k = 5  
gnuplot> plot [:][:v0x/k+0.1]  "ProjectileAirResistance.dat" \  
         using 1:2 with lines title "x(t)",v0x/k  
gnuplot> replot                "ProjectileAirResistance.dat" \  
         using 1:3 with lines title "y(t)",\  
         -(g/k)*x+(g/k**2)+v0y/k  
gnuplot> plot [:][-g/k-0.6:]   "ProjectileAirResistance.dat" \  
         using 1:4 with lines title "v_x(t)",0  
gnuplot> replot                "ProjectileAirResistance.dat" \  
         using 1:5 with lines title "v_y(t)",-g/k  
gnuplot> plot                  "ProjectileAirResistance.dat" \  
         using 2:3 with lines title "With air resistance k=5.0"  
gnuplot> replot                "Projectile.dat"              \  
         using 2:3 with lines title "No air resistance k=0.0"  
gnuplot> file = "ProjectileAirResistance.dat"  
gnuplot> set xrange [0:1.4];set yrange [0:1.4]  
gnuplot> t0=0;tf=0.91;dt=0.01  
gnuplot> load "animate2D.gnu"

Long commands have been continued to the next line as before. We defined the gnuplot variables v0x, v0y, g and k to have the values that we used when running the program. We can use them in order to construct the asymptotes of the plotted functions of time. The results are shown in figures 2.9 and 2.10.

The last example of this section will be that of the anisotropic harmonic oscillator. The force on the particle is

Fx = − m ω21x    Fy =  − mω22y
(2.11)

where the “spring constants”          2
k1 =  mω 1   and          2
k2 = m ω 2   are different in the directions of the axes x  and y  . The solutions of the dynamical equations of motion for x(0) = A  , y(0) = 0  , vx(0) = 0  and vy(0 ) = ω2A  are

x (t)  =   A cos(ω t)    y(t) = A sin (ω t)
                 1                    2
vx(t)  =   − ω1A sin (ω1t)    vy(t) = ω2A cos(ω2t).      (2.12)
If the angular frequencies ω
 1   and ω
 2   satisfy certain relations, the trajectories of the particle are closed and self intersect at a given number of points. The proof of these relations, as well as their numerical confirmation, is left as an exercise for the reader. The program listed below is in the file Lissajoux.f90:
!============================================================  
!File Lissajous.f90  
!Lissajous curves (special case)  
!x(t)= cos(o1 t), y(t)= sin(o2 t)  
!------------------------------------------------------------  
program Lissajous  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real x0,y0,R,x,y,vx,vy,t,t0,tf,dt  
 real o1,o2,T1,T2  
 real, parameter :: PI=3.1415927  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter omega1 and omega2:’  
 read  *,o1,o2  
 print *,’# Enter tf,dt:’  
 read  *,tf,dt  
 print *,’# o1= ’,o1, ’ o2= ’,o2  
 print *,’# t0= ’,0.0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 if(o1.le.0.0 .or. o2.le.0.0) stop ’omega1 or omega2<=0’  
 T1 = 2.0*PI/o1  
 T2 = 2.0*PI/o2  
 print *,’# T1= ’,T1,’ T2= ’,T2  
 open(unit=11,file=’Lissajous.dat’)  
!------------------------------------------------------------  
!Compute:  
 t   =  0.0  
 do while(t .le. tf)  
  x  =  cos(o1*t)  
  y  =  sin(o2*t)  
  vx = -o1*sin(o1*t)  
  vy =  o2*cos(o2*t)  
  write(11,*)t,x,y,vx,vy  
  t  =  t + dt  
 enddo  
 close(11)  
end program Lissajous

We have set A  = 1  in the program above. The user must enter the two angular frequencies ω1   and ω2   and the corresponding times. A typical session for the study of the problem is shown below:

> gfortran  Lissajous.f90 -o lsj  
> ./lsj  
 # Enter omega1 and omega2:  
3 5  
 # Enter tf,dt:  
10.0 0.01  
 # o1=   3. o2=   5.  
 # t0=   0. tf=   10. dt=   0.00999999978  
 # T1=   2.09439516 T2=   1.2566371  
>gnuplot  
gnuplot> plot   "Lissajous.dat" using 1:2 w l t "x(t)"  
gnuplot> replot "Lissajous.dat" using 1:3 w l t "y(t)"  
gnuplot> plot   "Lissajous.dat" using 1:4 w l t "v_x(t)"  
gnuplot> replot "Lissajous.dat" using 1:5 w l t "v_y(t)"  
gnuplot> plot   "Lissajous.dat" using 2:3 w l t "x-y for 3:5"  
gnuplot> file = "Lissajous.dat"  
gnuplot> set xrange [-1.1:1.1];set yrange [-1.1:1.1]  
gnuplot> t0=0;tf=10;dt=0.1  
gnuplot> load "animate2D.gnu"

The results for ω1 = 3  and ω2 = 5  are shown in figure 2.11.


pict

Figure 2.11: The trajectory of the anisotropic oscillator with ω  = 3
  1  and ω  = 5
  2  .

2.2 Motion in Space

By slightly generalizing the methods described in the previous section, we will study the motion of a particle in three dimensional space. All we have to do is to add an extra equation for the coordinate z(t)  and the component of the velocity vz(t)  . The structure of the programs will be exactly the same as before.


pict

Figure 2.12: The conical pendulum of the program ConicalPendulum.f90.

The first example is the conical pendulum, which can be seen in figure 2.12. The particle moves on the xy  plane with constant angular velocity ω  . The equations of motion are derived from the relations

Tz = T cos 𝜃 = mg     Txy = T sin 𝜃 = m ω2r,
(2.13)

where r =  lsin 𝜃  . Their solution14 is

x(t)  =   rcosωt

y(t)  =   rsin ωt
z(t)  =   − lcos 𝜃,                     (2.14)
where we have to substitute the values
           g
cos𝜃  =   ----
          ω√2l---------
sin 𝜃  =     1 − cos2 𝜃
          g  sin 𝜃
   r  =   -2-----.                       (2.15)
          ω  cos𝜃
For the velocity components we obtain
vx  =   − rωsin ωt

vy  =   rω cosωt
vz  =   0.                              (2.16)
Therefore we must have
           ∘  --
              g
ω ≥ ωmin =    -,
              l
(2.17)

and when ω  → ∞ , 𝜃 → π ∕2  .

In the program that we will write, the user must enter the parameters l  , ω  , the final time tf  and the time step δt  . We take t0 = 0  . The convention that we follow for the output of the results is that they should be written in a file where the first 7 columns are the values of t  , x  , y  , z  , vx  , vy  and vz  . Each line in this file is long and, in order to prevent Fortran from breaking it into two separate lines, we have to give an explicit format specification. See the discussion on page 119. The full program is listed below:

!============================================================  
!File ConicalPendulum.f90  
!Set pendulum angular velocity omega and display motion in 3D  
!------------------------------------------------------------  
program ConicalPendulum  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real :: l,r,x,y,z,vx,vy,vz,t,tf,dt  
 real :: theta,cos_theta,sin_theta,omega  
 real, parameter :: PI=3.1415927,g=9.81  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter l,omega: ’  
 read  *,l,omega  
 print *,’# Enter tf,dt:’  
 read  *,tf,dt  
 print *,’# l=  ’,l           ,’ omega=     ’,omega  
 print *,’# T=  ’,2.0*PI/omega,’ omega_min= ’,sqrt(g/l)  
 print *,’# t0= ’,0.0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 cos_theta = g/(omega*omega*l)  
 if( cos_theta .ge. 1) stop ’cos(theta)>= 1’  
 sin_theta = sqrt(1.0-cos_theta*cos_theta)  
 z = -g/(omega*omega) !they remain constant throught  
 vz= 0.0              !the  motion  
 r =  g/(omega*omega)*sin_theta/cos_theta  
 open(unit=11,file=’ConicalPendulum.dat’)  
!------------------------------------------------------------  
!Compute:  
 t   = 0.0  
 do while(t .le. tf)  
  x  =  r*cos(omega*t)  
  y  =  r*sin(omega*t)  
  vx = -r*sin(omega*t)*omega  
  vy =  r*cos(omega*t)*omega  
  write(11,100)t,x,y,z,vx,vy,vz  
  t  =  t + dt  
 enddo  
 close(11)  
100 FORMAT(20G15.7)  
end program ConicalPendulum

In order to compile and run the program we can use the commands shown below:

> gfortran  ConicalPendulum.f90 -o cpd  
> ./cpd  
 # Enter l,omega:  
1.0 6.28  
 # Enter tf,dt:  
10.0 0.01  
 # l=    1. omega=       6.28000021  
 # T=    1.00050724 omega_min=   3.132092  
 # t0=   0. tf=   10. dt=   0.00999999978

The results are recorded in the file ConicalPendulum.dat. In order to plot the functions x(t)  , y(t)  , z(t)  , v (t)
 x  , v (t)
 y  , v (t)
 z  we give the following gnuplot commands:

> gnuplot  
gnuplot> plot   "ConicalPendulum.dat" u 1:2 w l t "x(t)"  
gnuplot> replot "ConicalPendulum.dat" u 1:3 w l t "y(t)"  
gnuplot> replot "ConicalPendulum.dat" u 1:4 w l t "z(t)"  
gnuplot> plot   "ConicalPendulum.dat" u 1:5 w l t "v_x(t)"  
gnuplot> replot "ConicalPendulum.dat" u 1:6 w l t "v_y(t)"  
gnuplot> replot "ConicalPendulum.dat" u 1:7 w l t "v_z(t)"

The results are shown in figure 2.13.


pict pict

Figure 2.13: The plots of the functions x(t),y(t),z(t),v (t),v (t),v (t)
             x    y    z  of the program ConicalPendulum.f90 for ω = 6.28  , l = 1.0  .

In order to make a three dimensional plot of the trajectory, we should use the gnuplot command splot:

gnuplot> splot "ConicalPendulum.dat" u 2:3:4 w l t "r(t)"


pict

Figure 2.14: The plot of the particle trajectory ⃗r(t)  of the program ConicalPendulum.f90 for ω = 6.28  , l = 1.0  . We can click and drag with the mouse on the window and rotate the curve and see it from a different angle. At the bottom left of the window, we see the viewing direction, given by the angles 𝜃 = 55.0  degrees (angle with the z  axis) and ϕ = 62  degrees (angle with the x  axis).

The result is shown in figure 2.14. We can click on the trajectory and rotate it and view it from a different angle. We can change the plot limits with the command:

gnuplot> splot [-1.1:1.1][-1.1:1.1][-0.3:0.0] \  
 "ConicalPendulum.dat" using 2:3:4 w l t "r(t)"

We can animate the trajectory of the particle by using the file animate3D.gnu from the accompanying software. The commands are similar to the ones we had to give in the two dimensional case for the planar trajectories when we used the file animate2D.gnu:

gnuplot> file = "ConicalPendulum.dat"  
gnuplot> set xrange [-1.1:1.1];set yrange [-1.1:1.1]  
gnuplot> set zrange [-0.3:0]  
gnuplot> t0=0;tf=10;dt=0.1  
gnuplot> load "animate3D.gnu"

The result can be seen in figure 2.15.


pict

Figure 2.15: The particle trajectory ⃗r(t)  computed by the program ConicalPendulum.f90 for ω = 6.28  , l = 1.0  and plotted by the gnuplot script animate3D.gnu. The title of the plot shows the current time and the particles coordinates.

The program animate3D.gnu can be used on the data file of any program that prints t x y z as the first words on each of its lines. All we have to do is to change the value of the file variable in gnuplot.

Next, we will study the trajectory of a charged particle in a homogeneous magnetic field ⃗
B = B ˆz  . At time t0   , the particle is at ⃗r0 = x0ˆx  and its velocity is ⃗v0 = v0yˆy + v0zˆz  , see figure 2.16.


pict

Figure 2.16: A particle at time t = 0
 0  is at the position ⃗r = x ˆx
 0   0  with velocity ⃗v0 = v0yˆy +v0zˆz  in a homogeneous magnetic field ⃗
B = Bˆz  .

The magnetic force on the particle is F⃗ = q(⃗v × ⃗B ) = qBvy ˆx − qBvx ˆy  and the equations of motion are

        dvx                qB
ax  =   ----= ωvy     ω ≡  ---
        dt                  m
ay  =   dvy-= − ωvx
        dt
az  =   0.                                    (2.18)
By integrating the above equations with the given initial conditions we obtain
vx (t)  =   v0y sin ωt

 vy(t)  =   v0y cosωt
 vz(t)  =   v0z.                           (2.19)
Integrating once more, we obtain the position of the particle as a function of time
         (      v0y)   v0y
x(t)  =    x0 + ---  − --- cosωt =  x0cosωt
          v      ω      ω                  v
y(t)  =   -0ysinωt =  − x0 sin ωt    x0 = − -0y
          ω                                 ω
z(t)  =  v0zt,                                        (2.20)
where we have chosen x0 = − v0y∕ω  . This choice places the center of the circle, which is the projection of the trajectory on the xy  plane, to be at the origin of the coordinate system. The trajectory is a helix with radius R  = − x0   and pitch v0zT =  2πv0z∕ω  .

We are now ready to write a program that calculates the trajectory given by (2.20) . The user enters the parameters v
 0   and 𝜃  , shown in figure 2.16, as well as the angular frequency ω  (Larmor frequency). The components of the initial velocity are v0y = v0cos 𝜃  and v0z = v0 sin 𝜃  . The initial position is calculated from the equation x0 = − v0y∕ ω  . The program can be found in the file ChargeInB.f90:

!===========================================================  
!File ChargeInB.f90  
!A charged particle of mass m and charge q enters a magnetic  
!field B in +z direction. It enters with velocity  
!v0x=0,v0y=v0 cos(theta),v0z=v0 sin(theta), 0<=theta<pi/2  
!at the position x0=-v0y/omega, omega=q B/m  
!  
!Enter v0 and theta and see trajectory from  
!t0=0 to tf at step dt  
!------------------------------------------------------------  
program ChargeInB  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real :: x,y,z,vx,vy,vz,t,tf,dt  
 real :: x0,y0,z0,v0x,v0y,v0z,v0  
 real :: theta,omega  
 real, parameter :: PI=3.1415927  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter omega: ’  
 read  *,omega  
 print *,’# Enter v0, theta (degrees):’  
 read  *,v0,theta  
 print *,’# Enter tf,dt:’  
 read  *,tf,dt  
 print *,’# omega=  ’,omega ,’ T=     ’,2.0*PI/omega  
 print *,’# v0=     ’,v0,    ’ theta= ’,theta,’o (degrees)’  
 print *,’# t0=     ’,0.0,   ’ tf=    ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 if(theta.lt.0.0 .or. theta.ge.90.0)stop ’Illegal 0<theta<90’  
 theta = (PI/180.0)*theta !convert to radians  
 v0y   = v0*cos(theta)  
 v0z   = v0*sin(theta)  
 print *,’# v0x= ’,0.0,’ v0y= ’,v0y,’ v0z= ’,v0z  
 x0    = - v0y/omega  
 print *,’# x0=  ’,x0, ’ y0=  ’,0.0,’ z0=  ’,0.0  
 print *,’# xy plane: Circle with center (0,0) and R= ’,ABS(x0)  
 print *,’# step of helix: s=v0z*T= ’,v0z*2.0*PI/omega  
 open(unit=11,file=’ChargeInB.dat’)  
!------------------------------------------------------------  
!Compute:  
 t   = 0.0  
 vz  = v0z  
 do while(t .le. tf)  
  x  =  x0*cos(omega*t)  
  y  = -x0*sin(omega*t)  
  z  =  v0z*t  
  vx =  v0y*sin(omega*t)  
  vy =  v0y*cos(omega*t)  
  write(11,100)t,x,y,z,vx,vy,vz  
  t  =  t + dt  
 enddo  
 close(11)  
100 FORMAT(20G15.7)  
end program ChargeInB

A typical session in which we calculate the trajectories shown in figures 2.17 and 2.18 is shown below:


pict pict

Figure 2.17: The plots of the x(t),y(t),z(t),v (t),v (t),v(t)
             x    y   z  functions calculated by the program in ChargeInB.f90 for ω = 6.28  , x0 = 1.0  , 𝜃 = 20  degrees.

> gfortran  ChargeInB.f90 -o chg  
> ./chg  
 # Enter omega:  
6.28  
 # Enter v0, theta (degrees):  
1.0 20  
 # Enter tf,dt:  
10 0.01  
 # omega=    6.28000021 T=       1.00050724  
 # v0=       1. theta=   20.o (degrees)  
 # t0=       0. tf=      10. dt=   0.00999999978  
 # v0x=   0. v0y=   0.939692616 v0z=   0.342020124  
 # x0=   -0.149632573 y0=    0. z0=    0.  
 # xy plane: Circle with center (0,0) and R=   0.149632573  
 # step of helix: s=v0z*T=   0.342193604  
> gnuplot  
gnuplot> plot   "ChargeInB.dat" u 1:2   w l title "x(t)"  
gnuplot> replot "ChargeInB.dat" u 1:3   w l title "y(t)"  
gnuplot> replot "ChargeInB.dat" u 1:4   w l title "z(t)"  
gnuplot> plot   "ChargeInB.dat" u 1:5   w l title "v_x(t)"  
gnuplot> replot "ChargeInB.dat" u 1:6   w l title "v_y(t)"  
gnuplot> replot "ChargeInB.dat" u 1:7   w l title "v_z(t)"  
gnuplot> splot  "ChargeInB.dat" u 2:3:4 w l title "r(t)"  
gnuplot> file = "ChargeInB.dat"  
gnuplot> set xrange [-0.65:0.65];set yrange [-0.65:0.65]  
gnuplot> set zrange [0:1.3]  
gnuplot> t0=0;tf=3.5;dt=0.1  
gnuplot> load "animate3D.gnu"


pict

Figure 2.18: The trajectory ⃗r(t)  calculated by the program in ChargeInB.f90 for ω = 6.28  , v0 = 1.0  , 𝜃 = 20  degrees as shown by the program animate3D.gnu. The current time and the coordinates of the particle are printed on the title of the plot.

2.3 Trapped in a Box

In this section we will study the motion of a particle that is free, except when bouncing elastically on a wall or on certain obstacles. This motion is calculated by approximate algorithms that introduce systematic errors. These types of errors15 are also encountered in the study of more complicated dynamics, but the simplicity of the problem will allow us to control them in a systematic and easy to understand way.

2.3.1 The One Dimensional Box

The simplest example of such a motion is that of a particle in a “one dimensional box”. The particle moves freely on the x  axis for 0 < x <  L  , as can be seen in figure 2.19. When it reaches the boundaries x = 0  and x =  L  it bounces and its velocity instantly reversed. Its potential energy is

       {
          0     0 < x < L
V(x) =    + ∞   elsewhere   ,
(2.21)

which has the shape of an infinitely deep well. The force F =  − dV (x )∕dx = 0  within the box and F = ± ∞ at the position of the walls.


pict

Figure 2.19: A particle in a one dimensional box with its walls located at x = 0  and x = L  .

Initially we have to know the position of the particle x0   as well as its velocity v0   (the sign of v0   depends on the direction of the particle’s motion) at time t0   . As long as the particle moves within the box, its motion is free and

x (t)  =   x +  v (t − t )
           0    0     0
v (t)  =   v0.                              (2.22)
For a small enough change in time δt  , so that there is no bouncing on the wall in the time interval (t,t + δt)  , we have that
x(t + δt) =   x(t) + v(t)δt
v(t + δt) =   v(t).                         (2.23)
Therefore we could use the above relations in our program and when the particle bounces off a wall we could simple reverse its velocity v(t) → − v(t)  . The devil is hiding in the word “when”. Since the time interval δt  is finite in our program, there is no way to know the instant of the collision with accuracy better than ∼ δt  . However, our algorithm will change the direction of the velocity at time t + δt  , when the particle will have already crossed the wall. This will introduce a systematic error, which is expected to decrease with decreasing δt  . One way to implement the above idea is by constructing the loop
 do while(t .le. tf)  
  write(11,*)t,x,v  
  x = x + v*dt  
  t = t + dt  
  if(x .lt. 0.0 .or. x .gt. L) v = -v  
 enddo

where the last line gives the testing condition for the wall collision and the subsequent change of the velocity.

The full program that realizes the proposed algorithm is listed below and can be found in the file box1D_1.f90. The user can set the size of the box L, the initial conditions x0 and v0 at time t0, the final time tf and the time step dt:

!============================================================  
!File box1D_1.f90  
!Motion of a free particle in a box  0<x<L  
!Use integration with time step dt: x = x + v*dt  
!------------------------------------------------------------  
program box1D  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real ::  L,x0,v0,t0,tf,dt,t,x,v  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter L:’  
 read  *,L  
 print *,’# L = ’,L  
 if( L .le. 0.0) stop ’L must be positive.’  
 print *,’# Enter x0,v0:’  
 read  *,x0,v0  
 print *,’# x0= ’,x0,’ v0= ’,v0  
 if(x0 .lt. 0.0 .or. x0 .gt. L) stop ’illegal value of x0.’  
 if(v0 .eq. 0.0               ) stop ’illegal value of v0 = 0.’  
 print *,’# Enter t0,tf,dt:’  
 read  *,t0,tf,dt  
 print *,’# t0= ’,t0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 t = t0  
 x = x0  
 v = v0  
 open(unit=11,file=’box1D_1.dat’)  
!------------------------------------------------------------  
!Compute:  
 do while(t .le. tf)  
  write(11,*)t,x,v  
  x = x + v*dt  
  t = t + dt  
  if(x .lt. 0.0 .or. x .gt. L) v = -v  
 enddo  
 close(11)  
end program box1D

The computed data is recorded in the file box1D_1.dat in three columns. Compiling, running and plotting the trajectory using gnuplot can be done as follows:

> gfortran box1D_1.f90 -o box1  
> ./box1  
 # Enter L:  
10  
 # L =   10.  
 # Enter x0,v0:  
0 1.0  
 # x0=   0. v0=   1.  
 # Enter t0,tf,dt:  
0 100 0.01  
 # t0=   0. tf=   100. dt=   0.00999999978  
> gnuplot  
gnuplot> plot "box1D_1.dat" using  1:2 w l title "x(t)",\  
                                    0 notitle,10 notitle  
gnuplot> plot [:][-1.2:1.2] "box1D_1.dat" \  
                            using  1:3 w l title "v(t)"


pict pict

Figure 2.20: The trajectory x(t)  of a particle in a box with L = 10  , x = 0.0
 0  , v0 = 1.0  , t0 = 0  , δt = 0.01  . The plot to the right magnifies a detail when t ≈ 90  which exposes the systematic errors in determining the exact moment of the collision of the particle with the wall at tk = 90  and the corresponding maximum value of x (t)  , xm = L = 10.0  .

The trajectory x(t)  is shown in figure 2.20. The effects of the systematic errors can be easily seen by noting that the expected collisions occur every T ∕2 = L ∕v = 10  units of time. Therefore, on the plot to the right of figure 2.20, the reversal of the particle’s motion should have occurred at t = 90  , x =  L = 10  .

The reader should have already realized that the above mentioned error can be made to vanish by taking arbitrarily small δt  . Therefore, we naively expect that as long as we have the necessary computer power to take δt  as small as possible and the corresponding time intervals as many as possible, we can achieve any precision that we want. Well, that is true only up to a point. The problem is that the next position is determined by the addition operation x+v*dt and the next moment in time by t+dt. Floating point numbers of the REAL type have a maximum accuracy of approximately 7 significant decimal digits. Therefore, if the operands x and v*dt are real numbers differing by more than 7 orders of magnitude (v*dt≲ 10− 7   x), the effect of the addition x+v*dt=x, which is null! The reason is that the floating point unit of the processor has to convert both numbers x and v*dt into a representation having the same exponent and in doing so, the corresponding significant digits of the smaller number v*dt are lost. The result is less catastrophic when v*dt≲  10−a  x with 0 < a < 7  , but some degree of accuracy is also lost at each addition operation. And since we have accumulation of such errors over many intervals t→ t+dt, the error can become significant and destroy our calculation for large enough times. A similar error accumulates in the determination of the next instant of time t+dt, but we will discuss below how to make this contribution to the total error negligible. The above mentioned errors can become less detrimental by using floating point numbers of greater accuracy than the REAL type. For example REAL(8) numbers have approximately 16 significant decimal digits. But again, the precision is finite and the same type of errors are there only to be revealed by a more demanding and complicated calculation.

The remedy to such a problem can only be a change in the algorithm. This is not always possible, but in the case at hand this is easy to do. For example, consider the equation that gives the position of a particle in free motion

x (t) = x0 + v0(t − t0).
(2.24)

Let’s use the above relation for the parts of the motion between two collisions. Then, all we have to do is to reverse the direction of the motion and reset the initial position and time to be the position and time of the collision. This can be done by using the loop:

 t = t0  
 do while(t .le. tf)  
  x = x0 + v0*(t-t0)  
  write(11,*)t,x,v0  
  if( x .lt. 0.0 .or. x .gt. L)then  
   x0 =  x  
   t0 =  t  
   v0 = -v0  
  endif  
  t   =  t + dt

In the above algorithm, the error in the time of the collision is not vanishing but we don’t have the “instability” problem of the dt→  0  limit16 . Therefore we can isolate and study the effect of each type of error. The full program that implements the above algorithm is given below and can be found in the file box1D_2.f90:

!============================================================  
!File box1D_2.f90  
!Motion of a free particle in a box  0<x<L  
!Use constant velocity equation: x = x0 + v0*(t-t0)  
!Reverse velocity and redefine x0,t0 on boundaries  
!------------------------------------------------------------  
program box1D  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real ::  L,x0,v0,t0,tf,dt,t,x,v  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter L:’  
 read  *,L  
 print *,’# L = ’,L  
 if( L .le. 0.0) stop ’L must be positive.’  
 print *,’# Enter x0,v0:’  
 read  *,x0,v0  
 print *,’# x0= ’,x0,’ v0= ’,v0  
 if(x0 .lt. 0.0 .or. x0 .gt. L) stop ’illegal value of x0.’  
 if(v0 .eq. 0.0               ) stop ’illegal value of v0 = 0.’  
 print *,’# Enter t0,tf,dt:’  
 read  *,t0,tf,dt  
 print *,’# t0= ’,t0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 t = t0  
 open(unit=11,file=’box1D_2.dat’)  
!------------------------------------------------------------  
!Compute:  
 do while(t .le. tf)  
  x = x0 + v0*(t-t0)  
  write(11,*)t,x,v0  
  if( x .lt. 0.0 .or. x .gt. L)then  
   x0 =  x  
   t0 =  t  
   v0 = -v0  
  endif  
  t   =  t + dt  
 enddo  
 close(11)  
end program box1D

Compiling and running the above program is done as before and the results are stored in the file box1D_2.dat. The algorithm can be improved in order to compute the exact solution. We leave that as an exercise for the reader17 .

2.3.2 Errors

In this section we will study the effect of the systematic errors that we encountered in the previous section in more detail. We considered two types of errors: First, the systematic error of determining the instant of the collision of the particle with the wall. This error is reduced by taking a smaller time step δt  . Then, the systematic error that accumulates with each addition of two numbers with increasing difference in their orders of magnitude. This error is increased with decreasing δt  . The competition of the two effects makes the optimal choice of δt  the result of a careful analysis. Such a situation is found in many interesting problems, therefore it is quite instructive to study it in more detail.

When the exact solution of the problem is not known, the systematic errors are controlled by studying the behavior of the solution as a function of δt  . If the solutions are converging in a region of values of δt  , one gains confidence that the true solution has been determined up to the accuracy of the convergence.

In the previous sections, we studied two different algorithms, programmed in the files box1D_1.f90 and box1D_2.f90. We will refer to them as “method 1” and “method 2” respectively. We will study the convergence of the results as δt →  0  by fixing all the parameters except δt  and then study the dependence of the results on δt  . We will take L = 10  , v0 = 1.0  , x0 = 0.0  , t0 = 0.0  , tf =  95.0  , so that the particle will collide with the wall every 10 units of time. We will measure the position of the particle x (t ≈ 95)  18 as a function of δt  and study its convergence to a limit19 as δt → 0  .

The analysis requires a lot of repetitive work: Compiling, setting the parameter values, running the program and calculating the value of x(t ≈ 95)  for many values of δt  . We write the values of the parameters read by the program in a file box1D_anal.in:

10         L  
0 1.0      x0 v0  
0 95  0.05 t0 tf dt

Then we compile the program

> gfortran box1D_1.f90 -o box

and run it with the command:

> cat box1D_anal.in | ./box

By using the pipe |, we send the contents of box1D_anal.in to the stdin of the command ./box by using the command cat. The result x(t ≈ 95)  can be found in the last line of the file box1D_1.dat:

> tail -n 1  box1D_1.dat  
  94.9511948  5.45000267 -1.

The third number in the above line is the value of the velocity. In a file box1D_anal.dat we write δt  and the first two numbers coming out from the command tail. Then we decrease the value δt →  δt∕2  in the file box1D_anal.in and run again. We repeat for 12 more times until δt  reaches the value20 0.000012  . We do the same21 using method 2 and we place the results for x(t ≈ 95)  in two new columns in the file box1D_anal.dat. The result is

# ------------------------------------------  
#  dt     t1_95    x1(95)   x2(95)  
# ------------------------------------------  
0.050000 94.95119 5.450003 5.550126  
0.025000 94.97849 5.275011 5.174837  
0.012500 94.99519 5.124993 5.099736  
0.006250 94.99850 4.987460 5.063134  
0.003125 94.99734 5.021894 5.035365  
0.001563 94.99923 5.034538 5.017764  
0.000781 94.99939 4.919035 5.011735  
0.000391 94.99979 4.695203 5.005493  
0.000195 95.00000 5.434725 5.001935  
0.000098 94.99991 5.528124 5.000745  
0.000049 94.99998 3.358000 5.000330  
0.000024 94.99998 2.724212 5.000232  
0.000012 94.99999 9.240705 5.000158

Convergence is studied in figure 2.21. The 1st method maximizes its accuracy for δt ≈ 0.01  , whereas for δt < 0.0001  the error becomes >  10  % and the method becomes useless. The 2nd method has much better behavior that the 1st one.

We observe that as δt  decreases, the final value of t  approaches the expected tf = 95  . Why don’t we obtain t = 95  , especially when t∕δt  is an integer? How many steps does it really take to reach t ≈ 95  , when the expected number of those is ≈ 95∕δt  ? Each time you take a measurement, issue the command

> wc -l box1D_1.dat

which measures the number of lines in the file box1D_1.dat and compare this number with the expected one. The result is interesting:

# ----------------------  
#   dt   N       N0  
# ----------------------  
0.050000 1900    1900  
0.025000 3800    3800  
0.012500 7601    7600  
0.006250 15203   15200  
0.003125 30394   30400  
0.001563 60760   60780  
0.000781 121751  121638  
0.000391 243753  242966  
0.000195 485144  487179  
0.000098 962662  969387  
0.000049 1972589 1938775  
0.000024 4067548 3958333  
0.000012 7540956 7916666

where the second column has the number of steps computed by the program and the third one has the expected number of steps. We observe that the accuracy decreases with decreasing δt  and in the end the difference is about 5%! Notice that the last line should have given tf = 0.000012 ×  7540956 ≈ 90.5  , an error comparable to the period of the particle’s motion.

We conclude that one important source of accumulation of systematic errors is the calculation of time. This type of errors become more significant with decreasing δt  . We can improve the accuracy of the calculation significantly if we use the multiplication t=t0+i*dt instead of the addition t=t+dt, where i is a step counter:

!t = t  + dt     ! Not accurate,    avoid  
 t = t0 + i*dt   ! Better accuracy, prefer

The main loop in the program box1D_1.f90 becomes:

 t = t0  
 x = x0  
 v = v0  
 i = 0  
 do while(t .le. tf)  
  write(11,*)t,x,v  
  i = i  + 1  
  x = x  + v*dt  
  t = t0 + i*dt  
  if(x .lt. 0.0 .or. x .gt. L) v = -v  
 enddo

The full program can be found in the file box1D_4.f90 of the accompanying software. We call this “method 3”. We perform the same change in the file box1D_2.f90, which we store in the file box1D_5.f90. We call this “method 4”. We repeat the same analysis using methods 3 and 4 and we find that the problem of calculating time accurately practically vanishes. The result of the analysis can be found on the right plot of figure 2.21. Methods 2 and 4 have no significant difference in their results, whereas methods 1 and 3 do have a dramatic difference, with method 3 decreasing the error more than tenfold. The problem of the increase of systematic errors with decreasing δt  does not vanish completely due to the operation x=x+v*dt. This type of error is harder to deal with and one has to invent more elaborate algorithms in order to reduce it significantly. This will be discussed further in chapter 4.


pict pict

Figure 2.21: The error δx = 2|x (95)− x(95)|∕|x (95)+ x(95)|× 100
       i             i  where x (95)
 i  is the value calculated by method i = 1,2,3,4  and x (95)  the exact value according to the text.

2.3.3 The Two Dimensional Box

A particle is confined to move on the plane in the area 0 < x < Lx  and 0 < y <  Ly  . When it reaches the boundaries of this two dimensional box, it bounces elastically off its walls. The particle is found in an infinite depth orthogonal potential well. The particle starts moving at time t0   from (x0, y0)  and our program will calculate its trajectory until time tf  with time step δt  . Such a trajectory can be seen in figure 2.23.

If the particle’s position and velocity are known at time t  , then at time t + δt  they will be given by the relations

 x(t + δt)  =   x(t) + v (t)δt
                       x
 y(t + δt)  =   y(t) + vy(t)δt
vx(t + δt)  =   vx(t)

vy(t + δt)  =   vy(t).                         (2.25)
The collision of the particle off the walls is modeled by reflection of the normal component of the velocity when the respective coordinate of the particle crosses the wall. This is a source of the systematic errors that we discussed in the previous section. The central loop of the program is:
  i = i  + 1  
  t = t0 + i *dt  
  x = x  + vx*dt  
  y = y  + vy*dt  
  if(x .lt. 0.0 .or. x .gt. Lx) vx = -vx  
  if(y .lt. 0.0 .or. y .gt. Ly) vy = -vy

The full program can be found in the file box2D_1.f90. Notice that we introduced two counters nx and ny of the particle’s collisions with the walls:

!============================================================  
!File box2D_1.f90  
!Motion of a free particle in a box  0<x<Lx 0<y<Ly  
!Use integration with time step dt: x = x + vx*dt y=y+vy*dt  
!------------------------------------------------------------  
program box2D  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real(8) ::  Lx,Ly,x0,y0,v0x,v0y,t0,tf,dt,t,x,y,vx,vy  
 integer ::  i,nx,ny  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter Lx,Ly:’  
 read  *,Lx,Ly  
 print *,’# Lx = ’,Lx,’ Ly= ’,Ly  
 if( Lx .le. 0.0) stop ’Lx must be positive.’  
 if( Ly .le. 0.0) stop ’Ly must be positive.’  
 print *,’# Enter x0,y0,v0x,v0y:’  
 read  *,x0,y0,v0x,v0y  
 print *,’# x0= ’,x0,’ y0= ’,y0,’ v0x= ’,v0x,’ v0y= ’,v0y  
 if(x0 .lt. 0.0 .or. x0 .gt. Lx) stop ’illegal value x0’  
 if(y0 .lt. 0.0 .or. y0 .gt. Ly) stop ’illegal value y0’  
 if(v0x**2+v0y**2.eq. 0.0      ) stop ’illegal value v0=0’  
 print *,’# Enter t0,tf,dt:’  
 read  *,t0,tf,dt  
 print *,’# t0= ’,t0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 i  = 0  
 nx = 0  ;  ny = 0  
 t  = t0  
 x  = x0 ;  y  = y0  
 vx = v0x;  vy = v0y  
 open(unit=11,file=’box2D_1.dat’)  
!------------------------------------------------------------  
!Compute:  
 do while(t .le. tf)  
  write(11,*)t,x,y,vx,vy  
  i = i  + 1  
  t = t0 + i *dt  
  x = x  + vx*dt  
  y = y  + vy*dt  
  if(x .lt. 0.0 .or. x .gt. Lx) then  
   vx = -vx  
   nx =  nx + 1  
  endif  
  if(y .lt. 0.0 .or. y .gt. Ly) then  
   vy = -vy  
   ny =  ny + 1  
  endif  
 enddo  
 close(11)  
 print *,’# Number of collisions:’  
 print *,’# nx= ’,nx,’ ny= ’,ny  
end program box2D

A typical session for the study of a particle’s trajectory could be:

> gfortran box2D_1.f90 -o box  
> ./box  
 # Enter Lx,Ly:  
10.0 5.0  
 # Lx =   10. Ly=   5.  
 # Enter x0,y0,v0x,v0y:  
5.0 0.0 1.27 1.33  
 # x0=   5. y0=   0. v0x=   1.27 v0y=   1.33  
 # Enter t0,tf,dt:  
0 50 0.01  
 # t0=   0. tf=   50. dt=   0.01  
 # Number of collisions:  
 # nx=  6 ny=  13  
> gnuplot  
gnuplot> plot   "box2D_1.dat" using 1:2 w l title "x (t)"  
gnuplot> replot "box2D_1.dat" using 1:3 w l title "y (t)"  
gnuplot> plot   "box2D_1.dat" using 1:4 w l title "vx(t)"  
gnuplot> replot "box2D_1.dat" using 1:5 w l title "vy(t)"  
gnuplot> plot   "box2D_1.dat" using 2:3 w l title "x-y"

Notice the last line of output from the program: The particle bounces off the vertical walls 6 times (nx=6) and from the horizontal ones 13 (ny=13). The gnuplot commands construct the diagrams displayed in figures 2.22 and 2.23.


pict pict

Figure 2.22: The results for the trajectory of a particle in a two dimensional box given by the program box2D_1.f90. The parameters are Lx = 10  , Ly = 5  , x0 = 5  , y0 = 0  , v0x = 1.27  , v0y = 1.33  , t0 = 0  , tf = 50  , δt = 0.01  .


pict

Figure 2.23: The trajectory of the particle of figure 2.22 until t = 48  . The origin of the arrow is at the initial position of the particle and its end is at its current position. The bold lines mark the boundaries of the box.

In order to animate the particle’s trajectory, we can copy the file box2D_animate.gnu of the accompanying software to the current directory and give the gnuplot commands:

gnuplot> file = "box2D_1.dat"  
gnuplot> Lx = 10 ; Ly = 5  
gnuplot> t0 = 0  ; tf = 50; dt = 1  
gnuplot> load "box2D_animate.gnu"  
gnuplot> t0 = 0  ; dt = 0.5; load "box2D_animate.gnu"

The last line repeats the same animation at half speed. You can also use the file animate2D.gnu discussed in section 2.1.1. We add new commands in the file box2D_animate.gnu so that the plot limits are calculated automatically and the box is drawn on the plot. The arrow drawn is not the position vector with respect to the origin of the coordinate axes, but the one connecting the initial with the current position of the particle.

The next step should be to test the accuracy of your results. This can be done by generalizing the discussion of the previous section and is left as an exercise for the reader.

2.4 Applications

In this section we will study simple examples of motion in a box with different types of obstacles. We will start with a game of ... mini golf. The player shoots a (point) “ball” which moves in an orthogonal box of linear dimensions Lx  and L
  y  and which is open on the x = 0  side. In the box there is a circular “hole” with center at (xc, yc)  and radius R  . If the “ball” falls in the “hole”, the player wins. If the ball leaves out of the box through its open side, the player loses. In order to check if the ball is in the hole when it is at position (x,y )  , all we have to do is to check whether (x − xc)2 + (y − yc)2 ≤ R2   .


pict

Figure 2.24: The trajectory of the particle calculated by the program MiniGolf.f90 using the parameters chosen in the text. The moment of ... success is shown. At time t = 45.3  the particle enters the hole’s region which has its center at (8,2.5)  and its radius is 0.5  .

Initially we place the ball at the position (0,Ly ∕2)  at time t0 = 0  . The player hits the ball which leaves with initial velocity of magnitude v0   at an angle 𝜃  degrees with the x  axis. The program is found in the file MiniGolf.f90 and is listed below:

!============================================================  
!File MiniGolf.f  
!Motion of a free particle in a box  0<x<Lx 0<y<Ly  
!The box is open at x=0 and has a hole at (xc,yc) of radius R  
!Ball is shot at (0,Ly/2) with speed v0, angle theta (degrees)  
!Use integration with time step dt: x = x + vx*dt y=y+vy*dt  
!Ball stops in hole (success) or at x=0 (failure)  
!------------------------------------------------------------  
program MiniGolf  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real(8)            :: Lx,Ly,x0,y0,v0x,v0y,t0,tf,dt,t,x,y,vx,vy  
 real(8)            :: v0,theta,xc,yc,R,R2  
 real(8), parameter :: PI=3.14159265358979324D0  
 integer            :: i,nx,ny  
 character(7)       :: result  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter Lx,Ly:’  
 read  *,Lx,Ly  
 print *,’# Lx = ’,Lx,’ Ly= ’,Ly  
 if( Lx .le. 0.0) stop ’Lx must be positive.’  
 if( Ly .le. 0.0) stop ’Ly must be positive.’  
 print *,’# Enter hole position and radius: (xc,yc), R:’  
 read  *,xc,yc,R  
 print *,’# (xc,yc)= ( ’,xc,’ , ’,yc,’ ) R= ’,R  
 print *,’# Enter v0, theta(degrees):’  
 read  *,v0,theta  
 print *,’# v0= ’,v0,’ theta= ’,theta,’ degrees’  
 if(v0        .le. 0.0D0 ) stop ’illegal value of v0.’  
 if(ABS(theta).ge. 90.0D0) stop ’illegal value of theta.’  
 print *,’# Enter dt:’  
 read  *,dt  
 print *,’# dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 t0 = 0.0D0  
 x0 = 0.00001D0 ! small but non-zero  
 y0 = Ly/2.0  
 R2 = R*R  
 theta = (PI/180.0D0)*theta  
 v0x = v0*cos(theta)  
 v0y = v0*sin(theta)  
 print *,’# x0= ’,x0,’ y0= ’,y0,’ v0x= ’,v0x,’ v0y= ’,v0y  
 i  = 0  
 nx = 0  ;  ny = 0  
 t  = t0  
 x  = x0 ;  y  = y0  
 vx = v0x;  vy = v0y  
 open(unit=11,file=’MiniGolf.dat’)  
!------------------------------------------------------------  
!Compute:  
 do while( .TRUE. ) !forever!  
  write(11,*)t,x,y,vx,vy  
  i = i  + 1  
  t = t0 + i*dt  
  x = x  + vx*dt  
  y = y  + vy*dt  
  if(x .gt. Lx) then  
   vx = -vx  
   nx =  nx + 1  
  endif  
  if(y .lt. 0.0 .or. y .gt. Ly) then  
   vy = -vy  
   ny = ny  + 1  
  endif  
  if(x .le. 0.0D0)then  
   result = ’Failure’  
   exit !exit do loop  
  endif  
  if( ((x-xc)*(x-xc)+(y-yc)*(y-yc)) .le. R2)then  
   result = ’Success’  
   exit !exit do loop  
  endif  
 enddo  
 close(11)  
 print *,’# Number of collisions:’  
 print *,’# Result= ’,result,’ nx= ’,nx,’ ny= ’,ny  
end program MiniGolf

In order to run it, we can use the commands:

> gfortran MiniGolf.f90 -o mg  
> ./mg  
 # Enter Lx,Ly:  
10 5  
 # Lx =   10. Ly=   5.  
 # Enter hole position and radius: (xc,yc), R:  
8 2.5 0.5  
 # (xc,yc)= (   8. ,   2.5 ) R=   0.5  
 # Enter v0, theta(degrees):  
1 80  
 # v0=   1. theta=   80. degrees  
 # Enter dt:  
0.01  
 # dt=   0.01  
 # x0=   1.E-05 y0=   2.5 v0x=   0.173648178 v0y=   0.984807753  
 # Number of collisions:  
 # Result= Success nx=  0 ny=  9

You should construct the plots of the position and the velocity of the particle. You can also use the animation program found in the file MiniGolf_animate.gnu for fun. Copy it from the accompanying software to the current directory and give the gnuplot commands:

gnuplot> file = "MiniGolf.dat"  
gnuplot> Lx = 10;Ly = 5  
gnuplot> xc = 8; yc = 2.5 ; R = 0.5  
gnuplot> t0 = 0; dt = 0.1  
gnuplot> load "MiniGolf_animate.gnu"

The results are shown in figure 2.24.

The next example with be three dimensional. We will study the motion of a particle confined within a cylinder of radius R  and height L  . The collisions of the particle with the cylinder are elastic. We take the axis of the cylinder to be the z  axis and the two bases of the cylinder to be located at z = 0  and z = L  . This is shown in figure 2.26.

The collisions of the particle with the bases of the cylinder are easy to program: we follow the same steps as in the case of the simple box. For the collision with the cylinder’s side, we consider the projection of the motion on the x − y  plane. The projection of the particle moves within a circle of radius R  and center at the intersection of the z  axis with the plane. This is shown in figure 2.25. At the collision, the r  component of the velocity is reflected vr →  − vr  , whereas v𝜃  remains the same. The velocity of the particle before the collision is

⃗v  =   vxˆx + vyˆy
   =   v ˆr + v 𝜃ˆ                      (2.26)
        r     𝜃
and after the collision is
 ′      ′      ′
⃗v   =  vxxˆ+ v yyˆ
    =  − vrˆr + v𝜃ˆ𝜃                      (2.27)
From the relations
ˆr  =   cos𝜃ˆx + sin𝜃 ˆy

ˆ𝜃  =   − sin 𝜃ˆx + cos 𝜃ˆy,                   (2.28)
and v = ⃗v ⋅ ˆr
 r  , v  = ⃗v ⋅ ˆ𝜃
 𝜃 , we have that
vr  =  vx cos𝜃 + vy sin 𝜃
v𝜃  =  − vx sin 𝜃 + vy cos𝜃.                 (2.29)
The inverse relations are
vx  =  vr cos𝜃 − v𝜃 sin 𝜃
v   =  v  sin 𝜃 + v cos 𝜃.                  (2.30)
 y       r        𝜃
With the transformation vr →  − vr  , the new velocity in Cartesian coordinates will be
v′x  =   − vr cos𝜃 − v𝜃 sin𝜃
 ′
vy  =   − vr sin 𝜃 + v𝜃 cos𝜃.                (2.31)

pict

Figure 2.25: The elastic collision of the particle moving within the circle of radius      ⃗
R = |R| and center ⃗rc = xcˆx+ ycˆy  at the point ⃗r = xˆx+ yˆy  . We have that R⃗= (x − xc)ˆx +(y − yc)ˆy  . The initial velocity is ⃗v = vrrˆ+ v𝜃𝜃ˆ where ˆr ≡ ⃗R∕R  . After reflecting vr → − vr  the new velocity of the particle is ⃗v′ = − vrˆr +v𝜃ˆ𝜃 .

The transformation       ′
vx → vx  ,        ′
vy →  vy  will be performed in the subroutine reflectVonCircle(vx,vy,x,y,xc,yc,R). Upon entry to the subroutine, we provide the initial velocity (vx,vy), the collision point (x,y), the center of the circle (xc,yc) and the radius of the circle22 R. Upon exit from the subroutine, (vx,vy) have been replaced with the new values23 (v′,v′)
  x  y  .

The program can be found in the file Cylinder3D.f90 and is listed below:

!============================================================  
!File Cylinder3D.f90  
!Motion of a free particle in a cylinder with axis the z-axis,  
!radius R and 0<z<L  
!Use integration with time step dt: x = x + vx*dt  
!                                   y = y + vy*dt  
!                                   z = z + vz*dt  
!Use subroutine reflectVonCircle for collisions at r=R  
!------------------------------------------------------------  
program Cylinder3D  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real(8)  ::  x0,y0,z0,v0x,v0y,v0z,t0,tf,dt,t,x,y,z,vx,vy,vz  
 real(8)  ::  L,R,R2,vxy,rxy,r2xy,xc,yc  
 integer  ::  i,nr,nz  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter R, L:’  
 read  *,R,L  
 print *,’# R= ’,R,’ L= ’,L  
 if( R .le. 0.0) stop ’R must be positive.’  
 if( L .le. 0.0) stop ’L must be positive.’  
 print *,’# Enter x0,y0,z0,v0x,v0y,v0z:’  
 read  *,x0,y0,z0,v0x,v0y,v0z  
 rxy = DSQRT(x0*x0+y0*y0)  
 print *,’# x0 = ’,x0 ,’ y0 = ’,y0 ,’ z0=  ’,z0, ’ rxy= ’,rxy  
 print *,’# v0x= ’,v0x,’ v0y= ’,v0y,’ v0z= ’,v0z  
 if(rxy .gt. R                 )stop ’illegal value of rxy > R’  
 if(z0  .lt. 0.0D0             )stop ’illegal value of z0  < 0’  
 if(z0  .gt. L                 )stop ’illegal value of z0  > L’  
 if(v0x**2+v0y**2+v0z**2.eq.0.0)stop ’illegal value of v0 = 0.’  
 print *,’# Enter t0,tf,dt:’  
 read  *,t0,tf,dt  
 print *,’# t0= ’,t0,’ tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 i  = 0  
 nr = 0  ;  nz = 0  
 t  = t0  
 x  = x0 ;  y  = y0 ;  z  = z0  
 vx = v0x; vy  = v0y;  vz = v0z  
 R2 = R*R  
 xc = 0.0D0 !center of circle which is the projection of the  
 yc = 0.0D0 !cylinder on the xy plane  
 open(unit=11,file=’Cylinder3D.dat’)  
!------------------------------------------------------------  
!Compute:  
 do while(t .le. tf)  
  write(11,100)t,x,y,z,vx,vy,vz  
  i = i  + 1  
  t = t0 + i *dt  
  x = x  + vx*dt  
  y = y  + vy*dt  
  z = z  + vz*dt  
  if(z .lt. 0.0 .or. z .gt. L) then  
   vz = -vz           ! reflection on cylinder caps  
   nz =  nz + 1  
  endif  
  r2xy = x*x+y*y  
  if( r2xy .gt. R2)then  
   call reflectVonCircle(vx,vy,x,y,xc,yc,R)  
   nr  = nr + 1  
  endif  
 enddo  
 close(11)  
 print *,’# Number of collisions:’  
 print *,’# nr= ’,nr,’ nz= ’,nz  
 
100 FORMAT(100G28.16)  
end program Cylinder3D  
!------------------------------------------------------------  
!============================================================  
!------------------------------------------------------------  
subroutine reflectVonCircle(vx,vy,x,y,xc,yc,R)  
 implicit none  
 real(8)  :: vx,vy,x,y,xc,yc,R  
 real(8)  :: theta,cth,sth,vr,vth  
 
 theta = atan2(y-yc,x-xc)  
 cth   = cos(theta)  
 sth   = sin(theta)  
 
 vr    =  vx*cth + vy *sth  
 vth   = -vx*sth + vy *cth  
 
 vx    = -vr*cth - vth*sth !reflect vr -> -vr  
 vy    = -vr*sth + vth*cth  
 
 x     =  xc     + R*cth   !put x,y on the circle  
 y     =  yc     + R*sth  
end subroutine reflectVonCircle

Notice that the function atan2 is used for computing the angle theta. This function, when called with two arguments atan2(y,x), returns the angle 𝜃 = tan −1(y∕x)  in radians. The correct quadrant of the circle where (x, y)  lies is chosen. The angle that we want to compute is given by atan2(y-yc,x-xc). Then we apply equations (2.29) and (2.31) and in the last two lines we enforce the particle to be at the point (xc + R cos𝜃, yc + R sin𝜃)  , exactly on the circle.


pict

Figure 2.26: The trajectory of a particle moving inside a cylinder with R = 10  , L = 10  , computed by the program Cylinder3D.f90. We have chosen ⃗r0 = 1.0ˆx+ 2.2ˆy+ 3.1ˆz  , ⃗v0 = 0.93ˆx− 0.89yˆ+ 0.74ˆz  , t0 = 0  , tf = 500.0  , δt = 0.01  .

A typical session is shown below:

> gfortran Cylinder3D.f90 -o cl  
> ./cl  
 # Enter R, L:  
10.0 10.0  
 # R=   10. L=   10.  
 # Enter x0,y0,z0,v0x,v0y,v0z:  
1.0 2.2 3.1   0.93 -0.89 0.74  
 # x0 =   1. y0 =   2.2 z0=    3.1 rxy=   2.41660919  
 # v0x=   0.93 v0y=  -0.89 v0z=   0.74  
 # Enter t0,tf,dt:  
0.0 500.0 0.01  
 # t0=   0. tf=   500. dt=   0.01  
 # Number of collisions:  
 # nr=  33 nz=  37

In order to plot the position and the velocity as a function of time, we use the following gnuplot commands:

gnuplot> file="Cylinder3D.dat"  
gnuplot> plot file using 1:2 with lines title "  x(t)",\  
              file using 1:3 with lines title "  y(t)",\  
              file using 1:4 with lines title "  z(t)"  
gnuplot> plot file using 1:5 with lines title "v_x(t)",\  
              file using 1:6 with lines title "v_y(t)",\  
              file using 1:7 with lines title "v_z(t)"

We can also compute the distance of the particle from the cylinder’s axis        ∘ ----2------2-
r(t) =   x(t) +  y(t)   as a function of time using the command:

gnuplot> plot file using 1:(sqrt($2**2+$3**2)) w l t "r(t)"

In order to plot the trajectory, together with the cylinder, we give the commands:

gnuplot> L = 10 ; R = 10  
gnuplot> set urange [0:2.0*pi]  
gnuplot> set vrange [0:L]  
gnuplot> set parametric  
gnuplot> splot file using 2:3:4 with lines notitle,\  
                       R*cos(u),R*sin(u),v notitle

The command set parametric is necessary if one wants to make a parametric plot of a surface ⃗r(u,v ) = x (u,v)ˆx + y(u,v)ˆy + z(u,v )zˆ  . The cylinder (without the bases) is given by the parametric equations ⃗r(u,v ) = R cosuˆx + R sin uˆy + vˆz  with u ∈ [0,2π )  , v ∈ [0,L ]  .

We can also animate the trajectory with the help of the gnuplot script file Cylinder3D_animate.gnu. Copy the file from the accompanying software to the current directory and give the gnuplot commands:

gnuplot> R=10;L=10;t0=0;tf=500;dt=10  
gnuplot> load "Cylinder3D_animate.gnu"

The result is shown in figure 2.26.

The last example will be that of a simple model of a spacetime wormhole. This is a simple spacetime geometry which, in the framework of the theory of general relativity, describes the connection of two distant areas in space which are asymptotically flat. This means, that far enough from the wormhole’s mouths, space is almost flat - free of gravity. Such a geometry is depicted in figure 2.27. The distance traveled by someone through the mouths could be much smaller than the distance traveled outside the wormhole and, at least theoretically, traversable wormholes could be used for interstellar/intergalactic traveling and/or communications between otherwise distant areas in the universe. Of course we should note that such macroscopic and stable wormholes are not known to be possible to exist in the framework of general relativity. One needs an exotic type of matter with negative energy density which has never been observed. Such exotic geometries may realize microscopically as quantum fluctuations of spacetime and make the small scale structure of the geometry24 a “spacetime foam”.


pict

Figure 2.27: A typical geometry of space near a wormhole. Two asymptotically flat regions of space are connected through a “neck” which can be arranged to be of small length compared to the distance of the wormhole mouths when traveled from the outside space.

We will study a very simple model of the above geometry on the plane with a particle moving freely in it25 .


pict

Figure 2.28: A simple model of the spacetime geometry of figure 2.27. The particle moves on the whole plane except withing the two disks that have been removed. The neck of the wormhole is modeled by the two circles x (𝜃) = ±d ∕2± R cos𝜃  , y(𝜃) = R sin 𝜃  , − π < 𝜃 ≤ π  and has zero length since their points have been identified. There is a given direction in this identification, so that points with the same 𝜃  are the same (you can imagine how this happens by folding the plane across the y  axis and then glue the two circles together). The entrance of the particle through one mouth and exit through the other is done as shown for the velocity vector ⃗v → ⃗v′ .

We take the two dimensional plane and cut two equal disks of radius R  with centers at distance d  like in figure 2.28. We identify the points on the two circles such that the point 1 of the left circle is the same as the point 1 on the right circle, the point 2 on the left with the point 2 on the right etc. The two circles are given by the parametric equations x (𝜃) = d∕2 + R cos𝜃  , y(𝜃) = R sin𝜃  , − π < 𝜃 ≤  π  for the right circle and x(𝜃) = − d∕2 − R cos𝜃  , y (𝜃 ) = R sin 𝜃  , − π < 𝜃 ≤  π  for the left. Points on the two circles with the same 𝜃  are identified. A particle entering the wormhole from the left circle with velocity v  is immediately exiting from the right with velocity v′ as shown in figure 2.28.

Then we will do the following:

  1. Write a program that computes the trajectory of a particle moving in the geometry of figure 2.28. We set the limits of motion to be −  L∕2 ≤ x ≤  L∕2  and − L ∕2 ≤ y ≤ L ∕2  . We will use periodic boundary conditions in order to define what happens when the particle attempts to move outside these limits. This means that we identify the x = − L ∕2  line with the x = +L ∕2  line as well as the y = − L ∕2  line with the y = +L ∕2  line. The user enters the parameters R  , d  and L  as well as the initial conditions (x0, y0)  , (v0,ϕ)  where ⃗v0 = v0(cos ϕˆx + sin ϕyˆ)  . The user will also provide the time parameters tf  and dt  for motion in the time interval t ∈ [t = 0,t ]
     0      f  with step dt  .
  2. Plot the particle’s trajectory with (x0,y0) = (0,− 1)  , (v0,ϕ) = (1,10o)  tf = 40  , dt = 0.05  in the geometry with L = 20,d =  5,R = 1  .
  3. Find a closed trajectory which does not cross the boundaries |x | = L ∕2  , |y| = L ∕2  and determine whether it is stable under small perturbations of the initial conditions.
  4. Find other closed trajectories that go through the mouths of the wormhole and study their stability under small perturbations of the initial conditions.
  5. Add to the program the option to calculate the distance traveled by the particle. If the particle starts from (− x0,0)  and moves in the +  x  direction to the (x0,0)  , x0 > R  + d∕2  position, draw the trajectory and calculate the distance traveled on paper. Then confirm your calculation from the numerical result coming from your program.
  6. Change the boundary conditions, so that the particle bounces off elastically at |x | = L ∕2  , |y| = L∕2  and replot all the trajectories mentioned above.

Define the right circle c
 1   by the parametric equations

        d
x (𝜃) = --+ R cos𝜃,     y(𝜃) = R sin𝜃,     − π < 𝜃 ≤ π,
        2
(2.32)

and the left circle c
 2   by the parametric equations

         d
x(𝜃) = − --− R cos 𝜃,    y(𝜃) = R sin𝜃,     − π < 𝜃 ≤ π.
         2
(2.33)

The particle’s position changes at time dt  by

ti  =  idt
x   =  x    + v dt
 i       i−1    x
yi  =  yi−1 + vydt
                                        (2.34)
for i = 1, 2,...  for given (x0,y0)  , t0 = 0  and as long as ti ≤ tf  . If the point (xi,yi)  is outside the boundaries |x| = L∕2  , |y| = L ∕2  , we redefine x  →  x ± L
  i    i  , y →  y ±  L
 i    i  in each case respectively. Points defined by the same value of 𝜃  are identified, i.e. they represent the same points of space. If the point (xi,yi)  crosses either one of the circles c1   or c2   , then we take the particle out from the other circle.

pict

Figure 2.29: The particle crossing the wormhole through the right circle c1  with velocity ⃗v  . It emerges from c2  with velocity ⃗v′ . The unit vectors (ˆer,eˆ𝜃)  , (ˆe′r,ˆe′𝜃)  are computed from the parametric equations of the two circles c1  and c2  .

Crossing the circle c1   is determined by the relation

(       )
       d  2    2    2
  xi − 2-  +  yi ≤ R .
(2.35)

The angle 𝜃  is calculated from the equation

          (       )
𝜃 = tan −1  --yi---  ,
            xi − d
                 2
(2.36)

and the point (xi,yi)  is mapped to the point (x′i,y′i)  where

x′i = − d-− R cos𝜃,     y′i = yi,
       2
(2.37)

as can be seen in figure 2.29. For mapping      ′
⃗v → ⃗v , we first calculate the vectors

                          }    {
ˆer =    cos𝜃ˆx  +   sin𝜃 ˆy         ˆe′=   − cos𝜃xˆ  +   sin 𝜃ˆy
ˆe =   − sin𝜃ˆx  +   cos𝜃 ˆy   →     ˆer′=     sin𝜃xˆ  +  cos 𝜃ˆy  ,
 𝜃                                 𝜃
(2.38)

so that the velocity

⃗v = vrˆer + v 𝜃ˆe𝜃  →    ⃗v ′ = − vrˆe′r + v 𝜃eˆ′𝜃,
(2.39)

where the radial components are v  = ⃗v ⋅ ˆe
 r       r  and v  = ⃗v ⋅ ˆe
 𝜃       𝜃  . Therefore, the relations that give the “emerging” velocity  ′
⃗v are:

v  =    v cos 𝜃  +   v sin𝜃
 r       x            y
v𝜃 =  − vx sin 𝜃  +  vy cos𝜃 .
v′x =    vr cos 𝜃 +   v𝜃 sin𝜃
v′y =   − vr sin 𝜃 +   v𝜃 cos𝜃
(2.40)

Similarly we calculate the case of entering from c2   and emerging from c1   . The condition now is:

(       )
       d- 2    2    2
  xi + 2   + y i ≤ R .
(2.41)

The angle 𝜃  is given by

              (        )
            −1  --yi--
𝜃 = π −  tan     x  + d   ,
                  i  2
(2.42)

and the point (xi,yi)  is mapped to the point (x′,y′)
  i  i  where

x′=  d-+ R cos 𝜃,    y′ = yi.
 i   2                i
(2.43)

For mapping       ′
⃗v →  ⃗v , we calculate the vectors

                          }    {
ˆer =  − cos𝜃 ˆx  +   sin 𝜃ˆy         ˆe′r =    cos𝜃xˆ  +   sin 𝜃ˆy
ˆe =     sin𝜃 ˆx  +  cos 𝜃ˆy   →     ˆe′=   − sin𝜃xˆ  +  cos 𝜃ˆy  ,
 𝜃                                 𝜃
(2.44)

so that the velocity

⃗v = vrˆer + v𝜃ˆe𝜃  →    ⃗v′ = − vrˆe′+ v𝜃ˆe′.
                                r      𝜃
(2.45)

The emerging velocity  ′
⃗v is:

vr =  − vx cos𝜃  +   vy sin𝜃
v𝜃 =    vx sin 𝜃  +   vy cos𝜃
 ′                          .
vx′ =  − vr cos𝜃  −   v𝜃 sin𝜃
vy =   − vr sin 𝜃 +   v𝜃 cos𝜃
(2.46)

Systematic errors are now coming from crossing the two mouths of the wormhole. There are no systematic errors from crossing the boundaries |x| = L∕2  , |y| = L∕2  (why?). Try to think of ways to control those errors and study them.

The closed trajectories that we are looking for come from the initial conditions

(x0,y0,v0,ϕ) = (0,0,1,0)
(2.47)

and they connect points 1 of figure 2.28. They are unstable, as can be seen by taking ϕ →  ϕ + 𝜖  .

The closed trajectories that cross the wormhole and “wind” through space can come from the initial conditions

(x0,y0,v0,ϕ)  =   (− 9,0,1, 0)
(x0,y0,v0,ϕ)  =   (2.5,− 3,1,90o)
and cross the points 3 → 3  and 2 →  2 → 4 →  4  respectively. They are also unstable, as can be easily verified by using the program that you will write. The full program is listed below:
!============================================================  
program WormHole2D  
 implicit none  
!------------------------------------------------------------  
!Declaration of variables  
 real(8), parameter :: PI=3.14159265358979324D0  
 real(8) ::  Lx,Ly,L,R,d  
 real(8) ::  x0,y0,v0,theta  
 real(8) ::  t0,tf,dt  
 real(8) ::  t,x,y,vx,vy  
 real(8) ::  xc1,yc1,xc2,yc2,r1,r2  
 integer ::  i  
!------------------------------------------------------------  
!Ask user for input:  
 print *,’# Enter L,d,R:’  
 read  *,L,d,R  
 print *,’# L= ’,L,’ d= ’,d,’ R= ’,R  
 if( L .le. d+2.0D0*R) stop ’L <= d+2*R’  
 if( d .le.   2.0D0*R) stop ’d <=   2*R’  
 print *,’# Enter (x0,y0), v0, theta(degrees):’  
 read  *,x0,y0,v0,theta  
 print *,’# x0= ’,x0,’ y0   = ’,y0  
 print *,’# v0= ’,v0,’ theta= ’,theta,’ degrees’  
 if(v0 .le.   0.0D0  ) stop ’illegal value of v0.’  
 print *,’# Enter tf, dt:’  
 read  *,tf,dt  
 print *,’# tf= ’,tf,’ dt= ’,dt  
!------------------------------------------------------------  
!Initialize  
 theta = (PI/180.0D0)*theta  
 i     =  0  
 t     =  0.0D0  
 x     =  x0           ; y     =  y0  
 vx    =  v0*cos(theta); vy    =  v0*sin(theta)  
 print *,’# x0= ’,x,’ y0= ’,y,’ v0x= ’,vx,’ v0y= ’,vy  
!Wormhole’s centers:  
 xc1   =  0.5D0*d; yc1   =  0.0D0  
 xc2   = -0.5D0*d; yc2   =  0.0D0  
!Box limits coordinates:  
 Lx    =  0.5D0*L; Ly    =  0.5D0*L  
!Test if already inside cut region:  
 r1    = sqrt((x-xc1)**2+(y-yc1)**2)  
 r2    = sqrt((x-xc2)**2+(y-yc2)**2)  
 if( r1    .le. R ) stop ’r1 <= R’  
 if( r2    .le. R ) stop ’r2 <= R’  
!Test if outside box limits:  
 if(ABS(x) .ge. Lx) stop ’|x| >= Lx’  
 if(ABS(y) .ge. Ly) stop ’|y| >= Ly’  
 open(unit=11,file=’Wormhole.dat’)  
!------------------------------------------------------------  
!Compute:  
 do while( t .lt. tf )  
  write(11,*)t,x,y,vx,vy  
  i  = i+1  
  t  = i*dt  
  x  = x + vx*dt; y  = y + vy*dt  
! Toroidal boundary conditions:  
  if( x .gt.  Lx) x  = x - L  
  if( x .lt. -Lx) x  = x + L  
  if( y .gt.  Ly) y  = y - L  
  if( y .lt. -Ly) y  = y + L  
! Test if inside the cut disks  
  r1 = sqrt((x-xc1)**2+(y-yc1)**2)  
  r2 = sqrt((x-xc2)**2+(y-yc2)**2)  
  if( r1 .lt. R)then  
! Notice: we pass r1 as radius of circle, not R  
   call crossC1(x,y,vx,vy,dt,r1,d)  
  else if( r2 .lt. R)then  
   call crossC2(x,y,vx,vy,dt,r2,d)  
  endif  
! small chance here that still in C1 or C2, but OK since  
! another dt-advance given at the beginning of do-loop  
 enddo !do while( t .lt. tf )  
end program WormHole2D  
!=======================================================  
subroutine crossC1(x,y,vx,vy,dt,R,d)  
 implicit none  
 real(8) ::  x,y,vx,vy,dt,R,d  
 real(8) ::  vr,v0 !v0 -> vtheta  
 real(8) ::  theta,xc,yc  
 print *,’# Inside C1: (x,y,vx,vy,R)= ’,x,y,vx,vy,R  
 xc    =  0.5D0*d !center of C1  
 yc    =  0.0D0  
 theta =  atan2(y-yc,x-xc)  
 x     = -xc - R*cos(theta) !new x-value, y invariant  
!Velocity transformation:  
 vr    =  vx*cos(theta)+vy*sin(theta)  
 v0    = -vx*sin(theta)+vy*cos(theta)  
 vx    =  vr*cos(theta)+v0*sin(theta)  
 vy    = -vr*sin(theta)+v0*cos(theta)  
!advance x,y, hopefully outside C2:  
 x     = x + vx*dt  
 y     = y + vy*dt  
 print *,’# Exit   C2: (x,y,vx,vy  )= ’,x,y,vx,vy  
end subroutine crossC1  
!=======================================================  
subroutine crossC2(x,y,vx,vy,dt,R,d)  
 implicit none  
 real(8), parameter :: PI=3.14159265358979324D0  
 real(8) ::  x,y,vx,vy,dt,R,d  
 real(8) ::  vr,v0 !v0 -> vtheta  
 real(8) ::  theta,xc,yc  
 
 print *,’# Inside C2: (x,y,vx,vy,R)= ’,x,y,vx,vy,R  
 xc    = -0.5D0*d !center of C2  
 yc    =  0.0D0  
 theta =  PI-atan2(y-yc,x-xc)  
 x     = -xc + R*cos(theta) !new x-value, y invariant  
!Velocity transformation:  
 vr    = -vx*cos(theta)+vy*sin(theta)  
 v0    =  vx*sin(theta)+vy*cos(theta)  
 vx    = -vr*cos(theta)-v0*sin(theta)  
 vy    = -vr*sin(theta)+v0*cos(theta)  
!advance x,y, hopefully outside C1:  
 x     = x + vx*dt  
 y     = y + vy*dt  
 print *,’# Exit   C1: (x,y,vx,vy  )= ’,x,y,vx,vy  
end subroutine crossC2  

It is easy to compile and run the program. See also the files Wormhole.csh and Wormhole_animate.gnu of the accompanying software and run the gnuplot commands:

gnuplot> file  = "Wormhole.dat"  
gnuplot> R=1;d=5;L=20;  
gnuplot> ! ./Wormhole.csh  
gnuplot> t0=0;dt=0.2;load "Wormhole_animate.gnu"

You are now ready to answer the rest of the questions that we asked in our list.

2.5 Problems

  1. Change the program Circle.f90 so that it prints the number of full circles traversed by the particle.
  2. Add all the necessary tests on the parameters entered by the user in the program Circle.f90, so that the program is certain to run without problems. Do the same for the rest of the programs given in the same section.
  3. A particle moves with constant angular velocity ω  on a circle that has the origin of the coordinate system at its center. At time t0 = 0  , the particle is at (x0,y0)  . Write the program CircularMotion.f90 that will calculate the particle’s trajectory. The user should enter the parameters ω,x0,y0,t0,tf,δt  . The program should print the results like the program Circle.f90 does.
  4. Change the program SimplePendulum.f90 so that the user could enter a non zero initial velocity.
  5. Study the k →  0  limit in the projectile motion given by equations (2.10) . Expand  −kt           -1    2
e   = 1 − kt + 2!(kt) + ...  and keep the non vanishing terms as k →  0  . Then keep the next order leading terms which have a smaller power of k  . Program these relations in a file
    ProjectileSmallAirResistance.f90. Consider the initial conditions ⃗v =  ˆx + ˆy
 0  and calculate the range of the trajectory numerically by using the two programs
    ProjectileSmallAirResistance.f90, ProjectileAirResistance.f90. Determine the range of values of k  for which the two results agree within 5% accuracy.
  6. Write a program for a projectile which moves through a fluid with fluid resistance proportional to the square of the velocity. Compare the range of the trajectory with the one calculated by the program ProjectileAirResistance.f90 for the parameters shown in figure 2.10.
  7. Change the program Lissajous.f90 so that the user can enter a different amplitude and initial phase in each direction. Study the case where the amplitudes are the same and the phase difference in the two directions are π∕4,π ∕2,π,− π  . Repeat by taking the amplitude in the y  direction to be twice as much the amplitude in the x  direction.
  8. Change the program ProjectileAirResistance.f90, so that it can calculate also the k = 0  case.
  9. Change the program ProjectileAirResistance.f90 so that it can calculate the trajectory of the particle in three dimensional space. Plot the position coordinates and the velocity components as a function of time. Plot the three dimensional trajectory using splot in gnuplot and animate the trajectory using the gnuplot script animate3D.gnu.
  10. Change the program ChargeInB.f90 so that it can calculate the number of full revolutions that the projected particle’s position on the x − y  plane makes during its motion.
  11. Change the program box1D_1.f90 so that it prints the number of the particle’s collisions on the left wall, on the right wall and the total number of collisions to the stdout.
  12. Do the same for the program box1D_2.f90. Fill the table on page 311 the number of calculated collisions and comment on the results.
  13. Run the program box1D_1.f90 and choose L= 10, v0=1. Decrease the step dt up to the point that the particle stops to move. For which value of dt this happens? Increase v0=10,100. Until which value of dt the particle moves now? Why?
  14. Change the REAL declarations to REAL(8) in the program box1D_1.f90. Add explicit exponents D0 to all constants (e.g. 0.0→ 0.0D0). Compare your results to those obtained in section 2.3.2. Repeat problem 2.13. What do you observe?
  15. Change the program box1D_1.f90 so that you can study non elastic collisions  ′
v  = − ev  , 0 < e ≤ 1  with the walls.
  16. Change the program box2D_1.f90 so that you can study inelastic collisions with the walls, such that  ′
vx = − evx  ,  ′
vy = − evy  , 0 < e ≤ 1  .
  17. Use the method of calculating time in the programs box1D_4.f90 and box1D_5.f90 in order to produce the results in figure 2.21.
  18. Particle falls freely moving in the vertical direction. It starts with zero velocity at height h  . Upon reaching the ground, it bounces inelastically such that  ′
vy = − evy  with 0 < e ≤ 1  a parameter. Write the necessary program in order to study numerically the particle’s motion and study the cases e = 0.1,0.5,0.9,1.0  .
  19. Generalize the program of the previous problem so that you can study the case ⃗v0 = v0xˆx  . Animate the calculated trajectories.
  20. Study the motion of a particle moving inside the box of figure 2.30. Count the number of collisions of the particle with the walls before it leaves the box.

    pict

    Figure 2.30: Problem 2.20.

  21. Study the motion of the point particle on the “billiard table” of figure 2.31. Count the number of collisions with the walls before the particle enters into a hole. The program should print from which hole the particle left the table.

    pict

    Figure 2.31: Problem 2.21.

  22. Write a program in order to study the motion of a particle in the box of figure 2.32. At the center of the box there is a disk on which the particle bounces off elastically (Hint: use the routine reflectVonCircle of the program Cylinder3D.f90).

    pict

    Figure 2.32: Problem 2.22.

  23. In the box of the previous problem, put four disks on which the particle bounces of elastically like in figure 2.33.

    pict

    Figure 2.33: Problem 2.23.

  24. Consider the arrangement of figure 2.34. Each time the particle bounces elastically off a circle, the circle disappears. The game is over successfully if all the circles vanish. Each time the particle bounces off on the wall to the left, you lose a point. Try to find trajectories that minimize the number of lost points.

    pict

    Figure 2.34: Problem 2.24.

Chapter 3
Logistic Map

Nonlinear differential equations model interesting dynamical systems in physics, biology and other branches of science. In this chapter we perform a numerical study of the discrete logistic map as a “simple mathematical model with complex dynamical properties”  [21] similar to the ones encountered in more complicated and interesting dynamical systems. For certain values of the parameter of the map, one finds chaotic behavior giving us an opportunity to touch on this very interesting topic with important consequences in physical phenomena. Chaotic evolution restricts out ability for useful predictions in an otherwise fully deterministic dynamical system: measurements using slightly different initial conditions result in a distribution which is indistinguishable from the distribution coming from sampling a random process. This scientific field is huge and active and we refer the reader to the bibliography for a more complete introduction  [2122232425262738].

3.1 Introduction

The most celebrated application of the logistic map comes from the study of population growth in biology. One considers populations which reproduce at fixed time intervals and whose generations do not overlap.

The simplest (and most naive) model is the one that makes the reasonable assumption that the rate of population growth dP (t)∕dt  of a population P(t)  is proportional to the current population:

dP (t)
------= kP (t).
 dt
(3.1)

The general solution of the above equation is P (t) = P(0)ekt  showing an exponential population growth for k > 0  an decline for k <  0  . It is obvious that this model is reasonable as long as the population is small enough so that the interaction with its environment (adequate food, diseases, predators etc) can be neglected. The simplest model that takes into account some of the factors of the interaction with the environment (e.g. starvation) is obtained by the introduction of a simple non linear term in the equation so that

dP-(t) = kP (t)(1 − bP (t)).
  dt
(3.2)

The parameter k  gives the maximum growth rate of the population and b  controls the ability of the species to maintain a certain population level. The equation (3.2) can be discretized in time by assuming that each generation reproduces every δt  and that the n-th generation has population Pn  = P (tn)  where tn =  t0 + (n − 1)δt  . Then                       ′
P(tn+1) ≈ P (tn ) + δtP (tn)  and equation (3.1) becomes

Pn+1 =  rPn,
(3.3)

where r = 1 + kδt  . The solutions of the above equation are well approximated by Pn  ∼ P0ektn   ∝ e(r−1)n  so that we have population growth when r > 1  and decline when r < 1  . Equation (3.2) can be discretized as follows:

Pn+1 =  Pn(r − bPn).
(3.4)

Defining xn =  (b∕r )Pn  we obtain the logistic map

xn+1 =  rxn(1 − xn).
(3.5)

We define the functions

f (x) = rx(1 − x),     F(x,r) = rx (1 − x )
(3.6)

(their only difference is that, in the first one, r  is considered as a given parameter), so that

xn+1 =  f(xn) = f(2)(xn−1) = ...= f (n)(x1) = f(n+1)(x0),
(3.7)

where we use the notation   (1)
f   (x) = f(x)  ,  (2)
f   (x ) = f(f(x))  ,  (3)
f  (x) = f (f(f(x)))  , ... for function composition. In what follows, the derivative of f  will be useful:

f ′(x) =  ∂F-(x,-r)-= r(1 − 2x).
           ∂x
(3.8)

Since we interpret xn  to be the fraction of the population with respect to its maximum value, we should have 0 ≤ xn ≤  1  for each1 n  . The function f(x)  has one global maximum for x = 1∕2  which is equal to f(1∕2 ) = r ∕4  . Therefore, if r > 4  , then f(1∕2) > 1  , which for an appropriate choice of x0   will lead to xn+1  = f(xn) > 1  for some value of n  . Therefore, the interval of values of r  which is of interest for our model is

0 < r ≤ 4.
(3.9)

The logistic map (3.5) may be viewed as a finite difference equation and it is a one step inductive relation. Given an initial value x0   , a sequence of values {x0,  x1,  ...,  xn,  ...  } is produced. This will be referred2 to as the trajectory of x0   . In the following sections we will study the properties of these trajectories as a function of the parameter r  .

The solutions of the logistic map are not known except in special cases. For r = 2  we have

     1 (            2n)
xn = -- 1 − (1 − x0)   ,
     2
(3.10)

and for3 r = 4

         2 n              1-  − 1√ ---
xn =  sin (2  π𝜃),    𝜃 =  π sin     x0.
(3.11)

For r = 2  , limn →∞ xn =  1∕2  whereas for r = 4  we have periodic trajectories resulting in rational 𝜃  and non periodic resulting in irrational 𝜃  . For other values of r  we have to resort to a numerical computation of the trajectories of the logistic map.

3.2 Fixed Points and 2n  Cycles

It is obvious that if the point   ∗
x is a solution of the equation x = f(x )  , then        ∗
xn =  x ⇒          ∗
xn+k =  x for every k ≥  0  . For the function f(x ) = rx (1 − x)  we have two solutions

x∗1 = 0     and     x∗2 = 1 − 1∕r.
(3.12)

We will see that for appropriate values of r  , these solutions are attractors of most of the trajectories. This means that for a range of values for the initial point 0 ≤ x0 ≤  1  , the sequence {xn} approaches asymptotically one of these points as n →  ∞ . Obviously the (measure zero) sets of initial values {x0} = {x ∗1} and {x0 } = {x∗}
          2 result in trajectories attracted by x∗
 1   and x∗
 2   respectively. In order to determine which one of the two values is preferred, we need to study the stability of the fixed points   ∗
x 1   and  ∗
x2   . For this, assume that for some value of n  , xn  is infinitesimally close to the fixed point x∗ so that

           ∗
  xn   =  x  + 𝜖n
xn+1   =  x∗ + 𝜖n+1.                     (3.13)
Since
xn+1 =  f(xn) = f (x ∗ + 𝜖n) ≈ f (x ∗) + 𝜖nf ′(x∗) = x∗ + 𝜖nf′(x∗),
(3.14)

where we used the Taylor expansion of the analytic function f(x∗ + 𝜖 )
        n  about   ∗
x and the relation  ∗       ∗
x  = f (x )  , we have that           ′  ∗
𝜖n+1 = 𝜖nf (x )  . Then we obtain

|     |
|𝜖n+1 |
||-----|| = |f′(x∗)|.
  𝜖n
(3.15)

Therefore, if |f′(x ∗)| < 1  we obtain lim      𝜖 =  0
   n→ ∞  n  and the fixed point x ∗ is stable: the sequence {xn+k } approaches  ∗
x asymptotically. If   ′ ∗
|f (x )| > 1  then the sequence {xn+k} deviates away from  ∗
x and the fixed point is unstable. The limiting case |f′(x∗)| = 1  should be studied separately and it indicates a change in the stability properties of the fixed point. In the following discussion, these points will be shown to be bifurcation points.

For the function f (x ) = rx(1 − x)  with f ′(x) = r(1 − 2x)  we have that f ′(0) = r  and f′(1 − 1∕r ) = 2 − r  . Therefore, if r < 1  the point x∗1 = 0  is an attractor, whereas the point x∗=  1 − 1∕r < 0
 2  is irrelevant. When r > 1  , the point x∗ = 0
 1  results in |f′(x∗)| = r > 1
     1  , therefore x∗
 1   is unstable. Any initial value x0   near  ∗
x1   deviates from it. Since for 1 <  r < 3  we have that 0 ≤ |f′(x∗2)| = |2 − r| < 1  , the point x∗2   is an attractor. Any initial value x0 ∈ (0,1 )  approaches  ∗
x2 = 1 − 1∕r  . When      (1)
r = rc  = 1  we have the limiting case   ∗    ∗
x 1 = x2 = 0  and we say that at the critical value r(c1)= 1  the fixed point x∗1   bifurcates to the two fixed points x∗1   and x ∗
  2   .

As r  increases, the fixed points continue to bifurcate. Indeed, when      (2)
r = rc  =  3  we have that  ′  ∗
f (x2) = 2 − r = − 1  and for      (2)
r > rc  the point  ∗
x2   becomes unstable. Consider the solution of the equation x = f (2)(x)  . If 0 < x ∗ < 1  is one of its solutions and for some n  we have that xn =  x∗ , then x    =
  n+2  x    =
  n+4  ...=  x     =
 n+2k  ...=  x ∗ and x    =
 n+1  x    =
 n+3  ...=  xn+2k+1 =  ...=     ∗
f(x )  (therefore    ∗
f(x )  is also a solution). If      ∗
0 < x3 <    ∗
x 4 < 1  are two such different solutions with  ∗       ∗
x3 = f(x 4)  ,  ∗       ∗
x4 = f (x3)  , then the trajectory is periodic with period 2. The points x∗3   , x∗4   are such that they are real solutions of the equation

f(2)(x ) = r2x(1 − x)(1 − rx(1 − x)) = x,
(3.16)

and at the same time they are not the solutions x∗=  0
 1  x∗ = 1 − 1∕r
 2  of the equation4 x =  f(2)(x)  , the polynomial above can be written in the form (see  [22] for more details)

  (     (      ) )
              1-       2
x   x −   1 − r    (Ax  + Bx  + C ) = 0.
(3.17)

By expanding the polynomials (3.16) , (3.17) and comparing their coefficients we conclude that A = − r3   , B  = r2(r + 1)  and C  = − r(r + 1)  . The roots of the trinomial in (3.17) are determined by the discriminant       2
Δ  = r (r + 1)(r − 3)  . For the values of r  of interest (1 <  r ≤ 4  ), the discriminant becomes positive when      (2)
r > rc  = 3  and we have two different solutions

  ∗             √ -2---------
x α = ((r + 1) ∓  r  − 2r − 3)∕(2r)    α =  3,4.
(3.18)

When r = r(c2)  we have one double root, therefore a unique fixed point.

The study of the stability of the solutions of x =  f(2)(x)  requires the same steps that led to the equation (3.15) and we determine if the absolute value of  (2)′
f   (x)  is greater, less or equal to one. By noting that5 f (2)′(x ) =
      3  f(2)′(x ) =
      4  f ′(x )f ′(x )
    3     4  = − r2 + 2r + 4  , we see that for      (2)
r = rc  =  3  ,  (2)′  ∗
f   (x 3) =   (2)′ ∗
f   (x4) = 1  and for      (3)      √ --
r = rc  = 1 +   6 ≈ 3.4495  , f (2)′(x3) =  f(2)′(x4) = − 1  . For the intermediate values             √ --
3 < r < 1 +   6  the derivatives   (2)′ ∗
|f   (xα)| < 1  for α = 3,4  . Therefore, these points are stable solutions of       (2)
x = f   (x)  and the points  ∗  ∗
x1,x2   bifurcate to  ∗
xα  , α = 1,2, 3,4  for r = r(2c) = 3  . Almost all trajectories with initial points in the interval [0,1]  are attracted by the periodic trajectory with period 2, the “2-cycle”    ∗  ∗
{x 3,x4} .

Using similar arguments we find that the fixed points x∗α  , α = 1, 2,3,4  bifurcate to the eight fixed points x∗
 α  , α = 1, ...,8  when               √ --
r = r(c3) = 1 +   6  . These are real solutions of the equation that gives the 4-cycle       (4)
x =  f  (x)  . For  (3)        (4)
rc  <  r < rc ≈  3.5441  , the points  ∗
xα  , α = 5, ...,8  are a stable 4-cycle which is an attractor of almost all trajectories of the logistic map6 . Similarly, for  (4)        (5)
rc  < r < rc  the 16 fixed points of the equation       (8)
x =  f  (x)  give a stable 8-cycle, for  (5)        (6)
rc  < r < rc  a stable 16-cycle etc7 . This is the phenomenon which is called period doubling which continues ad infinitum. The points  (n)
rc  are getting closer to each other as n  increases so that          (n)
limn →∞ rc  =  rc ≈ 3.56994567  . As we will see, rc  marks the onset of the non-periodic, chaotic behavior of the trajectories of the logistic map.


pict pict

Figure 3.1: (Left) Some trajectories of the logistic map with x = 0.1
 0  and various values of r  . We can see the first bifurcation for  (1)
rc  = 1  from  ∗
x1 = 0  to  ∗
x2 = 1− 1∕r  . (Right) Trajectories of the logistic map for  (2)           (3)
rc  < r = 3.5 < rc  . The three curves start from three different initial points. After a transient period, depending on the initial point, one obtains a periodic trajectory which is a 2-cycle. The horizontal lines are the expected values x∗  = ((r+ 1)∓ √r2-−-2r−-3)∕(2r)
 3,4  (see text).

Computing the bifurcation points becomes quickly intractable and we have to resort to a numerical computation of their values. Initially we will write a program that computes trajectories of the logistic map for chosen values of r  and x
  0   . The program can be found in the file logistic.f90 and is listed below:

!===========================================================  
!Discrete Logistic Map  
!===========================================================  
program logistic_map  
 implicit none  
 integer :: NSTEPS,i  
 real(8) :: r,x0,x1  
! ----- Input:  
 print *,’# Enter NSTEPS, r, x0:’  
 read  *,NSTEPS,r,x0  
 print *,’# NSTEPS = ’,NSTEPS  
 print *,’# r      = ’,r  
 print *,’# x0     = ’,x0  
! ----- Initialize:  
 open(unit=33,file=’log.dat’)  
 write(33,*) 0,x0  
! ----- Calculate:  
 do i=1,NSTEPS  
  x1 = r * x0 * (1.0D0-x0)  
  write(33,*)i,x1  
  x0 = x1  
 enddo  
 close(33)  
end program logistic_map

The program is compiled and run using the commands:

> gfortran logistic.f90 -o l  
> echo "100 0.5 0.1" | ./l

The command echo prints to the stdout the values of the parameters NSTEPS=100, r=0.5 and x0=0.1. Its stdout is redirected to the stdin of the command ./l by using a pipe via the symbol |, from which the program reads their value and uses them in the calculation. The results can be found in two columns in the file log.dat and can be plotted using gnuplot. The plots are put in figure 3.1 and we can see the first two bifurcations when r  goes past the values  (1)
rc  and  (2)
rc  . Similarly, we can study trajectories which are 2n  -cycles when r  crosses the values  (n− 1)
rc  .


pict pict

Figure 3.2: Cobweb plots of the logistic map for r = 2.8  and 3.3  . (Left) The left plot is an example of a fixed point   ∗     ∗
x  = f(x )  . The green line is y = f(x)  and the blue line is y = f(2)(x)  . The trajectory ends at the unique non zero intersection of the diagonal and y = f(x )  which is x∗2 = 1 − 1∕r  . The trajectory intersects the curve y = f(2)(x )  at the same point. y = f (2)(x)  does not intersect the diagonal anywhere else. (Right) The right plot shows an example of a 2-cycle. y = f(2)(x)  intersects the diagonal at two additional points determined by  ∗
x3  and  ∗
x4  . The trajectory ends up on the orthogonal (x∗3,x∗3)  , (x∗4,x ∗3)  , (x∗4,x∗4)  , (x∗3,x∗4)  .


pict pict

Figure 3.3: (Left) A 4-cycle for r = 3.5  . The blue curve is y = f(4)(x)  which intersects the diagonal at four points determined by xα  , α = 5,6,7,8  . The four cycle passes through these points. (Right) a non periodic orbit for r = 3.7  when the system exhibits chaotic behavior.

Another way to depict the 2-cycles is by constructing the cobweb plots: We start from the point (x0,0)  and we calculate the point (x0,x1)  , where x1 = f (x0)  . This point belongs on the curve y =  f(x)  . The point (x0,x1)  is then projected on the diagonal y = x  and we obtain the point (x ,x )
  1  1  . We repeat n  times obtaining the points (xn,xn+1 )  and (xn+1,xn+1 )  on y = f(x )  and y = x  respectively. The fixed points   ∗      ∗
x  =  f(x )  are at the intersections of these curves and, if they are attractors, the trajectories will converge on them. If we have a 2n  -cycle, we will observe a periodic trajectory going through points which are solutions to the equation x =  f(2n)(x)  . This exercise can be done by using the following program, which can be found in the file logistic1.f90:

!===========================================================  
! Discrete Logistic Map  
! Map the trajectory in 2d space (plane)  
!===========================================================  
program logistic_map  
 implicit none  
 integer :: NSTEPS,i  
 real(8) :: r,x0,x1  
! ----- Input:  
 print *,’# Enter NSTEPS, r, x0:’  
 read  *,NSTEPS,r,x0  
 print *,’# NSTEPS = ’,NSTEPS  
 print *,’# r      = ’,r  
 print *,’# x0     = ’,x0  
! ----- Initialize:  
 open(unit=33,file=’trj.dat’)  
! ----- Calculate:  
 write(33,*) 0, x0,0  
 do i=1,NSTEPS  
  x1 = r * x0 * (1.0D0-x0)  
  write(33,*) 2*i-3,x0,x1  
  write(33,*) 2*i-2,x1,x1  
  x0 = x1  
 enddo  
 close(33)  
end program logistic_map

Compiling and running this program is done exactly as in the case of the program in logistic.f90. We can plot the results using gnuplot. The plot in figure 3.2 can be constructed using the commands:

gnuplot> set size square  
gnuplot> f(x) = r*x*(1.0-x)  
gnuplot> r = 3.3  
gnuplot> plot "<echo 50 3.3 0.2|./l;cat trj.dat" using 2:3 w l  
gnuplot> replot f(x) ,f(f(x)),x

The plot command shown above, runs the program exactly as it is done on the command line. This is accomplished by using the symbol <, which reads the plot from the stdout of the command "echo 50 3.3 0.2|./l;cat trj.dat". Only the second command "echo trj.dat" writes to the stdout, therefore the plot is constructed from the contents of the file trj.dat. The following line adds the plots of the functions f(x)  , f(2)(x ) = f(f(x))  and of the diagonal y = x  . Figures 3.2 and 3.3 show examples of attractors which are fixed points, 2-cycles and 4-cycles. An example of a non periodic trajectory is also shown, which exhibits chaotic behavior which can happen when r > rc ≈ 3.56994567  .

3.3 Bifurcation Diagrams

The bifurcations of the fixed points of the logistic map discussed in the previous section can be conveniently shown on the “bifurcation diagram”. We remind to the reader that the first bifurcations happen at the critical values of r

r(c1)<  r(2c) < r(c3) < ...<  r(nc) < ...<  rc,
(3.19)

where  (1)
rc  = 1  ,  (2)
rc  = 3  ,  (3)      √ --
rc  = 1 +   6  and              (n)
rc = limn →∞ rc  ≈ 3.56994567  . For r(cn)<  r < r(nc+1 )  we have 2n  fixed points x∗α  , α =  1,2,...,2n  of x =  f(2n)(x)  . By plotting these points x∗ (r )
 α  as a function of r  we construct the bifurcation diagram. These can be calculated numerically by using the program bifurcate.f90. In this program, the user selects the values of r  that she needs to study and for each one of them the program records the point of the 2n− 1   -cycles8   ∗
x α(r)  ,       n−1      n−1          n
α =  2    + 1,2    + 2,...,2  . This is easily done by computing the logistic map several times until we are sure that the trajectories reach the stable state. The parameter NTRANS in the program determines the number of points that we throw away, which should contain all the transient behavior. After NTRANS steps, the program records NSTEPS points, where NSTEPS should be large enough to cover all the points of the  n− 1
2   -cycles or depict a dense enough set of values of the non periodic orbits. The program is listed below:

!===========================================================  
! Bifurcation Diagram of the Logistic Map  
!===========================================================  
program bifurcation_diagram  
 implicit none  
 real(8),parameter :: rmin   = 2.5D0  
 real(8),parameter :: rmax   = 4.0D0  
 integer,parameter :: NTRANS = 500   !Number of discarded steps  
 integer,parameter :: NSTEPS = 100   !Number of recorded  steps  
 integer,parameter :: RSTEPS = 2000  !Number of values of r  
 integer           :: i  
 real(8)           :: r,dr,x0,x1  
! ----- Initialize:  
 open(unit=33,file=’bif.dat’)  
 dr     = (rmax-rmin)/RSTEPS !Increment in r  
! ----- Calculate:  
 r = rmin  
 do while ( r .le. rmax)  
  x0 = 0.5D0  
! ---- Transient steps: skip  
  do i=1,NTRANS  
   x1 = r * x0 * (1.0D0-x0)  
   x0 = x1  
  enddo  
  do i=1,NSTEPS  
   x1 = r * x0 * (1.0D0-x0)  
   write(33,*) r,x1  
   x0 = x1  
  enddo  
  r = r + dr  
 enddo ! do while  
 close(33)  
end program bifurcation_diagram


pict pict

Figure 3.4: (Left) The bifurcation diagram computed by the program bifurcate.f90 for 2.5 < r < 4  . Notice the first bifurcation points followed by intervals of chaotic, non-periodic orbits interrupted by intermissions of stable periodic trajectories. The chaotic trajectories take values in subsets of the interval (0,1)  . For r = 4  they take values within the whole (0,1)  . One can see that for r = 1 +√8 ≈ 3.8284  we obtain a 3-cycle which subsequently bifurcates to 3⋅2n  -cycles. (Right) The diagram on the left is magnified in a range of r  showing the self-similarity of the diagram at all scales.

The program can be compiled and run using the commands:

> gfortran bifurcate.f90 -o b  
> ./b;

The left plot of figure 3.4 can be constructed by the gnuplot commands:

gnuplot> plot "bif.dat" with dots

We observe the fixed points and the 2n  -cycles for r < rc  . When r  goes past rc  , the trajectories become non-periodic and exhibit chaotic behavior. Chaotic behavior will be discussed more extensively in the next section. For the time being, we note that if we measure the distance between the points    (n)    (n+1)    (n)
Δr    =  rc    − rc  , we find that it decreases constantly with n  so that

         (n)
lim  -Δr-----= δ ≈ 4.669201609,
n→∞  Δr (n+1)
(3.20)

where δ  is the Feigenbaum constant. An additional constant α  , defined by the quotient of the separation of adjacent elements Δwn  of period doubled attractors from one double to the next Δwn+1   , is

      Δwn
 lim  ------- = α ≈ 2.502907875.
n→ ∞ Δwn+1
(3.21)

It is also interesting to note the appearance of a 3-cycle right after         √ --
r = 1 +   8 ≈ 3.8284 >  rc  ! By using the theorem of Sharkovskii, Li and Yorke9 showed that any one dimensional system has 3-cycles, therefore it will have cycles of any length and chaotic trajectories. The stability of the 3-cycle can be studied from the solutions of       (3)
x =  f  (x)  in exactly the same way that we did in equations (3.16) and (3.17) (see  [22] for details). Figure 3.5 magnifies a branch of the 3-cycle. By magnifying different regions in the bifurcation plot, as shown in the right plot of figure 3.4, we find similar shapes to the branching of the 3-cycle.


pict

Figure 3.5: Magnification of one of the three branches of the 3-cycle for r > 1+ √8-  . To the left, we observe the temporary halt of the chaotic behavior of the trajectory, which comes back as shown in the plot to the right after an intermission of stable periodic trajectories.

Figure 3.4 shows that between intervals of chaotic behavior we obtain “windows” of periodic trajectories. These are infinite but countable. It is also quite interesting to note that if we magnify a branch withing these windows, we obtain a diagram that is similar to the whole diagram! We say that the bifurcation diagram exhibits self similarity. There are more interesting properties of the bifurcation diagram and we refer the reader to the bibliography for a more complete exposition.

We close this section by mentioning that the qualitative properties of the bifurcation diagram are the same for a whole class of functions. Feigenbaum discovered that if one takes any function that is concave and has a unique global maximum, its bifurcation diagram behaves qualitatively the same way as that of the logistic map. Examples of such functions10 studied in the literature are          r(1−x)
g(x) = xe   , u(x ) = rsin (πx)  and             2
w(x) = b − x   . The constants δ  and α  of equations (3.20) and (3.21) are the same of all these mappings. The functions that result in chaotic behavior are studied extensively in the literature and you can find a list of those in  [28].

3.4 The Newton-Raphson Method

In order to determine the bifurcation points, one has to solve the nonlinear, polynomial, algebraic equations       (n)
x = f   (x)  and  (n)′
f   (x) = − 1  . For this reason, one has to use an approximate numerical calculation of the roots, and the simple Newton-Raphson method will prove to be a good choice.

Newton-Raphson’s method uses an initial guess x
 0   for the solution of the equation g (x ) = 0  and computes a sequence of points x1,  x2,  ...,  xn,  xn+1,  ...  that presumably converges to one of the roots of the equation. The computation stops at a finite n  , when we decide that the desired level of accuracy has been achieved. In order to understand how it works, we assume that g(x )  is an analytic function for all the values of x  used in the computation. Then, by Taylor expanding around xn  we obtain

                              ′
g(xn+1) = g(xn) + (xn+1 − xn)g (x) + ....
(3.22)

If we wish to have g(xn+1) ≈ 0  , we choose

             g (x  )
xn+1 =  xn − ----n-.
             g′(xn)
(3.23)

The equation above gives the Newton-Raphson method for one equation g(x ) = 0  of one variable x  . Different choices for x0   will possibly lead to different roots. When g′(x )  , g ′′(x )  are non zero at the root and g ′′′(x)  is bounded, the convergence of the method is quadratic with the number of iterations. This means that there is a neighborhood of the root α  such that the distance Δxn+1  = xn+1 − α  is                 2
Δxn+1  ∝  (Δxn  )   . If the root α  has multiplicity larger than 1, convergence is slower. The proofs of these statements are simple and can be found in  [29].

The Newton-Raphson method is simple to program and, most of the times, sufficient for the solution of many problems. In the general case it works well only close enough to a root. We should also keep in mind that there are simple reasons for the method to fail. For example, when g′(xn ) = 0  for some n  , the method stops. For functions that tend to 0  as x →  ±∞ , it is easy to make a bad choice for x0   that does not lead to convergence to a root. Sometimes it is a good idea to combine the Newton-Raphson method with the bisection method. When the derivative g′(x )  diverges at the root we might get into trouble. For example, the equation |x|ν = 0  with 0 < ν <  1∕2  , does not lead to a convergent sequence. In some cases, we might enter into non-convergent cycles  [8]. For some functions the basin of attraction of a root (the values of x0   that will converge to the root) can be tiny. See problem 13.

As a test case of our program, consider the equation

         ∘ -------
𝜖tan 𝜖 =   ρ2 − 𝜖2
(3.24)

which results from the solution of Schrödinger’s equation for the energy spectrum of a quantum mechanical particle of mass m  in a one dimensional potential well of depth V0   and width L  . The parameters     ∘  ------------
𝜖 =    mL2E  ∕(2ℏ)  and     ∘  ------------
ρ =    mL2V0 ∕(2ℏ )  . Given ρ  , we solve for 𝜖  which gives the energy E  . The function g(x)  and its derivative g′(x)  are

                    ∘ --2---2-
 g(x)  =   xtan x −   ρ  − x
g ′(x)  =   ∘---x-----+ ---x-- + tan x.            (3.25)
             ρ2 − x2   cos2 x
The program of the Newton-Raphson method for solving the equation g (x) = 0  can be found in the file nr.f90:
!===========================================================  
!Newton Raphson for a function of one variable  
!===========================================================  
program NewtonRaphson  
 implicit none  
 real(8), parameter :: rho  = 15.0D0  
 real(8), parameter :: eps  = 1D-6  
 integer, parameter :: NMAX = 1000  
 real(8) :: x0, x1, err, g, gp  
 integer :: i  
 print *, ’Enter x0: ’  
 read  *, x0  
 err = 1.0D0  
 print *,’iter           x                        error    ’  
 print *,’-------------------------------------------------’  
 print *, 0,x0,err  
 do i=1,NMAX  
!value of function g(x):  
  g   = x0*tan(x0)-sqrt(rho*rho-x0*x0)  
!value of the derivative g’(x):  
  gp  = x0/sqrt(rho*rho-x0*x0)+x0/(cos(x0)**2)+tan(x0)  
  x1  = x0 - g/gp  
  err = ABS(x1-x0)  
  print *,i,x1,err  
  if(err .lt. eps) exit  
  x0 = x1  
 enddo  
end program NewtonRaphson

In the program listed above, the user is asked to set the initial point x0   . We fix ρ =  rho = 15  . It is instructive to make the plot of the left and right hand sides of (3.24) and make a graphical determination of the roots from their intersections. Then we can make appropriate choices of the initial point x0   . Using gnuplot, the plots are made with the commands:

gnuplot> g1(x) = x*tan(x)  
gnuplot> g2(x) = sqrt(rho*rho-x*x)  
gnuplot> plot [0:20][0:20]  g1(x), g2(x)


pict

Figure 3.6: Plots of the right and left hand sides of equation (3.24) . The intersections of the curves determine the solutions of the equation and their approximate graphical estimation can serve as initial points x0  for the Newton-Raphson method.

The compilation and running of the program can be done as follows:

> gfortran nr.f90 -o n  
> echo "1.4"|./n  
 Enter x0:  
 iter x                         error  
 -------------------------------------------------  
 0    1.3999999999999999        1.0000000000000000  
 1    1.5254292024457967        0.12542920244579681  
 2    1.5009739120496131        2.4455290396183660E-002  
 3    1.4807207017202200        2.0253210329393090E-002  
 4    1.4731630533073483        7.5576484128716537E-003  
 5    1.4724779331237687        6.8512018357957949E-004  
 6    1.4724731072313519        4.8258924167932093E-006  
 7    1.4724731069952235        2.3612845012621619E-010

We conclude that one of the roots of the equation is 𝜖 ≈ 1.472473107  . The reader can compute more of these roots by following these steps by herself.

The method discussed above can be easily generalized to the case of two equations. Suppose that we need to solve simultaneously two algebraic equations g1(x1,x2) = 0  and g2(x1,x2) = 0  . In order to compute a sequence (x10,x20)  , (x11,x21)  , ...  , (x1n, x2n)  , (x1(n+1),x2(n+1))  , ...  that may converge to a root of the above system of equations, we Taylor expand the two functions around (x1n,x2n)

                                                   ∂g  (x  ,x  )
g1(x1(n+1),x2(n+1))  =  g1(x1n,x2n) + (x1(n+1) − x1n)--1--1n--2n-
                                                        ∂x1
                                          ∂g1(x1n,x2n)-
                       +   (x2(n+1) − x2n )   ∂x       + ...
                                                 2
g2(x1(n+1),x2(n+1))  =  g2(x1n,x2n) + (x1(n+1) − x1n)∂g2-(x1n,x2n)
                                                        ∂x1
                                          ∂g2(x1n,x2n)
                       +   (x2(n+1) − x2n )------------+ ....  (3.26)
                                              ∂x2
Defining δx1 = (x1(n+1) − x1n)  and δx2 =  (x2 (n+1) − x2n)  and setting g1(x1(n+1),x2(n+1)) ≈ 0  , g2(x1(n+1),x2(n+1)) ≈ 0  , we obtain
    ∂g1       ∂g1
δx1 ----+ δx2 ----  =  − g1
    ∂x1       ∂x2
δx  ∂g2-+ δx  ∂g2-  =  − g .                 (3.27)
   1∂x1      2∂x2         2
This is a linear 2 × 2  system of equations
A11δx1 + A12 δx2  =  b1

A21δx1 + A22 δx2  =  b2,                   (3.28)
where Aij = ∂gi∕∂xj  and bi = − gi  , with i,j = 1,2  . Solving for δxi  we obtain
x1(n+1) =   x1n + δx1
x2(n+1) =   x2n + δx2.                    (3.29)
The iterations stop when δxi  become small enough.

As an example, consider the equations with g1(x) = 2x2 − 3xy +  y − 2  , g2(x) = 3x + xy +  y − 1  . We have A11 =  4x − 3y  , A12 =  1 − 3x  , A21 = 3 + y  , A22 =  1 + x  . The program can be found in the file nr2.f90:

!===========================================================  
!Newton Raphson of two functions of two variables  
!===========================================================  
program NewtonRaphson2  
 implicit none  
 real(8), parameter :: eps  = 1D-6  
 integer, parameter :: NMAX = 1000  
 real(8) :: A(2,2),b(2),dx(2)  
 real(8) :: x,y, err  
 integer :: i  
 print *, ’Enter x0,y0: ’  
 read  *, x,y  
 err = 1.0D0  
 print *,’iter           x          y             error    ’  
 print *,’-------------------------------------------------’  
 print *, 0,x,y,err  
 do i=1,NMAX  
  b(1)   = -(2.0D0*x*x-3.0D0*x*y+y-2.0D0) ! -g1(x,y)  
  b(2)   = -(3.0D0*x + x*y + y - 1.0D0)   ! -g2(x,y)  
! dg1/dx                    dg1/dy  
  A(1,1) = 4.0D0*x-3.0D0*y; A(1,2) = 1.0D0-3.0D0*x  
! dg2/dx                    dg2/dy  
  A(2,1) = 3.0D0+y        ; A(2,2) = 1.0D0+x  
  call solve2x2(A,B,dx)  
  x = x + dx(1)  
  y = y + dx(2)  
  err = 0.5D0*SQRT(dx(1)**2+dx(2)**2)  
  print *,i,x,y,err  
  if(err .lt. eps) exit  
 enddo  
end program NewtonRaphson2  
!===========================================================  
subroutine solve2x2(A,b,dx)  
 implicit none  
 real(8) :: A(2,2),b(2),dx(2)  
 real(8) :: num1,num2,det  
 num1 = A(2,2)*b(1)-A(1,2)*b(2)  
 num2 = A(1,1)*b(2)-A(2,1)*b(1)  
 det  = A(1,1)*A(2,2)-A(1,2)*A(2,1)  
 if(det .eq. 0.0D0) stop ’solve2x2: det=0’  
 dx(1)= num1/det  
 dx(2)= num2/det  
end subroutine solve2x2

In order to guess the region where the real roots of the systems lie, we make a 3-dimensional plot using gnuplot:

gnuplot> set isosamples 20  
gnuplot> set hidden3d  
gnuplot> splot 2*x**2-3*x*y+y-2,3*x+y*x+y-1,0

We plot the functions gi(x, y)  together with the plane x = 0  . The intersection of the three surfaces determine the roots we are looking for. Compiling and running the program can be done by using the commands:

> gfortran nr2.f90 -o n  
> echo 2.2 1.5 |./n  
 Enter x0,y0:  
 iter    x          y      error  
 --------------------------------------  
 0  2.20000000  1.50000000 1.0000  
 1  0.76427104  0.26899383 0.9456  
 2  0.73939531 -0.68668275 0.4780  
 3  0.74744506 -0.71105605 1.2834E-002  
 4  0.74735933 -0.71083147 1.2019E-004  
 5  0.74735932 -0.71083145 1.2029E-008  
> echo 0 1 |./n  
.................  
 5 -0.10899022  1.48928857 4.3461E-012  
> echo -5 0|./n  
 6 -6.13836909 -3.77845711 3.2165E-013

The computation above leads to the roots (0.74735932,  − 0.71083145 )  , (− 0.10899022,  1.48928857 )  , (− 6.13836909,  − 3.77845711 )  .

The Newton-Raphson method for many variables becomes hard quite soon: One needs to calculate the functions as well as their derivatives, which is prohibitively expensive for many problems. It is also hard to determine the roots, since the method converges satisfactorily only very close to the roots. We refer the reader to  [8] for more information on how one can deal with these problems.

3.5 Calculation of the Bifurcation Points

In order to determine the bifurcation points for r < rc  we will solve the algebraic equations x =  f(k)(x)  and f(k)′(x) = − 1  . At these points, k  -cycles become unstable and 2k  -cycles appear and are stable. This happens when r = r(cn)  , where k = 2n −2   . We will look for solutions (x∗,r(cn))
  α  for α =  k + 1,k + 2,...,2k  .

We define the functions F (x,r) = f(x ) = rx (1 − x)  and   (k)         (k)
F   (x,r) = f   (x)  as in equation (3.6) . We will solve the algebraic equations:

g1(x,r)  =   x − F(k)(x,r) = 0
                (k)
g (x,r)  =   ∂F---(x,r)-+ 1 = 0.               (3.30)
 2               ∂x
According to the discussion of the previous section, in order to calculate the roots of these equations we have to solve the linear system (3.28) , where the coefficients are
  b   =  − g (x,r) = − x + F (k)(x,r)
   1        1             (k)
  b   =  − g (x,r) = − ∂F---(x,-r)−  1
   2        2              ∂x
          ∂g (x,r)       ∂F (k)(x, r)
A11   =   --1------= 1 − -----------
            ∂x               ∂x
          ∂g1(x,r)-    ∂F-(k)(x,-r)
A12   =      ∂r    = −     ∂r
          ∂g (x,r)   ∂2F (k)(x, r)
A21   =   --2------= ------------
            ∂x           ∂x2
          ∂g2(x,r)-  ∂2F-(k)(x,-r)
A22   =      ∂r    =     ∂x∂r    .                (3.31)
The derivatives will be calculated approximately using finite differences
   (k)            (k)             (k)
∂F---(x,-r)  ≈   F---(x-+-𝜖,r)-−-F---(x-−-𝜖,r)-
    ∂x                        2𝜖
∂F (k)(x, r)      F(k)(x, r + 𝜖) − F (k)(x,r − 𝜖)
-----------  ≈   ----------------------------,        (3.32)
    ∂r                        2𝜖
and similarly for the second derivatives
                  ∂F(k)(x+ 𝜖,r)   ∂F(k)(x− 𝜖,r)
∂2F-(k)(x,r-)     -----∂x-2--−------∂x-2--
    ∂x2       ≈             2-𝜖
                     {       2                                              }
                    1- F-(k)(x-+-𝜖,r)-−-F-(k)(x,r)   F-(k)(x,r)-−-F-(k)(x-−-𝜖,r)
              =     𝜖              𝜖             −             𝜖
                      {                                          }
              =     1- F (k)(x + 𝜖,r) − 2F (k)(x,r) + F (k)(x − 𝜖,r)
                    𝜖2
∂2F (k)(x,r )      ∂F(k)(x+-𝜖x,r)-− ∂F(k)(x−𝜖x,r)
------------  ≈  -----∂r------------∂r----
   ∂x ∂r             {      2𝜖x
                 -1--  F-(k)(x-+-𝜖x,r +-𝜖r)-−-F-(k)(x-+-𝜖x,r −-𝜖r)
              =  2 𝜖                     2𝜖
                   x                       r                    }
                        F-(k)(x-−-𝜖x,r +-𝜖r)-−-F-(k)(x-−-𝜖x,r −-𝜖r)
                      −                   2𝜖r
                       {
              =  --1--  F (k)(x + 𝜖x,r + 𝜖r) − F (k)(x + 𝜖x,r − 𝜖r)
                 4 𝜖x𝜖r                                         }
                      − F(k)(x − 𝜖x,r + 𝜖r) + F (k)(x − 𝜖x,r − 𝜖r)       (3.33)
We are now ready to write the program for the Newton-Raphson method like in the previous section. The only difference is the approximate calculation of the derivatives using the relations above and the calculation of the function F (k)(x,r)  by a routine that will compose the function f(x)  k  -times. The program can be found in the file bifurcationPoints.f90:
!===========================================================  
!        bifurcationPoints.f  
! Calculate bifurcation points of the discrete logistic map  
! at period k by solving the condition  
! g1(x,r) = x - F(k,x,r)   = 0  
! g2(x,r) = dF(k,x,r)/dx+1 = 0  
! determining when the Floquet multiplier becomes 1  
! F(k,x,r) iterates F(x,r) = r*x*(x-1) k times  
! The equations are solved by using a Newton-Raphson method  
!===========================================================  
program bifurcationPoints  
 implicit none  
 real(8),parameter :: tol=1.0D-10  
 integer :: k,iter  
 real(8) :: r0,x0  
 real(8) :: A(2,2),B(2),dX(2)  
 real(8) :: error  
 real(8) :: F,dFdx,dFdr,d2Fdx2,d2Fdrdx  
! ---- Input:  
 print *,’# Enter k,r0,x0:’  
 read  *,k,r0,x0  
 print *,’# Period k= ’,k  
 print *,’# r0= ’,r0,’ x0= ’,x0  
! ---- Initialize  
 error   = 1.0D0           !initial large value of error>tol  
 iter    = 0  
 do while(error .gt. tol)  
! ---- Calculate jacobian matrix  
  A(1,1) = 1.0D0-dFdx(k,x0,r0)  
  A(1,2) = -dFdr     (k,x0,r0)  
  A(2,1) = d2Fdx2    (k,x0,r0)  
  A(2,2) = d2Fdrdx   (k,x0,r0)  
  B(1)   = -x0 +    F(k,x0,r0)  
  B(2)   = -dFdx     (k,x0,r0)-1.0D0  
! ---- Solve a 2x2 linear system:  
  call solve2x2(A,B,dX)  
  x0     = x0 + dX(1)  
  r0     = r0 + dX(2)  
  error  = 0.5D0*sqrt(dX(1)**2+dX(2)**2)  
  iter   = iter+1  
  print*,iter,’x0= ’,x0,’ r0= ’,r0,’ err=’,error  
 enddo !do while(error .gt. tol)  
end program bifurcationPoints  
!===========================================================  
!Function F(k,x,r) and its derivatives  
real(8) function F(k,x,r)  
 implicit none  
 real(8) :: x,r,x0  
 integer k,i  
 
 x0  = x  
 do i=1,k  
  x0 = r*x0*(1.0D0-x0)  
 enddo  
 F   = x0  
 
end function F  
! ----------------------------------  
real(8) function dFdx(k,x,r)  
 implicit none  
 real(8) :: x,r,eps  
 real(8) :: F  
 integer k  
 
 eps     = 1.0D-6*x  
 dFdx    = (F(k,x+eps,r)-F(k,x-eps,r))/(2.0D0*eps)  
end function dFdx  
! ----------------------------------  
real(8) function dFdr(k,x,r)  
 implicit none  
 real(8) :: x,r,eps  
 real(8) :: F  
 integer k  
 
 eps     = 1.0D-6*r  
 dFdr    = (F(k,x,r+eps)-F(k,x,r-eps))/(2.0D0*eps)  
end function dFdr  
! ----------------------------------  
real(8) function d2Fdx2(k,x,r)  
 implicit none  
 real(8) :: x,r,eps  
 real(8) :: F  
 integer k  
 
 eps     = 1.0D-6*x  
 d2Fdx2  = (F(k,x+eps,r)-2.0D0*F(k,x,r)+F(k,x-eps,r))/(eps*eps)  
end function d2Fdx2  
! ----------------------------------  
real(8) function d2Fdrdx(k,x,r)  
 implicit none  
 real(8) :: x,r,epsx,epsr  
 real(8) :: F  
 integer k  
 
 epsx    = 1.0D-6*x  
 epsr    = 1.0D-6*r  
 d2Fdrdx = (F(k,x+epsx,r+epsr)-F(k,x+epsx,r-epsr) &  
      -F(k,x-epsx,r+epsr)+F(k,x-epsx,r-epsr))     &  
          /(4.0D0*epsx*epsr)  
end function d2Fdrdx  
!===========================================================  
subroutine solve2x2(A,b,dx)  
 implicit none  
 real(8) :: A(2,2),b(2),dx(2)  
 real(8) :: num1,num2,det  
 num1 = A(2,2)*b(1)  - A(1,2)*b(2)  
 num2 = A(1,1)*b(2)  - A(2,1)*b(1)  
 det  = A(1,1)*A(2,2)- A(1,2)*A(2,1)  
 if(det .eq. 0.0D0) stop ’solve2x2: det = 0’  
 dx(1) = num1/det  
 dx(2) = num2/det  
end subroutine solve2x2

Compiling and running the program can be done as follows:

> gfortran bifurcationPoints.f90 -o b  
> echo 2 3.5 0.5 |./b  
 # Enter k,r0,x0:  
 # Period k=            2  
 # r0= 3.5000000000000 x0= 0.50000000000  
 1 x0= 0.4455758353187 r0= 3.38523275827 err= 6.35088E-002  
 2 x0= 0.4396562547624 r0= 3.45290970406 err= 3.39676E-002  
 3 x0= 0.4399593001407 r0= 3.44949859951 err= 1.71226E-003  
 4 x0= 0.4399601690333 r0= 3.44948974267 err= 4.44967E-006  
 5 x0= 0.4399601689937 r0= 3.44948974281 err= 7.22160E-011  
> echo 2 3.5 0.85 | ./b  
 .................  
 4 x0= 0.8499377795512 r0= 3.44948974275 err= 1.85082E-011  
> echo 4 3.5 0.5 |./b  
 .................  
 5 x0= 0.5235947861540 r0= 3.54409035953 err= 1.86318E-011  
> echo 4 3.5 0.35 | ./b  
 .................  
 5 x0= 0.3632903374118 r0= 3.54409035955 err= 5.91653E-013

The above listing shows the points of the 2-cycle and some of the points of the 4-cycle. It is also possible to compare the calculated value r(3c)=  3.449490132  with the expected one           √ --
r(3c)=  1 +   6  ≈ 3.449489742  . Improving the accuracy of the calculation is left as an exercise for the reader who has to control the systematic errors of the calculations and achieve better accuracy in the computation of  (4)
rc  .

3.6 Liapunov Exponents

We have seen that when r > rc ≈ 3.56994567  , the trajectories of the logistic map become non periodic and exhibit chaotic behavior. Chaotic behavior mostly means sensitivity of the evolution of a dynamical system to the choice of initial conditions. More precisely, it means that two different trajectories constructed from infinitesimally close initial conditions, diverge very fast from each other. This implies that there is a set of initial conditions that densely cover subintervals of (0,1)  whose trajectories do not approach arbitrarily close to any cycle of finite length.

Assume that two trajectories have x
  0   , ˜x
  0   as initial points and Δx0  = x0 − ˜x0   . When the points xn  , ˜xn  have a distance Δxn  = ˜xn − xn  that for small enough n  increases exponentially with n  (the “time”), i.e.

            λn
Δxn  ∼ Δx0e   ,     λ > 0,
(3.34)

the system is most likely exhibiting chaotic behavior11 . The exponent λ  is called a Liapunov exponent. A useful equation for the calculation of λ  is

           n∑−1
λ = lim  1-   ln |f ′(x )|.
    n→ ∞ n           k
           k=0
(3.35)

This relation can be easily proved by considering infinitesimal 𝜖 ≡ |Δx0 | so that λ =  lim  lim  1ln |Δxn  |∕𝜖
     n→∞ 𝜖→0 n  . Then we obtain

  ˜x1  =   f(˜x0) = f (x0 + 𝜖) ≈ f (x0) + 𝜖f ′(x0)
                 ′
      =   x1 + 𝜖f (x0) ⇒
Δx1--     ˜x1-−-x1-    ′
  𝜖   =      𝜖    ≈  f (x0 )

                            ′                  ′      ′
  ˜x2  =   f(˜x1) = f (x1 + 𝜖f (x0)) ≈ f(x1 ) + (𝜖f (x0))f(x1 )
      =   x2 + 𝜖f′(x0)f′(x1 ) ⇒
Δx2       ˜x2 − x2
----- =   --------≈  f′(x0 )f′(x1)
  𝜖          𝜖

  ˜x3  =   f(˜x2) = f (x2 + 𝜖f ′(x0)f ′(x1)) ≈ f(x2) + (𝜖f′(x0 )f′(x1))f ′(x2)
      =   x  + 𝜖f′(x )f′(x  )f′(x ) ⇒
           3        0     1     2
Δx3-- =   ˜x3-−-x3-≈  f′(x0 )f′(x1)f ′(x2).                           (3.36)
  𝜖          𝜖
We can show by induction that |Δxn |∕𝜖 ≈ f′(x0)f′(x1)f′(x2 )...f′(xn−1)  and by taking the logarithm and the limits we can prove (3.35) .

pict

Figure 3.7: A plot of |Δx  |∕𝜖
    n  for the logistic map for r = 3.6  , x = 0.2
 0  . Note the convergence of the curves as 𝜖 → 0  and the approximate exponential behavior in this limit. The two lines are fits to the equation (3.34) and give λ = 0.213(4)  and λ = 0.217(6)  respectively.

A first attempt to calculate the Liapunov exponents could be made by using the definition (3.34) . We modify the program logistic.f90 so that it calculates two trajectories whose initial distance is 𝜖 =  epsilon:

!===========================================================  
!Discrete Logistic Map:  
!Two trajectories with close initial conditions.  
!===========================================================  
program logistic_map  
 implicit none  
 integer :: NSTEPS,i  
 real(8) :: r,x0,x1,x0t,x1t,epsilon  
 
! ----- Input:  
 print *,’# Enter NSTEPS, r, x0, epsilon:’  
 read  *,NSTEPS,r,x0,epsilon  
 print *,’# NSTEPS  = ’,NSTEPS  
 print *,’# r       = ’,r  
 print *,’# x0      = ’,x0  
 print *,’# epsilon = ’,epsilon  
 
 x0t = x0+epsilon  
! ----- Initialize:  
 open(unit=33,file=’lia.dat’)  
 write(33,*) 1,x0,x0t,ABS(x0t-x0)/epsilon  
! ----- Calculate:  
 do i=2,NSTEPS  
  x1  = r * x0  * (1.0D0-x0 )  
  x1t = r * x0t * (1.0D0-x0t)  
  write(33,*)i,x1,x1t,ABS(x1t-x1)/epsilon  
  x0  = x1; x0t = x1t  
 enddo  
 close(33)  
end program logistic_map

After running the program, the quantity |Δxn |∕𝜖  is found at the fourth column of the file lia.dat. The curves of figure 3.7 can be constructed by using the commands:

> gfortran liapunov1.f90 -o l  
> gnuplot  
gnuplot> set logscale y  
gnuplot> plot \  
  "<echo 200 3.6 0.2 1e-15 |./l;cat lia.dat" u 1:4 w l

The last line plots the stdout of the command "echo 200 3.6 0.2 1e-15 |./l;cat lia.dat", i.e. the contents of the file lia.dat produced after running our program using the parameters NSTEPS = 200  , r = 3.6  , x0 = 0.2  and epsilon     − 15
= 10   . The gnuplot command set logscale y, puts the y axis in a logarithmic scale. Therefore an exponential function is shown as a straight line and this is what we see in figure 3.7: The points |Δxn |∕𝜖  tend to lie on a straight line as 𝜖  decreases. The slopes of these lines are equal to the Liapunov exponent λ  . Deviations from the straight line behavior indicates corrections and systematic errors, as we point out in figure 3.7. A different initial condition results in a slightly different value of λ  , and the true value can be estimated as the average over several such choices. We estimate the error of our computation from the standard error of the mean. The reader should perform such a computation as an exercise.

One can perform a fit of the points |Δxn |∕𝜖  to the exponential function in the following way: Since |Δxn |∕𝜖 ∼ C exp (λn )  ⇒  ln (|Δxn |∕𝜖) = λn + c  , we can make a fit to a straight line instead. Using gnuplot, the relevant commands are:

gnuplot> fit [5:53] a*x+b \  
         "<echo 500 3.6 0.2 1e-15 |./l;cat lia.dat"\  
         using 1:(log($4)) via a,b  
gnuplot> replot exp(a*x+b)

The command shown above fits the data to the function a*x+b by taking the 1st column and the logarithm of the 4th column (using 1:(log($4))) of the stdout of the command that we used for creating the previous plot. We choose data for which 5 ≤  n ≤ 53  ([5:53]) and the fitting parameters are a,b (via a,b). The second line, adds the fitted function to the plot.


pict

Figure 3.8: Plot of the sum (1∕n) ∑N+n −1ln|f′(x )|
       k=N         k as a function of n  for the logistic map with r = 3.8  , N = 2000  for different initial conditions x0 = 0.20,0.35,0.50,0.75,0.90  . The different curves converge in the limit n → ∞ to λ = 0.4325(10)  .

Now we are going to use equation (3.35) for calculating λ  . This equation is approximately correct when (a) we have already reached the steady state and (b) in the large n  limit. For this reason we should study if we obtain a satisfactory convergence when we (a) “throw away” a number of NTRANS steps, (b) calculate the sum (3.35) for increasing NSTEPS= n  (c) calculate the sum (3.35) for many values of the initial point x0   . This has to be carefully repeated for all values of r  since each factor will contribute differently to the quality of the convergence: In regions that manifest chaotic behavior (large λ  ) convergence will be slower. The program can be found in the file liapunov2.f90:

!===========================================================  
!Discrete Logistic Map:  
!Liapunov exponent from sum_i ln|f’(x_i)|  
! NTRANS: number of discarded iteration in order to discard  
!         transient behavior  
! NSTEPS: number of terms in the sum  
!===========================================================  
program logistic_map  
 implicit none  
 integer :: NTRANS,NSTEPS,i  
 real(8) :: r,x0,x1,sum  
 
! ----- Input:  
 print *,’# Enter NTRANS,NSTEPS, r, x0:’  
 read  *,NTRANS,NSTEPS,r,x0  
 print *,’# NTRANS  = ’,NTRANS  
 print *,’# NSTEPS  = ’,NSTEPS  
 print *,’# r       = ’,r  
 print *,’# x0      = ’,x0  
 
 do i=1,NTRANS  
  x1  = r * x0  * (1.0D0-x0 )  
  x0  = x1  
 enddo  
 sum = log(ABS(r*(1.0D0-2.0D0*x0)))  
! ----- Initialize:  
 open(unit=33,file=’lia.dat’)  
 write(33,*) 1,x0,sum  
! ----- Calculate:  
 do i=2,NSTEPS  
  x1  = r * x0  * (1.0D0-x0 )  
  sum = sum + log(ABS(r*(1.0D0-2.0D0*x1)))  
  write(33,*)i,x1,sum/i  
  x0  = x1  
 enddo  
 close(33)  
end program logistic_map  

After NTRANS steps, the program calculates NSTEPS times the sum of the terms ln |f′(xk )| = ln|r(1 − 2xk)| . At each step the sum divided by the number of steps i is printed to the file lia.dat. Figure 3.6 shows the results for r = 3.8  . This is a point where the system exhibits strong chaotic behavior and convergence is achieved after we compute a large number of steps. Using NTRANS = 2000  and NSTEPS ≈ 70000  the achieved accuracy is about 0.2  % with λ =  0.4325 ± 0.0010 ≡ 0.4325(10)  . The main contribution to the error comes from the different paths followed by each initial point chosen. The plot can be constructed with the gnuplot commands:

> gfortran liapunov2.f90 -o l  
> gnuplot  
gnuplot> plot \  
 "<echo 2000 70000 3.8 0.20 |./l;cat lia.dat" u 1:3 w l,\  
 "<echo 2000 70000 3.8 0.35 |./l;cat lia.dat" u 1:3 w l,\  
 "<echo 2000 70000 3.8 0.50 |./l;cat lia.dat" u 1:3 w l,\  
 "<echo 2000 70000 3.8 0.75 |./l;cat lia.dat" u 1:3 w l,\  
 "<echo 2000 70000 3.8 0.90 |./l;cat lia.dat" u 1:3 w l

The plot command runs the program using the parameters NTRANS = 2000  , NSTEPS = 70000  , r = 3.8  and x0 =  0.20,0.35,0.50,0.75,0.90  and plots the results from the contents of the file lia.dat.

In order to determine the regions of chaotic behavior we have to study the dependence of the Liapunov exponent λ  on the value of r  . Using our experience coming from the careful computation of λ  before, we will run the program for several values of r  using the parameters NTRANS = 2000  , NSTEPS = 60000  from the initial point x0 =  0.2  . This calculation gives accuracy of the order of 1  %. If we wish to measure λ  carefully and estimate the error of the results, we have to follow the steps described in figures 3.7 and 3.8. The program can be found in the file liapunov3.f90 and it is a simple modification of the previous program so that it can perform the calculation for many values of r  .

!===========================================================  
!Discrete Logistic Map:  
!Liapunov exponent from sum_i ln|f’(x_i)|  
!Calculation for r in [rmin,rmax] with RSTEPS steps  
! RSTEPS: values or r studied: r=rmin+(rmax-rmin)/RSTEPS  
! NTRANS: number of discarded iteration in order to discard  
!         transient behavior  
! NSTEPS: number of terms in the sum  
! xstart: value of initial x0 for every r  
!===========================================================  
program logistic_map  
 implicit none  
 real(8),parameter :: rmin   = 2.5D0  
 real(8),parameter :: rmax   = 4.0D0  
 real(8),parameter :: xstart = 0.2D0  
 integer,parameter :: RSTEPS = 1000  
 integer,parameter :: NSTEPS = 60000  
 integer,parameter :: NTRANS = 2000  
 integer :: i,ir  
 real(8) :: r,x0,x1,sum,dr  
 
 open(unit=33,file=’lia.dat’)  
 dr = (rmax-rmin)/(RSTEPS-1)  
 do ir=0,RSTEPS-1  
  r = rmin+ir*dr  
  x0= xstart  
  do i=1,NTRANS  
   x1  = r * x0  * (1.0D0-x0 )  
   x0  = x1  
  enddo  
  sum = log(ABS(r*(1.0D0-2.0D0*x0)))  
! ----- Calculate:  
  do i=2,NSTEPS  
   x1  = r * x0  * (1.0D0-x0 )  
   sum = sum + log(ABS(r*(1.0D0-2.0D0*x1)))  
   x0  = x1  
  enddo  
  write(33,*)r,sum/NSTEPS  
 enddo !do ir=0,RSTEPS-1  
 close(33)  
end program logistic_map

The program can be compiled and run using the commands:

> gfortran liapunov3.f90 -o l  
> ./l &

The character & makes the program ./l to run in the background. This is recommended for programs that run for a long time, so that the shell returns the prompt to the user and the program continues to run even after the shell is terminated.


pict

Figure 3.9: The Liapunov exponent λ  of the logistic map calculated via equation (3.35) . Note the chaotic behavior that manifests for the values of r  where λ > 0  and the windows of stable k  -cycles where λ < 0  . Compare this plot with the bifurcation diagram of figure 3.4. At the points where λ = 0  we have onset of chaos (or “edge of chaos”) with manifestation of weak chaos (i.e. |Δxn| ∼ |Δx0|nω  ). At these points we have transitions from stable k  -cycles to strong chaos. We observe the onset of chaos for the first time when r = rc ≈ 3.5699  , at which point λ = 0  (for smaller r  the plot seems to touch the λ = 0  line, but in fact λ  takes negative values with |λ| very small).

The data are saved in the file lia.dat and we can make the plot shown in figure 3.7 using gnuplot:

gnuplot> plot  "lia.dat"  with lines notitle ,0 notitle

Now we can compare figure 3.9 with the bifurcation diagram shown in figure 3.4. The intervals with λ < 0  correspond to stable k  -cycles. The intervals where λ >  0  correspond to manifestation of strong chaos. These intervals are separated by points with λ =  0  where the system exhibits weak chaos. This means that neighboring trajectories diverge from each other with a power law |Δxn | ∼ |Δx0 |nω  instead of an exponential, where ω = 1∕(1 − q)  is a positive exponent that needs to be determined. The parameter q  is the one usually used in the literature. Strong chaos is obtained in the q →  1  limit. For larger r  , switching between chaotic and stable periodic trajectories is observed each time λ  changes sign. The critical values of r  can be computed with relatively high accuracy by restricting the calculation to a small enough neighborhood of the critical point. You can do this using the program listed above by setting the parameters rmin and rmax.


pict pict

Figure 3.10: The distribution functions p(x)  of x  of the logistic map for r = 3.59  (left) and 3.8  (right). The chaotic behavior appears to be weaker for r = 3.59  , and this is reflected on the value of the entropy. One sees that there exist intervals of x  with p(x) = 0  which become smaller and vanish as r  gets close to 4. This distribution is very hard to be distinguished from a truly random distribution.

We can also study the chaotic properties of the trajectories of the logistic map by computing the distribution p (x )  of the values of x  in the interval (0,1)  . After the transitional period, the distribution p (x )  for the k  cycles will have support only at the points of the k  cycles, whereas for the chaotic regimes it will have support on subintervals of (0,1)  . The distribution function p(x)  is independent for most of the initial points of the trajectories. If one obtains a large number of points from many trajectories of the logistic map, it will be practically impossible to understand that these are produced by a deterministic rule. For this reason, chaotic systems can be used for the production of pseudorandom numbers, as we will see in chapter 11. By measuring the entropy, which is a measure of disorder in a system, we can quantify the “randomness” of the distribution. As we will see in chapter 12, it is given by the equation

       ∑
S = −     pk ln pk,
        k
(3.37)

where p
 k  is the probability of observing the state k  . In our case, we can make an approximate calculation of S  by dividing the interval (0,1)  to N  subintervals of width Δx  . For given r  we obtain a large number M  of values xn  of the logistic map and we compute the histogram hk  of their distribution in the intervals (xk,xk + Δx )  . The probability density is obtained from the limit of p  = h ∕(M  Δx )
 k    k  as M  becomes large and Δx  small (large N  ). Indeed, ∑N
  k=1pk Δx =  1  converges to ∫ 1
 0 p(x)dx =  1  . We will define        ∑
S =  −   Nk=1 pk lnpk Δx  .


pict

Figure 3.11: The distribution p(x)  of x  for the logistic map for r = 4  . We observe strong chaotic behavior, p(x)  has support over the whole interval (0,1)  and the entropy is large. The solid line is the analytic form of the distribution p(x) = π−1x−1∕2(1− x)−1∕2  which is known for r = 4   [30]. This is the beta distribution for a = 1∕2  , b = 1∕2  .

The program listed below calculates pk  for chosen values of r  , and then the entropy S  is calculated using (3.37) . It is a simple modification of the program in liapunov3.f90 where we add the parameter NHIST counting the number of intervals N  for the histograms. The probability density is calculated in the array p(NHIST). The program can be found in the file entropy.f90:

!===========================================================  
!Discrete Logistic Map:  
!Entropy calculation from S=-sum_i p_i ln p_i  
!Calculation for r in [rmin,rmax] with RSTEPS steps  
! RSTEPS: values or r studied: r=rmin+(rmax-rmin)/RSTEPS  
! NHIST : number of histogram bins for calculation of p_i  
! NSTEPS: number of values of x in the histograms  
! NTRANS: number of discarted iteration in order to discard  
!         transient behavior  
! xstart: value of initial x0 for every r  
!===========================================================  
program logistic_map  
 implicit none  
 real(8),parameter :: rmin   = 2.5D0  
 real(8),parameter :: rmax   = 4.0D0  
 real(8),parameter :: xstart = 0.2D0  
 integer,parameter :: RSTEPS = 1000  
 integer,parameter :: NHIST  = 10000  
 integer,parameter :: NTRANS = 2000  
 integer,parameter :: NSTEPS = 5000000  
 real(8),parameter :: xmin=0.0D0,xmax=1.0D0  
 integer :: i,ir,isum,n  
 real(8) :: r,x0,x1,sum,dr,dx  
 real(8) :: p(NHIST),S  
 
 open(unit=33,file=’entropy.dat’)  
 p  = 0.0D0  
 dr = (rmax-rmin)/(RSTEPS-1)  
 dx = (xmax-xmin)/(NHIST -1)  
 do ir=0,RSTEPS-1  
  r = rmin+ir*dr  
  x0= xstart  
  do i=1,NTRANS  
   x1  = r * x0  * (1.0D0-x0 )  
   x0  = x1  
  enddo  
!make histogram:  
  n=INT(x0/dx)+1;p(n)=p(n)+1.0D0  
  do i=2,NSTEPS  
   x1  = r * x0  * (1.0D0-x0 )  
   n   = INT(x1/dx)+1  
   p(n)=p(n)+1.0D0  
   x0  = x1  
  enddo  
!p(k) is now histogram of x-values.  
!Normalize so that sum_k p(k)*dx=1  
!to get probability distribution:  
  p    = p/NSTEPS/dx  
!sum all non zero terms: p(n)*log(p(n))*dx  
  S    = -SUM(p*log(p),MASK=p.gt.0.0D0)*dx  
  write(33,*)r,S  
 enddo !do ir=0,RSTEPS-1  
 close(33)  
!print the last probability distribution:  
 open(unit=34,file=’entropy_hist.dat’)  
 do n=1,NHIST  
  x0 = xmin +(n-1)*dx + 0.5D0*dx  
  write(34,*) r,x0,p(n)  
 enddo  
 close(34)  
end program logistic_map


pict

Figure 3.12: The entropy S = − ∑  p lnp Δx
        k k   k  for the logistic map as a function of r  . The vertical line is rc ≈ 3.56994567  which marks the beginning of chaos and the horizontal is the corresponding entropy. The entropy is low for small values of r  , where we have the stable 2n  cycles, and large in the chaotic regimes. S  drops suddenly when we pass to a (temporary) periodic behavior interval. We clearly observe the 3-cycle for r = 1+ √8-≈ 3.8284  and the subsequent bifurcations that we observed in the bifurcation diagram (figure 3.4) and the Liapunov exponent diagram (figure 3.9).

For the calculation of the distribution functions and the entropy we have to choose the parameters which control the systematic error. The parameter NTRANS should be large enough so that the transitional behavior will not contaminate our results. Our measurements must be checked for being independent of its value. The same should be done for the initial point xstart. The parameter NHIST controls the partitioning of the interval (0,1)  and the width Δx  , so it should be large enough. The parameter NSTEPS is the number of “measurements” for each value of r  and it should be large enough in order to reduce the “noise” in p
  k  . It is obvious that NSTEPS should be larger when Δx  becomes smaller. Appropriate choices lead to the plots shown in figures 3.10 and 3.11 for r = 3.59  , 3.58  and 4  . We see that stronger chaotic behavior means a wider distribution of the values of x  .

The entropy is shown in figure 3.12. The stable periodic trajectories lead to small entropy, whereas the chaotic ones lead to large entropy. There is a sudden increase in the value of the entropy at the beginning of chaos at r = rc  , which increases even further as the chaotic behavior becomes stronger. During the intermissions of the chaotic behavior there are sudden drops in the value of the entropy. It is quite instructive to compare the entropy diagrams with the corresponding bifurcation diagrams (see figure 3.4) and the Liapunov exponent diagrams (see figure 3.9). The entropy is increasing until r  reaches its maximum value 4, but this is not done smoothly. By magnifying the corresponding areas in the plot, we can see an infinite number of sudden drops in the entropy in intervals of r  that become more and more narrow.

3.7 Problems

Several of the programs that you need to write for solving the problems of this chapter can be found in the Problems directory of the accompanying software of this chapter.

  1. Confirm that the trajectories of the logistic map for r < 1  are falling off exponentially for large enough n  .
    1. Choose r = 0.5  and plot the trajectories for x0 =  0.1 − 0.9  with step 0.1  for n =  1,...,1000  . Put the y  axis in a logarithmic scale. From the resulting curves discuss whether you obtain an exponential falloff.
    2. Fit the points xn  for n > 20  to the function ce−ax  and determine the fitting parameters a  and c  . How do these parameters depend on the initial point x
 0   ? You can use the following gnuplot commands for your calculation:
      gnuplot> !gfortran logistic.f90 -o l  
      gnuplot> a=0.7;c=0.4;  
      gnuplot> fit [10:] c*exp(-a*x) \  
       "<echo 1000 0.5 0.5|./l;cat log.dat" via a,c  
      gnuplot> plot c*exp(-a*x),\  
       "<echo 1000 0.5 0.5|./l;cat log.dat" w l

      As you can see, we set NSTEPS = 1000, r = 0.5, x0 = 0.5. By setting the limits [10:] to the fit command, the fit includes only the points xn ≥  10  , therefore avoiding the transitional period and the deviation from the exponential falloff for small n  .

    3. Repeat for r = 0.3 − 0.9  with step 0.1  and for r = 0.99,0.999  . As you will be approaching r = 1  , you will need to discard more points from near the origin. You might also need to increase NSTEPS. You should always check graphically whether the fitted exponential function is a good fit to the points xn  for large n  . Construct a table for the values of a  as a function of r  .

    The solutions of the equation (3.3) is e(r−1)x  . How is this related to the values that you computed in your table?

  2. Consider the logistic map for r = 2  . Choose NSTEPS=100 and calculate the corresponding trajectories for x0=0.2, 0.3, 0.5, 0.7, 0.9. Plot them on the same graph. Calculate the fixed point x ∗2   and compare your result to the known value 1 − 1∕r  . Repeat for x0= 10 −α  for α = − 1,− 2,− 5,− 10,− 20,− 25  . What do you conclude about the point  ∗
x1 = 0  ?
  3. Consider the logistic map for r = 2.9,2.99,2.999  . Calculate the stable point x∗
 2   and compare your result to the known value 1 − 1∕r  . How large should NSTEPS be chosen each time? You may choose x0=0.3.
  4. Consider the logistic map for r = 3.2  . Take x0=0.3, 0.5, 0.9 and NSTEPS=300 and plot the resulting trajectories. Calculate the fixed points x∗3   and x∗4   by using the command tail log.dat. Increase NSTEPS and repeat so that you make sure that the trajectory has converged to the 2-cycle. Compare their values to the ones given by equation (3.18) . Make the following plots:
    gnuplot> plot   \  
     "<echo 300 3.2 0.3|./l;awk ’NR%2==0’ log.dat"  w l  
    gnuplot> replot \  
     "<echo 300 3.2 0.3|./l;awk ’NR%2==1’ log.dat"  w l

    What do you observe?

  5. Repeat the previous problem for r = 3.4494  . How big should NSTEPS be chosen so that you obtain x∗
 3   and x ∗
  4   with an accuracy of 6 significant digits?
  6. Repeat the previous problem for r = 3.5  and 3.55  . Choose NSTEPS = 1000, x0 = 0.5. Show that the trajectories approach a 4-cycle and an 8-cycle respectively. Calculate the fixed points x ∗
  5   -x∗
 8   and x∗
 9   -x ∗
  16   .
  7. Plot the functions f (x )  ,   (2)
f   (x)  ,  (4)
f  (x)  , x  for given r  on the same graph. Use the commands:
    gnuplot> set samples 1000  
    gnuplot> f(x) = r*x*(1-x)  
    gnuplot> r=1;plot [0:1] x,f(x),f(f(x)),f(f(f(f(x))))

    The command r=1 sets the value of r  . Take r = 2.5  , 3  , 3.2  ,    √ --
1 +  6  , 3.5  . Determine the fixed points and the k  -cycles from the intersections of the plots with the diagonal y = x  .

  8. Construct the cobweb plots of figures 3.2 and 3.4 for r = 2.8,3.3  and 3.5  . Repeat by dropping from the plot an increasing number of initial points, so that in the end only the k  -cycles will remain. Do the same for r = 3.55  .
  9. Construct the bifurcation diagrams shown in figure 3.4.
  10. Construct the bifurcation diagram of the logistic map for 3.840 < r < 3.851  and for 0.458 <  x < 0.523  . Compute the first four bifurcation points with an accuracy of 5 significant digits by magnifying the appropriate parts of the plots. Take NTRANS=15000.
  11. Construct the bifurcation diagram of the logistic map for 2.9 < r < 3.57  . Compute graphically the bifurcation points  (n)
rc  for n = 2,  3,  4,  5,  6,  7,  8  . Make sure that your results are stable against variations of the parameters NTRANS, NSTEPS as well as from the choice of branching point. From the known values of r(nc)  for n = 2,3  , and from the dependence of your results on the choices of NTRANS, NSTEPS, estimate the accuracy achieved by this graphical method. Compute the ratios   (n)    (n−1)   (n+1)    (n)
(rc  − rc   )∕(rc    −  rc )  and compare your results to equation (3.20) .
  12. Choose the values of ρ  in equation (3.24) so that you obtain only one energy level. Compute the resulting value of the energy. When do we have three energy levels?
  13. Consider the polynomial         3     2
g(x) = x −  2x −  11x + 12  . Find the roots obtained by the Newton-Raphson method when you choose x0 = 2.35287527,  2.35284172,  2.35283735,  2.352836327,  2.352836323  . What do you conclude concerning the basins of attraction of each root of the polynomial? Make a plot of the polynomial in a neighborhood of its roots and try other initial points that will converge to each one of the roots.
  14. Use the Newton-Raphson method in order to compute the 4-cycle x∗5,...,x∗8   of the logistic map. Use appropriate areas of the bifurcation diagram so that you can choose the initial points correctly. Check that your result for r(4)
 c  is the same for all x∗
 α  . Tune the parameters chosen in your calculation on order to improve the accuracy of your measurements.
  15. Repeat the previous problem for the 8-cycle  ∗      ∗
x9,...,x16   and  (5)
rc  .
  16. Repeat the previous problem for the 16-cycle x∗ ,...,x∗
 17      32   and r(c6)  .
  17. Calculate the critical points r(nc)  for n = 3,...,17  of the logistic map using the Newton-Raphson method. In order to achieve that, you should determine the bifurcation points graphically in the bifurcation diagram first and then choose the initial points in the Newton-Raphson method appropriately. The program in bifurcationPoints.f90 should read the parameters eps, epsx, epsr from the stdin so that they can be tuned for increasing n  . If these parameters are too small the convergence will be unstable and if they are too large you will have large systematic errors. Using this method, try to reproduce table 3.1






    n   (n)
rc  n   (n)
rc




    23.0000000000 103.56994317604
    33.4494897429 113.569945137342
    43.544090360 123.5699455573912
    53.564407266 133.569945647353
    63.5687594195 143.5699456666199
    73.5696916098 153.5699456707464
    83.56989125938 163.56994567163008
    93.56993401837 173.5699456718193




    r =  3.56994567 ...
 c





    Table 3.1: The values of r(nc)  for the logistic map calculated for problem 17.  (∞ )
rc  ≡ rc  is taken from the bibliography.

  18. Calculate the ratios Δr (n)∕Δr (n+1)   of equation (3.20) using the results of table 3.1. Calculate Feigenbaum’s constant and comment on the accuracy achieved by your calculation.
  19. Estimate Feigenbaum’s constant δ  and the critical value rc  by assuming that for large enough n  ,  (n)
rc  ≈  rc − C δ−n  . This behavior is a result of equation (3.20) . Fit the results of table 3.1 to this function and calculate δ  and r
 c  . This hypothesis is confirmed in figure 3.13 where we can observe the exponential convergence of  (n)
rc  to rc  . Construct the same plot using the parameters of your calculation.
    Hint: You can use the following gnuplot commands:
    gnuplot> nmin=2;nmax=17  
    gnuplot> r(x)= rc-c*d**(-x)  
    gnuplot> fit [nmin:nmax] r(x)  "rcrit" u 1:2 via rc,c,d  
    gnuplot> plot "rcrit", r(x)  
    gnuplot> print rc,d

    The file rcrit contains the values of table 3.1. You should vary the parameters nmin, nmax and repeat until you obtain a stable fit.


    pict

    Figure 3.13: Test of the relation r(cn)≈ rc − C δ−n  discussed in problem 17. The parameters used in the plot are approximately r = 3.5699457
 c  , δ = 4.669196  and C = 12.292  .

  20. Use the Newton-Raphson method to calculate the first three bifurcation points after the appearance of the 3-cycle for         √--
r = 1 +  8  . Choose one bifurcation point of the 3-cycle, one of the 6-cycle and one of the 12-cycle and magnify the bifurcation diagram in their neighborhood.
  21. Consider the map describing the evolution of a population
                       r(1−xn)
xn+1 = p(xn ) = xne      .
    (3.38)

    1. Plot the functions x  , p(x )  , p(2)(x)  , p(4)(x)  for r = 1.8,2  , 2.6  , 2.67  , 2.689  for 0 < x < 8  . For which values of r  do you expect to obtain stable k  -cycles?
    2. For the same values of r  plot the trajectories with initial points x0 = 0.2,0.5,0.7  . For each r  make a separate plot.
    3. Use the Newton-Raphson method in order to determine the points  (n)
rc  for n = 3,4,5  as well as the first two bifurcation points of the 3-cycle.
    4. Construct the bifurcation diagram for 1.8 < r < 4  . Determine the point marking the onset of chaos as well as the point where the 3-cycle starts. Magnify the diagram around a branch that you will choose.
    5. Estimate Feigenbaum’s constant δ  as in problem 17. Is your result compatible with the expectation of universality for the value of δ  ? Is the value of rc  the same as that of the logistic map?
  22. Consider the sine map:
    xn+1 = s(xn) = r sin(πxn ).
    (3.39)

    1. Plot the functions x  , s(x)  , s(2)(x )  , s(4)(x)  , s(8)(x)  for r = 0.65  , 0.75  , 0.84  , 0.86  , 0.88  . Which values of r  are expected to lead to stable k  -cycles?
    2. For the same values of r  , plot the trajectories with initial points x0 = 0.2,0.5,0.7  . Make one plot for each r  .
    3. Use the Newton-Raphson method in order to determine the points  (n)
rc  for n = 3,4,5  as well as the first two bifurcation points of the 3-cycle.
    4. Construct the bifurcation diagram for 0.6 <  r < 1  . Within which limits do the values of x  lie in? Repeat for 0.6 < r < 2  . What do you observe? Determine the point marking the onset of chaos as well as the point where the 3-cycle starts. Magnify the diagram around a branch that you will choose.
  23. Consider the map:
                  2
xn+1 = 1 − rx n.
    (3.40)

    1. Construct the bifurcation diagram for 0 < r < 2  . Within which limits do the values of x  lie in? Determine the point marking the onset of chaos as well as the point where the 3-cycle starts. Magnify the diagram around a branch that you will choose.
    2. Use the Newton-Raphson method in order to determine the points  (n)
rc  for n = 3,4,5  as well as the first two bifurcation points of the 3-cycle.
  24. Consider the tent map:
                               {
                              rxn        0 ≤ xn ≤ 12
xn+1 = rmin {xn, 1 − xn} =    r(1 − xn)  1 < xn ≤ 1  .
                                         2
    (3.41)

    Construct the bifurcation diagram for 0 < r < 2  . Within which limits do the values of x  lie in? On the same graph, plot the functions r∕2  , r − r2∕2  .

    Magnify the diagram in the area 1.407 < r < 1.416  and 0.580 < x < 0.588  . At which point do the two disconnected intervals within which xn  take their values merge into one? Magnify the areas 1.0 < r < 1.1  , 0.4998 < x < 0.5004  and 1.00 < r < 1.03  , 0.4999998 <  x < 0.5000003  and determine the merging points of two disconnected intervals within which xn  take their values.

  25. Consider the Gauss map (or mouse map):
    x    =  e−rx2n + q.
 n+1
    (3.42)

    Construct the bifurcation diagram for − 1 < q <  1  and r = 4.5,  4.9,  7.5  . Make your program to take as the initial point of the new trajectory to be the last one of the previous trajectory and choose x0 = 0  for q = − 1  . Repeat for x0 = 0.7,0.5,− 0.7  . What do you observe? Note that as q  is increased, we obtain bifurcations and “anti-bifurcations”.

  26. Consider the circle map:
    xn+1 =  [xn + r − q sin(2πxn )]  mod1.
    (3.43)

    (Make sure that your program keeps the values of xn  so that 0 ≤ xn <  1  ). Construct the bifurcation diagram for 0 < q < 2  and r = 1∕3  .

  27. Use the program in liapunov.f90 in order to compute the distance between two trajectories of the logistic map for r = 3.6  that originally are at a distance Δx0 =  10−15   . Choose x0 = 0.1,  0.2,0.3,  0.4,  0.5,0.6,  0.7,0.8,  0.9,  0.99,0.999  and calculate the Liapunov exponent by fitting to a straight line appropriately. Compute the mean value and the standard error of the mean.
  28. Calculate the Liapunov exponent for r = 3.58,  3.60,  3.65,  3.70,  3.80  for the logistic map. Use both ways mentioned in the text. Choose at least 5 different initial points and calculate the mean and the standard error of the mean of your results. Compare the values of λ  that you obtain with each method and comment.
  29. Compute the critical value rc  numerically as the limit lim rc(n)
n→∞  for the logistic map with an accuracy of nine significant digits. Use the calculation of the Liapunov exponent λ  given by equation (3.35) .
  30. Compute the values of r  of the logistic map numerically for which we (a) enter a stable 3-cycle (b) reenter into the chaotic behavior. Do the calculation by computing the Liapunov exponent λ  and compare your results with the ones obtained from the bifurcation diagram.
  31. Calculate the Liapunov exponent using equation (3.35) for the following maps:
                  r(1−xn)
xn+1   =  xne       ,    1.8 < r < 4
xn+1   =  r sin (πxn),     0.6 < r <  1
                 2
xn+1   =  1 − rx n,    0 < r < 2
xn+1   =  e− rx2n + q,     r = 7.5,− 1 < q < 1
          [                     ]
xn+1   =    xn + 1-− q sin(2πxn )    mod1,   0 <  q < 2,    (3.44)
                 3
    and construct the diagrams similar to the ones in figure 3.9. Compare your plots with the respective bifurcation diagrams (you may put both graphs on the same plot). Use two different initial points x0 = 0,0.2  for the Gauss map (xn+1 =  e−rx2n + q  ) and observe the differences. For the circle map (xn+1 =  [xn + 1 ∕3 − qsin(2πxn )]  mod1  ) study carefully the values 0 < q < 0.15  .
  32. Reproduce the plots in figures 3.10, 3.11 and 3.12. Compute the function p(x)  for r = 3.68  , 3.80  , 3.93  and 3.98  . Determine the points where you have stronger chaos by observing p(x)  and the corresponding values of the entropy. Compute the entropy for r ∈ (3.95,4.00)  by taking RSTEPS=2000 and estimate the values of r  where we enter to and exit from chaos. Compare your results with the computation of the Liapunov exponent.
  33. Consider the Hénon map:
                          2
xn+1   =  yn + 1 − ax n
 y     =  bx                               (3.45)
  n+1        n
    1. Construct the two bifurcation diagrams for xn  and yn  for b = 0.3  , 1.0 < a <  1.5  . Check if the values a = 1.01,1.4  that we will use below correspond to stable periodic trajectories or chaotic behavior.
    2. Write a program in a file attractor.f90 which will take NINIT = NL × NL initial conditions (x0 (i),y0(i))  i = 1,...,  NL on a NL× NL lattice of the square xm  ≤ x0 ≤  xM  , ym ≤ y0 ≤  yM  . Each of the points (x0(i),y0(i))  will evolve according to equation (3.45) for n =  NSTEPS steps. The program will print the points (xn(i),yn(i))  to the stdout. Choose xm  = ym =  0.6  , xM =  yM =  0.8  , NL=  200  .
    3. Choose a =  1.01  , b = 0.3  and plot the points (xn (i),yn(i))  for n = 0,  1,  2,  3,  10,  20,  30,  40,  60,  1000  on the same diagram.
    4. Choose a = 1.4  , b = 0.3  and plot the points (xn (i),yn(i))  for n = 0,...,7  on the same diagram.
    5. Choose a = 1.4  , b = 0.3  and plot the points (xn(i),yn(i))  for n = 999  on the same diagram. Observe the Hénon strange attractor and its fractal properties. It is characterized by a Hausdorff12 dimension dH  = 1.261 ± 0.003  . Then magnify the regions
      {(x,y)|  − 1.290 < x < − 1.270,  0.378 < y < 0.384 },
{(x,y)|  1.150 <  x < − 1.130,   0.366 < y < 0.372 },
{(x,y)|   0.108 < x <  0.114,   0.238 < y < 0.241 },
{(x,y)|   0.300 < x <  0.320,   0.204 < y < 0.213 },
{(x,y)|   1.076 < x <  1.084,   0.090 < y < 0.096 },

{(x,y)|   1.216 < x <  1.226,   0.032 < y < 0.034 }.

  34. Consider the Duffing map:
    x      =  y
  n+1       n             3
yn+1   =  − bxn + ayn − yn.                  (3.46)
    1. Construct the two bifurcation diagrams for xn  and yn  for b = 0.3  , 0 < a < 2.78  . Choose four different initial conditions (x0,y0) =       √ --    √ --
(±1 ∕  2,±1 ∕  2)  . What do you observe?
    2. Use the program attractor.f90 from problem 33 in order to study the attractor of the map for b = 0.3  , a = 2.75  .
  35. Consider the Tinkerbell map:
    xn+1  =   x2 − y2 + axn + byn
           n    n
yn+1  =   2xnyn + cxn + dyn.                  (3.47)
    1. Choose a = 0.9  , b = − 0.6013  , c = 2.0  , d = 0.50  . Plot a trajectory on the plane by plotting the points (xn,yn)  for n = 0,...,10000  with (x0,y0) = (− 0.72, − 0.64)  .
    2. Use the program attractor.f90 from problem 33 in order to study the attractor of the map for the values of the parameters a  , b  , c  , d  given above. Choose xm  = − 0.68  , xM =  − 0.76  , y  = − 0.60
 m  , y   = − 0.68
 M  , n = 10000  .
    3. Repeat the previous question by taking d = 0.27  .

Chapter 4
Motion of a Particle

In this chapter we will study the numerical solution of classical equations of motion of one dimensional mechanical systems, e.g. a point particle moving on the line, the simple pendulum etc. We will make an introduction to the numerical integration of ordinary differential equations with initial conditions and in particular to the Euler and Runge-Kutta methods. We study in detail the examples of the damped harmonic oscillator and of the damped pendulum under the influence of an external periodic force. The latter system is nonlinear and exhibits interesting chaotic behavior.

4.1 Numerical Integration of Newton’s Equations

Consider the problem of the solution of the dynamical equations of motion of one particle under the influence of a dynamical field given by Newton’s law. The equations can be written in the form

d2⃗x-
dt2 =  ⃗a(t,⃗x,⃗v),
(4.1)

where

            ⃗
⃗a(t,⃗x,⃗v) ≡  F-    ⃗v =  d⃗x.
            m          dt
(4.2)

From the numerical analysis point of view, the problems that we will discuss are initial value problems for ordinary differential equations where the initial conditions

⃗x(t0) = ⃗x0     ⃗v(t0) = ⃗v0,
(4.3)

determine a unique solution ⃗x(t)  . The equations (4.1) are of second order with respect to time and it is convenient to write them as a system of twice as many first order equations:

d⃗x-=  ⃗v     d⃗v-= ⃗a(t,⃗x,⃗v ).
dt          dt
(4.4)

In particular, we will be interested in the study of the motion of a particle moving on a line (1 dimension), therefore the above equations become

   dx             dv
   ---=  v        --- = a(t,x,v)  1-dimension
   dt              dt
x(t0) = x0        v(t0) = v0.                           (4.5)
When the particle moves on the plane (2 dimensions) the equations of motion become
   dx-             dvx-
   dt = vx          dt = ax (t,x, vx,y,vy)  2-dimensions
   dy              dv
   ---= vy         --y-= ay(t,x,vx, y,vy)
   dt               dt
x (t0) = x0         vx(t0) = v0x
 y(t0) = y0        vy(t0) = v0y,                              (4.6)

4.2 Prelude: Euler Methods

As a first attempt to tackle the problem, we will study a simple pendulum of length l  in a homogeneous gravitational field g  (figure 4.1).


pict

Figure 4.1: A simple pendulum of length l  in a homogeneous gravitational field g  .


pict

Figure 4.2: Convergence of Euler’s method for a simple pendulum with period            2
T ≈ 1.987(ω  = 10.0)  for several values of the time step Δt  which is determined by the number of integration steps Nt= 50− 100,000  . The solution is given for 𝜃0 = 0.2  , ω0 = 0.0  and we compare it with the known solution for small angles with α(t) ≈ − (g∕l)𝜃  .


pict

Figure 4.3: Convergence of the Euler-Cromer method, similarly to figure 4.2. We observe a faster convergence compared to Euler’s method.


pict

Figure 4.4: Convergence of the Euler-Verlet method, similarly to figure 4.2. We observe a faster convergence than Euler’s method, but the roundoff errors make the results useless for Nt≳ 50,000  (note what happens when Nt= 100,000  . Why?).


pict

Figure 4.5: Convergence of Euler’s method for the simple pendulum like in figure 4.2 for 𝜃0 = 3.0  , ω0 = 0.0  . The behavior of the angular velocity is shown and we notice unstable behavior for Nt≲ 1,000  .


pict

Figure 4.6: Convergence of Euler-Cromer’s method, like in figure 4.5. We observe a faster convergence than for Euler’s method.


pict

Figure 4.7: Convergence of the Euler-Verlet method, similarly to figure 4.5. We observe a faster convergence compared to Euler’s method but that the roundoff errors make the results unstable for Nt≳ 18,000  .

The equations of motion are given by the differential equations

d2𝜃        g
--2- =   − -sin 𝜃
dt         l
 d𝜃- =   ω,                              (4.7)
 dt
which can be rewritten as a first order system of differential equations
 d𝜃- =   ω
 dt
dω-        g-
 dt  =   − l sin𝜃    ,                      (4.8)
The equations above need to be written in a discrete form appropriate for a numerical solution with the aid of a computer. We split the interval of time of integration [ti,tf]  to N  − 1  equal intervals1 of width Δt  ≡ h  , where h = (tf − ti)∕(N −  1)  . The derivatives are approximated by the relations (xn+1 −  xn)∕Δt ≈  x′n  , so that
ωn+1  =   ωn + αnΔt

𝜃n+1  =   𝜃n + ωnΔt.                       (4.9)
where α = − (g∕l)sin𝜃  is the angular acceleration. This is the so-called Euler method. The error at each step is estimated to be of order (Δt )2   . This is most easily seen by Taylor expanding around the point tn  and neglecting all terms starting from the second derivative and beyond2 . What we are mostly interested in is in the total error of the estimate of the functions we integrate for at time t
 f  ! We expect that errors accumulate in an additive way at each integration step, and since the number of steps is N  ∝ 1∕ Δt  the total error should be        2
∝ (Δt ) × (1∕Δt ) = Δt  . This is indeed what happens, and we say that Euler’s method is a first order method. Its range of applicability is limited and we only study it for academic reasons. Euler’s method is asymmetric because it uses information only from the beginning of the integration interval (t,t + Δt )  . It can be put in a more balanced form by using the velocity at the end of the interval (t,t + Δt)  . This way we obtain the Euler-Cromer method with a slightly improved behavior, but which is still of first order
ωn+1  =   ωn + αnΔt
𝜃n+1  =   𝜃n + ωn+1Δt.                    (4.10)

An improved algorithm is the Euler–Verlet method which is of second order and gives total error3 ∼ (Δt )2   . This is given by the equations

                             2
𝜃n+1  =  2 𝜃n − 𝜃n−1 + αn (Δt )
          𝜃n+1 −-𝜃n−1-
 ωn   =      2 Δt    .                        (4.11)

The price that we have to pay is that we have to use a two step relation in order to advance the solution to the next step. This implies that we have to carefully determine the initial conditions of the problem which are given only at one given time ti  . We make one Euler time step backwards in order to define the value of 𝜃0   . If the initial conditions are 𝜃1 = 𝜃 (ti)  , ω1 = ω(ti)  , then we define

                  1       2
𝜃0 = 𝜃1 − ω1Δt  + -α1 (Δt) .
                  2
(4.12)

It is important that at this step the error introduced is not larger than      2
𝒪 (Δt )  , otherwise it will spoil and eventually dominate the 𝒪(Δt2 )  total error of the method introduced by the intermediate steps. At the last step we also have to take

ωN  =  𝜃N-−-𝜃N-−1.
          Δt
(4.13)

Even though the method has smaller total error than the Euler method, it becomes unstable for small enough Δt  due to roundoff errors. In particular, the second equation in (4.11) gives the angular velocity as the ratio of two small numbers. The problem is that the numerator is the result of the subtraction of two almost equal numbers. For small enough Δt  , this difference has to be computed from the last digits of the finite representation of the numbers 𝜃n+1   and 𝜃n  in the computer memory. The accuracy in the determination of (𝜃n+1 − 𝜃n)  decreases until it eventually becomes exactly zero. For the first equation of (4.11) , the term α  Δt2
  n   is smaller by a factor Δt  compared to the term αnΔt  in Euler’s method. At some point, by decreasing Δt  , we obtain      2
αn Δt  ≪  2𝜃n − 𝜃n−1   and the accuracy of the method vanishes due to the finite representation of real number in the memory of the computer. When the numbers αn Δt2   and 2𝜃n − 𝜃n−1   differ from each other by more that approximately seven orders of magnitude, adding the first one to the second is equivalent to adding zero and the contribution of the acceleration vanishes4 .

Writing programs that implement the methods discussed so far is quite simple. We will write a program that compares the results from all three methods Euler, Euler–Cromer and Euler–Verlet. The main program is mainly a user interface, and the computation is carried out by three subroutines euler, euler_cromer and euler_verlet. The user must provide the function accel(x) which gives the angular acceleration as a function of x. The variable x in our problem corresponds to the angle theta  . For starters we take accel(x)= -10.0 * sin(x), the acceleration of the simple pendulum.

The data structure is very simple: Three real arrays REAL T(P), X(P) and V(P) store the times tn  , the angles 𝜃n  and the angular velocities ωn  for n =  1,...,Nt . The user determines the time interval for the integration from ti = 0  to tf = Tfi and the number of discrete times Nt. The latter should be less than P, the size of the arrays. She also provides the initial conditions 𝜃  = Xin
 0 and ω  =  Vin
  0 . After this, we call the main integration routines which take as input the initial conditions, the time interval of the integration and the number of discrete times Xin,Vin,Tfi,Nt. The output of the routines is the arrays T,X,V which store the results for the time, position and velocity respectively. The results are printed to the files euler.dat, euler_cromer.dat and euler_verlet.dat.

After setting the initial conditions and computing the time step Δt  ≡ h = Tfi ∕(Nt − 1)  , the integration in each of the subroutines is performed in do loops which advance the solution for time Δt  . The results are stored at each step in the arrays T,X,V. For example, the central part of the program for Euler’s method is:

 T(1) = 0.0  
 X(1) = Xin  
 V(1) = Vin  
 h    = Tfi/(Nt-1)  
 do i = 2,Nt  
  T(i) = T(i-1)+h  
  X(i) = X(i-1)+V(i-1)*h  
  V(i) = V(i-1)+accel(X(i-1))*h  
 enddo

Some care has to be taken in the case of the Euler–Verlet method where one has to initialize the first two steps, as well as take special care for the last step for the velocity:

 T(1)    = 0.0  
 X(1)    = Xin  
 V(1)    = Vin  
 X0      =     X(1)   - V(1) * h + accel(X(1))  *h*h/2.0  
 T(2)    = h  
 X(2)    = 2.0*X(1)   - X0       + accel(X(1))  *h*h  
 do i    = 3,Nt  
  ..............  
 enddo  
 V(Nt)= (X(Nt)-X(Nt-1))/h

The full program can be found in the file euler.f90 and is listed below:

!=========================================================  
!Program to integrate equations of motion for accelerations  
!which are functions of x with the method of Euler,  
!Euler-Cromer and Euler-Verlet.  
!The user sets initial conditions and the subroutines return  
!X(t) and V(t)=dX(t)/dt in arrays T(1..Nt),X(1..Nt),V(1..Nt)  
!The user provides number of times Nt and the final  
!time Tfi. Initial time is assumed to be t_i=0 and the  
!integration step h = Tfi/(Nt-1)  
!The user programs a real function accel(x) which gives the  
!acceleration  dV(t)/dt as function of X.  
!NOTE: T(1) = 0 T(Nt) = Tfi  
!=========================================================  
program diff_eq_euler  
 implicit none  
 integer,parameter:: P=110000 ! The size of the arrays  
 real,dimension(P):: T,X,V    ! time t,x(t),v(t)=dx/dt  
 real     :: Xin,Vin,Tfi      ! initial conditions  
 integer  :: Nt,i  
!The user provides initial conditions X_0,V_0 final time t_f  
!and Nt:  
 print *,’Enter X_0,V_0,t_f,Nt (t_i=0):’  
 read(5,*)Xin,Vin,Tfi,Nt  
!This check is necessary in order to avoid memory  
!access violations:  
 if(Nt .ge. P )then  
  print *,’Nt must be strictly less than P. Nt,P= ’,Nt,P  
  stop  
 endif  
!Xin= X(1), Vin=V(1), T(1)=0 and the routine gives evolution in  
!T(2..Nt), X(2..Nt), V(2..Nt) which we print in a file  
 call euler(Xin,Vin,Tfi,Nt,T,X,V)  
 open(unit=20,file="euler.dat")  
 do i=1,Nt  
!Each line in data file has time, position, velocity:  
  write(20,*) T(i),X(i),V(i)  
 enddo  
 close(20) !we close the unit to be reused below  
!------------------------------------  
!We repeat everything for each method  
 call euler_cromer(Xin,Vin,Tfi,Nt,T,X,V)  
 open(unit=20,file="euler_cromer.dat")  
 do i=1,Nt  
  write(20,*) T(i),X(i),V(i)  
 enddo  
 close(20)  
!------------------------------------  
 call euler_verlet(Xin,Vin,Tfi,Nt,T,X,V)  
 open(unit=20,file="euler_verlet.dat")  
 do i=1,Nt  
  write(20,*) T(i),X(i),V(i)  
 enddo  
 close(20)  
!------------------------------------  
end program diff_eq_euler  
!=========================================================  
!Function which returns the value of acceleration at  
!position x used in the integration subroutines  
!euler, euler_cromer and euler_verlet  
!=========================================================  
real function accel(x)  
 implicit none  
 real x  
 accel = -10.0*sin(x)  
end function accel  
!=========================================================  
!Driver routine for integrating equations of motion  
!using the Euler method  
!Input:  
!Xin=X(1), Vin=V(1) -- initial condition at t=0,  
!Tfi the final time and Nt the number of times  
!Output:  
!The arrays T(1..Nt), X(1..Nt), V(1..Nt) which  
!gives x(t_k)=X(k), dx/dt(t_k)=V(k), t_k=T(k) k=1..Nt  
!where for k=1 we have the initial condition.  
!=========================================================  
subroutine euler(Xin,Vin,Tfi,Nt,T,X,V)  
 implicit none  
 integer :: Nt  
 real,dimension(Nt) :: T,X,V    !time t,x(t),v(t)=dx/dt  
 real    :: Xin,Vin,Tfi  
 integer :: i  
 real    :: h,accel     !**declare the function accel**  
!Initial conditions set here:  
 T(1) = 0.0  
 X(1) = Xin  
 V(1) = Vin  
!h is the time step Dt  
 h    = Tfi/(Nt-1)  
 do i = 2,Nt  
  T(i) = T(i-1)+h          ! time advances by Dt=h  
  X(i) = X(i-1)+V(i-1)*h   ! advancement of position  
  V(i) = V(i-1)+accel(X(i-1))*h !and velocity.  
 enddo  
 
end subroutine euler  
!=========================================================  
!Driver routine for integrating equations of motion  
!using the Euler-Cromer method  
!Input:  
!Xin=X(1), Vin=V(1) -- initial condition at t=0,  
!Tfi the final time and Nt the number of times  
!Output:  
!The arrays T(1..Nt), X(1..Nt), V(1..Nt) which  
!gives x(t_i)=X(i), dx/dt(t_i)=V(i), t_i=T(i) i=1..Nt  
!where for i=1 we have the initial condition.  
!=========================================================  
subroutine euler_cromer(Xin,Vin,Tfi,Nt,T,X,V)  
 implicit none  
 integer :: Nt  
 real,dimension(Nt):: T,X,V !time t,x(t),v(t)=dx/dt  
 real    :: Xin,Vin,Tfi  
 integer :: i  
 real    :: h,accel  
 
 T(1) = 0.0  
 X(1) = Xin  
 V(1) = Vin  
 h    = Tfi/(Nt-1)  
 do i = 2,Nt  
  T(i) = T(i-1)+h  
  V(i) = V(i-1)+accel(X(i-1))*h  
  !here is the difference compared to Euler  
  X(i) = X(i-1)+V(i)*h  
 enddo  
 
end subroutine euler_cromer  
!=========================================================  
!Driver routine for integrating equations of motion  
!using the Euler - Verlet method  
!Input:  
!Xin=X(1), Vin=V(1) -- initial condition at t=0,  
!Tfi the final time and Nt the number of times  
!Output:  
!The arrays T(1..Nt), X(1..Nt), V(1..Nt) which  
!gives x(t_i)=X(i), dx/dt(t_i)=V(i), t_i=T(i) i=1..Nt  
!where for i=1 we have the initial condition.  
!=========================================================  
subroutine euler_verlet(Xin,Vin,Tfi,Nt,T,X,V)  
 implicit none  
 integer :: Nt  
 real,dimension(Nt):: T,X,V !time t,x(t),v(t)=dx/dt  
 real    :: Xin,Vin,Tfi  
 integer :: i  
 real    :: h,h2,X0,o2h  
 real    :: accel  
!Initial conditions set here:  
 T(1)    = 0.0  
 X(1)    = Xin  
 V(1)    = Vin  
 h       = Tfi/(Nt-1)   ! time step  
 h2      = h*h             ! time step squared  
 o2h     = 0.5/h           ! h/2  
!We have to initialize one more step: X0 corresponds to ’X(0)’  
 X0      =     X(1)   - V(1) * h + accel(X(1))  *h2/2.0  
 T(2)    = h  
 X(2)    = 2.0*X(1)   - X0       + accel(X(1))  *h2  
!Now i starts from 3:  
 do i    = 3,Nt  
  T(i)   = T(i-1)+h  
  X(i)   = 2.0*X(i-1) - X(i-2)   + accel(X(i-1))*h2  
  V(i-1) = o2h * (X(i)-X(i-2))  
 enddo  
!Notice that we have one more step for the velocity:  
 V(Nt)= (X(Nt)-X(Nt-1))/h  
end subroutine euler_verlet

Compiling the running the program can be done with the commands:

> gfortran euler.f90 -o euler  
> ./euler  
 Enter X_0,V_0,t_f,Nt (t_i=0):  
0.2 0.0 6.0 1000  
> ls  euler*.dat  
euler_cromer.dat  euler.dat  euler_verlet.dat  
> head -n 5 euler.dat  
   0.000000      0.2000000       0.000000  
  6.0060062E-03  0.2000000     -1.1932093E-02  
  1.2012012E-02  0.1999283     -2.3864185E-02  
  1.8018018E-02  0.1997850     -3.5792060E-02  
  2.4024025E-02  0.1995700     -4.7711499E-02

The last command shows the first 5 lines of the file euler.dat. We see the data for the time, the position and the velocity stored in 3 columns. We can graph the results using gnuplot:

gnuplot> plot "euler.dat" using 1:2 with lines  
gnuplot> plot "euler.dat" using 1:3 with lines

These commands result in plotting the positions and the velocities as a function of time respectively. We can add the results of all methods to the last plot with the commands:

gnuplot> replot "euler_cromer.dat" using 1:3 with lines  
gnuplot> replot "euler_verlet.dat" using 1:3 with lines

The results can be seen in figures 4.24.7. Euler’s method is unstable unless we take a quite small time step. The Euler–Cromer method behaves impressively better. The results converge and remain constant for Nt∼ 100, 000  . The Euler–Verlet method converges much faster, but roundoff errors kick in soon. This is more obvious in figure 4.7 where the initial angular position is large. For small angles we can compare with the solution one obtains for the harmonic pendulum (sin(𝜃) ≈ 𝜃  ):

            g        2
α(𝜃)  =   − l𝜃 ≡ − Ω  𝜃

 𝜃(t)  =   𝜃0cos(Ωt) + (ω0∕Ω )sin(Ωt)
ω (t)  =   ω0cos(Ωt ) − (𝜃0Ω )sin(Ωt).            (4.14)
In figures 4.24.4 we observe that the results agree with the above formulas for the values of Δt  where the methods converge. This way we can check our program for bugs. The plot of the functions above can be done with the following gnuplot commands5 :
gnuplot> set dummy t  
gnuplot> omega2  = 10  
gnuplot> X0      = 0.2  
gnuplot> V0      = 0.0  
gnuplot> omega   = sqrt(omega2)  
gnuplot> x(t)    = X0 * cos(omega * t) +(V0/omega)*sin(omega*t)  
gnuplot> v(t)    = V0 * cos(omega * t) -(omega*X0)*sin(omega*t)  
gnuplot> plot x(t), v(t)

The results should not be compared only graphically since subtle differences can remain unnoticed. It is more desirable to plot the differences of the theoretical values from the numerically computed ones which can be done using the commands:

gnuplot> plot "euler.dat" using 1:($2-x($1)) with lines  
gnuplot> plot "euler.dat" using 1:($3-v($1)) with lines

The command using 1:($2-x($1)) puts the values found in the first column on the x  axis and the value found in the second column minus the value of the function x(t) for t  equal to the value found in the first column on the y  axis. This way, we can make the plots shown in6 figures 4.11-4.14.

4.3 Runge–Kutta Methods

Euler’s method is a one step finite difference method of first order. First order means that the total error introduced by the discretization of the integration interval [ti,tf]  by N  discrete times is of order ∼  𝒪 (h )  , where h ≡  Δt = (tf − ti)∕N  is the time step of the integration. In this section we will discuss a generalization of this approach where the total error will be of higher order in h  . This is the class of Runge-Kutta methods which are one step algorithms where the total discretization error is of order       p
∼ 𝒪 (h )  . The local error introduced at each step is of order        p+1
∼  𝒪 (h    )  leading after N  = (tf − ti)∕Δt  steps to a maximum error of order

                             tf − ti              1
∼ 𝒪 (hp+1) × N  = 𝒪 (hp+1) × -------∼ 𝒪 (hp+1) × --=  𝒪 (hp ).
                               Δt                h
(4.15)

In such a case we say that we have a Runge-Kutta method of pth  order. The price one has to pay for the increased accuracy is the evaluation of the derivatives of the functions in more than one points in the interval (t,t + Δt )  .

Let’s consider for simplicity the problem with only one unknown function x (t)  which evolves in time according to the differential equation:

dx-=  f(t,x).
dt
(4.16)


pict

Figure 4.8: The geometry of the step of the Runge-Kutta method of 1st  order given by equation (4.17) .

Consider the first order method first. The most naive approach would be to take the derivative to be given by the finite difference

dx-   xn+1-−--xn
 dt ≈     Δt     = f (tn,xn ) ⇒ xn+1 = xn +  hf(tn,xn).
(4.17)

By Taylor expanding, we see that the error at each step is 𝒪 (h2)  , therefore the error after integrating from t →  t
i    f  is 𝒪 (h)  . Indeed,

                          dx
xn+1 = x (tn + h) = xn + h ---(xn ) + 𝒪 (h2) = xn + hf (tn,xn) + 𝒪 (h2 ).
                          dt
(4.18)

The geometry of the step is shown in figure 4.8. We start from point 1 and by linearly extrapolating in the direction of the derivative k1 ≡ f(tn,xn)  we determine the point xn+1   .


pict

Figure 4.9: The geometry of an integration step of the 2nd order Runge-Kutta method given by equation (4.19) .

We can improve the method above by introducing an intermediate point 2. This process is depicted in figure 4.9. We take the point 2 in the middle of the interval (tn,tn+1)  by making a linear extrapolation from x
  n  in the direction of the derivative k ≡  f(t ,x )
 1      n  n  . Then we use the slope at point 2 as an estimator of the derivative within this interval, i.e. k2 ≡ f (tn+1∕2,xn+1∕2) = f(tn + h∕2,xn +  (h ∕2)k1)  . We use k2   to linearly extrapolate from xn  to xn+1   . Summarizing, we have that

   k1  =  f (tn,xn )
                 h       h
   k2  ≡  f (tn + --,xn + --k1)
                 2       2
xn+1   =  xn + hk2.                           (4.19)
For the procedure described above we have to evaluate f  twice at each step, thereby doubling the computational effort. The error at each step (4.19) becomes       3
∼ 𝒪 (h )  , however, giving a total error of       2           2
∼ 𝒪 (h ) ∼ 𝒪 (1∕N  )  . So for given computational time, (4.19) is superior to (4.17) .

pict

Figure 4.10: The geometry of an integration step of the Runge-Kutta method of 4th order given by equation (4.20) .

We can further improve the accuracy gain by using the Runge–Kutta method of 4th order. In this case we have 4 evaluations of the derivative f  per step, but the total error becomes now        4
∼  𝒪(h  )  and the method is superior to that of (4.19) 7. The process followed is explained geometrically in figure 4.10. We use 3 intermediate points for evolving the solution from xn  to xn+1   . Point 2 is determined by linearly extrapolating from xn  to the midpoint of the interval (tn,tn+1 = tn + h)  by using the direction given by the derivative k1 ≡ f (tn,xn )  , i.e. x2 = xn +  (h ∕2)k1   . We calculate the derivative k  ≡ f (t + h ∕2,x  + (h∕2)k )
 2      n         n         1  at the point 2 and we use it in order to determine point 3, also located at the midpoint of the interval (tn,tn+1)  . Then we calculate the derivative k3 ≡  f(tn + h ∕2,xn + (h∕2)k2)  at the point 3 and we use it to linearly extrapolate to the end of the interval (tn, tn+1 )  , thereby obtaining point 4, i.e. x4 = xn +  hk3   . Then we calculate the derivative k  ≡ f (t + h, x +  hk )
 4      n       n     3  at the point 4, and we use all four derivative k ,k ,k
 1  2  3   and k4   as estimators of the derivative of the function in the interval (tn,tn+1)  . If each derivative contributes with a particular weight in this estimate, the discretization error can become ∼ 𝒪 (h5)  . Such a choice is

  k1  =   f(tn,xn)
                 h-      h-
  k2  =   f(tn + 2 ,xn + 2k1 )
                 h       h
  k3  =   f(tn + --,xn + -k2 )
                 2       2
  k4  =   f(tn + h,xn + hk3 )
               h
xn+1  =   xn + --(k1 + 2k2 + 2k3 + k4 ).           (4.20)
               6
We note that the second term of the last equation takes an average of the four derivatives with weights 1∕6  , 1∕3  , 1∕3  and 1∕6  respectively. A generic small change in these values will increase the discretization error to worse than h5   .

We remind to the reader the fact that by decreasing h  the discretization errors decrease, but that roundoff errors will start showing up for small enough h  . Therefore, a careful determination of h  that minimizes the total error should be made by studying the dependence of the results as a function of h  .

4.3.1 A Program for the 4th Order Runge–Kutta

Consider the problem of the motion of a particle in one dimension. For this, we have to integrate a system of two differential equations (4.5) for two unknown functions of time x1(t) ≡ x(t)  and x2 (t) ≡ v(t)  so that

dx1-=  f1(t,x1,x2)    dx2- = f2(t,x1,x2)
 dt                    dt
(4.21)

In this case, equations (4.20) generalize to:

   k11  =   f1(tn,x1,n,x2,n )
   k21  =   f2(tn,x1,n,x2,n )

   k12  =   f1(tn + h-,x1,n + h-k11,x2,n + h-k21)
                   2         2           2
                   h-       h-          h-
   k22  =   f2(tn + 2 ,x1,n +  2k11,x2,n +  2k21)
                   h        h           h
   k13  =   f1(tn + --,x1,n + --k12,x2,n + --k22)
                   2         2           2
   k    =   f (t  + h-,x   + h-k  ,x   + h-k  )
    23       2 n   2   1,n    2 12  2,n    2 22
   k14  =   f1(tn + h,x1,n + hk13,x2,n + hk23)

   k24  =   f2(tn + h,x1,n + hk13,x1,n + hk23)
                  h-
x1,n+1  =   x1,n + 6 (k11 + 2k12 + 2k13 + k14)
                  h
x2,n+1  =   x1,n + --(k21 + 2k22 + 2k23 + k24).         (4.22)
                  6

Programming this algorithm is quite simple. The main program is an interface between the user and the driver routine of the integration. The user enters the initial and final times ti =  Ti and tf =  Tf and the number of discrete time points Nt. The initial conditions are x1(ti) =  X10, x2(ti) =  X20. The main data structure consists of three real arrays T(P), X1(P), X2(P) which store the times ti ≡ t1,t2,...,tNt ≡ tf  and the corresponding values of the functions x1(tk)  and x (t)
 2 k  , k = 1, ...,Nt . The main program calls the driver routine RK(T,X1,X2,Ti,Tf,X10,X20,Nt) which “drives” the heart of the program, the subroutine RKSTEP(t,x1,x2,dt) which performs one integration step using equations (4.22) . RKSTEP evolves the functions x1, x2 at time t by one step h =  dt. The routine RK stores the calculated values in the arrays T, X1 and X2 at each step. When RK returns the control to the main program, all the results are stored in T, X1 and X2, which are subsequently printed in the file rk.dat. The full program is listed below and can be found in the file rk.f90:

!========================================================  
!Program to solve a 2 ODE system using Runge-Kutta Method  
!User must supply derivatives  
!dx1/dt=f1(t,x1,x2) dx2/dt=f2(t,x1,x2)  
!as real functions  
!Output is written in file rk.dat  
!========================================================  
program rk_solve  
 implicit none  
 integer, parameter :: P=110000  
 real,dimension(P)  :: T,X1,X2  
 real    :: Ti,Tf,X10,X20  
 integer :: Nt  
 integer :: i  
!Input:  
 print *,’Runge-Kutta Method for 2-ODEs Integration’  
 print *,’Enter Nt,Ti,TF,X10,X20:’  
 read  *, Nt,Ti,Tf,X10,X20  
 print *,’Nt = ’,Nt  
 print *,’Time: Initial Ti =’,Ti,’ Final Tf=’,Tf  
 print *,’           X1(Ti)=’,X10,’ X2(Ti)=’,X20  
 if(Nt.gt.P) stop ’Nt>P’  
!The Calculation:  
 call RK(T,X1,X2,Ti,Tf,X10,X20,Nt)  
!Output:  
 open(unit=11,file=’rk.dat’)  
 do i=1,Nt  
  write(11,*)T(i),X1(i),X2(i)  
 enddo  
 close(11)  
 
end program rk_solve  
!========================================================  
!The functions f1,f2(t,x1,x2) provided by the user  
!========================================================  
real function f1(t,x1,x2)  
 implicit none  
 real :: t,x1,x2  
 f1=x2           !dx1/dt= v = x2  
end function f1  
!--------------------------------------------------------  
real function f2(t,x1,x2)  
 implicit none  
 real :: t,x1,x2  
 f2=-10.0D0*x1   !harmonic oscillator  
end function f2  
!========================================================  
!RK(T,X1,X2,Ti,Tf,X10,X20,Nt) is the driver  
!for the Runge-Kutta integration routine RKSTEP  
!Input: Initial and final times Ti,Tf  
!       Initial values at t=Ti  X10,X20  
!       Number of steps of integration: Nt-1  
!       Size of arrays T,X1,X2  
!Output: real arrays T(Nt),X1(Nt),X2(Nt) where  
!T(1) = Ti X1(1) = X10 X2(1) = X20  
!          X1(k) = X1(at t=T(k)) X2(k) = X2(at t=T(k))  
!T(Nt)=TF  
!========================================================  
subroutine RK(T,X1,X2,Ti,Tf,X10,X20,Nt)  
 implicit none  
 integer :: Nt  
 real,dimension(Nt):: T,X1,X2  
 real    :: Ti,Tf,X10,X20  
 real    :: dt  
 real    :: TS,X1S,X2S !values of time and X1,X2 at given step  
 integer :: i  
!Initialize variables:  
 dt      = (Tf-Ti)/(Nt-1)  
 T (1)   = Ti  
 X1(1)   = X10  
 X2(1)   = X20  
 TS      = Ti  
 X1S     = X10  
 X2S     = X20  
!Make RK steps: The arguments of RKSTEP  
!are replaced with the new ones!  
 do i=2,Nt  
  call RKSTEP(TS,X1S,X2S,dt)  
  T (i)  = TS  
  X1(i)  = X1S  
  X2(i)  = X2S  
 enddo  
end subroutine RK  
!========================================================  
!Subroutine RKSTEP(t,x1,x2,dt)  
!Runge-Kutta Integration routine of ODE  
!dx1/dt=f1(t,x1,x2) dx2/dt=f2(t,x1,x2)  
!User must supply derivative functions:  
!real function f1(t,x1,x2)  
!real function f2(t,x1,x2)  
!Given initial point (t,x1,x2) the routine advances it  
!by time dt.  
!Input : Inital time t    and function values x1,x2  
!Output: Final  time t+dt and function values x1,x2  
!Careful!: values of t,x1,x2 are overwritten...  
!========================================================  
subroutine RKSTEP(t,x1,x2,dt)  
 implicit none  
 real :: t,x1,x2,dt  
 real :: f1,f2  
 real :: k11,k12,k13,k14,k21,k22,k23,k24  
 real :: h,h2,h6  
 
 h  =dt      !h =dt, integration step  
 h2 =0.5D0*h !h2=h/2  
 h6 =h/6.0   !h6=h/6  
 
 k11=f1(t,x1,x2)  
 k21=f2(t,x1,x2)  
 k12=f1(t+h2,x1+h2*k11,x2+h2*k21)  
 k22=f2(t+h2,x1+h2*k11,x2+h2*k21)  
 k13=f1(t+h2,x1+h2*k12,x2+h2*k22)  
 k23=f2(t+h2,x1+h2*k12,x2+h2*k22)  
 k14=f1(t+h ,x1+h *k13,x2+h *k23)  
 k24=f2(t+h ,x1+h *k13,x2+h *k23)  
 
 t  =t+h  
 x1 =x1+h6*(k11+2.0D0*(k12+k13)+k14)  
 x2 =x2+h6*(k21+2.0D0*(k22+k23)+k24)  
 
end subroutine RKSTEP  

4.4 Comparison of the Methods


pict

Figure 4.11: The discrepancy of the numerical results of the Euler method from the analytic solution for the simple harmonic oscillator. The parameters chosen are ω2 = 10  , ti = 0  , tf = 6  , x(0) = 0.2  , v(0) = 0  and the number of steps is N  = 50,500,5,000,50,000  . Observe that the error becomes approximately ten times smaller each time according to the expectation of being of order ∼ 𝒪(Δt)  .


pict

Figure 4.12: Like in figure 4.11 for the Euler-Cromer method. The error becomes approximately ten times smaller each time according to the expectation of being of order ∼ 𝒪(Δt)  .


pict

Figure 4.13: Like in figure 4.11 for the Euler-Verlet method. The error becomes approximately 100 times smaller each time according to the expectation of being of order ∼ 𝒪(Δt2)  .


pict

Figure 4.14: Like in figure 4.11 for the 4th order Runge–Kutta method. The error becomes approximately   −4
10  times smaller each time according to the expectation of being of order ∼ 𝒪 (Δt4 )  . The roundoff errors become apparent for 50,000  steps.


pict

Figure 4.15: Like in figure 4.11 for the case of mechanical energy for the Euler method.


pict

Figure 4.16: Like in figure 4.11 for the case of mechanical energy for the Euler–Cromer method.


pict

Figure 4.17: Like in figure 4.11 for the case of mechanical energy for the Euler–Verlet method.


pict

Figure 4.18: Like in figure 4.11 for the case of mechanical energy for the 4th order Runge–Kutta method. Roundoff errors appear for large enough number of steps.

In this section we will check our programs for correctness and accuracy w.r.t. discretization and roundoff errors. The simplest test is to check the results against a known analytic solution of a simple model. This will be done for the simple harmonic oscillator. Our programs will need small changes which are summarized below. First, we will need to use higher accuracy variables and we will change all variables of type REAL to REAL(8). For this we need to change the corresponding declarations in the beginning of each (sub)program. For each numerical constant in the program we need to put an explicit exponent with the letter D instead of an E. For example 0.5 → 0.5D0, 1.2E-3 → 1.2D-3 etc. Then we need to alter the functions that compute the acceleration of the particle to give a = − ω2x  . We will take ω2 =  10  (T ≈  1.987  ). Therefore the relevant part of the program in euler.f90 becomes

real(8) function accel(x)  
 implicit none  
 real(8) :: x  
 accel = -10.0D0*x  
end function accel

and that of the program in rk.f90 becomes

real(8) function f2(t,x1,x2)  
 implicit none  
 real(8) :: t,x1,x2  
 f2=-10.0D0*x1  
end function f2

The programs are run for a given time interval ti = 0  to tf = 6  with the initial conditions x =  0.2
 0  , v =  0
 0  . The time step Δt  is varied by varying the number of steps Nt-1. The computed numerical solution is compared to the well known solution for the simple harmonic oscillator

 a(x)  =  − ω2x

xh(t)  =  x0 cos(ωt) + (v0∕ω)sin(ωt)
vh(t)  =  v0 cos(ωt) − (x0 ω)sin(ωt),            (4.23)
We study the deviation δx(t) = |x (t) − xh(t)| and δv(t) = |v (t) − vh(t)| as a function of the time step Δt  . The results are shown in figures 4.114.14. We note that for the Euler method and the Euler–Cromer method, the errors are of order 𝒪 (Δt )  as expected. However, the latter has smaller errors compared to the first one. For the Euler–Verlet method, the error turns out to be of order      2
𝒪 (Δt  )  whereas for the 4th order Runge–Kutta is of order8       4
𝒪 (Δt  )  .

Another way for checking the numerical results is by looking at a conserved quantity, like the energy, momentum or angular momentum, and study its deviation from its original value. In our case we study the mechanical energy

     1        1
E  = --mv2 +  -m ω2x2
     2        2
(4.24)

which is computed at each step. The deviation δE  = |E − E0 | is shown in figures 4.154.18.

4.5 The Forced Damped Oscillator

In this section we will study a simple harmonic oscillator subject to a damping force proportional to its velocity and an external periodic driving force, which for simplicity will be taken to have a sinusoidal dependence in time,

d2x     dx
----+ γ ---+ ω20x =  a0sinωt,
dt2     dt
(4.25)

where F (t) = ma0 sin ωt  and ω  is the angular frequency of the driving force.

Consider initially the system without the influence of the driving force, i.e. with a0 = 0  . The real solutions of the differential equation9 which are finite for t → + ∞ are given by

               √ ------            √ ------
x0(t) = c1e −(γ+   γ2−4ω20)t∕2 + c2e−(γ−  γ2− 4ω20)t∕2,   γ2 − 4ω2 > 0,
                                                        0
(4.26)

          −γt∕2     − γt∕2     2     2
x0(t) = c1e     + c2e     t,  γ  − 4ω0 = 0,
(4.27)

                     ( ∘ -----------  )
x0(t)  =   c1e−γt∕2 cos    − γ2 + 4ω20t∕2

                         (∘  -----------  )
            +c2e −γt∕2 sin     − γ2 + 4 ω20t∕2 ,  γ2 − 4ω20 < 0.(4.28)
In the last case, the solution oscillates with an amplitude decreasing exponentially with time.

In the a0 > 0  case, the general solution is obtained from the sum of a special solution xs(t)  and the solution of the homogeneous equation x0 (t)  . A special solution can be obtained from the ansatz xs(t) = A sinωt + B  cosωt  , which when substituted in (4.25) and solved for A  and B  we find that

        a0 [(ω20 − ω2 )cosωt + γ ωsin ωt]
xs(t) = ---------2----2-2----2-2-------,
               (ω 0 − ω ) +  ω γ
(4.29)

and

x(t) = x (t) + x (t).
        0       s
(4.30)

The solution x0 (t)  decreases exponentially with time and eventually only xs(t)  remains. The only case where this is not true, is when we have resonance without damping for ω  = ω0   , γ = 0  . In that case the solution is

x(t) = c1cosωt + c2 sin ωt + -a0-(cosωt + 2(ωt )sin ωt) .
                            4ω2
(4.31)

The first two terms are the same as that of the simple harmonic oscillator. The last one increases the amplitude linearly with time, which is a result of the influx of energy from the external force to the oscillator.

Our program will be a simple modification of the program in rk.f90. The main routines RK(T,X1,X2,T0,TF,X10,X20,Nt) and RKSTEP(t,x1,x2,dt) remain as they are. We only change the user interface. The basic parameters ω0   , ω  , γ  , a0   are entered interactively by the user from the standard input stdin. These parameters should be accessible also by the function f2(t,x1,x2), and one way to be able to do this, is to store them in variables which are placed in a common block. Such variables are accessible to all subprograms that declare a common block with the same name using a COMMON declaration. Such a declaration is shown in the following lines

 real(8) ::     omega_0,omega,gamma,a_0,omega_02,omega2  
 common /params/omega_0,omega,gamma,a_0,omega_02,omega2

which when written in a (sub)program, the (sub)program gains access to the “memory position” params where the values of the variables are stored. Another point that needs our attention is the function f2(t,x1,x2) which now takes the velocity v → x2 in its arguments:

real(8) function f2(t,x1,x2)  
 implicit none  
 real(8)        omega_0,omega,gamma,a_0,omega_02,omega2  
 common /params/omega_0,omega,gamma,a_0,omega_02,omega2  
 real(8) t,x1,x2,a  
 a = a_0*cos(omega*t)  
 f2=-omega_02*x1-gamma*x2+a  
end function f2

The main program found in the file dlo.f90 is listed below. The subroutines RK, RKSTEP are the same as in rk.f90 and should also be included in the same file.

!========================================================  
!Program to solve Damped Linear Oscillator  
!using 4th order Runge-Kutta Method  
!Output is written in file dlo.dat  
!========================================================  
program dlo_solve  
 implicit none  
 integer, parameter  :: P=110000  
 real(8),dimension(P):: T,X1,X2  
 real(8) :: Ti,Tf,X10,X20  
 real(8) :: Energy  
 real(8) ::     omega_0,omega,gamma,a_0,omega_02,omega2  
 common /params/omega_0,omega,gamma,a_0,omega_02,omega2  
 integer :: Nt, i  
!Input:  
 print *,’Runge-Kutta Method for DLO Integration’  
 print *,’Enter omega_0, omega, gamma, a_0:’  
 read  *, omega_0,omega,gamma,a_0  
 omega_02 = omega_0*omega_0  
 omega2   = omega  *omega  
 print *, ’omega_0= ’,omega_0,’ omega= ’, omega  
 print *, ’gamma=   ’,gamma,  ’ a_0=   ’,a_0  
 print *,’Enter Nt,Ti,TF,X10,X20:’  
 read  *, Nt,Ti,Tf,X10,X20  
 print *,’Nt = ’,Nt  
 print *,’Time: Initial Ti =’,Ti,’ Final Tf=’,Tf  
 print *,’           X1(Ti)=’,X10,’ X2(Ti)=’,X20  
 if(Nt.gt.P) stop ’Nt>P’  
!The Calculation:  
 call RK(T,X1,X2,Ti,Tf,X10,X20,Nt)  
!Output:  
 open(unit=11,file=’dlo.dat’)  
 write(11,*)’# Damped Linear Oscillator - dlo’  
 write(11,*)’# omega_0= ’,omega_0,’ omega= ’, omega,&  
      ’ gamma= ’,gamma,’ a_0= ’,a_0  
 do i=1,Nt  
  Energy = 0.5D0*X2(i)*X2(i)+0.5D0*omega_02*X1(i)*X1(i)  
  write(11,*)T(i),X1(i),X2(i),Energy  
 enddo  
 close(11)  
end program dlo_solve  
!========================================================  
!The functions f1,f2(t,x1,x2) provided by the user  
!========================================================  
real(8) function f1(t,x1,x2)  
 implicit none  
 real(8) t,x1,x2  
 f1=x2           !dx1/dt= v = x2  
end function f1  
!--------------------------------------------------------  
real(8) function f2(t,x1,x2)  
 implicit none  
 real(8)        omega_0,omega,gamma,a_0,omega_02,omega2  
 common /params/omega_0,omega,gamma,a_0,omega_02,omega2  
 real(8) t,x1,x2,a  
 a = a_0*cos(omega*t)  
 f2=-omega_02*x1-gamma*x2+a  
end function f2


pict

Figure 4.19: The position as a function of time for the damped oscillator for several values of γ  and ω0 = 3.145  .


pict

Figure 4.20: The phase space trajectory for the damped oscillator for several values of γ  and ω0 = 3.145  . Note the attractor at (x,v) = (0,0)  where all trajectories are “attracted to” as t → +∞ .


pict

Figure 4.21: The amplitude of oscillation for the damped oscillator for several values of γ  and ω0 = 3.145  . Note the exponential damping of the amplitude with time.


pict

Figure 4.22: The period of oscillation of the damped oscillator for several values of γ  and ω0 = 3.145  . The axes are chosen so that equation (4.28)       2    2   2
(2π∕T ) = 4ω0 − γ  can be easily verified. The points in the plot are our measurements whereas the straight line is the theoretical prediction, the diagonal y = x

The results are shown in figures 4.194.22. Figure 4.19 shows the transition from a damped motion for γ > 2ω0   to an oscillating motion with damping amplitude for γ < 2 ω0   . The exponential decrease of the amplitude is shown in figure 4.21, whereas the dependence of the period T  from the damping coefficient γ  is shown in figure 4.22. Motivated by equation (4.28) , written in the form

      (    )
  2     2π-      2
4ω0 −   T    = γ ,
(4.32)

we construct the plot in figure 4.22. The right hand side of the equation is put on the horizontal axis, whereas the left hand side on the vertical. Equation (4.32) predicts that both quantities are equal and all measurements should lie on a particular line, the diagonal y = x  . The period T  can be estimated from the time between two consecutive extrema of x(t)  or two consecutive zeros of the velocity v(t)  (see figure 4.19).

Finally it is important to study the trajectory of the system in phase space. This can be seen10 in figure 4.20. A point in this space is a state of the system and a trajectory describes the evolution of the system’s states in time. We see that all such trajectories end up as t →  +∞ to the point (0,0)  , independently of the initial conditions. Such a point is an example of a system’s attractor.


pict

Figure 4.23: The period of oscillation for the forced damped oscillator for different initial conditions. We have chosen ω0 = 3.145  , ω = 2.0  , γ = 0.5  and a0 = 1.0  . We note that after the transient behavior the system oscillates harmonically according to the relation x(t) = x0(ω)cos(ωt + δ)  .


pict

Figure 4.24: The oscillation amplitude x (ω)
 0  as a function of ω  for the forced damped oscillator, where ω0 = 3.145  , γ = 0.5  and a0 = 1.0  . We observe a resonance for ω ≈ ω0  . The points of the plot are our measurements and the line is the theoretical prediction given by equation (4.33) .


pict

Figure 4.25: A phase space trajectory of the forced damped oscillator with ω0 = 3.145  , ω = 2.0  , γ = 0.5  and a0 = 1.0  . The harmonic oscillation which is the steady state of the system is an ellipse, which is an attractor of all the phase space trajectories that correspond to different initial conditions.


pict

Figure 4.26: The trajectory shown in figure 4.25 for t > 100  . The trajectory is almost on top of an ellipse corresponding to the steady state motion of the system. This ellipse is an attractor of the system.

Next, we add the external force and study the response of the system to it. The system exhibits a transient behavior that depends on the initial conditions. For large enough times it approaches a steady state that does not depend on (almost all of) the initial conditions. This can be seen in figure 4.23. This is easily understood for our system by looking at equations (4.26) – (4.28) . We see that the steady state xs(t)  becomes dominant when the exponentials have damped away. xs(t)  can be written in the form

 x (t)  =   x0(ω) cos(ωt +  δ(ω ))
                    a0                            ω γ
x0(ω)  =   ∘----2--------------,     tan δ(ω ) = -2----2-.  (4.33)
             (ω 0 − ω2 )2 + γ2ω2                ω  − ω0
These equations are verified in figure 4.24 where we study the dependence of the amplitude x0(ω)  on the angular frequency of the driving force. Finally we study the trajectory of the system in phase space. As we can see in figure 4.20, this time the attractor is an ellipse, which is a one dimensional curve instead of a zero dimensional point. For large enough times, all trajectories approach their attractor asymptotically.

4.6 The Forced Damped Pendulum

In this section we will study a non-linear dynamical system which exhibits interesting chaotic behavior. This is a simple model which, despite its deterministic nature, the prediction of its future behavior becomes intractable after a short period of time. Consider a simple pendulum in a constant gravitational field whose motion is damped by a force proportional to its velocity and it is under the influence of a vertical, harmonic external driving force:

 2
d-𝜃-+ γd-𝜃+  ω20 sin 𝜃 = − 2A cosωt sin 𝜃.
dt2     dt
(4.34)

In the equation above, 𝜃  is the angle of the pendulum with the vertical axis, γ  is the damping coefficient, ω20 = g∕L  is the pendulum’s natural angular frequency, ω  is the angular frequency of the driving force and 2A  is the amplitude of the external angular acceleration caused by the driving force.

In the absence of the driving force, the damping coefficient drives the system to the point (𝜃, ˙𝜃) = (0, 0)  , which is an attractor for the system. This continues to happen for small enough A  , but for A >  Ac  the behavior of the system becomes more complicated.

The program that integrates the equations of motion of the system can be obtained by making trivial changes to the program in the file dlo.f90. This changes are listed in detail below, but we note that X1 ↔  𝜃  , X2 ↔  ˙𝜃  , a_0 ↔  A  . The final program can be found in the file fdp.f90. It is listed below, with the understanding that the commands in between the dots are the same as in the programs found in the files dlo.f90, rk.f90.

!========================================================  
!Program to solve Forced Damped Pendulum  
!using 4th order Runge-Kutta Method  
!Output is written in file fdp.dat  
!========================================================  
program dlo_solve  
 implicit none  
 integer, parameter  :: P=1010000  
 ................................  
  Energy = 0.5D0*X2(i)*X2(i)+omega_02*(1.0D0-cos(X1(i)))  
 ................................  
end program dlo_solve  
!--------------------------------------------------------  
real(8) function f2(t,x1,x2)  
 implicit none  
 real(8)        omega_0,omega,gamma,a_0,omega_02,omega2  
 common /params/omega_0,omega,gamma,a_0,omega_02,omega2  
 real(8) t,x1,x2  
 f2=-(omega_02+2.0D0*a_0*cos(omega*t))*sin(x1)-gamma*x2  
end function f2  
!========================================================  
subroutine RKSTEP(t,x1,x2,dt)  
 implicit none  
 ................................  
 real(8),parameter :: pi =3.14159265358979324D0  
 real(8),parameter :: pi2=6.28318530717958648D0  
 ................................  
 x1 =x1+h6*(k11+2.0D0*(k12+k13)+k14)  
 x2 =x2+h6*(k21+2.0D0*(k22+k23)+k24)  
 if( x1 .gt.  pi) x1 = x1 - pi2  
 if( x1 .lt. -pi) x1 = x1 + pi2  
end subroutine RKSTEP

The final lines in the program are added so that the angle is kept within the interval [− π,π]  .

In order to study the system’s properties we will set ω0 =  1  , ω = 2  , and γ =  0.2  unless we explicitly state otherwise. The natural period of the pendulum is T0 =  2π∕ω0 =  2π ≈ 6.28318530717958648  whereas that of the driving force is T  = 2π∕ω  = π ≈ 3.14159265358979324  . For A <  Ac  , with Ac ≈  0.18  , the point (𝜃, ˙𝜃) = (0,0)  is an attractor, which means that the pendulum eventually stops at its stable equilibrium point. For Ac < A  < 0.71  the attractor is a closed curve, which means that the pendulum at its steady state oscillates indefinitely without circling through its unstable equilibrium point at 𝜃 = ± π  . The period of motion is found to be twice that of the driving force. For 0.72 < A  < 0.79  the attractor is an open curve, because at its steady state the pendulum crosses the 𝜃 = ± π  point. The period of the motion becomes equal to that of the driving force. For 0.79 < A ≲  1.033  we have period doubling for critical values of A  , but the trajectory is still periodic. For even larger values of A  the system enters into a chaotic regime where the trajectories are non periodic. For A  ≈ 3.1  we find the system in a periodic steady state again, whereas for A ≈  3.8  4.448  we have period doubling. For A ≈  4.4489  we enter into a chaotic regime again etc. These results can be seen in figures 4.274.29. The reader should construct the bifurcation diagram of the system by solving problem 20 of this chapter.


pict pict
pict pict

Figure 4.27: A phase space trajectory of the forced damped pendulum. The parameters chosen are ω0 = 1.0  , ω = 2.0  , γ = 0.2  and A = 0.60,0.72,0.85,1.02  . We observe the phenomenon of period doubling.


pict pict
pict pict

Figure 4.28: A phase space trajectory of the forced damped pendulum. The parameters chosen are ω0 = 1.0  , ω = 2.0  , γ = 0.2  and A = 1.031,1.033,1.04,1.4  . We observe the chaotic behavior of the system.


pict pict
pict pict

Figure 4.29: A phase space trajectory of the forced damped pendulum. The parameters chosen are ω0 = 1.0  , ω = 2.0  , γ = 0.2  and A = 1.568,3.8,4.44,4.5  . We observe the system exiting and reentering regimes of chaotic behavior.


pict pict

Figure 4.30: A Poincaré diagram for the forced damped pendulum in its chaotic regime. The parameters chosen are ω0 = 1.0  , ω = 2.0  , γ = 0.2  and A = 1.4,4.5  .

We can also use the so called Poincaré diagrams in order to study the chaotic behavior of a system. These are obtained by placing a point in phase space when the time is an integer multiple of the period of the driving force. Then, if for example the period of the motion is equal to that of the period of the driving force, the Poincaré diagram consists of only one point. If the period of the motion is an n  –multiple of the period of the driving force then the Poincaré diagram consists of only n  points. Therefore, in the period doubling regime, the points of the Poincaré diagram double at each period doubling point. In the chaotic regime, the Poincaré diagram consists of an infinite number of points which belong to sets that have interesting fractal structure. One way to construct the Poincaré diagram numerically, is to process the data of the output file fdp.dat using awk11 :

awk -v o=$omega -v nt=$Nt -v tf=$TF \  
 ’BEGIN{T=6.283185307179/o;dt=tf/nt;} $1%T<dt{print $2,$3}’\  
 fdp.dat

where $omega, $Nt, $TF are the values of the angular frequency ω  , the number of points of time and the final time tf  . We calculate the period T and the time step dt in the program. Then we print those lines of the file where the time is an integer multiple of the period12 . This is accomplished by the modulo operation $1 % T. The value of the expression $1 % T < dt is true when the remainder of the division of the first column ($1) of the file fdp.dat with the period T is smaller than dt. The results in the chaotic regime are displayed in figure 4.30.

We close this section by discussing another concept that helps us in the analysis of the dynamical properties of the pendulum. This is the concept of the basin of attraction which is the set of initial conditions in phase space that lead the system to a specific attractor. Take for example the case for A  > 0.79  in the regime where the pendulum at its steady state has a circular trajectory with a positive or negative direction. By taking a large sample of initial conditions and recording the direction of the resulting motion after the transient behavior, we obtain figure 4.31.


pict pict

Figure 4.31: Basin of attraction for the forced damped pendulum. The parameters chosen are ω0 = 1.0  , ω = 2.0  , γ = 0.2  and A = 0.85,1.4  .

4.7 Appendix: On the Euler–Verlet Method

Equations (4.11) can be obtained from the Taylor expansion

                          ′     (Δt-)2 ′′     (Δt-)3 ′′′            4
𝜃(t + Δt ) =  𝜃(t) + (Δt)𝜃 (t) +   2!  𝜃 (t) +  3!  𝜃 (t) + 𝒪 ((Δt ) )
                                     2            3
𝜃(t − Δt ) =  𝜃(t) − (Δt)𝜃′(t) + (Δt-)-𝜃′′(t) − (Δt-)-𝜃′′′(t) + 𝒪 ((Δt )4).
                                  2!           3!
By adding and subtracting the above equations we obtain
𝜃(t + Δt) + 𝜃(t − Δt)  =   2𝜃(t) + (Δt )2𝜃′′(t) + 𝒪 ((Δt)4)
                                 ′            3
𝜃(t + Δt) − 𝜃(t − Δt)  =   2(Δt)𝜃 (t) + 𝒪 ((Δt) )          (4.35)
which give equations (4.11)
                                       2              4
𝜃 (t + Δt ) =   2𝜃(t) − 𝜃(t − Δt ) + (Δt ) α(t) + 𝒪((Δt ) )
               𝜃(t + Δt ) − 𝜃(t − Δt)         2
      ω(t) =   --------2(Δt-)------- + 𝒪 ((Δt ))           (4.36)
From the first equation and equations (4.9) we obtain:
𝜃(t + Δt ) = 𝜃(t) + ω (t)(Δt ) + 𝒪 ((Δt )2)
(4.37)

When we perform a numerical integration, we are interested in the total error accumulated after N  − 1  integration steps. In this method, these errors must be studied carefully:

Therefore the total error is 𝒪 ((Δt)2)  .

We also mention the Velocity Verlet method or the Leapfrog method. In this case we use the velocity explicitly:

                       1-     2
𝜃n+1  =   𝜃n + ωnΔt  + 2αn Δt
               1
ωn+12  =   ωn + --αnΔt
               2
ωn+1  =   ωn+ 1+  1αn+1 Δt.                   (4.39)
              2   2
The last step uses the acceleration αn+1   which should depend only on the position 𝜃
 n+1   and not on the velocity.

The Verlet methods are popular in molecular dynamics simulations of many body systems. One of their advantages is that the constraints of the system of particles are easily encoded in the algorithm.

4.8 Appendix: 2nd order Runge–Kutta Method

In this appendix we will show how the choice of the intermediate point 2 in equation (4.17) reduces the error by a power of h  . This choice is special, since by choosing another point (e.g. t = tn + 0.4h  ) the result would have not been the same. Indeed, from the relation

dx                         ∫  tn+1
---= f (t,x) ⇒  xn+1 = xn +        f(t,x)dx.
dt                           tn
(4.40)

By Taylor expanding around the point (tn+1 ∕2,xn+1 ∕2)  we obtain

                                       df
f (t,x) = f (tn+1∕2,xn+1∕2) + (t − tn+1∕2)-(tn+1∕2) + 𝒪 (h2).
                                       dt
(4.41)

Therefore

∫ tn+1
      f(t,x)dx
 tn
                                df        (t − t    )2||tn+1
=  f(tn+1∕2,xn+1∕2)(tn+1 − tn) + --(tn+1∕2)------n+1∕2--||
                                dt              2      tn
  + 𝒪 (h2 )(t    − t )
           n+1    n              {               2               2}
=  f(t     ,x     )h +  df-(t    )   (tn+1-−-tn+1∕2)-−  (tn −-tn+1∕2)
      n+1∕2  n+1∕2     dt  n+1∕2          2                2
        2
  + 𝒪 (h  )h                      {            }
                       df--         h2-  (−-h)2         3
=  f(tn+1∕2,xn+1∕2)h +  dt(tn+1∕2)   2  −   2     +  𝒪(h )
                           3
=  f(tn+1∕2,xn+1∕2)h + 𝒪 (h ).                                  (4.42)
Note that for the vanishing of the 𝒪 (h)  term it is necessary to place the intermediate point at time tn+1∕2   .

This is not a unique choice. This can be most easily seen by a different analysis of the Taylor expansion. Expanding around the point (tn,xn)  we obtain

                         dxn    1            d2xn
xn+1  =   xn + (tn+1 − tn)--- + --(tn+1 − tn)2---2-+ 𝒪 (h3)
                       2  dt    2             dt
      =   x  + hf  + h--dfn + 𝒪 (h3)
           n     n    2 dt
                     h2 ( ∂fn    ∂fn dxn)        3
      =   xn + hfn + ---  ----+  --------  + 𝒪 (h )
                      2 (  ∂t    ∂x  dt)
                     h2-  ∂fn-   ∂fn-          3
      =   xn + hfn +  2    ∂t +  ∂x fn   + 𝒪 (h ),          (4.43)
where we have set fn ≡ f(tn,xn)  , dxdnt ≡  dxdt(xn )  etc. We define
  k1  =   f(tn,xn) = fn
  k2  =   f(tn + ah,xn + bhk1)

xn+1  =   xn + h(c1k1 + c2k2).                 (4.44)
and we will determine the conditions so that the terms 𝒪 (h2)  of the last equation in the error are identical with those of equation (4.43) . By expanding k2   we obtain
k2 =   f(tn + ah,xn +  bhk1)
                            ∂f-                     2
   =   f(tn,xn + bhk1 ) + ha ∂t (tn,xn + bhk1) + 𝒪 (h )
                       ∂f             ∂f               2
   =   f(tn,xn ) + hbk1---(tn,xn ) + ha---(tn,xn) + 𝒪 (h  )
              {        ∂x     }       ∂t
   =   f  + h  a ∂fn-+ bk  ∂fn-  + 𝒪 (h2)
        n         ∂t     1 ∂x
              {  ∂f        ∂f }
   =   fn + h  a --n-+ bfn --n-  + 𝒪 (h2)                  (4.45)
                  ∂t       ∂x
Substituting in (4.44) we obtain
xn+1  =   xn + h(c1k1 + c2k2)
                {                  (                )         }
      =   xn + h  c1fn + c2fn + c2h  a∂fn- + bfn∂fn-  + 𝒪 (h2)
                                       ∂t        ∂x
                             h2 (       ∂fn            ∂fn)
      =   xn + h(c1 + c2)fn + ---  (2c2a)----+  (2c2b)fn ----
                              2          ∂t            ∂x
            + 𝒪 (h3 ).                                           (4.46)
All we need is to choose
c1 + c2  =  1
  2c2a   =  1

   2c2b  =  1.                        (4.47)
The choice c1 = 0  , c2 = 1  , a = b = 1∕2  leads to equation (4.19) . Some other choices in the bibliography are c =  1∕2
 2  and c =  3∕4
 2  .

4.9 Problems

  1. Prove that the total error in the Euler–Cromer method is of order Δt  .
  2. Reproduce the results in figures 4.114.18
  3. Improve your programs so that there is no accumulation of roundoff error in the calculation of time when h is very small for the methods Euler, Euler-Cromer, Euler-Verlet and Runge-Kutta. Repeat the analysis of the previous problem.
  4. Make the appropriate changes in your programs of the Euler, Euler-Cromer, Euler-Verlet and Runge-Kutta methods so that all floating variables change from REAL→ REAL(8). Repeat the analysis of the previous problem.
  5. Compare the results obtained from the Euler, Euler-Cromer, Euler-Verlet, Runge-Kutta methods for the following systems where the analytic solution is known:
    1. Particle falling in a constant gravitational field. Consider the case v(0) = 0  , m =  1  , g = 10  .
    2. Particle falling in a constant gravitational field moving in a fluid from which exerts a force F =  − kv  on the particle. Consider the case v (0 ) = 0  , m  = 1  , g = 10  k = 0.1,1.0,2.0  . Calculate the limiting velocity of the particle numerically and compare the value obtained to the theoretical expectation.
    3. Repeat for the case of a force of resistance of magnitude         2
|F | = kv   .
  6. Consider the damped harmonic oscillator
    d2x     dx
--2-+ γ ---+ ω20x = 0.
dt      dt
    (4.48)

    Take ω0 =  3.145  , γ = 0.5  and calculate its mechanical energy as a function of time. Is it monotonic? Why? (show that d(E ∕m )∕dt = − γv2   ). Repeat for γ = 4,5,6, 7,8  . When is the system oscillating and when it’s not? Calculate numerically the critical value of γ  for which the system passes from a non oscillating to an oscillating regime. Compare your results with the theoretical expectations.

  7. Reproduce the results of figures 4.194.22.
  8. Reproduce the results of figures 4.234.26. Calculate the phase δ(ω )  numerically and compare with equation (4.33) .
  9. Consider a simple model for a swing. Take the damped harmonic oscillator and a driving force which periodically exerts a momentary push with angular frequency ω  . Define “momentary” to be an impulse given by the acceleration a0   by an appropriately small time interval Δt  . The acceleration is 0  for all other times. Calculate the amplitude x0(ω)  for ω0 = 3.145  and γ = 0.5  .
  10. Consider a “half sine” driving force on a damped harmonic oscillator
           (
       {  a0cos ωt  cosωt >  0
a(t) =    0         cosωt ≤  0
       (
    Study the transient behavior of the system for several initial conditions and calculate its steady state motion for ω  = 3.145
 0  and γ = 0.5  . Calculate the amplitude x0(ω )  .
  11. Consider the driving force on a damped oscillator given by
           1-   1-       -2-          -2--
a(t) = π +  2 cos ω + 3π cos 2ωt − 15π cos 4ωt
    Study the transient behavior of the system for several initial conditions and calculate its steady state motion for ω0 = 3.145  and γ = 0.5  . Calculate the amplitude x0(ω )  . Compare your results with those of the previous problem and comment about.
  12. Write a program that simulates N  identical, independent harmonic oscillators. Take N  = 20  and choose random initial conditions for each one of them. Study their trajectories in phase space and check whether they cross each other. Comment on your results.
  13. Place the N =  20  harmonic oscillators of the previous problem in a small square in phase space whose center is at the origin of the axes. Consider the evolution of the system in time. Does the shape of the rectangle change in time? Does the area change in time? Explain...
  14. Repeat the previous problem when each oscillator is damped with γ = 0.5  . Take ω0 =  3.145  .
  15. Consider the forced damped oscillator with ω = 2  , ω0 =  1.0  , γ =  0.2  . Study the transient behavior of the system in the plots of 𝜃(t)  , 𝜃˙(t)  for A =  0.1,0.5,0.79,0.85,1.03,1.4  .
  16. Consider the forced damped pendulum with ω = 2  , ω  = 1.0
  0  , γ = 0.2  and study the phase space trajectories for A  =  0.1, 0.19, 0.21, 0.25, 0.5, 0.71, 0.79, 0.85, 1.02, 1.031, 1.033, 1.05, 1.08, 1.1, 1.4, 1.8, 3.1, 3.5, 3.8, 4.2, 4.42, 4.44, 4.445, 4.447, 4.4488. Consider both the transient behavior and the steady state motion.
  17. Reproduce the results in figures 4.30.
  18. Reproduce the results in figures 4.31.
  19. Consider the forced damped oscillator with
    ω0 = 1,  ω  = 2,  γ =  0.2
    After the transient behavior, the motion of the system for A =  0.60  , A =  0.75  and A  = 0.85  is periodic. Measure the period of the motion with an accuracy of three significant digits and compare it with the natural period of the pendulum and with the period of the driving force. Take as initial conditions the following pairs: (𝜃0, ˙𝜃0) =  (3.1, 0.0)  , (2.5,0.0)  , (2.0,0.0)  , (1.0, 0.0)  , (0.2,0.0)  , (0.0,1.0)  , (0.0, 3.0)  , (0.0,6.0)  . Check if the period is independent of the initial conditions.
  20. Consider the forced damped pendulum with
    ω0 = 1,  ω  = 2,  γ =  0.2
    Study the motion of the pendulum when the amplitude A  takes values in the interval [0.2,5.0]  . Consider specific discrete values of A  by splitting the interval above in subintervals of width equal to δA =  0.002  . For each value of A  , record in a file the value of A  , the angular position and the angular velocity of the pendulum when tk = k π  with k = ktrans,ktrans + 1,ktrans + 2,...,kmax  :
    A     𝜃(t )    ˙𝜃(t )
         k        k
    The choice of ktrans  is made so that the transient behavior will be discarded and study only the steady state of the pendulum. You may take kmax = 500  , ktrans = 400  , ti = 0  , tf = 500 π  , and split the intervals [tk,tk + π]  to 50 subintervals. Choose 𝜃0 = 3.1  , ˙
𝜃0 = 0  .

    1. Construct the bifurcation diagram by plotting the points (A,𝜃(tk))  .
    2. Repeat by plotting the points (A, 𝜃˙(tk))  .
    3. Check whether your results depend on the choice of 𝜃0   , 𝜃˙0   . Repeat your analysis for 𝜃0 = 0  , 𝜃˙0 = 1  .
    4. Study the onset of chaos: Take A ∈ [1.0000,1.0400]  with δA =  0.0001  and A ∈ [4.4300,4.4500]  with δA = 0.0001  and compute with the given accuracy the value Ac  where the system enters into the chaotic behavior regime.
    5. The plot the points (𝜃(tk),𝜃˙(tk))  for A =  1.034,  1.040,  1.080,  1.400,  4.450,  4.600  . Put 2000 points for each value of A  and commend on the strength of the chaotic behavior of the pendulum.

Chapter 5
Planar Motion

In this chapter we will study the motion of a particle moving on the plane under the influence of a dynamical field. Special emphasis will be given to the study of the motion in a central field, like in the problem of planetary motion and scattering. We also study the motion of two or more interacting particles moving on the plane, which requires the solution of a larger number of dynamical equations. These problems can be solved numerically by using Runge–Kutta integration methods, therefore this chapter extends and applies the numerical methods studied in the previous chapter.

5.1 Runge–Kutta for Planar Motion

In two dimensions, the initial value problem that we are interested in, is solving the system of equations (4.6)

dx-=  v         dvx-=  a (t,x, v ,y,v )
dt     x         dt     x      x    y
dy              dvy
dt-=  vy        -dt-=  ay(t,x, vx,y,vy).             (5.1)

The 4th order Runge-Kutta method can be programmed by making small modifications of the program in the file rk.f90. In order to facilitate the study of many different dynamical fields, for each field we put the code of the respective acceleration in a different file. The code which is common for all the forces, namely the user interface and the implementation of the Runge–Kutta method, will be put in the file rk2.f90. The program that computes the acceleration will be put in a file named rk_XXX.f90, where XXX is a string of characters that identifies the force. For example, the file rk2_hoc.f90 contains the program computing the acceleration of the simple harmonic oscillator, the file rk2_g.f90 the acceleration of a constant gravitational field ⃗g = − gˆy  etc.

Different force fields will require the use of one or more coupling constants which need to be accessible to the code in the main program and some subroutines. For this reason, we will provide two variables k1, k2 in a common block:

 real(8) ::        k1,k2  
 common /couplings/k1,k2

This common block will be accessed by the acceleration functions f3 and f4, the function energy and the main program where the user will enter the values of k1 and k2. The initial conditions are stored in the variables X10 ↔  x0   , X20 ↔  y0   , V10 ↔  vx0   , V20 ↔  vy0   , and the values of the functions of time will be stored in the arrays X1(P) ↔  x(t)  , X2(P) ↔  y(t)  , V1(P) ↔  vx(t)  , V2(P) ↔  vy(t)  . The integration is performed by a call to the subroutine

 call RK(T,X1,X2,V1,V2,Ti,Tf,X10,X20,V10,V20,Nt)

The results are written to the file rk2.dat. Each line in this file contains the time, position, velocity and the total mechanical energy, where the energy is calculated by the function energy(t,x1,x2,v1,v2):

 open(unit=11,file=’rk2.dat’)  
 do i=1,Nt  
  write(11,*)T(i),X1(i),X2(i),V1(i),V2(i),&  
      energy(T(i),X1(i),X2(i),V1(i),V2(i))  
 enddo

The code for the function energy, which is different for each force field, is written in the same file with the acceleration. The code for the subroutine RKSTEP(t,x1,x2,x3,x4,dt) should be extended in order to integrate four instead of two functions. The full code is listed below:

!========================================================  
!Program to solve a 4 ODE system using Runge-Kutta Method  
!User must supply derivatives  
!dx1/dt=f1(t,x1,x2,x3,x4) dx2/dt=f2(t,x1,x2,x3,x4)  
!dx3/dt=f3(t,x1,x2,x3,x4) dx4/dt=f4(t,x1,x2,x3,x4)  
!as real(8) functions  
!Output is written in file rk2.dat  
!========================================================  
program rk2_solve  
 implicit none  
 integer,parameter   :: P=1010000  
 real(8),dimension(P):: T,X1,X2,V1,V2  
 real(8) :: Ti,Tf,X10,X20,V10,V20  
 integer :: Nt, i  
 real(8) ::        k1,k2  
 common /couplings/k1,k2  
 real(8) :: energy,E0,EF,DE  
!Input:  
 print *,’Runge-Kutta Method for 4-ODEs Integration’  
 print *,’Enter coupling constants:’  
 read  *, k1,k2  
 print *,’k1= ’,k1,’ k2= ’,k2  
 print *,’Enter Nt,Ti,Tf,X10,X20,V10,V20:’  
 read  *,       Nt,Ti,TF,X10,X20,V10,V20  
 print *,’Nt = ’,Nt  
 print *,’Time: Initial Ti =’,Ti,’ Final Tf=’,Tf  
 print *,’           X1(Ti)=’,X10,’ X2(Ti)=’,X20  
 print *,’           V1(Ti)=’,V10,’ V2(Ti)=’,V20  
!The Calculation:  
 call RK(T,X1,X2,V1,V2,Ti,Tf,X10,X20,V10,V20,Nt)  
!Output:  
 open(unit=11,file=’rk2.dat’)  
 do i=1,Nt  
  write(11,*)T(i),X1(i),X2(i),V1(i),V2(i),&  
      energy(T(i),X1(i),X2(i),V1(i),V2(i))  
 enddo  
 close(11)  
!Rutherford scattering angles:  
 print *,’v-angle: ’,atan2(V2(Nt),V1(Nt))  
 print *,’b-angle: ’,2.0D0*atan(k1/(V10*V10*X20))  
 E0 = energy(Ti   ,X10   ,X20   ,V10   ,V20   )  
 EF = energy(T(Nt),X1(Nt),X2(Nt),V1(Nt),V2(Nt))  
 DE = ABS(0.5D0*(EF-E0)/(EF+E0))  
 print *,’E0,EF, DE/E= ’,E0,EF,DE  
end program rk2_solve  
!========================================================  
!The velocity functions f1,f2(t,x1,x2,v1,v2)  
!========================================================  
real(8) function f1(t,x1,x2,v1,v2)  
 implicit none  
 real(8) :: t,x1,x2,v1,v2  
 f1=v1           !dx1/dt= v1  
end function f1  
!--------------------------------------------------------  
real(8) function f2(t,x1,x2,v1,v2)  
 implicit none  
 real(8) :: t,x1,x2,v1,v2  
 f2=v2           !dx2/dt= v2  
end function f2  
!========================================================  
!RK(T,X1,X2,V1,V2,Ti,Tf,X10,X20,V10,V20,Nt) is the driver  
!for the Runge-Kutta integration routine RKSTEP  
!Input: Initial and final times Ti,Tf  
!       Initial values at t=Ti  X10,X20,V10,V20  
!       Number of steps of integration: Nt-1  
!       Size of arrays T,X1,X2,V1,V2  
!Output: real arrays T(Nt),X1(Nt),X2(Nt),  
!                          V1(Nt),V2(Nt) where  
!T(1) = Ti X1(1) = X10 X2(1) = X20 V1(1) = V10 V2(1) = V20  
!          X1(k) = X1(at t=T(k)) X2(k) = X2(at t=T(k))  
!          V1(k) = V1(at t=T(k)) V2(k) = V2(at t=T(k))  
!T(Nt)= Tf  
!========================================================  
subroutine RK(T,X1,X2,V1,V2,Ti,Tf,X10,X20,V10,V20,Nt)  
 implicit none  
 integer :: Nt  
 real(8),dimension(Nt)::T,X1,X2,V1,V2  
 real(8) :: Ti ,Tf  
 real(8) :: X10,X20  
 real(8) :: V10,V20  
 real(8) :: dt  
 real(8) :: TS,X1S,X2S !values of time and X1,X2 at given step  
 real(8) ::    V1S,V2S  
 integer :: i  
!Initialize variables:  
 dt     = (Tf-Ti)/(Nt-1)  
 T (1)  = Ti  
 X1(1)  = X10; X2(1) = X20  
 V1(1)  = V10; V2(1) = V20  
 TS     = Ti  
 X1S    = X10; X2S   = X20  
 V1S    = V10; V2S   = V20  
!Make RK steps: The arguments of RKSTEP are  
!replaced with the new ones  
 do i=2,Nt  
  call RKSTEP(TS,X1S,X2S,V1S,V2S,dt)  
  T(i)  = TS  
  X1(i) = X1S; X2(i) = X2S  
  V1(i) = V1S; V2(i) = V2S  
 enddo  
end subroutine RK  
!========================================================  
!Subroutine RKSTEP(t,x1,x2,dt)  
!Runge-Kutta Integration routine of ODE  
!dx1/dt=f1(t,x1,x2,x3,x4) dx2/dt=f2(t,x1,x2,x3,x4)  
!dx3/dt=f3(t,x1,x2,x3,x4) dx4/dt=f4(t,x1,x2,x3,x4)  
!User must supply derivative functions:  
!real function f1(t,x1,x2,x3,x4)  
!real function f2(t,x1,x2,x3,x4)  
!real function f3(t,x1,x2,x3,x4)  
!real function f4(t,x1,x2,x3,x4)  
!Given initial point (t,x1,x2) the routine advances it  
!by time dt.  
!Input : Inital time t    and function values x1,x2,x3,x4  
!Output: Final  time t+dt and function values x1,x2,x3,x4  
!Careful!: values of t,x1,x2,x3,x4 are overwritten...  
!========================================================  
subroutine RKSTEP(t,x1,x2,x3,x4,dt)  
 implicit none  
 real(8) :: t,x1,x2,x3,x4,dt  
 real(8) :: f1,f2,f3,f4  
 real(8) :: k11,k12,k13,k14,k21,k22,k23,k24  
 real(8) :: k31,k32,k33,k34,k41,k42,k43,k44  
 real(8) :: h,h2,h6  
 
 h =dt       !h =dt, integration step  
 h2=0.5D0*h  !h2=h/2  
 h6=h/6.0D0  !h6=h/6  
 
 k11=f1(t,x1,x2,x3,x4)  
 k21=f2(t,x1,x2,x3,x4)  
 k31=f3(t,x1,x2,x3,x4)  
 k41=f4(t,x1,x2,x3,x4)  
 
 k12=f1(t+h2,x1+h2*k11,x2+h2*k21,x3+h2*k31,x4+h2*k41)  
 k22=f2(t+h2,x1+h2*k11,x2+h2*k21,x3+h2*k31,x4+h2*k41)  
 k32=f3(t+h2,x1+h2*k11,x2+h2*k21,x3+h2*k31,x4+h2*k41)  
 k42=f4(t+h2,x1+h2*k11,x2+h2*k21,x3+h2*k31,x4+h2*k41)  
 
 k13=f1(t+h2,x1+h2*k12,x2+h2*k22,x3+h2*k32,x4+h2*k42)  
 k23=f2(t+h2,x1+h2*k12,x2+h2*k22,x3+h2*k32,x4+h2*k42)  
 k33=f3(t+h2,x1+h2*k12,x2+h2*k22,x3+h2*k32,x4+h2*k42)  
 k43=f4(t+h2,x1+h2*k12,x2+h2*k22,x3+h2*k32,x4+h2*k42)  
 
 k14=f1(t+h ,x1+h *k13,x2+h *k23,x3+h *k33,x4+h *k43)  
 k24=f2(t+h ,x1+h *k13,x2+h *k23,x3+h *k33,x4+h *k43)  
 k34=f3(t+h ,x1+h *k13,x2+h *k23,x3+h *k33,x4+h *k43)  
 k44=f4(t+h ,x1+h *k13,x2+h *k23,x3+h *k33,x4+h *k43)  
 
 t =t+h  
 x1=x1+h6*(k11+2.0D0*(k12+k13)+k14)  
 x2=x2+h6*(k21+2.0D0*(k22+k23)+k24)  
 x3=x3+h6*(k31+2.0D0*(k32+k33)+k34)  
 x4=x4+h6*(k41+2.0D0*(k42+k43)+k44)  
 
end subroutine RKSTEP

5.2 Projectile Motion

Consider a particle in the constant gravitational field near the surface of the earth which moves with constant acceleration ⃗g = − gˆy  so that

                                          1  2
x(t)  =  x0 + v0xt ,  y(t)  =  y0 + v0yt − 2gt
vx(t) =  v0x       ,  vy(t) =  v0y − gt
ax(t) =  0         ,  ay(t) =  − g
(5.2)

The particle moves on a parabolic trajectory that depends on the initial conditions

             (    )
(y − y0)  =     v0y  (x − x0) − 1-g- (x −  x0)2
               v0x             2v20x
                             tan2 𝜃
         =   tan𝜃 (x − x0 ) − -----(x − x0)2,           (5.3)
                             4hmax
where tan 𝜃 = v0y∕v0x  is the direction of the initial velocity and hmax   is the maximum height of the trajectory.

pict pict
pict pict

Figure 5.1: Plots of x(t)  , y(t)  , vx(t)  , vy(t)  for a projectile fired in a constant gravitational field ⃗g = − 10.0ˆy  with initial velocity ⃗v0 = ˆx + ˆy  .


pict pict

Figure 5.2: (Left) The parabolic trajectory of a projectile fired in a constant gravitational field ⃗g = − 10.0ˆy  with initial velocity ⃗v0 = ˆx+ ˆy  . (Right) The deviation of the projectile’s energy from its initial value is due to numerical errors.

The acceleration ax(t) = 0  ay(t) = − g  (ax ↔ f3 , ay ↔ f4) and the mechanical energy is coded in the file rk2_g.f90:

!========================================================  
!The acceleration functions f3,f4(t,x1,x2,v1,v2) provided  
!by the user  
!========================================================  
!Free fall in constant gravitational filed with  
!g = -k2  
real(8) function f3(t,x1,x2,v1,v2)  
 implicit none  
 real(8) :: t,x1,x2,v1,v2  
 real(8) ::         k1,k2  
 common  /couplings/k1,k2  
 f3=0.0D0    !dx3/dt=dv1/dt=a1  
end function f3  
!--------------------------------------------------------  
real(8) function f4(t,x1,x2,v1,v2)  
 implicit none  
 real(8) :: t,x1,x2,v1,v2  
 real(8) ::         k1,k2  
 common  /couplings/k1,k2  
 f4=-k1   !dx4/dt=dv2/dt=a2  
end function f4  
!--------------------------------------------------------  
real(8) function energy(t,x1,x2,v1,v2)  
 implicit none  
 real(8) :: t,x1,x2,v1,v2  
 real(8) ::         k1,k2  
 common  /couplings/k1,k2  
 energy = 0.5D0*(v1*v1+v2*v2) + k1*x2  
end function energy

In order to calculate a projectile’s trajectory you may use the following commands:

> gfortran -O2 rk2.f90 rk2_g.f90 -o rk2  
> ./rk2  
 Runge-Kutta Method for 4-ODEs Integration  
 Enter coupling constants:  
10.0 0.0  
 k1=    10.000000       k2=    0.000000  
 Enter Nt,Ti,Tf,X10,X20,V10,V20:  
20000 0.0 0.2 0.0 0.0 1.0 1.0  
 Nt=        20000  
 Time: Initial Ti =   0.000000     Final Tf=   0.200000  
            X1(Ti)=   0.000000       X2(Ti)=   0.000000  
            V1(Ti)=   1.000000       V2(Ti)=   1.000000

The analysis of the results contained in the file rk2.dat can be done using gnuplot:

gnuplot> set terminal x11 1  
gnuplot> plot "rk2.dat" using 1:2 with lines title "x(t)"  
gnuplot> set terminal x11 2  
gnuplot> plot "rk2.dat" using 1:3 with lines title "y(t)"  
gnuplot> set terminal x11 3  
gnuplot> plot "rk2.dat" using 1:4 with lines title "vx(t)"  
gnuplot> set terminal x11 4  
gnuplot> plot "rk2.dat" using 1:5 with lines title "vy(t)"  
gnuplot> set terminal x11 5  
gnuplot> plot "rk2.dat" using 1:($6-1.0) w lines t "E(t)-(0)"  
gnuplot> set terminal x11 6  
gnuplot> set size square  
gnuplot> set title "Trajectory"  
gnuplot> plot "rk2.dat" using 2:3 with lines notit

The results can be seen in figures 5.1 and 5.2. We note a small increase in the mechanical energy which is due to the accumulation of numerical errors.

We can animate the trajectory by writing a script of gnuplot commands in a file rk2_animate.gpl

icount = icount+skip  
plot  "<cat -n rk2.dat"  \  
  using 3:($1<= icount ? $4: 1/0) with lines notitle  
# pause 1  
if(icount < nlines ) reread

Before calling the script, the user must set the values of the variables icount, skip and nlines. Each time gnuplot reads the script, it plots icount number of lines from rk2.dat. Then the script is read again and a new plot is made with skip lines more than the previous one, unless icount < nlines. The plotted “file” "<cat -n rk2.dat" is the standard output (stdout) of the command cat -n rk2.dat which prints to the stdout the contents of the file rk2.dat line by line, together with the line number. Therefore the plot command reads data which are the line number, the time, the coordinate x  , the coordinate y  etc. The keyword using in

  using 3:($1<= icount ? $4: 1/0)

instructs the plot command to use the 3rd column on the horizontal axis and if the first column is less than icount ($1<= icount) put on the vertical axis the value of the 4th column if the first column is less than icount. Otherwise ($1 > icount) it prints an undefined number (1/0) which makes gnuplot print nothing at all. You may also uncomment the command pause if you want to make the animation slower. In order to run the script from gnuplot, issue the commands

gnuplot> icount = 10  
gnuplot> skip   = 200  
gnuplot> nlines = 20000  
gnuplot> load "rk2_animate.gpl"

The scripts shown above can be found in the accompanying software. More scripts can be found there that automate many of the boring procedures. The usage of two of these is explained below. The first one is in the file rk2_animate.csh:

> ./rk2_animate.csh -h  
Usage: rk2_animate.csh -t [sleep time] -d [skip points] <file>  
Default file is rk2.dat  
Other options:  
   -x: set lower value in xrange  
   -X: set lower value in xrange  
   -y: set lower value in yrange  
   -Y: set lower value in yrange  
   -r: automatic determination of x-y range  
> ./rk2_animate.csh -r -d 500 rk2.dat

The last line is a command that animates a trajectory read from the file rk2.dat. Each animation frame contains 500 more points than the previous one. The option -r calculates the plot range automatically. The option -h prints a short help message.

A more useful script is in the file rk2.csh.

> ./rk2.csh -h  
Usage: rk2.csh -f <force> k1 k2 x10 x20 v10 v20 STEPS t0 tf  
Other Options:  
 -n Do not animate trajectory  
Available forces (value of <force>):  
1: ax=-k1             ay= -k2 y           Harmonic oscillator  
2: ax= 0              ay= -k1             Free fall  
3: ax= -k2     vx     ay= -k2    vy - k1  Free fall + \  
                                          air resistance ~ v  
4: ax= -k2 |v| vx     ay= -k2 |v|vy - k1  Free fall + \  
                                          air resistance ~ v^2  
5: ax= k1*x1/r^3      ay= k1*x2/r^3       Coulomb Force  
....

The option -h prints operating instructions. A menu of forces is available, and a choice can be made using the option -f. The rest of the command line consists of the parameters read by the program in rk2.f90, i.e. the coupling constants k1, k2, the initial conditions x10, x20, v10, v20 and the integration parameters STEPS, t0 and tf. For example, the commands

[literate={-}{{\texttt{-}}}1]  
> rk2.csh -f 2 -- 10.0 0.0 0.0 0.0 1.0 1.0 20000 0.0 0.2  
> rk2.csh -f 1 -- 16.0 1.0 0.0 1.0 1.0 0.0 20000 0.0 6.29  
> rk2.csh -f 5 -- 10.0 0.0 -10 0.2 10. 0.0 20000 0.0 3.00

compute the trajectory of a particle in the constant gravitational field discussed above, the trajectory of an anisotropic harmonic oscillator (k1 = ax =  − ω21x  , k2 = ay = − ω2y
        2  ) and the scattering of a particle in a Coulomb field – try them! I hope that you will have enough curiosity to look “under the hood” of the scripts and try to modify them or create new ones. Some advise to the lazy guys: If you need to program your own force field follow the recipe: Write the code of your acceleration field in a file named e.g. rk2_myforce.f90 as we did with rk2_g.f90. Edit the file rk2.csh and modify the line

set forcecode = (hoc g vg v2g cb)

to

set forcecode = (hoc g vg v2g cb myforce)

(the variable $forcecode may have more entries than the ones shown above). Count the order of the string myforce, which is 6 in our case. In order to access this force field from the command line, use the option -f 6:

> rk2.csh -f 6 -- .......

Now, we will study the effect of the air resistance on the motion of the projectile. For small velocities this is a force proportional to the velocity F⃗r =  − mk ⃗v  , therefore

ax  =   − kvx
ay  =   − kvy − g.                        (5.4)
By taking
               v0x (       )
 x(t)  =   x0 + ---  1 − e−kt
                k(       ) (        )
 y(t)  =   y0 + 1- v0y + g-  1 − e−kt  − g-t
               k        k               k
vx(t)  =   v0xe−kt
          (      g )       g
vy(t)  =    v0y + -- e− kt − --,                        (5.5)
                 k         k
we obtain the motion of a particle with terminal velocity vy(+∞  ) = − g ∕k  (x(+ ∞ ) =  const., y(+ ∞ ) ∼ t  ).

The acceleration caused by the air resistance is programmed in the file (k1 ↔  g  , k2 ↔ k  ) rk2_vg.f90:

!========================================================  
!The acceleration functions f3,f4(t,x1,x2,v1,v2) provided  
!by the user  
!========================================================  
!Free fall in constant gravitational filed with  
!ax = -k2 vx    ay = -k2 vy - k1  
real(8) function f3(t,x1,x2,v1,v2)  
 implicit none  
 real(8) :: t,x1,x2,v1,v2  
 real(8) ::         k1,k2  
 common  /couplings/k1,k2  
 f3=-k2*v1   !dx3/dt=dv1/dt=a1  
end function f3  
!--------------------------------------------------------  
real(8) function f4(t,x1,x2,v1,v2)  
 implicit none  
 real(8) :: t,x1,x2,v1,v2  
 real(8) ::         k1,k2  
 common  /couplings/k1,k2  
 f4=-k2*v2-k1   !dx4/dt=dv2/dt=a2  
end function f4

The results are shown in figure 5.3 where we see the effect of an increasing air resistance on the particle trajectory. The effect of a resistance force of the form F⃗r =  − mkv2 ˆv  is shown in figure 5.4.


pict pict

Figure 5.3: The trajectory of a projectile moving in a constant gravitational field ⃗g = − 10ˆy  with air resistance causing acceleration ⃗ar = − k⃗v  for k = 0,0.2,1,5,10,20,30  . The left plot has ⃗v(0) = ˆx + ˆy  and the right plot has ⃗v(0) = 5ˆx + 5ˆy  .


pict pict

Figure 5.4: The trajectory of a projectile moving in a constant gravitational field ⃗g = − 10yˆ  with air resistance causing acceleration ⃗ar = − kv2ˆv  for k = 0,0.2,1,5,10,20,30  . The left plot has ⃗v(0) = ˆx+ ˆy  and the right plot has ⃗v(0) = 5ˆx +5yˆ  .

5.3 Planetary Motion

Consider the simple planetary model of a “sun” of mass M  and a planet “earth” at distance r  from the sun and mass m  such that m  ≪  M  . According to Newton’s law of gravity, the earth’s acceleration is

           GM        GM
⃗a = ⃗g = − --2-ˆr = − --3--⃗r,
            r         r
(5.6)

where              − 11--m3----
G = 6.67 × 10    kgr⋅sec2   ,               30
M  = 1.99 × 10  kgr  ,               24
m  = 5.99 × 10  kgr  . When the hypothesis m ≪  M  is not valid, the two body problem is reduced to that of the one body problem with the mass replaced by the reduced mass μ

1-= -1 + -1-.
μ   m    M
The force of gravity is a central force. This implies conservation of the angular momentum ⃗
L = ⃗r × ⃗p  with respect to the center of the force, which in turn implies that the motion is confined on one plane. We choose the z  axis so that
⃗L = Lz ˆk = m (xvy − yvx)ˆk.
(5.7)

The force of gravity is conservative and the mechanical energy

E  = 1-mv2 −  GmM----
     2          r
(5.8)

is conserved. If we choose the origin of the coordinate axes to be the center of the force, the equations of motion (5.6) become

          GM
ax  =   − --3-x
           r
a   =   − GM--y,                         (5.9)
 y         r3
where r2 = x2 + y2   . This is a system of two coupled differential equations for the functions x(t)  , y(t)  . The trajectories are conic sections which are either an ellipse (bound states - “planet”), a parabola (e.g. escape to infinity when the particle starts moving with speed equal to the escape velocity) or a hyperbola (e.g. scattering).

Kepler’s third law of planetary motion states that the orbital period T  of a planet satisfies the equation

  2    4π2  3
T   = GM---a ,
(5.10)

where a  is the semi-major axis o