100% found this document useful (7 votes)
29 views

Get Python Programming and Numerical Methods: A Guide for Engineers and Scientist 1st Edition Qingkai Kong free all chapters

Kong

Uploaded by

yatomauley
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (7 votes)
29 views

Get Python Programming and Numerical Methods: A Guide for Engineers and Scientist 1st Edition Qingkai Kong free all chapters

Kong

Uploaded by

yatomauley
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Experience Seamless Full Ebook Downloads for Every Genre at textbookfull.

com

Python Programming and Numerical Methods: A Guide


for Engineers and Scientist 1st Edition Qingkai
Kong

https://textbookfull.com/product/python-programming-and-
numerical-methods-a-guide-for-engineers-and-scientist-1st-
edition-qingkai-kong/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Numerical Methods for Engineers and Scientists Using


MATLAB Second Edition Esfandiari

https://textbookfull.com/product/numerical-methods-for-engineers-and-
scientists-using-matlab-second-edition-esfandiari/

textboxfull.com

Programming For Computations - Python: A Gentle


Introduction To Numerical Simulations With Python 3.6
Svein Linge
https://textbookfull.com/product/programming-for-computations-python-
a-gentle-introduction-to-numerical-simulations-with-python-3-6-svein-
linge/
textboxfull.com

Numerical Methods for Engineers 8ed 2021 8th Edition


Chapra S

https://textbookfull.com/product/numerical-methods-for-
engineers-8ed-2021-8th-edition-chapra-s/

textboxfull.com

Numerical Methods for Engineers and Scientists Using


MATLAB® Ramin S. Esfandiari

https://textbookfull.com/product/numerical-methods-for-engineers-and-
scientists-using-matlab-ramin-s-esfandiari/

textboxfull.com
Applied Numerical Methods with MATLAB for Engineers and
Scientists Fourth Edition Steven C. Chapra Dr.

https://textbookfull.com/product/applied-numerical-methods-with-
matlab-for-engineers-and-scientists-fourth-edition-steven-c-chapra-dr/

textboxfull.com

Numerical Methods in Physics with Python 1st Edition Alex


Gezerlis

https://textbookfull.com/product/numerical-methods-in-physics-with-
python-1st-edition-alex-gezerlis/

textboxfull.com

NUMERICAL METHODS IN ENGINEERING WITH PYTHON 1st Edition


Kiusalaas Jaan

https://textbookfull.com/product/numerical-methods-in-engineering-
with-python-1st-edition-kiusalaas-jaan/

textboxfull.com

Prototyping Python Dashboards for Scientists and


Engineers: Build and Deploy a Complete Dashboard with
Python 1st Edition Houlahan
https://textbookfull.com/product/prototyping-python-dashboards-for-
scientists-and-engineers-build-and-deploy-a-complete-dashboard-with-
python-1st-edition-houlahan/
textboxfull.com

Beginning Julia Programming: For Engineers and Scientists


1st Edition Sandeep Nagar

https://textbookfull.com/product/beginning-julia-programming-for-
engineers-and-scientists-1st-edition-sandeep-nagar/

textboxfull.com
Python Programming and
Numerical Methods
A Guide for Engineers and Scientists

Qingkai Kong
Timmy Siauw
Alexandre M. Bayen
Python Programming and
Numerical Methods
A Guide for Engineers and Scientists
Academic Press is an imprint of Elsevier
125 London Wall, London EC2Y 5AS, United Kingdom
525 B Street, Suite 1650, San Diego, CA 92101, United States
50 Hampshire Street, 5th Floor, Cambridge, MA 02139, United States
The Boulevard, Langford Lane, Kidlington, Oxford OX5 1GB, United Kingdom
Copyright © 2021 Elsevier Inc. All rights reserved.

MATLAB® is a trademark of The MathWorks, Inc. and is used with permission.


The MathWorks does not warrant the accuracy of the text or exercises in this book.
This book’s use or discussion of MATLAB® software or related products does not constitute endorsement or sponsorship by
The MathWorks of a particular pedagogical approach or particular use of the MATLAB® software.
No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical,
including photocopying, recording, or any information storage and retrieval system, without permission in writing from the
publisher. Details on how to seek permission, further information about the Publisher’s permissions policies and our
arrangements with organizations such as the Copyright Clearance Center and the Copyright Licensing Agency, can be found
at our website: www.elsevier.com/permissions.
This book and the individual contributions contained in it are protected under copyright by the Publisher (other than as may
be noted herein).
Notices
Knowledge and best practice in this field are constantly changing. As new research and experience broaden our
understanding, changes in research methods, professional practices, or medical treatment may become necessary.
Practitioners and researchers must always rely on their own experience and knowledge in evaluating and using any
information, methods, compounds, or experiments described herein. In using such information or methods they should be
mindful of their own safety and the safety of others, including parties for whom they have a professional responsibility.
To the fullest extent of the law, neither the Publisher nor the authors, contributors, or editors, assume any liability for any
injury and/or damage to persons or property as a matter of products liability, negligence or otherwise, or from any use or
operation of any methods, products, instructions, or ideas contained in the material herein.

Library of Congress Cataloging-in-Publication Data


A catalog record for this book is available from the Library of Congress

British Library Cataloguing-in-Publication Data


A catalogue record for this book is available from the British Library

ISBN: 978-0-12-819549-9

For information on all Academic Press publications


visit our website at https://www.elsevier.com/books-and-journals

Publisher: Katey Birtcher


Acquisitions Editor: Steve Merken
Editorial Project Manager: Alice Grant
Production Project Manager: Kamesh Ramajogi
Designer: Miles Hitchen
Typeset by VTeX
To Fanyu, Fanqi, and Fan without whom this book would have been finished
¨ To Xianzhong, and Ping!
earlier 
To the students of UC Berkeley’s E7 class: past, present, and future.
To my daughter Myriam,
who is discovering the world of programming.
Contents

List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

PART 1 INTRODUCTION TO PYTHON PROGRAMMING


CHAPTER 1 Python Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Getting Started With Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.1 Setting Up Your Working Environment . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2 Three Ways to Run Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2 Python as a Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3 Managing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.3.1 Managing Packages Using Package Managers . . . . . . . . . . . . . . . . . . . 15
1.3.2 Install Packages From Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4 Introduction to Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4.1 Starting the Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4.2 Within the Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.4.3 How Do I Close a Notebook? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.4.4 Shutting Down the Jupyter Notebook Server . . . . . . . . . . . . . . . . . . . . 20
1.5 Logical Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.6 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.6.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
CHAPTER 2 Variables and Basic Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1 Variables and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2 Data Structure – String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.3 Data Structure – List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.4 Data Structure – Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.5 Data Structure – Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.6 Data Structure – Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.7 Introducing NumPy Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.8 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.8.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.8.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
CHAPTER 3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.1 Function Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.1.1 Built-In Functions in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.1.2 Define Your Own Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.2 Local Variables and Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
vii
viii Contents

3.3 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67


3.4 Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.5 Functions as Arguments to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.6 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
3.6.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
3.6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
CHAPTER 4 Branching Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.1 If-Else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.2 Ternary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.3 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.3.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.3.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
CHAPTER 5 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.1 For-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.2 While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
5.3 Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
5.3.1 List Comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
5.3.2 Dictionary Comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5.4 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5.4.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5.4.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
CHAPTER 6 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.1 Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.2 Divide-and-Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
6.2.1 Tower of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
6.2.2 Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.3 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.3.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.3.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
CHAPTER 7 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
7.1 Introduction to OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
7.2 Class and Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.2.1 Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.2.2 Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.2.3 Class vs Instance Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.3 Inheritance, Encapsulation, and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . 127
7.3.1 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
7.3.2 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
7.3.3 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
7.4 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
7.4.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
7.4.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Contents ix

CHAPTER 8 Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135


8.1 Complexity and Big-O Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
8.2 Complexity Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
8.3 The Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
8.3.1 Using the Magic Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
8.3.2 Use Python Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
8.3.3 Use Line Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
8.4 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
8.4.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
8.4.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
CHAPTER 9 Representation of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
9.1 Base-N and Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
9.2 Floating Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
9.3 Round-Off Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
9.3.1 Representation Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
9.3.2 Round-Off Error by Floating-Point Arithmetic . . . . . . . . . . . . . . . . . . 152
9.3.3 Accumulation of Round-Off Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
9.4 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
9.4.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
9.4.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
CHAPTER 10 Errors, Good Programming Practices, and Debugging . . . . . . . . . . . . . . . . 157
10.1 Error Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
10.2 Avoiding Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
10.2.1 Plan Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
10.2.2 Test Everything Often . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
10.2.3 Keep Your Code Clean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
10.3 Try/Except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
10.4 Type Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
10.5 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
10.5.1 Activating Debugger After Running Into an Exception . . . . . . . . . . . 168
10.5.2 Activating Debugger Before Running the Code . . . . . . . . . . . . . . . . 171
10.5.3 Add a Breakpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
10.6 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
10.6.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
10.6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
CHAPTER 11 Reading and Writing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
11.1 TXT Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
11.1.1 Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
11.1.2 Appending a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
11.1.3 Reading a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
11.1.4 Dealing With Numbers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 178
11.2 CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
11.2.1 Writing and Opening a CSV File . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
x Contents

