*This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.*

*The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!*

| Contents | Acknowledgment >

# Preface¶

## Purpose¶

Because programming has become an essential component of engineering, science, medicine, media, business, finance, and many other fields, it is important for scientists and engineers to have a basic foundation in computer programming to be competitive. This book introduces programming to students from a wide range of backgrounds and gives them programming and mathematical tools that will be useful throughout their careers.

For the most part, this book follows the standard material taught at the University of California, Berkeley, in the class *E7: Introduction to computer programming for scientists and engineers*. This class is taken by most science and engineering freshmen in the College of Engineering, and by undergraduate students from other disciplines, including physics, biology, Earth, and cognitive sciences. The course was originally taught in Matlab, but with the recent trend of the data science movement at Berkeley, the Division of Data Sciences agreed on and supported the transform of this course into a Pythonoriented course to prepare students from different fields for further data science courses. The course has two fundamental goals:

Teach Python programming to science and engineering students who do not have prior exposure to programming.

Introduce a variety of numerical analysis tools that are useful for solving science and engineering problems.

These two goals are reflected in the two parts of this book:

Introduction to Programming for Scientists and Engineers

Introduction to Numerical Methods

This book is written based on the book *An Introduction to MATLAB® Programming and Numerical Methods for Engineers* by Timmy Siauw and Alexandre Bayen. The current book was first written in Jupyter Notebook for interactive purposes, and then converted to LaTeX. Most of the codes showing in this book are from the Jupyter Notebook code cells, which can be run directly in the notebook cell.

Because this book covers such a wide range of topics, no topic is covered in great depth. Each chapter has been designed to be covered in at most two lecture hours, even though there are entire semester courses dedicated to these same chapters. Rather than an in-depth treatment, this book is intended to give students a wide breadth of programming knowledge and mathematical vocabulary on which they can expand.

We believe that just like learning a new foreign language, learning to program can be fun and illuminating. We hope that as you journey through this book, you will agree.

## Prerequisites¶

This book is designed to introduce programming and numerical methods to students who have *absolutely no* prior experience with programming, which we hope is reflected in the pace, tone, and content of the text. For the purpose of programming, we assume the reader has the following prerequisite knowledge:

Understanding of the computer monitor and keyboard/mouse input devices

Understanding of the folder structure used to store files in most operating systems

For the mathematical portions of the text, we assume the reader has the following prerequisite knowledge:

High school level algebra and trigonometry

Introductory, college-level calculus

That’s it! Anything in the text that assumes more than this is our mistake, and we apologize in advance for any instances that might pop up.

## Organization¶

Part 1 teaches the fundamental concepts of programming. Chapter 1 introduces the reader to Python and Jupyter Notebook. Chapters 2 through 7 teach the fundamentals of programming. Proficiency in the material from these chapters should provide enough background to enable you to program almost anything you imagine. Chapter 8 provides the theory that characterizes computer programs based on how fast they run, and Chapter 9 gives insights into how computers represent numbers and their effect on arithmetic. Chapter 10 provides useful tips on good programming practices to limit mistakes from popping up in computer code, and tells the user how to find them when they do. Chapter 11 explains how to store data over the long term and how to make results from Python useful outside of Python (i.e., for other programs). Chapter 12 introduces Python’s graphical features that allow you to produce plots and charts, which is a really useful feature for engineers and scientists to visualize results. Finally, Chapter 13 introduces basics about the parallel programming in Python to take advantage of the multicore design of today’s computers.

Part 2 gives an overview of a variety of numerical methods that are useful for engineers. Chapter 14 gives a crash course in linear algebra. Although theoretical in nature, linear algebra is the single most critical concept for understanding many advanced engineering topics. Chapter 15 discusses eigenvalues and eigenvectors, which are important tools in engineering and science, and the ways we can utilize them. Chapter 16 is about regression, a mathematical term that is simply a method of fitting theoretical models to observed data. Chapter 17 is about inferring the value of a function between data points, a framework known as “interpolation.” Chapter 18 introduces the idea of approximating functions with polynomials, which can be useful for simplifying complicated functions. Chapter 19 teaches two algorithms for finding roots of functions, that is, finding an x such that f (x) = 0, where f is a function. Chapters 20 and 21 cover methods of approximating the derivative and integral of a function, respectively. Chapters 22 and Chapter 23 introduce a mathematical model type called “ordinary differential equations.” These two chapters focus on different problems, i.e., initial value problems and boundary value problems, and present several methods for finding their solutions. Chapter 24 introduces the concepts of “discrete Fourier transform” and “fast Fourier transform” and their use in digital signal processing. As data science is popular these days, at the end of this book, chapter 25 gives a brief tour of machine learning to motivate you to learn more after you finish this book.

