1st Edition

# Introduction to Numerical Programming A Practical Guide for Scientists and Engineers Using Python and C/C++

**Also available as eBook on:**

Makes Numerical Programming More Accessible to a Wider Audience

Bearing in mind the evolution of modern programming, most specifically emergent programming languages that reflect modern practice, Numerical Programming: A Practical Guide for Scientists and Engineers Using Python and C/C++ utilizes the author’s many years of practical research and teaching experience to offer a systematic approach to relevant programming concepts. Adopting a practical, broad appeal, this user-friendly book offers **guidance to anyone interested in using numerical programming to solve science and engineering problems.** **Emphasizing methods generally used in physics and engineering—from elementary methods to complex algorithms—it gradually incorporates algorithmic elements with increasing complexity.**

Develop a Combination of Theoretical Knowledge, Efficient Analysis Skills, and Code Design Know-How

The book encourages algorithmic thinking, which is essential to numerical analysis. Establishing the fundamental numerical methods, application numerical behavior and graphical output needed to foster algorithmic reasoning, coding dexterity, and a scientific programming style, it enables readers to successfully navigate relevant algorithms, understand coding design, and develop efficient programming skills. The book incorporates real code, and includes examples and problem sets to assist in hands-on learning.

- Begins with an overview on approximate numbers and programming in Python and C/C++, followed by discussion of basic sorting and indexing methods, as well as portable graphic functionality
- Contains methods for function evaluation, solving algebraic and transcendental equations, systems of linear algebraic equations, ordinary differential equations, and eigenvalue problems
- Addresses approximation of tabulated functions, regression, integration of one- and multi-dimensional functions by classical and Gaussian quadratures, Monte Carlo integration techniques, generation of random variables, discretization methods for ordinary and partial differential equations, and stability analysis

This text introduces platform-independent numerical programming using Python and C/C++, and appeals to advanced undergraduate and graduate students in natural sciences and engineering, researchers involved in scientific computing, and engineers carrying out applicative calculations.

**Approximate Numbers**

Sources of Errors in Numerical Calculations

Absolute and Relative Errors

Representation of Numbers

Significant Digits

Errors of Elementary Operations

References and Suggested Further Reading

Basic Programming Techniques

Programming Concepts

Functions and Parameters

Passing Arguments to Python Functions

Passing Arguments to C/C++ Functions

Arrays in Python

Dynamic Array Allocation in C/C++

Basic Matrix Operations

References and Suggested Further Reading

Elements of Scientific Graphics

The Tkinter Package

The Canvas Widget

Simple Tkinter Applications

Plotting Functions of One Variable

Graphics Library graphlib.py

Creating Plots in C++ Using the Library graphlib.py

References and Suggested Further Reading

Sorting and Indexing

Introduction

Bubble Sort

Insertion Sort

Quicksort

Indexing and Ranking

Implementations in C/C++

Problems

References and Suggested Further Reading

Evaluation of Functions

Evaluation of Polynomials by Horner’s Scheme

Evaluation of Analytic Functions

Continued Fractions

Orthogonal Polynomials

Spherical Harmonics Associated Legendre Functions

Spherical Bessel Functions

Implementations in C/C++

Problems

References and Suggested Further Reading

Algebraic and Transcendental Equations

Root Separation

Bisection Method

Method of False Position

Method of Successive Approximations

Newton’s Method

Secant Method

Birge–Vieta Method

Newton’s Method for Systems of Nonlinear Equations

Implementations in C/C++

Problems

References and Suggested Further Reading

Systems of Linear Equations

Introduction

Gaussian Elimination with Backward Substitution

Gauss–Jordan Elimination

LU Factorization

Inversion of Triangular Matrices

Cholesky Factorization

Tridiagonal Systems of Linear Equations

Block Tridiagonal Systems of Linear Equations

Complex Matrix Equations

Jacobi and Gauss–Seidel Iterative Methods

Implementations in C/C++

Problems

References and Suggested Further Reading

Eigenvalue Problems

Introduction

Diagonalization of Matrices by Similarity Transformations

Jacobi Method

Generalized Eigenvalue Problems for Symmetric Matrices

Implementations in C/C++

Problems

References and Suggested Further Reading