11.2.2 Reading a CSV File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180


11.2.3 Beyond NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
11.3 Pickle Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
11.3.1 Writing to a Pickle File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
11.3.2 Reading a Pickle File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
11.3.3 Reading in Python 2 Pickle File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
11.4 JSON Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
11.4.1 JSON Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
11.4.2 Writing a JSON File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
11.4.3 Reading a JSON File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
11.5 HDF5 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
11.5.1 Reading an HDF5 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
11.6 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
11.6.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
11.6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
CHAPTER 12 Visualization and Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
12.1 2D Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
12.2 3D Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
12.3 Working With Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
12.4 Animations and Movies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
12.5 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
12.5.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
12.5.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
CHAPTER 13 Parallelize Your Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
13.1 Parallel Computing Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
13.1.1 Process and Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
13.1.2 Python’s GIL Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
13.1.3 Disadvantages of Using Parallel Computing . . . . . . . . . . . . . . . . . . . 227
13.2 Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
13.2.1 Visualize the Execution Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
13.3 Using Joblib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
13.4 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
13.4.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
13.4.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

PART 2 INTRODUCTION TO NUMERICAL METHODS


CHAPTER 14 Linear Algebra and Systems of Linear Equations . . . . . . . . . . . . . . . . . . . 235
14.1 Basics of Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
14.1.1 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
14.1.2 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
14.1.3 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
14.2 Linear Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
14.3 Systems of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Contents xi

14.4 Solutions to Systems of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 246


14.4.1 Gauss Elimination Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
14.4.2 Gauss–Jordan Elimination Method . . . . . . . . . . . . . . . . . . . . . . . . . 250
14.4.3 LU Decomposition Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
14.4.4 Iterative Methods – Gauss–Seidel Method . . . . . . . . . . . . . . . . . . . . 253
14.5 Solving Systems of Linear Equations in Python . . . . . . . . . . . . . . . . . . . . . . 256
14.6 Matrix Inversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
14.7 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
14.7.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
14.7.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
CHAPTER 15 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
15.1 Eigenvalues and Eigenvectors Problem Statement . . . . . . . . . . . . . . . . . . . . 265
15.1.1 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
15.1.2 The Motivation Behind Eigenvalues and Eigenvectors . . . . . . . . . . . 265
15.1.3 The Characteristic Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
15.2 The Power Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
15.2.1 Finding the Largest Eigenvalue . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
15.2.2 The Inverse Power Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
15.2.3 The Shifted Power Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
15.3 The QR Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
15.4 Eigenvalues and Eigenvectors in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
15.5 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
15.5.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
15.5.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
CHAPTER 16 Least Squares Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
16.1 Least Squares Regression Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . 279
16.2 Least Squares Regression Derivation (Linear Algebra) . . . . . . . . . . . . . . . . . 280
16.3 Least Squares Regression Derivation (Multivariate Calculus) . . . . . . . . . . . . 281
16.4 Least Squares Regression in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
16.4.1 Using the Direct Inverse Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
16.4.2 Using the Pseudo-Inverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
16.4.3 Using numpy.linalg.lstsq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
16.4.4 Using optimize.curve_fit From SciPy . . . . . . . . . . . . . . . . . . . . . 285
16.5 Least Squares Regression for Nonlinear Functions . . . . . . . . . . . . . . . . . . . . 286
16.5.1 Log Tricks for Exponential Functions . . . . . . . . . . . . . . . . . . . . . . . 286
16.5.2 Log Tricks for Power Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
16.5.3 Polynomial Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
16.5.4 Using optimize.curve_fit From SciPy . . . . . . . . . . . . . . . . . . . . . 289
16.6 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
16.6.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
16.6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
CHAPTER 17 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
17.1 Interpolation Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
xii Contents

17.2 Linear Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296


17.3 Cubic Spline Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
17.4 Lagrange Polynomial Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
17.4.1 Using the Lagrange Function From SciPy . . . . . . . . . . . . . . . . . . . . 304
17.5 Newton’s Polynomial Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
17.6 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
17.6.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
17.6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
CHAPTER 18 Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
18.1 Expressing Functions Using a Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . 315
18.2 Approximations Using Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
18.3 Discussion About Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
18.3.1 Truncation Errors for Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . 320
18.3.2 Estimating Truncation Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
18.3.3 Round-Off Errors for Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . 322
18.4 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
18.4.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
18.4.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
CHAPTER 19 Root Finding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
19.1 Root Finding Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
19.2 Tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
19.3 Bisection Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
19.4 Newton–Raphson Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
19.5 Root Finding in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
19.6 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
19.6.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
19.6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
CHAPTER 20 Numerical Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
20.1 Numerical Differentiation Problem Statement . . . . . . . . . . . . . . . . . . . . . . . 337
20.2 Using Finite Difference to Approximate Derivatives . . . . . . . . . . . . . . . . . . . 338
20.2.1 Using Finite Difference to Approximate Derivatives With Taylor
Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
20.3 Approximating of Higher Order Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . 344
20.4 Numerical Differentiation With Noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
20.5 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
20.5.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
20.5.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
CHAPTER 21 Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
21.1 Numerical Integration Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 353
21.2 Riemann Integral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
21.3 Trapezoid Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
21.4 Simpson’s Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
21.5 Computing Integrals in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Contents xiii

21.6 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365


