Programming for Computations - Python: A Gentle Introduction to Numerical Simulations with Python

Svein Ohm Linge, Hans Petter Langtangen · OAPEN (The OAPEN Foundation) · 2018

Target Audience and Background Knowledge This book was written for students, teachers, engineers, and scientists who know nothing about programming and numerical methods from before but who seek a minimum of the fundamental skills required to get started with programming as a tool for solving scientific and engineering problems.Some knowledge of one-and multivariable calculus is assumed.The basic programming concepts are presented in Chaps.1-5 (about 150 pages), before practical applications of these concepts are demonstrated in important mathematical subjects addressed in the remaining parts of the book (Chaps.6-9).Each chapter is followed by a set of exercises that covers a wide range of application areas, e.g., biology, geology, statistics, physics, and mathematics.The exercises were particularly designed to bring across important points from the text.Learning the very basics of programming should not take long, but as with any other craft, mastering the skill requires continued and extensive practice.Some beginning practice is gained through Chaps.6-9, but the authors strongly emphasize that this is only a start.Students should continue to practice programming in subsequent courses, while those who exercise self-study should keep up the learning process through continued application of the craft.The book is a good starting point when teaching computer programming as an integrated part of standard university courses in mathematics and natural science.In our experience, such an integration is doable and indeed rewarding.Numerical Methods An overall goal with this book is to motivate computer programming as a very powerful tool for doing mathematics.All examples are related to mathematics and its use in engineering and science.However, to solve mathematical problems through computer programming, we need numerical methods.Explaining basic numerical methods is therefore an integral part of the book.Our choice of topics is governed by what is most needed in science and engineering, as well as in the teaching of applied natural science courses.Mathematical models are then central, with differential equations constituting the most frequent type of models.Consequently, the numerical focus in this book is on differential equations.As soft pedagogical starters for the programming of mathematics, we have chosen the topics of numerical integration and root finding.We remark that the book is deliberately brief on numerical methods.This is because our focus is on implementing numerical algorithms, and to develop reliable, working programs, the programmer must be confident about the basic ideas of the numerical approximations involved.The Computer Language: Python We have chosen to use the programming language Python, because this language gives a very compact and readable code that closely resembles the mathematical recipe for solving the problem at hand.Python also has a gentle learning curve.Other computer languages, like Fortran, C, and C++, have a strong position in science and engineering.During the last two decades, however, there has been a significant shift in popularity from these compiled languages to more high-level and easier-to-read languages, for instance, MATLAB, Python, R, Maple, Mathematica, and IDL.This latter class of languages is computationally less efficient but superior with respect to overall human problem-solving efficiency.This book emphasizesx Preface how to think like a programmer, rather than focusing on technical language details.Thus, the book should put the reader in a good position for learning other programming languages later, including the classic ones: Fortran, C, and C++. How This Book Is DifferentThere are numerous texts on computer programming and numerical methods, so how does the present one differ from the existing literature?Compared to standard books on numerical methods, our book has a much stronger emphasis on the craft of programming and on verification.We want to give students a thorough understanding of how to think about programming as a problemsolving method and how to provide convincing evidence for program correctness.Even though there are lots of books on numerical methods where many algorithms have a corresponding computer implementation (see, e.g., [1, 3-6, 10, 15-17, 20, 23, 25, 27-31]-the latter two apply Python), it is often assumed that the reader "can program" beforehand.The present book teaches the craft of structured programming along with the fundamental ideas of numerical methods.In this book, unit testing and corresponding test functions are introduced early on.We also put much emphasis on coding algorithms as functions, as opposed to "flat programs," which often dominate in the literature and among practitioners.Functions are reusable because they utilize the general formulation of a mathematical algorithm such that it becomes applicable to a large class of problems.There are also numerous books on computer programming, but not many that really emphasize how to think about programming in the context of numerical methods and scientific applications.One such book is [11], which gives a comprehensive introduction to Python programming and the thinking about programming as a computer scientist.Sometimes, however, one needs a text like the present one.It does not go so deep into language-specific details, but rather targets the shortest path to reliable mathematical problem-solving through programming.With this attitude in mind, a lot of topics were left out of the present book, simply because they were not strictly needed in the mathematical problem-solving process.Examples of such topics are object-oriented programming and Python dictionaries (of which the latter omission is possibly subject to more debate).If you find the present book too shallow, [11] might be the right choice for you.That source should also work nicely as a more in-depth successor of the present text.Whenever the need for a structured introduction to programming arises in science and engineering courses, the present book may be your option, either for self-study or for use in organized teaching.The thinking, habits, and practice covered herein will put readers in a firm position for utilizing and understanding the power of computers for problem-solving in science and engineering. Changes to the First EditionPreface xi chapters.Explanations are now more complete, previous examples have been modified, new examples have been added, and more.In particular, the importing of code is now elaborated on in a greater detail, so is the making of modules.Also, Sect.4.2 is new, illustrating the important stepwise strategy of code writing through a dedicated example.The five first chapters now cover about 150 pages that explain, in a brief and simple manner, all the code basics required to follow the remaining parts of the book.3. The new Chap.6 (Computing Integrals and Testing Code) and Chap.7 (Solving Nonlinear Algebraic Equations) are seen as gentle first applications of programming to problem-solving in mathematics.Both these chapters now precede the mathematically more challenging Chaps.8 and 9, which treat basic numerical solving of ODEs and PDEs, respectively (the chapter Solving Nonlinear Algebraic Equations was, in the first edition, the final chapter of the book, but it seems more appropriate to let it act as a "warm-up" chapter, together with the new Chap.6, for the two final chapters on differential equation solving).4. Section 8.1 (Filling a Water Tank: Two Cases) is new, particularly written for readers who lack experience with differential equations.5. Section 8.5 (Rate of Convergence) is new, explaining convergence rate related to differential equations.6.New exercises have been added, e.g., on Fibonacci numbers, the Leapfrog method, Adams-Bashforth methods, and more.7. Errors and typos have been corrected, and many explanations have been reformulated throughout.

Read the paper · More papers on PaperTik