Contents
Preface
At present, friendly, yet, powerful tools have been developed to program equally powerful computers. The programming language Python, one such tool, is simple to learn, but very powerful. It is being used in wide range of applications: scientific computing, data analysis, machine learning (ML) and artificial intelligence (AI), internet programming, GUI, etc. Consequently, researchers are employing Python for numerical computing, as well as AI and ML.
Keeping this in mind, I chose Python as the programming language for teaching numerical computing in my Computational Physics course (PHY473A). We also use Python for writing large softwares including parallel computing and post processing. The present book is a compilation of the course material and the tools developed in our computational laboratory. The contents and the usage of the book is discussed below.
Contents of the book: The book is in two parts. The first part covers the Python programming language in a reasonable detail, while the second part contains the numerical algorithms and associated Python codes. The book has discussion on all the numerical tools: interpolation, integration, differentiation, solvers for ordinary and partial differential equations, Fourier transforms, boundary value problems, linear algebra, and Monte Carlo methods. In addition, I also include plotting tools, error analysis, nondimensionalization, and an overview of computer hardware and software.
The computer programs in the book have been tested. For convenience, the codes are available at the website https://sites.google.com/view/py-comp. The website will also host affiliated material, such as PPTs, video lectures, color figures, etc.
Usage of the book: This book is suitable for advanced undergraduate and graduate students. It does not require any previous training in programming, but it does require basic understanding of calculus and differential equations. The material could be covered in 40 lectures at a fast pace.However, I recommend that the instructor and students can skip topics that they find complex and somewhat unnecessary.
Programming is learnt by practice. Hence, I strongly urge the students to program enthusiastically, including the examples and exercise of the book. In my course, we used Prutor for evaluating the exercises submitted by the students. We plan to provide access to the exercises of the course on Prutor (https://prutor.ai/mkvatiitkanpur/#).
Acknowledgements: For the significant contents of the book, I am grateful to the course TAs (teaching assistants), especially Soumyadeep Chatterjee, Abhishek Kumar, Manohar Sharma. Special thanks to PHY473As enthusiastic students, whose deep questions led to clarity in the presentation in the book. I also thank Mani Chandra for introducing me to Python way back in 2008, and to other students of Simulation and Modelling Laboratory, where coding is done for fun.
Next, thanks to Python creator, Guido van Rossum, and many developers for creating such a wonderful programming environment, that too for free! I am grateful to Anancoda for the free Python distribution, and to the creators of Scrivener that made writing this book a joy. The front cover was designed using Canva software. I gratefully acknowledge Wikipedia and Wikieducator for Figures 3, 4, 5, 33, and 138 of the book.
I am also thankful to my friends, Rahul Garg, Prachi Garg, Harshawardhan Wanare, Rajesh Ranjan, Anurag Gupta for the encouragement, and Manmohan Dewbanshi for help during the writing of this book. Finally, special thanks to Nitin Soni and his publication team at Adhyyan Books for the assistance in publication of this book.
Feedback request: I request the readers to send their comments and feedback to me at . Even though I have tried hard to make this book free of errors, I am sure some lacuna still remain. I will be grateful if such errors are brought to my attention.
Mahendra Verma
IIT Kanpur
CHAPTER ONE
Introduction
INTRODUCTION TO COMPUTING
COMPUTER IS ONE device that has impacted all walks of life. It is employed for scientific research, e-commerce, banking, cloud computing, etc. On a personal level, we use computers for surfing internet, emailing, social networking, etc. See Section Applications of Computers for some of the computer applications.
ENIAC ( Electronic Numerical Integrator and Computer , made in 1945) was the first general-purpose digital computer. It was used for making Hydrogen bomb and Monte Carlo simulations. A succession of better and faster computers have been built since then. Scientists and engineers have used these machines to research in physics, chemistry, biology, fluids, engineering, geo- and astrophysics, etc. In this book, we will focus on generic computation tools used in science and engineering.
Both hardware and software have evolved leaps and bounds over the years. In early days, Fortran , short form for Formula Translation , was the de facto programming language for scientific applications. Even though Fortran is still a dominant language in scientific programming, many scientists and engineers have moved to modern languages such as C, C++, and Python. Among these languages, Python has become very popular due to its simplicity and availability of large number of Python modules. In this book, we will use Python language for writing programs. An added benefit of Python is that it has fantastic post-processing tools, such as plotting, reading/writing data, etc. In addition, Python is widely used in machine learning (ML) and artificial intelligence (AI). Python is also becoming popular for high-performance computing (HPC). However, in this book we will not cover ML, AI, and HPC.
Traditionally, a scientist was either an experimentalist or a theorists . The former primarily work on experiments, while the latter on theoretical modelling. However, after the prominence of computers, a new category of researchers called computational scientists have emerged. These researchers excel in designing computers and writing large softwares. Another important point to note is that computers are often used to simulate physical systems, e.g., Earths atmosphere, stars, galaxies, large molecules, automobiles, etc. Thus, computers perform virtual experiments. Scientists strive to make accurate models of physical systems using the inputs from experiments and/or computer simulations. Thus, computers provide an interesting and powerful window of opportunity for understanding the world.
Often, experiments and computer simulations complement each other. For example, hydrodynamic experiments can achieve very high Reynolds numbers, but computer simulations can reach only moderate Reynolds numbers (10). However, computer simulations can probe velocity field at any point of the flow, which may not be possible in many experiments (e.g., in an opaque liquid). Also, it is impossible to perform experiments on a star, but we can quite comfortably simulate stars on a computer, at least, approximately.
This book is ideally suited for advanced undergraduate and early graduate students. Part-I of the book includes discussions on Python language. In this part, I also cover error analysis and basics of computer hardware, whose knowledge is required for estimating memory and time complexity of a computational problem. Part-II of the book covers introduction to computational methodsnumerical interpolation; integration; differentiation; solvers of ordinary and partial differential-equations; Fourier transforms; linear algebra; Monte Carlo methods; etc. I have attempted to present these topics coherently by highlighting connections among them. For example, the formulas of numerical integration are derived using Lagrange interpolation.
I hope you will enjoy learning these tools.