Modeling of Tabulated Functions

Interpolation and Regression

Lagrange Interpolation Polynomial

Neville’s Interpolation Method

Cubic Spline Interpolation

Linear Regression

Multilinear Regression Models

Nonlinear Regression: The Levenberg–Marquardt Method

Implementations in C/C++

Problems

References and Suggested Further Reading

Integration of Functions

Introduction

Trapezoidal Rule; A Heuristic Approach

The Newton–Cotes Quadrature Formulas

Trapezoidal Rule

Simpson’s Rule

Adaptive Quadrature Methods

Romberg’s Method

Improper Integrals: Open Formulas

Midpoint Rule

Gaussian Quadratures

Multidimensional Integration

Adaptive Multidimensional Integration

Implementations in C/C++

Problems

References and Suggested Further Reading

Monte Carlo Method

Introduction

Integration of Functions

Importance Sampling

Multidimensional Integrals

Generation of Random Numbers

Implementations in C/C++

Problems

References and Suggested Further Reading

Ordinary Differential Equations

Introduction

Taylor Series Method

Euler’s Method

Runge–Kutta Methods

Adaptive Step Size Control

Methods for Second-Order ODEs

Numerov’s Method

Shooting Methods for Two-Point Problems

Finite-Difference Methods for Linear Two-Point Problems

Implementations in C/C++

Problems

References and Suggested Further Reading

Partial Differential Equations

Introduction

Boundary-Value Problems for Elliptic Differential Equations

Initial-Value Problems for Parabolic Differential Equations

Time-Dependent Schrödinger Equation

Initial-value Problems for Hyperbolic Differential Equations

Implementations in C/C++

Problems

References and Suggested Further Reading

Appendices

Index

### Biography

Titus Adrian Beu , professor of theoretical and computational physics at the University Babes-Bolyai from Cluj-Napoca, Romania, has been active in the broader field of computational physics for more than 30 years. His research topics have evolved from Tokamak plasma and nuclear reactor calculations in the 1980s, collision theory and molecular cluster spectroscopy in the 1990s, to fullerenes and nanofluidics simulations in recent years. Development of ample computer codes has been at the core of all research projects the author has conducted. In parallel, he has lectured on general programming techniques and advanced numerical methods, general simulation methods, and advanced molecular dynamics.

"This book is written for advanced undergraduate and graduate students in natural sciences and engineering for a one- or two-semester course. The material presented includes many topics treated in a numerical analysis course and contains a lot of coding examples in Phyton, and C/C++. ... The value of the book lies in the presentation of the programming and a program example on almost every second page."

—Gudula Rünger,Zentralblatt MATH, 1308"In a relatively crowded field of numerical programming books, this is the only one to include both Python and C code examples. … a good reference for a one- or two-semester course for advanced undergraduate students or for graduate students in science or engineering."

—Optics & Photonics News, 2015"Working through this book you will become an expert in numerical techniques … . Your journey will be a real pleasure since the book focuses on thorough explanations, hands-on code examples, and graphical representations."

—Professor Dr. Alexander K. Hartmann, Institute for Physics, University of Oldenburg"… by taking simple mathematical examples and translating them into what the computer actually does, this textbook provides us with a good picture of what is achieved in a numerical simulation. A basic necessity for whoever wants to address more complex coding and algorithms!"

—Professor Michel Mareschal, Department of Physics, Université Libre de Bruxelles"This is a useful book, both as a text and as a reference for computational physics students and instructors. Right from the first chapter about errors it provides an invaluable resource for aspects that are often not sufficiently emphasised, despite their importance for reliable calculations. I strongly recommend it for everyone's bookshelf."

—Professor Joan Adler, Technion, Israel Institute of Technology"… a comprehensive introduction to classical numerical methods for advanced-level undergraduate students in the physical sciences, engineering, and applied mathematics. The book is novel in teaching both numerics and the art of programming, via clear and simple codes in Python and C/++. … I will recommend it to my students."

—Professor Mike Wheatland, The University of Sydney"This book is unique in providing an introduction to [numerical methods, programming languages, and graphics]. Each topic is clearly explained and can be practiced using ready-made computer programs."

—Professor R.I. Campeanu, York University, Toronto