21.6.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
21.6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
CHAPTER 22 Ordinary Differential Equations (ODEs) Initial-Value Problems . . . . . . . . . . 371
22.1 ODE Initial Value Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
22.2 Reduction of Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
22.3 The Euler Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
22.4 Numerical Error and Instability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
22.5 Predictor–Corrector and Runge–Kutta Methods . . . . . . . . . . . . . . . . . . . . . . 382
22.5.1 Predictor–Corrector Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
22.5.2 Runge–Kutta Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
22.6 Python ODE Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
22.7 Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
22.7.1 Multistep Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
22.7.2 Stiffness ODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
22.8 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
22.8.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
22.8.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
CHAPTER 23 Boundary-Value Problems for Ordinary Differential Equations (ODEs) . . . . . 399
23.1 ODE Boundary Value Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 399
23.2 The Shooting Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
23.3 The Finite Difference Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
23.4 Numerical Error and Instability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
23.5 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
23.5.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
23.5.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
CHAPTER 24 Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
24.1 The Basics of Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
24.1.1 Modeling a Wave Using Mathematical Tools . . . . . . . . . . . . . . . . . . 415
24.1.2 Characteristics of a Wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
24.2 Discrete Fourier Transform (DFT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
24.2.1 DFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
24.2.2 The Inverse DFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
24.2.3 The Limit of DFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
24.3 Fast Fourier Transform (FFT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
24.3.1 Symmetries in the DFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
24.3.2 Tricks in FFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
24.4 FFT in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
24.4.1 FFT in NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
24.4.2 FFT in SciPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
24.4.3 More Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
24.5 Summary and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
24.5.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
xiv Contents

24.5.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441


Appendix A Getting Started With Python in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 445
A.1 Getting Started With Python in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
A.1.1 Setting Up Your Working Environment in Windows . . . . . . . . . . . . . . 445
A.1.2 Three Ways to Run Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
List of Figures

Fig. 1.1 The Miniconda download page; choose the installer based on your operating system. 4
Fig. 1.2 Screen shot of running the installer in a terminal. 5
Fig. 1.3 The default installation location of your file system. 5
Fig. 1.4 Quick way to check if Miniconda was installed successfully and the programs are run properly. 6
Fig. 1.5 Installation process for the packages that will be used in the rest of the book. 6
Fig. 1.6 Run “Hello World” in IPython shell by typing the command. “print” is a function that is
discussed later in the book that will print out anything within the parentheses. 7
Fig. 1.7 Example of a Python script file example using Visual Studio Code. Type in the commands you
want to execute and save the file with a proper name. 8
Fig. 1.8 To run the Python script from command line, type “python hello_world.py”. This line tells
Python to execute the commands that saved in this file. 8
Fig. 1.9 To launch a Jupyter notebook server, type jupyter notebook in the command line, which
will open a browser page as shown here. Click the “New” button on the top right and choose
“Python3”. This will create a Python notebook from which to run Python code. 9
Fig. 1.10 To run the Hello World example within Jupyter notebook, type the command in the code cell
(the grey boxes) and press Shift + Enter to execute it. 10
Fig. 1.11 An example demonstrating the interactive search for functions within IPython by typing TAB
after the dot. The grey box shown all available functions. 12
Fig. 1.12 The help document of pip after executing pip help. 16
Fig. 1.13 Using pip list to show all the packages installed on your machine. 17
Fig. 1.14 Using pip show to get detailed information about a installed package. 17
Fig. 1.15 The Jupyter notebook dashboard after launching the server. Red arrows (light grey arrows in
print version) are pointing to you the most common features in the dashboard. 19
Fig. 1.16 A quick view of a notebook. The Header of the notebook shows the name of the notebook. The
menu has various drop-down lists that let you access to all the functionalities of the notebook.
The tool bar provides you some shortcuts for the commonly used functionalities. 20
Fig. 1.17 Truth tables for the logical and/or. 22
Fig. 1.18 Truth tables for the logical XOR. 25
Fig. 2.1 String index for the example of "Hello World". 31
Fig. 2.2 Example of list index. 36
Fig. 5.1 Interrupt the kernel by pressing the little square. 99
Fig. 6.1 Recursion tree for factorial(3). 107
Fig. 6.2 Recursion tree for factorial(5). 108
Fig. 6.3 Illustration of the Tower of Hanoi: In eight steps, all disks are transported from pole 1 to pole 3,
one at a time, by moving only the disk at the top of the current stack, and placing only smaller
disks on top of larger disks. 111
Fig. 6.4 Breakdown of one iteration of the recursive solution of the Tower of Hanoi problem. 111
Fig. 6.5 Pascal’s triangle. 118
Fig. 8.1 Illustration of running time for complexity log(n), n, and n2 . 138
xv
xvi List of Figures

Fig. 8.2 The profiling result from prun. 141


Fig. 8.3 The line by line profiling result from line_profiler. 142
Fig. 9.1 Binary addition. 146
Fig. 9.2 Binary multiplication. 147
Fig. 9.3 Illustration of −12.0 that is represented in computer with 64-bit. Each square is one bit, with the
green square representing 1 and grey square as zero. 148
Fig. 11.1 The content in the text file we write. 177
Fig. 11.2 Append a line to the end of an existing file. 177
Fig. 11.3 The NumPy array we saved in the file. 179
Fig. 11.4 Open the csv file using Microsoft Excel. 181
Fig. 11.5 Open the csv file using a text editor. 181
Fig. 12.1 Plot of x versus y. 190
Fig. 12.2 Plot of the function f (x) = x 2 for −5 ≤ x ≤ 5. 191
Fig. 12.3 Plot of the function f (x) = x 2 for −5 ≤ x ≤ 5 using dashed line. 192
Fig. 12.4 Using different colors and markers. 192
Fig. 12.5 Adding title and axis labels to a figure. 193
Fig. 12.6 Using seaborn-poster style. 194
Fig. 12.7 Using a legend in the figure. 195
Fig. 12.8 Changing limits of the figure and turning on grid. 196
Fig. 12.9 Different type of plots. 198
Fig. 12.10 Saving a figure. 199
Fig. 12.11 3D axes. 200
Fig. 12.12 3D scatter plot. 202
Fig. 12.13 3D surface plot. 204
Fig. 12.14 3D wireframe and surface plot. 205
Fig. 12.15 Simple world map. 206
Fig. 12.16 World map with background. 207
Fig. 12.17 Simple US map. 208
Fig. 12.18 US map with various features. 209
Fig. 12.19 Bay area map with higher resolution. 210
Fig. 12.20 Add more entities on the map. 211
Fig. 13.1 Parallel computing vs. serial computing. 226
Fig. 13.2 The hardware on the author’s laptop with multicore processor. 226
Fig. 14.1 Graph for Problem 12. 263
Fig. 16.1 Illustration of the L2 projection of Y on the range of A. 281
Fig. 17.1 Illustration of the interpolation problem: estimate the value of a function in-between data
points. 295
Fig. 19.1 Illustration of the intermediate value theorem. If sign(f (a)) and sign(f (b)) are not equal, then
there exists a c in (a, b) such that f (c) = 0. 327
Fig. 19.2 Illustration of the bisection method. The sign of f (m) is checked to determine if the root is
contained in the interval (a, m) or (m, b). This new interval is used in the next iteration of the
bisection method; in the case depicted in the figure, the root is in the interval (m, b). 328
Fig. 19.3 Illustration of Newton step for a smooth function, g(x). 330
Fig. 20.1 Numerical grid used to approximate functions. 337
List of Figures xvii

Fig. 20.2 Finite difference approximation of the derivative. 339


Fig. 20.3 Illustration of the forward difference, the backward difference, and the central difference. Note
the difference in slopes depending on the method used. 341
Fig. 21.1 Illustration of the integral. The integral from a to b of the function f is the area below the curve
(shaded in grey). 353
Fig. 21.2 Illustration of the trapezoid integral procedure. The area below the curve is approximated by a
sum of areas of trapezoids that approximate the function. 357
Fig. 21.3 Illustration of the Simpson integral formula. Discretization points are grouped by three, and a
parabola is fit between the three points. This can be done by a typical interpolation polynomial.
The area under the curve is approximated by the area under the parabola. 360
Fig. 21.4 Illustration of the accounting procedure to approximate the function f by the Simpson rule for
the entire interval [a, b]. 361
Fig. 22.1 Pendulum system. 372
Fig. 22.2 The illustration of the explicit Euler method. 376
Fig. 22.3 The illustration of the stiffness equation. 390
Fig. 23.1 Heat flow in a pin fin. The variable L is the length of the pin fin, which starts at x = 0 and
finishes at x = L. The temperatures at two ends are T0 and TL , with Ts being the surrounding
environment temperature. 400
Fig. 23.2 Target shooting analogy to the shooting method. 401
Fig. 23.3 Illustration of the finite difference method. 406
Fig. 24.1 Period and amplitude of a sine wave. 418
Fig. 24.2 Wavelength and amplitude of a sine wave. 418
Fig. 24.3 More general wave form. 422
Fig. 24.4 Illustration of Fourier transform with time and frequency domain signal. 422
Fig. 24.5 Signal generated before with 3 frequencies. 433
Fig. A.1 The Miniconda download page, choose the installer based on your operating system. 446
Fig. A.2 Screen shot of running the installer in Anaconda prompt. 446
Fig. A.3 The default installation location of your file system. 447
Fig. A.4 Open the Anaconda prompt from the start menu. 448
Fig. A.5 A quick way to check if your installed Miniconda runs properly. 449
Fig. A.6 Installation process for the packages that will be used in the rest of the book. 449
Fig. A.7 Run "Hello World" in IPython shell by typing the command, “print” is a function that we will
learn to print out anything within the parentheses. 450
Fig. A.8 A Python script file example using Visual Studio Code. You type in the commands you want to
execute and save the file with a proper name. 450
Fig. A.9 To run the Python script from command line, we can type “python hello_world.py”. This line
tells Python that we will execute the commands that were saved in this file. 451
Fig. A.10 To launch a Jupyter notebook server, type jupyter notebook in the command line, which
will open a browser page as shown here. Click “New” button at the top right corner, and choose
“Python3” which will create a Python notebook to run Python code. 451
Fig. A.11 Run the Hello World example within Jupyter notebook. Type the command in the code cell
(the grey boxes) and press Shift + Enter to execute it. 452
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 un-
dergraduate 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
Python-oriented 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.
All the Jupyter Notebook codes can be found at pythonnumericalmethods.berkeley.edu.
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.
xix
xx Preface

PREREQUISITES
This book is designed to introduce programming and numerical methods to students who have abso-
lutely no prior experience with computer programming. We hope this underlying concept 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 level of knowledge is our mistake, and
we apologize in advance for any confusion of instances where concepts are unclear.

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. Fi-
nally, 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 eigenval-
ues 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 algo-
rithms 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, respec-
tively. 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 bound-
How to Read This Book? xxi

ary 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.

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 to master computer programming without engaging with the material, internalizing it,
and putting it into constant use.
We suggest that 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 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 if you wish to improve your
problem-solving skills. In Part 2, Chapter 14 should be read first since subsequent chapters rely on
linear algebraic concepts. The remaining chapters can be read in any order. We do suggest reading
Chapters 17 and 18 before Chapters 19 and 20.
Throughout the text, there are words written in boldface. When you encounter one of them, 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 and overwhelming the reader, we have punctuated the material
with smaller blocks of text. Different blocks of text have different functions. For example,
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 believe will make programming easier. Note that 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, developing bad habits, or misunderstanding key concepts.
WHAT IS HAPPENING? These sections follow Python in scrutinizing detail to help you under-
stand 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 that end every chapter. The Summary section lists the main take-aways 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 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 the programming
novice with possibilities when the task is actually straightforward. This book presents a single way of
performing a task to provide structure for the new user’s learning experience and to keep that user on
xxii Preface

track and 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 use your own judgment to given the tools we have provided herein to decide
which way is better.
We hope you enjoy the book!

WHY PYTHON?
Python is a high-level and general-purpose computer language that lends itself to many applications.
As it is beginner friendly, we hope that you will find it easy to learn and that it is 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 find that there are several ways to solve the same problem. Perhaps its great strength is
that it has a great user community that supports it, with lots of packages to essentially plug in and go
with very little efforts. With the ongoing popular trend, Python suits the goals of data science today.
Python is free (open source), and most of the packages are also free for use. The idea of an open source
programming language makes a huge difference in the learning curve. Not only you can use these
packages for free, but also you can learn many advanced skills from the source code of these packages
developed by other users. We hope you can enjoy your learning of Python presented here 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
Acknowledgements

The first version of this book was written at a time when the standard generalist language taught in
engineering (and beyond) at UC Berkeley was Matlab. Its genesis goes back to the mid-2000s, which
precede the current era of data science, machine learning, in which Python emerged as a commonly
used language across the engineering profession. The first version was thus written as part of the E7
class at UC Berkeley, which introduces many students to programming and numerical analysis. It
would never have been written without the help of colleagues, teams of Graduate Student Instructors
(GSI), graders, and administrative staff members who helped us through the challenging process of
teaching E7 to several hundreds of students each semester at UC Berkeley. Furthermore, the first edi-
tion of this book would never have reached completion without the help of the students who had the
patience to read the book and give us their feedback. In the process of teaching E7 numerous times,
we have interacted with thousands of students, dozens of GSIs and graders, and a dozen colleagues
and administrators, and we apologize to those we will inevitably forget given the number of people
involved. We are extremely grateful for guidance from our colleagues Professors Panos Papadopou-
los, Roberto Horowitz, Michael Frenklach, Andy Packard, Tad Patzek, Jamie Rector, Raja Sengupta,
Mike Cassidy, and Samer Madanat. We owe thanks particularly to Professors Roberto Horowitz, Andy
Packard, Sanjay Govindjee, and Tad Patzek for sharing the material they used for the class, which
contributed to the material in this book. We also thank Professors Rob Harley and Sanjay Govindjee
for using a draft of this book during the semesters they taught E7 and giving us feedback that helped
improve the manuscript. The smooth running of the semester course gave the authors the time and
energy to produce this book. Managing the course was greatly facilitated by numerous administrative
staff members who bore much of the logistic load. We are particularly grateful to Joan Chamberlain,
Shelley Okimoto, Jenna Tower, and Donna Craig. Civil and Environmental Engineering Vice Chair
Bill Nazaroff deserves particular recognition for assigning the second author to teach the class in 2011.
Without this assignment the two authors of this book would not have had an opportunity to work to-
gether and write this book. E7 is notoriously the hardest class to teach at UC Berkeley in the College
of Engineering. However, it continued to run smoothly over the many semesters we learned to teach
this class, mainly due to the help of the talented GSIs we had the pleasure of working with. During the
years the coauthors taught the class, a series of legendary head GSIs have contributed to shaping the
class and making it a meaningful experience for students. In particular, Scott Payne, James Lew, Claire
Saint-Pierre, Kristen Parish, Brian McDonald, and Travis Walter have in their respective roles led a
team of dedicated GSI to exceed expectations. The GSI and grader team during the Spring of 2011
greatly influenced the material of this book. For their contribution during that critical semester, we
thank Jon Beard, Leah Anderson, Marc Lipoff, Sebastien Blandin, Sam Chiu, Rob Hansen, Jiangchuan
Huang, Brad Adams, Ryan Swick, Pranthik Samal, Matthieu Lewandowski, and Romain Bourcier. We
are also grateful to Claire Johnson and Katherine Mellis for finding errors in the text and helping us
incorporate edits into the manuscript. We are indebted to the E7 students for their patience with us
and their thorough reading of the material. Having seen thousands of them through the years, we are
sorry to only be able to mention a few for their extraordinary feedback and performance: Gursham-
njot Singh, Sabrina Nicolle Atienza, Yi Lu, Nicole Schauser, Harrison Lee, Don Mai, Robin Parrish,
xxiii
xxiv Acknowledgements

and Mara Minner. In 2018, as the UC Berkeley campus was already deeply engaged in the transition
leading to the birth of the Division of Computing, Data Science, and Society, numerous conversations
started on the need for UC Berkeley students to learn Python, which in the mean time had become
a commodity of choice for employment in most tech companies. Thus, this book started with the in-
tention of preparing engineering and science students with basic data science tools. The UC Berkeley
Division of Computing, Data Science, and Society played an active role in creating this book for a lower
division course to prepare students for further study. We thank Cathryn Carson and David Culler for
their support in writing this book and for the discussions on how to make it better. Their help happened
in parallel to the herculean efforts they led to build the Division of Computing, Data Science, and So-
ciety. It is one of the many expressions of their scientific generosity and dedication to building a rich
and innovative data science environment at UC Berkeley. Finally, we also appreciate the care and help
from Eric Van Dusen and Keeley Takimoto. About two thirds of the book are adapted from the original
Matlab version – An Introduction to MATLAB® Programming and Numerical Methods for Engineers
by the two last authors. We thank Jennifer Grannen, Brian Mickel, Nick Bourlier, and Austin Chang
for their help to convert some of the Matlab code to Python. We are (again!) grateful to Claire Johnson
for her help with the second version of the book, and to Jennifer Taggart for finding errors in the text
and helping us incorporate edits into the manuscript. We also thank the Berkeley Seismsology Lab for
the support of writing this book and the Python training over the years.

Qingkai Kong
Timmy Siauw
Alexandre M. Bayen
June 2020
CHAPTER

PYTHON BASICS
1
CONTENTS
1.1 Getting Started With Python ......................................................................................... 3
1.1.1 Setting Up Your Working Environment ............................................................. 3
1.1.2 Three Ways to Run Python Code .................................................................... 7
1.2 Python as a Calculator ............................................................................................... 9
1.3 Managing Packages .................................................................................................. 15
1.3.1 Managing Packages Using Package Managers .................................................... 15
Install a Package ............................................................................................. 15
Upgrade a Package.......................................................................................... 16
Uninstall a Package.......................................................................................... 16
Other Useful Commands ................................................................................... 16
1.3.2 Install Packages From Source ....................................................................... 18
1.4 Introduction to Jupyter Notebook ................................................................................... 18
1.4.1 Starting the Jupyter Notebook....................................................................... 18
1.4.2 Within the Notebook .................................................................................. 19
1.4.3 How Do I Close a Notebook?......................................................................... 20
1.4.4 Shutting Down the Jupyter Notebook Server ...................................................... 20
1.5 Logical Expressions and Operators ................................................................................ 20
1.6 Summary and Problems .............................................................................................. 23
1.6.1 Summary ............................................................................................... 23
1.6.2 Problems ............................................................................................... 23

1.1 GETTING STARTED WITH PYTHON


1.1.1 SETTING UP YOUR WORKING ENVIRONMENT
The first step in using Python is to set up the working environment on the computer. This section
introduces the initial processes to get it started.
There are different ways to install Python and related packages, and we recommend using Ana-
conda1 or Miniconda2 to install and manage your packages. Depending on the operating system (OS)
you are using (i.e., Windows, Mac OS X, or Linux), you will need to download a specific installer for
your machine. Both Anaconda and Miniconda are aimed at providing easy ways to manage the Python
work environment in scientific computing and data sciences.

1 https://www.anaconda.com/download/.
2 https://conda.io/miniconda.html.

Python Programming and Numerical Methods. https://doi.org/10.1016/B978-0-12-819549-9.00010-5


Copyright © 2021 Elsevier Inc. All rights reserved.
3
4 CHAPTER 1 PYTHON BASICS

FIGURE 1.1
The Miniconda download page; choose the installer based on your operating system.

In this example, we will use Mac OS X to show you how to install Miniconda (the process of which
is very similar to installing on Linux). For Windows users, please skip the rest of this section and read
Appendix A on the installation instructions. The main differences between Anaconda and Miniconda
are as follows:
• Anaconda is a complete distribution framework that includes the Python interpreter, package man-
ager, and the commonly used packages in scientific computing.
• Miniconda is a “light” version of Anaconda that does not include the commonly used packages.
You need to install all the different packages yourself, but it does include the Python interpreter and
package manager.
The option we’ve chosen here is Miniconda, and we will install only those packages that we will
need. The Miniconda install process is described below:
Step 1. Download the Miniconda installer from the website.3 The download page is shown in
Fig. 1.1. Here you can choose a different installer based on your OS. In this example, we choose Mac
OS X and Python 3.7.
Step 2. Open a terminal (on a Mac, you can search for “terminal” in Spotlight search). Run the
installer from the terminal using the commands showing in Fig. 1.2. After you run the installer, follow
the guide to finish the installation.
Note that although you can change the installation location by giving it an alternative location on
your machine, the default is your home directory (Fig. 1.3).
After installation, you can check the installed packages by typing the commands shown in Fig. 1.4.

3 https://conda.io/miniconda.html.
1.1 GETTING STARTED WITH PYTHON 5

FIGURE 1.2
Screen shot of running the installer in a terminal.

FIGURE 1.3
The default installation location of your file system.
6 CHAPTER 1 PYTHON BASICS

FIGURE 1.4
Quick way to check if Miniconda was installed successfully and the programs are run properly.

FIGURE 1.5
Installation process for the packages that will be used in the rest of the book.
1.1 GETTING STARTED WITH PYTHON 7

Step 3. As shown in Fig. 1.5, install the basic packages used in this book: ipython, numpy, scipy,
pandas, matplotlib, and jupyter notebook. In a later section, we will talk about the management of
the packages using pip and conda.

1.1.2 THREE WAYS TO RUN PYTHON CODE


There are different ways to run Python code and they all have different usages. This section will intro-
duce the three different ways to get you started.
Using the Python shell or IPython shell. The easiest way to run Python code is through the Python
shell or IPython shell (which stands for Interactive Python). The IPython shell is more powerful than
the Python shell, including features such as Tab autocompletion, color-highlighted error messages, ba-
sic UNIX shell integration, and so on. Since we’ve just installed IPython, let us try to run the “Hello
World” example using it. To launch the IPython shell, we begin by typing ipython in a terminal (see
Fig. 1.6). Then we run a Python command by typing it into the shell and pressing Enter. We imme-
diately see the results from the command. For example, we can print out “Hello World” by using the
print function in Python, as shown in Fig. 1.6.
Run a Python script/file from the command line. The second way to run Python code is to put all
the commands into a file and save it as a file with the extension .py; the extension of the file can be
anything, but by convention, it is usually .py. For example, use your favorite text editor (here we used

FIGURE 1.6
Run “Hello World” in IPython shell by typing the command. “print” is a function that is discussed later in the book
that will print out anything within the parentheses.
8 CHAPTER 1 PYTHON BASICS

FIGURE 1.7
Example of a Python script file example using Visual Studio Code. Type in the commands you want to execute and
save the file with a proper name.

FIGURE 1.8
To run the Python script from command line, type “python hello_world.py”. This line tells Python to execute the
commands that saved in this file.

Visual Studio Code4 ) to type the commands you wish to execute in a file called hello_world.py, as
shown in Fig. 1.7, which is then run from terminal (see Fig. 1.8).

4 https://code.visualstudio.com.
1.2 PYTHON AS A CALCULATOR 9

FIGURE 1.9
To launch a Jupyter notebook server, type jupyter notebook in the command line, which will open a browser
page as shown here. Click the “New” button on the top right and choose “Python3”. This will create a Python
notebook from which to run Python code.

Using Jupyter Notebook. The third way to run Python is through Jupyter Notebook, which is a very
powerful browser-based Python environment. We will discuss this in details later in this chapter. The
example presented here is to demonstrate how quickly we can run the code using Jupyter notebook.
If you type jupyter notebook in the terminal, a local web page will pop up; use the upper right button
to create a new Python3 notebook, as shown in Fig. 1.9.
Running code in Jupyter notebook is easy. Type your code in the cell and press Shift + Enter
to run the cell; the results will be shown below the code (Fig. 1.10).

1.2 PYTHON AS A CALCULATOR


Python contains functions found in any standard graphing calculator. An arithmetic operation is either
addition, subtraction, multiplication, division, or powers between two numbers. An arithmetic opera-
tor is a symbol that Python has reserved to mean one of the aforementioned operations. These symbols
are + for addition, - for subtraction, * for multiplication, / for division, and ** for exponentiation.
An instruction or operation is executed when it is resolved by the computer. An instruction is
executed at the command prompt by typing it where you see the >>> symbol appears in the Python
shell (or the In [1]: sign in IPython) and then pressing Enter. In the case of the Jupyter notebook,
type the operation in the code cell and Shift + Enter. Since we will use Jupyter notebook for the
rest of the book, to familiarize yourself with all the different options, all examples in this section will
be shown in the IPython shell – see the previous section for how to begin working in IPython. For
Windows users, you will use the Anaconda prompt shown in the Appendix A instead of using the
terminal.
Another Random Scribd Document
with Unrelated Content
However that might be, the Huns were active enough in the waters
through which the Colodia plied. It was only two days after Whistler
and George Belding had saved the living remainder of the Zeppelin
crew when an S O S call was picked up by the port wireless station
and transmitted to the destroyer. It was possible that the ship in peril
was too far away for the Colodia to be of service; nevertheless she
started out of the harbor within ten minutes of the reception of the
aero plea for help.
The weather was rough, and the ship barely dropped the
headlands below the horizon at sunset. They were bound, doubtless,
on a useless night trip. And yet, such ventures were a part of the
work of the destroyers and must be expected by their crews.
When night had fallen there was only a pale radiance resting on
the sea while broken wind clouds drove athwart a gray and dreary
sky. No stars were visible. From behind the weather screen of the
bridge, where the two watch officers were stationed, nothing could
be seen ahead but the phosphorescent flash of waves otherwise as
black as ink. These flashes, where the waves broke at their crests,
decreased rather than aided the powers of vision.
The crew of the Colodia were by this time so well used to their
work that there were few false alarms as the ship tore on through the
dark seas. Such errands as this were part of the expectation—
almost of routine. The destroyers at night fairly “smelled” their way
from point to point.
Now and then a porpoise shot straight toward the Colodia, leaving
a sparkling wake so like that of a torpedo that the lookout might be
excused for giving a mistaken warning. But the men knew the real
thing now, and the gunners did not bang away at fish or floating
débris as they had in the beginning.
“Why, even Isa Bopp has not for a long time raised a flivver,” said
Al Torrance, discussing this matter with George Belding and
Whistler. “And Ikey has stopped straining his eyes when he’s off
duty. One time he would have hollered ‘wolf’ if he’d seen a dill pickle
floating three hundred yards off our weather bow.”
“That’s all right,” said Whistler. “But Ikey won the first gold piece
for sighting a German sub when he first went to sea on this old knife-
blade. He’s got eyes for something besides dill pickles, has Ikey.”
The crackling radio was intercepting messages from other ships—
all kinds of ships. The S O S call was no longer being repeated; but
the Colodia’s officers had learned the position of the vessel that
called for help at the start, and the destroyer did not swerve from her
course. She roared on through the dark sea directly for the spot
indicated.
“There’s nothing fancy in this job, George,” Phil Morgan said to
their new chum. “Nothing like a good, slap-dash battle with the Hun
fleet, such as we had a few weeks ago, or even chasing a Hun raider
out of Zeebrugge, or Kiel. But the old Colodia has had ‘well done’
signaled her by the fleet admiral more than once.”
“You bet!” Al Torrance put in. “We’ve sunk more than one of the U-
boats. We’re one of ‘the terrors of the sea,’ boy—like the song tells
about. That is what they call our flotilla.”
“Ah! I’ve heard all that before,” Belding said, in some disgust. “I
want to see action!”
As it chanced, he saw action on this very cruise. First, however,
came the conclusion of the incident that had brought them out of
port, chasing a phantom S O S.
A light burning low on the water was spied about ten o’clock. It
could be nothing but an open boat, and the Colodia’s prow was
turned more directly toward it. The sea was really too rough for a
submarine to be awash, yet the Huns had been known to linger in
the vicinity of their victims so as to catch the rescuing vessel
unaware. A sharp lookout was maintained as the Colodia steamed
onward.
The torch in the open boat flared and smoked, while the boat
pitched and tossed—seemingly scarcely under command of its crew.
There was no sign of any other craft in the vicinity. The signal from
the attacked ship having stopped hours before, without much doubt
she had sunk.
And but one boat remained!
The destroyer sped down within hailing distance of the open boat,
burning signals of her own meanwhile. Getting on the weather
quarter of the castaways, the latter were ordered to pull to the
Colodia.
The boat held only nineteen survivors of the Newcastle Boy, a
collier that had been torpedoed by a submarine. There had been a
second boat, and both had been shelled after the collier sank, and
the mate, who was in command of these rescued castaways, feared
his captain’s boat was utterly lost. Had the sea not been so rough,
he said, the Germans would have succeeded in sinking his boat, too.
Whistler was on duty amidships and he overheard much of the
report made by the collier’s mate to Lieutenant Commander Lang
and the conversation among the officers thereon.
He was particularly impressed by the inquiries the destroyer’s
commander made regarding the nature of the attack, the type of U-
boat that did the deed, and similar details.
A close track was kept of all these submarine attacks. The
methods of certain submarine commanders could usually be traced.
These reports were kept by the British Admiralty and were intended,
at the end of the war, to assist in identifying U-boat commanders
who had committed atrocities. Those men should, in the end, not
escape punishment for their horrid crimes.
This attack upon the Newcastle Boy had been particularly brutal.
There were four wounded men in the mate’s boat. If the captain’s
boat were lost, the missing would total twenty-six.
The Colodia, swinging in wide circles through the rough sea,
remained near the scene of the catastrophe until morning. They
discovered no trace of the sunken ship, although the mate declared
she had gone down within a mile of the spot where the destroyer had
picked up the survivors.
But at daybreak the watchful lookouts did spy a broken oar and
part of the bow of the captain’s lifeboat—its air-compartment keeping
it afloat. No human being was there to be seen, and the conclusion
was unescapable that the Hun had done his best to “sink without
trace” another helpless boat’s crew.
It was mid-afternoon, however, before the Colodia left the vicinity
of the tragedy. There was a desire in the hearts of her crew and
officers to sight the submarine that had committed this atrocity.
Finally, however, the American naval vessel was swung about for
port and began to pick up speed. These destroyers never seem to
go anywhere at an easy pace; they are always “rushed” in their
schedule.
Having given up hope of catching the particular submarine that
had sunk the Newcastle Boy, the Colodia’s lookouts did not,
however, fail to watch for other submersibles. Men stationed in the
tops, on the bridge, and in both bow and stern, trained keen eyes
upon the surrounding sea as the destroyer dashed on her way.
Ikey Rosenmeyer and his special chum, Frenchy Donahue, were
in the bows on watch. Even those two “gabbers,” as Al Torrance
called them, knew enough to keep their tongues still while on duty;
and nobody on the destroyer had keener vision than Ikey and
Frenchy.
Almost together the two hailed the bridge:
“Off the port bow, sir!” while Ikey added “Starboard your helm!”
A great cry went up from amidships. The Colodia escaped the
object just beneath the surface by scarcely a boat’s length. Men
sprang to the depth-bomb arms and the crews to their guns.
But it was not a submarine. A great wave caused by the swift
shifting of the Colodia’s helm, brought the object almost to the
surface.
“A mine!” roared the crew.
The destroyer’s speed was slackened instantly. She swung
broadside to the menace. A few snappy commands, and two of the
deck guns roared.
Instantly a geyser of water and smoke rose from the sea. The
explosion of the mine could have been seen for many miles. Had the
destroyer collided with it——
“We’d have gone to Davy Jones’ locker, sure enough, fellows,”
said Al Torrance. “Those mines the Huns are sowing through these
seas now would blow up the Brooklyn Bridge. Suppose the
Leviathan, troop ship, scraped her keel on that thing?”
There was much discussion all over the destroyer about the mine.
It suggested that the submarine that had sunk the Newcastle Boy
might be a mine-sower. That fact would help identify the submarine,
for all types of German submersibles are not fitted with mine wells.
“You see how it is, George,” said Phil Morgan to their new chum.
“These seas around here are just as safe as a powder factory—just
about! How does it make you feel?”
“Pshaw!” returned Belding, “didn’t I tell you we almost caught a
sub when I was out on the Sirius? I don’t believe the Heinies have
got so many of ’em, after all.”
“Never you mind,” said Whistler. “They’ve got enough if they have
but one, believe me! Just think how we fellows used to gas about
submarines and all that. Before the war, I mean! We never dreamed
any country would use them as the Germans have.”
The tone of the whole crew after the narrow escape from the mine
was intense. They were on the lookout for almost anything to
happen. Before mid-afternoon, while still out of sight of land, the top
hailed the deck officers.
“Steamer in sight, sir!”
The position and course of the stranger was given, and
immediately everybody who had glasses turned them in the
indicated direction. The destroyer’s course was changed a trifle, for
everything that floated on the sea was examined by the Allied patrol.
Soon the high, rusted sides of an ancient tramp steamship hove
into the view of all. She was a two-stack steamer, and despite her
evident age and frowsiness she was making good time toward the
Thames.
“Taking a chance,” Ensign MacMasters said to Whistler and his
friends. “That is what she is doing. She’s not even camouflaged. Her
owner has found some daredevil fellows to run her and will make a
fortune in a single voyage—or lose the ship, one or the other. Great
gamblers, some of these old ship owners.”
“Gamblers with men’s lives,” said George Belding. “I should know.
My father is in the business; but he does not take such chances as
that.”
“Not even with the Redbird?” whispered Whistler anxiously. “I don’t
know about Phoebe and Alice sailing on her.”
“Oh, pshaw! there’s no danger over yonder,” declared George.
“We’ve driven all the Huns from the Western Atlantic.”
“Hope so,” returned Whistler.
Just then a cry rose from some of the men on deck. The destroyer
was near enough to the tramp steamship now to observe what went
on aboard of her. They saw men running about her deck. Then
followed the “Bang! Bang! Bang!” of her deck guns.
The guns were aimed for the far side of the tramp—the object they
were aiming at being out of sight. But the destroyer’s crew knew
what that fusillade meant.
“A sub! She’s got a sub under her guns!” was the yell that rose all
over the Colodia.
Swift orders from the bridge and instantly the destroyer shot ahead
like a mettlesome horse under spur and whip.
CHAPTER XI—ACTION
If action was what George Belding craved, he was getting it.
Everybody aboard the United States destroyer Colodia was on the
alert as the craft leaped ahead to full speed for the spot where the
rusty-sided tramp steamship was popping away with her deck guns
at some object as yet not in view from the destroyer.
The merchant ship was being conned on a zig-zag course,
evidently in an attempt to dodge an expected torpedo. Her hull hid
whatever she was shooting at from the crew of the Colodia; but the
latter did not doubt the nature of the big ship’s erratic course.
At top speed the Colodia rushed to the fray, and on suddenly
rounding the stern of the tramp, a great shout rose from the boys
ranged along the destroyer’s rail:
“There she is!”
The cry was drowned by the salvo of guns discharged at the
conning tower of the German submersible not more than a thousand
yards from the tramp ship. The position of the German craft had
been excellent at first for a shot at the merchant vessel; but her first
torpedo had evidently missed its objective. Now with the destroyer in
view, the Hun let drive a second missile and then began to
submerge.
The torpedo’s wake could be seen by the lookouts on the Colodia
the instant it left its tube. The tramp vessel evaded the explosive; but
the destroyer was directly in the torpedo’s path.
There was real danger at this moment. Quickly swerved as she
might be, it was not at all sure that the Colodia could escape the
torpedo. Every man and boy aboard was at his station; among them
Al Torrance was placed at the starboard rail. He was armed, like
many of his mates, with a rifle.
As the destroyer shot across the path of the torpedo Torry fitted
the butt of his rifle into the hollow of his shoulder, huddled his cheek
against the stock, and brought the cross-sights of the rifle full upon
the sharklike projectile.
The rifle report was almost instantaneous with the roar of the
torpedo. The latter blew up not twenty yards from the destroyer’s rail!
“Hi! Hi! Hi!” yelled the mates of the keen-sighted Torrance.
“Well done!” called the officer of the watch through his
megaphone. “Well done, Torrance!”
The whole crew cheered again, and Al’s flaming face
acknowledged their appreciation. Mr. MacMasters came quickly to
wring the lad’s hand in appreciation.
“Good for you, Torrance,” he said. “Your name goes down on the
log for that.”
“Aw, she wouldn’t have hit us anyway,” said Al, quite overcome by
so much praise.
“Never mind. It showed accurate marksmanship and good work,
too. Those autoprojectiles are dangerous to leave drifting about the
seas. You get a good mark, my boy.”
Meanwhile the Colodia, swerving not a hair from her course,
reached and overran the spot where the submersible had sunk. The
order rang out and the depth bomb was dropped. Then the destroyer
scurried out of the way to escape the effect of the deep-down
explosion.
Up from the depths rose a mound of muddy water. It rose twenty
feet above the surface, and the spray shot twice as high. The
thundering explosion shook the running destroyer in every part. The
effect of the discharge upon what was under the sea must have
been terrible.
Half a mile away the Colodia swerved and circled, to pass again
over the spot where the bomb had been dropped. The boys leaned
over the rails to watch for anything in the water that might prove that
the submarine had been wrecked. There was not a bit of wreckage;
but suddenly Ikey Rosenmeyer shrieked:
“Oil! Oil! Oh, bully! Oil!”
A roar of other voices took up the cry. Great bubbles of oil rose to
the surface. The Colodia passed over a regular “slick” of fluid that
could mean nothing but that the tanks of the submersible had been
ripped open by the explosion of the depth bomb.
Morgan found George Belding standing beside him and looking
back at the oil-streaked waves with a very serious visage.
“What’s on your mind?” asked the Seacove lad.
“It seems terrible, doesn’t it, Phil?” said Belding. “All those fellows!
Gone like that!” and he snapped his fingers.
“Well,” returned Whistler, “you wanted action, didn’t you? Now I
guess you’ve had enough for a while.”
“I believe you,” agreed his friend solemnly.
But the work and life of the boys on the destroyer was not
altogether made up of such scenes and incidents as these that have
been related. Just at this time the troop ships were coming across
from America in great convoys and the Colodia sometimes had less
than half a day in port between trips. Four or five hours ashore in the
English port, or at Brest where the greater number of ships from
America landed their freight and human cargoes, was the utmost
freedom that the Navy Boys and their mates secured.
There were extra calls, now and then, like these which have been
related herein. When an S O S call is picked up by shore or ship
radio, every Naval vessel within reach is sure to make for the point of
peril.
The life was not altogether exciting, however, for there were many
days of tedious watching and waiting in which it seemed that the Hun
boats had all scurried back to their bases and the patrols scarcely
raised a porpoise, much less one of the “steel sharks of the sea.”
At Brest, well along in the month following the introduction of
George Belding to the Colodia, the young fellow from New York got a
cablegram from his father mentioning the date of the Redbird’s
sailing for Bahia with his own family and Philip Morgan’s sisters
aboard.
Whether the treasure of gold coin was to be part of the ship’s
burthen or not, the cablegram did not state. George had written his
father about his lost letters and papers and of the probability that the
knowledge of the treasure would reach those Germans who would
consider the ship bound for South America, and all she carried, their
legitimate prey.
If information of the treasure of gold coin had been sent by the spy
from the Zeppelin to his associates in the United States, there might
be already afoot a plot to get possession of Mr. Belding’s gold. The
boys of the Colodia had not heard of the capture of the spy who had
disappeared in George Belding’s uniform. Much as they had inquired
in England, they had been able to learn absolutely nothing.
Phil Morgan had even been to see Franz Eberhardt at the port
hospital where the young German was confined while his arm was
being skilfully treated by the English surgeons. Later the German
youth had been taken to an internment camp in one of the back
shires. Before he had gone Whistler had tried to get him to talk again
about “Cousin Emil.” But Franz had become wary.
He was no longer acting “the schmardie,” as Hans Hertig had
called him. He had begun to see something of England and had
learned something of the character of the English. To be a prisoner,
and well treated as he was, was a much more serious situation than
had at first appeared.
But he refused to say anything at all of Cousin Emil. Whether it
really was Franz Eberhardt’s cousin with whom the Navy Boys and
“Willum” Johnson had had their adventure, the fact remained that as
far as the boys knew, a German spy was at large in England, And he
had information in his possession that might possibly injure Mr.
Belding and his affairs.
The Seacove boys were all now interested in the sailing of the
Redbird. If Whistler’s two sisters alone had been sailing for Bahia the
others would have felt a personal anxiety in the matter.
“Wish the old Colodia was going to convoy that Redbird,” Al
Torrance said. “Eh, fellows?”
“By St. Patrick’s piper that played the last snake out of Ireland!”
declared Frenchy Donahue, “’twould be the foinest of luck if she
was.”
“Oi! oi! Ain’t it so?” murmured Ikey. “And that Alice Morgan such a
pretty girl! I hope that Redbird gets to Bahia safe.”
“As far as we can hear,” said Whistler cheerfully, “there are neither
submarines nor raiders now in the Western Atlantic. They seem to
have been chased out, boys.”
This supposition, however, did not prove to be founded on fact; for
on the very next occasion that the Colodia was in the French port,
Brest, there was much excitement regarding a new German raider
reported to have got out of Zeebrugge and run to the southward,
doing damage on small craft along the French coast. This was
before the British Captain Carpenter with the Vindictive bottled up
that outlet of German ships.
Some denied that it was a raider at all, but a big, new submarine
that was built with upperworks to look like a steam carrier when she
was on the surface. However, she had a name, it being the Sea
Pigeon, instead of a letter and number. The whole fleet of destroyers
was soon on the lookout for this strange vessel, and the American
commanders offered liberal rewards to the owners of the sharp eyes
who first spotted the new Hun terror of the seas.
The Colodia went to sea to meet a new convoy from America, “all
set” as the boys said, to make a killing if they ran across the Sea
Pigeon.
“Well, we got the Graf von Posen,” Ikey Rosenmeyer said, with
cheerful optimism, “so why not this here Pigeon ship? We’re the
boys that bring home the bacon, aren’t we?”
“Aw, Ikey!” groaned Frenchy Donahue. “Can’t you ever forget you
were brought up in a delicatessen shop? ‘Bring home the bacon,’
indade!”
CHAPTER XII—WIRELESS WHISPERS
On duty with the morning watch, just after sick call at half past eight,
Phil Morgan and George Belding met right abaft the radio station.
There was half an hour or so before the divisions would be piped to
fall in for muster and inspection, and the two friends could chat a
little.
“Well, the folks are on the sea, as we are, Phil, if the Redbird
sailed as per schedule,” Belding said.
“I sha’n’t feel really happy till we hear they are at Bahia,”
responded Whistler, shaking his head.
“Right-o! But the Redbird is a fine ship, and just as safe as a
house.”
“But she’s a sailing ship—and slow.”
“Not so slow, if anybody should ask you,” returned Belding smiling.
“A four-master?”
“And square rigged. A real ship. No schooner-rig, or half-and-half.
Captain Jim Lowder thinks she is the finest thing afloat. Of course,
she is thirty years old; but she was built to last. Regular passenger
sailing ship, with a round-the-world record that would make the
British tea ships sit up and take notice. Her cabin finished in
mahogany, staterooms in white enamel—simply fine!”
“I didn’t know they had such sailing ships,” said Whistler in
wonder.
“Oh, there are a few left. The Huns haven’t sunk them all. Nor
have the steam craft put such as the Redbird out of commission. You
couldn’t get Captain Jim Lowder to take out a steam vessel. He
abominates the ‘iron pots,’ as he calls the steam freighters.
“But sailing ships like the Redbird are kept out of the European
trade if possible. Even Captain Lowder must admit that a sailing ship
is not in the game of fighting subs.”
“That is the way I feel. Wish your folks and mine were going south
on a steamer, George.”
“No fear. They will be all right,” was Belding’s reassuring reply.
“Just the same I’d feel a lot better if all the Hun subs and raiders
were bottled up at their bases.”
“By the way,” said Belding, “what do you think of this Sea Pigeon
we hear so much talk about? Think there is such a craft?”
“Why not? We know that some kind of an enemy vessel slipped
along south and evaded our patrol, leaving a trail of sunken and
torpedoed ships behind her.”
“But a huge submarine, with superstructure and all——”
“That is only a guess,” laughed Whistler. “Personally, I believe this
Sea Pigeon is a raider and no submarine at all. A submarine of the
size reported would use up a lot of petrol.”
“That’s all right,” said Belding quickly. “She could get supplies
down along the Spanish coast. There are plenty of people that way
friendly to the Germans.”
At the moment they heard the sudden chatter of the radio
instrument. Belding turned instantly to put his head into the little
room. The operator smiled and nodded to him.
“Something doing,” he muttered. “One of you chaps want to take
this message to the com?”
“Let’s have it,” said Whistler, quickly, holding out his hand.
“I’d like to put on that harness myself,” said Belding. “We had a
wireless on the roof of our house in New York before the war.
Government made us wreck it.”
“Jinks!” exclaimed Whistler, waiting for the operator to write out the
message received and slip it into an envelope. “Do you know how to
work one of these things, George?”
“I know something about it,” admitted Belding. “What’s it all
about?” he asked the operator.
“Orders for us,” said the man. “You’ll know soon enough. We’re
due for new cruising grounds, boys. But keep your tongues still till
the com eases the information to all hands.”
He had finished the receipt and “repeat” of the message. Whistler
took the envelope and sprang away with it to the commander’s
quarters.
He knew by the expression on Mr. Lang’s face when he scanned
the message that there was something big in view. The commanding
officer of the Colodia swiftly wrote a reply and gave it to Whistler for
the radio man. Belding was still hanging about the wireless room. His
face was flushed and his eyes shone.
“Do you know what it is all about, Phil?” he whispered.
“Not a thing. But the Old Man,” said Whistler, “is some excited.”
Rumor that changed orders had reached the Colodia spread
abroad before muster and inspection. The usual physical drills were
gone through while the boys’ minds were on tiptoe. Even the order at
four bells to relieve the wheel and lookout startled the crew, so
expectant were they.
But nothing happened until just before retreat from drill at eleven-
thirty. Commander Lang then made his appearance. He went to the
quarter and addressed the crew.
“We have been honored by an order to go freelancing after a
suspected vessel, supposed to be a German raider, last and recently
reported to be off the Azores,” he said. “Because we were successful
some months ago in taking the Graf von Posen, we are assigned to
this work.”
At this point the crew broke into cheers, and with a smile the
commanding officer waved his hand for the boatswain’s mates to
pipe retreat.
The Colodia was at this time sailing within sight of half a dozen
other destroyers bound out to pick up the expected convoy. After a
little her wireless crackled a curt “good-bye” to her companions, and
the Colodia changed her course for a more southerly one.
The chances, for and against, of overhauling the Sea Pigeon were
volubly discussed, from the commander’s offices to the galley, and
everybody, including the highest officer and the most humble
steward’s boy, had a vital interest in the destroyer’s objective.
To attempt to chase a ship like this German raider about the ocean
was a most uncertain task.
“But if the luck of the Colodia runs true to form,” Al Torrance
expressed it, “we shall turn the trick.”
“That this Sea Pigeon is a raider and not a submarine, seems to
be an established fact,” Belding said. “Sparks got some private
information from the radio station at the Azores and says the ship is
a fast steamer made over from some big, fat Heinie’s steam yacht he
used to race before the war. She has just sunk a wheat ship from the
Argentine.”
“Sparks” is the nickname usually applied to the radio operator
aboardship, and George Belding was quite friendly with the chief of
the wireless force on the destroyer.
“George gets all these ‘wireless whispers’ because he has a pull,”
said Whistler, smiling. “If anything ever happens to Sparks, I expect
we’d see George in there with his head harnessed.”
“And it’s no bad job!” cried Al enthusiastically. “I’ve often wished I
could listen in on this radio stuff.”
“Oi, oi! That just goes to show the curiosity of you,” declared Ikey
Rosenmeyer, with serious air. “It is a trait of your character that
should be suppressed, Torry.”
CHAPTER XIII—THE SUPER-SUBMERSIBLE
The boys from Seacove and George Belding—but especially the last
and Phil Morgan—had a second topic of daily conversation quite as
interesting, if not as exciting, as that of the German raider, in chase
of which the Colodia was now driving at top-speed into the
southwest.
This topic was the fruitful one of the Redbird and her cruise to
Bahia. If the big sailing ship had left New York on the date promised,
then the Belding family and Phil’s sisters would now be off Hatteras
—perhaps even farther south.
“For you can believe me, Belding,” Al Torrance declared earnestly,
and speaking with all the sea-wisdom acquired during his naval
experience, “that Captain Lawdor would not sail right out across the
Gulf Stream and make the Azores or the Canaries a landfall, as he
might have done before Hun submarines got to littering up the
Atlantic as they do now.”
“We cannot be altogether sure of his course,” murmured George
Belding.
“Sailing vessels hate to head into the current of the Gulf Stream,”
added Whistler, likewise in doubt.
“You chaps are determined to expect the very worst that can
happen, aren’t you? Like a fellow going to have a tooth extracted,”
said Al, with disgust. “Now, listen here! It stands to reason that news
of this new raider, the Sea Pigeon, or whatever it is they call her, was
transmitted to the other side of the periscope pond. George’s father
and the captain of the Redbird would be warned before they sailed
from New York of this new danger—if not afterward, by wireless. Of
course the ship has a radio plant, hasn’t she?”
“Of course,” agreed the shipowner’s son.
“Nuff said! They never in this world, then, would take the usual
course of sailing ships for South America. They would not cross the
Gulf Stream. It will take the Redbird a little longer to buck the
northerly set of the current; but that is what Captain Lawdor will do,
take it from me! I figure they are now about off Hatteras, following
the usual course of the coasting vessels.”
“Not much leeway for a big sailing ship,” muttered George.
“Better hugging the shore, even stormy old Hatteras, which we
know something about, eh, fellows?” added Al, “than dodging subs
and raiders out in the broad Atlantic.”
He had an old chart and was marking off the possible course of
the Redbird with a lead pencil.
“Good work, Torry,” said Frenchy Donahue. “It’s navigation officer
you’ll be next.”
They were all five deeply interested, and each day they worked
out the probable course of the sailing ship, as well as figuring the
distance she probably had sailed during the elapsed twenty-four
hours.
“I only hope,” George Belding said, “that we overtake this Sea
Pigeon and finish her before her commander takes it into his head to
steam across the ocean to the western lanes of travel. If the raider
should intercept father’s ship——”
“Ah, say!” cried Frenchy, “that ‘if’ is the biggest word in the
language, if it has only two letters. Don’t worry, Belding.”
That advice was easy to give. George and Whistler remained very
anxious, however; indeed, they could not help being. Nor did the
activities aboard the destroyer during the next few days much take
their thought off the Redbird and her company and cargo.
They talked but little—even to their closest boy friends—about the
possibility of there being a great store of coined gold aboard the
Redbird. Just the same, this fact they knew would cause the ship to
be an object of keen attraction to any sea-raider who might hear of it.
The spy from the Zeppelin had secured George Belding’s letters in
which the gold treasure was mentioned and Mr. Belding’s voyage in
the Redbird explained. More than a month had elapsed between the
spy-chase behind the little English port and the sailing of the square-
rigged ship from New York for Bahia, Brazil.
“And you know,” George once said, “a whole lot can happen in a
month. Those Germans have an ‘underground telegraph’ that beats
anything the negroes and their Northern sympathizers had during,
and previous to, our Civil War.”
“Aw, don’t bring up ancient history,” growled Al, who tried to be
cheerful, but who found it hard work when the older boys seemed
determined to see the dark side of the shield. “I’ve forgotten ’most all
I ever knew about every war before this one we’re into with both feet
—and then some!”
“Sure, Torry,” put in Frenchy Donahue, “don’t you remember the
war of that showman who antedated Barnum—the one they say got
a herd of elephants over the Alps to fight for him?”
“Oi, oi! Hannibal!” cried Ikey.
“Say! it would take a friend of yours to do that, Frenchy,” said Al in
disgust. “I’ve always had my doubts about that fellow, Hannibal.”
“Besides,” went on Ikey, going back to Belding’s statement, “it’s
nothing to do with ‘underground’ or any other telegraph. The
Germans use wireless. If that spy got news across the pond——”
“Right-o!” broke in George, with increased good-nature and an
answering smile. “But let’s ‘supposing.’ That spy has had ample time
to transmit to friends on the other side of the ocean information
about the gold my father is carrying to South America.”
“Why,” said Whistler, slowly unpuckering his lips, “he might even
have crossed to New York himself by this time—if the British didn’t
catch him.”
“If they had caught him wouldn’t we have been told?” asked
Belding quickly.
“How? By whom?” demanded Whistler.
“Say!” declared Al vigorously, “the British War Office makes a clam
look like it had a tongue hung in the middle and running at both
ends!”
“Now you’ve said something!” muttered Frenchy.
“That’s right! The world doesn’t even know how many submarines
have been sunk and captured, already yet,” declared Ikey excitedly.
“And we won’t know, it’s likely, till the end of the war.”
“What’s the odds?” growled Al.
“You got to hand it to them,” sighed Whistler. “The British have
great powers of self-restraint.”
“You said it!” again put in Frenchy.
“Well,” Ikey said, more moderately, “if that chap that came near
sending Belding here west, was that schmardie’s brother——”
“Cousin!” interposed Whistler.
“Well—anyhow and anyway—Emil Eberhardt—I say!” cried Ikey,
“he might have got free and gone over to New York by submarine, or
someway, like Whistler says.”
“What do you suppose he’d do if he wanted to get that money off
the Redbird?” asked Frenchy, big-eyed.
“Ask us an easier one,” begged Al Torrance.
“You kids are letting your imaginations run away with you,” put in
Phil Morgan.
But in secret the two older boys—Belding and Whistler—did not
consider the idea of the spy reaching New York before the Redbird
sailed at all impossible.
“That chap with the broken arm we took off the wrecked Zep,”
Belding remarked once to Morgan, “told you his cousin, the ‘super-
spy,’ was bound for America, didn’t he?”
“He dropped such a hint,” admitted the Seacove lad. “But pshaw!
we don’t even know that Franz Eberhardt referred to the fellow we
had our adventure with.”
“I know! I know!” muttered George Belding. “But I do wish Willum
Johnson, the strong man, had got his hands on that spy.”
“‘If wishes were horses——’”
“Sure! And perhaps it is all right. At any rate, father must have got
my letter before he sailed, in which I told him all about losing the
papers and warning him about German plotters. Of course he must
have got that letter.”
But this thought would have afforded them little comfort had the
two friends known that the ship which bore George Belding’s letter of
warning had been sunk off the Irish coast by a German U-boat, and
that that particular freight or mail for the United States would
probably not be recovered until after the war.
The Colodia touched at St. Michael and then at Fayal, receiving in
both ports information of the escapades of the new raider. Lastly she
had been heard of far to the west.
Perhaps she was going across the ocean to prey on the American
coastwise trade! This was a suggestion that put the Seacove boys
and Belding on edge.
There was, however, something rather uncertain about the stories
regarding the Sea Pigeon. Some of the merchant crews that had
already met her, declared her to be a huge new submarine—a
submersible that looked like a steam freighter when she was afloat,
and that she was all of three hundred feet long.
“Some boat, that!” observed Mr. MacMasters. “We’ve seen ’em
with false upperworks, boys. But you know, even the Deutschland
was no such submarine as this one they tell about.”
Whistler put forth the idea that there were two ships working in
these waters; but not many accepted this until, the day after they left
Fayal, and the destroyer was traveling west, Sparks suddenly picked
up an S O S from the south. The Argentine steamship Que Vida was
sending out frantic calls for help. She was being shelled by a
monster submarine two hundred miles off the port of Funchal of the
Madeiras.
“This is the real thing—Sea Pigeon or not!” the radio operator
confided to George Belding. “She’s the super-sub we’ve been
hearing about. The operator on this Buenos Aires’ ship says she
came right up out of the sea at dawn and opened fire with guns fore
and aft. Has used a torpedo, and has upperworks like a regular
honest-to-goodness steam freighter.
“There! He’s off again!” he exclaimed, as the radio began to spark,
and he turned back to the machine.
So was the Colodia off again, and at full speed, dashing away in
quest of the Que Vida and the great submersible that had attacked
her.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like