## How to read this book?¶

Learning to program is all about practice, practice, and practice. Just like learning a new language, there is no way you will learn to program well without engaging with the material, internalizing it, and putting it into constant use.

As you go through the text, you should ideally have Jupyter Notebook open or the interactive website in front of you, and run all of the numerous examples that are provided. Go slowly. Taking the time to really understand what Python is doing in every example will pay large dividends compared to “powering through” the text like a novel.

In terms of the text itself, Chapters 1 through 5 should be read and understood first since they cover the fundamentals of programming. Chapters 6 through 11 can be covered in any order. Chapter 12 and 13 on plotting and parallel programming are must-read chapters that will improve your skills to better solve problems. In Part II, Chapter 14 should be read first since subsequent chapters rely on linear algebraic concepts. The remaining chapters can be read in any order. However, it will be helpful to read Chapters 17 and 18 before Chapter 19 and 20.

Throughout the text, there will be words written in boldface. When you encounter one of these words, you should take the time to commit the word to memory and understand its meaning in the context of the material being presented.

To keep the text from running on, we punctuate the material with smaller blocks. Following is a description of each kind of block.

**TRY IT!** This is the most common block in the text. It will usually have a short description of a problem and/or an activity. We strongly recommend that you actually try all of these in Python.
**TIP!** This block gives some advice that we think will make programming easier for you. However, the blocks do not contain any new material that is essential for understanding the key concepts of the text.
**EXAMPLE:** These sections are concrete examples of new concepts. They are designed to help you think about new concepts. However, they do not necessarily need to be tried.
**WARNING!** Learning to program can have many pitfalls. These sections contain information that will help you avoid confusion, building bad habits, or misunderstanding key concepts.
**WHAT IS HAPPENING?** These sections follow Python in scrutinizing detail to help you understanding what goes on when Python executes programs.
**CONSTRUCTION:** In programming there are standard architectures that are reserved to perform common and important tasks. These sections outline these architectures and how to use them.

There are two sections to end every chapter. The Summary section gives a list of the main points of the chapter. These points should be intuitive to you by the end of the chapter. The Problems section gives exercises that will reinforce concepts from the chapter.

As one final note, there are too many ways of doing the same thing in Python. Although at first this can seem like a useful feature, it can make learning Python confusing or overload you with possibilities when the task is actually straightforward. This book presents a single way of performing a task to provide structure for your learning experience and to keep you from being inundated by extraneous information. You may discover solutions that differ from the text’s solutions but solve the problem just the same or even better! We encourage you to find these alternative methods, and leave it up to experience and your own judgement to decide which way is better.

We hope you enjoy the book!

## Why Python?¶

Python is a high-level, and general-purpose language that you can do many things with it. It is really beginner friendly so that you will feel easy to learn and fun to play with it. The language itself is very flexible which means that there are no hard rules on how to build features, and you will have more flexibility solving problems with different methods. The most important thing for Python is that it has a great community support it and provides lots of packages that you can essentially plug in and go with very little efforts. These days with the ongoing popular trend of data science, Python is one of the languages that suit the goal of data science very well. Besides, Python is free, and most of the packages are also free for you to use. The idea of open source really makes a difference that not only you can use these packages for free easily, but also you can learn many advanced skills from the source code of these packages. We hope you can enjoy your learning of Python and use it in your work and life.

## Python and Package Versions¶

This book was written using Python 3. Here is a list of packages with their versions that used in this book. As these packages are constantly under development, some features may be added, removed, or changed in the versions on your computer.

jupyter - 1.0.0

ipython - 7.5.0

numpy - 1.16.4

scipy - 1.2.1

h5py - 2.9.0

matplotlib - 3.1.0

cartopy - 0.17.0

joblib - 0.13.2

scikit-learn - 0.20.1