0% found this document useful (0 votes)
290 views

Python For Scientific Computing and A I Information

Uploaded by

angeline
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
290 views

Python For Scientific Computing and A I Information

Uploaded by

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

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/369596858

Python for Scientific Computing and ArtificiaI Intelligence (Information), CRC


Press (2023)

Book · March 2023


DOI: 10.1201/9781003285816

CITATIONS READS

0 286

1 author:

Stephen Lynch
Manchester Metropolitan University
115 PUBLICATIONS   1,694 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Quantum technologies based on ultracold ions (atoms). Precision measurements. View project

Dynamical Systems with Applications using Mathematica, 2nd Edition View project

All content following this page was uploaded by Stephen Lynch on 29 March 2023.

The user has requested enhancement of the downloaded file.


CRC Press Web Pages

Information about the book:

Summary
Table of Contents
Foreword
Preface
Index

Python for Scientific Computing and Artificial Intelligence is split into 3 parts: in Section 1, the reader is introduced to
the Python programming language and shown how Python can aid in the understanding of advanced High School
Mathematics. In Section 2, the reader is shown how Python can be used to solve real-world problems from a broad
range of scientific disciplines. Finally, in Section 3, the reader is introduced to neural networks and shown how
TensorFlow (written in Python) can be used to solve a large array of problems in Artificial Intelligence (AI).
This book was developed from a series of national and international workshops that the author has been delivering
for over twenty years. The book is beginner friendly and has a strong practical emphasis on programming and
computational modelling.
Features:
• No prior experience of programming is required.
• Online GitHub repository available with codes for readers to practice.
• Covers applications and examples from biology, chemistry, computer science, data science, electrical and
mechanical engineering, economics, mathematics, physics, statistics and binary oscillator computing.
• Full solutions to exercises are available as Jupyter notebooks on the Web.

Support Material
GitHub Repository of Python Files and Notebooks:
https://github.com/proflynch/CRC-Press/
Solutions to All Exercises:
Section 1: An Introduction to Python: https://drstephenlynch.github.io/webpages/Solutions_Section_1.html
Section 2: Python for Scientific Computing: https://drstephenlynch.github.io/webpages/Solutions_Section_2.html
Section 3: Artificial Intelligence: https://drstephenlynch.github.io/webpages/Solutions_Section_3.html PTO
Python for Scientific
Computing and Artificial
Intelligence
Python for Scientific Computing and Artificial Intelligence is split into 3 parts: in Section 1,
the reader is introduced to the Python programming language and shown how Python can aid in
the understanding of advanced High School Mathematics. In Section 2, the reader is shown how
Python can be used to solve real-world problems from a broad range of scientific disciplines.
Finally, in Section 3, the reader is introduced to neural networks and shown how TensorFlow
(written in Python) can be used to solve a large array of problems in Artificial Intelligence (AI).

This book was developed from a series of national and international workshops that the author
has been delivering for over twenty years. The book is beginner friendly and has a strong practi-
cal emphasis on programming and computational modeling.

Features:

• No prior experience of programming is required.


• Online GitHub repository available with codes for readers to practice.
• Covers applications and examples from biology, chemistry, computer science, data science,
electrical and mechanical engineering, economics, mathematics, physics, statistics and bi-
nary oscillator computing.
• Full solutions to exercises are available as Jupyter notebooks on the Web.

In 2022, Stephen Lynch was named a National Teaching Fellow, which celebrates and recognises
individuals who have made an outstanding impact on student outcomes and teaching in higher
education. He won the award for his work in programming in the STEM subjects, research
feeding into teaching, and widening participation (using experiential and object-based learn-
ing). Although educated as a pure mathematician, Stephen’s many interests now include applied
mathematics, cell biology, electrical engineering, computing, neural networks, nonlinear optics
and binary oscillator computing, which he co-invented with a colleague. He has authored 2 in-
ternational patents for inventions, 8 books, 4 book chapters, over 40 journal articles, and a few
conference proceedings. Stephen is a Fellow of the Institute of Mathematics and Its Applica-
tions (FIMA) and a Senior Fellow of the Higher Education Academy (SFHEA). He is currently
a Reader with MMU and was an Associate Lecturer with the Open University from 2008-2012.
In 2010, Stephen volunteered as a STEM Ambassador, in 2012, he was awarded MMU Public
Engagement Champion status, and in 2014 he became a Speaker for Schools. He runs national
workshops on “Python for A-Level Mathematics and Beyond,” and international workshops on
“Python for Scientific Computing and TensorFlow for Artificial Intelligence.” He has run work-
shops in China, Malaysia, Singapore, and the USA.
Chapman & Hall/CRC
The Python Series
About the Series
Python has been ranked as the most popular programming language, and it is widely used in
education and industry. This book series will offer a wide range of books on Python for students
and professionals. Titles in the series will help users learn the language at an introductory and
advanced level, and explore its many applications in data science, AI, and machine learning.
Series titles can also be supplemented with Jupyter notebooks.

Image Processing and Acquisition using Python, Second Edition


Ravishankar Chityala, Sridevi Pudipeddi
Python Packages
Tomas Beuzen and Tiffany-Anne Timbers
Statistics and Data Visualisation with Python
Jesús Rogel-Salazar
Introduction to Python for Humanists
William J.B. Mattingly
Python for Scientific Computing and Artificial Intelligence
Stephen Lynch

For more information about this series please visit: https://www.crcpress.com/Chapman--HallCRC/


book-series/PYTH
Python for Scientific
Computing and Artificial
Intelligence

Stephen Lynch
Manchester Metropolitan University, United Kingdom
First edition published 2023
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742

and by CRC Press


4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN

CRC Press is an imprint of Taylor & Francis Group, LLC

© 2023 Stephen Lynch

Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot
assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers
have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright
holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowl-
edged please write and let us know so we may rectify in any future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or
utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including pho-
tocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission
from the publishers.

For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the
Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are
not available on CCC please contact mpkbookspermissions@tandf.co.uk

Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for
identification and explanation without intent to infringe.

Library of Congress Cataloging-in-Publication Data


[Insert LoC Data here when available]

ISBN: 978-1-032-25873-7 (hbk)


ISBN: 978-1-032-25871-3 (pbk)
ISBN: 978-1-003-28581-6 (ebk)

DOI: 10.1201/9781003285816

Typeset in LM Roman
by KnowledgeWorks Global Ltd.

Publisher’s note: This book has been prepared from camera-ready copy provided by the authors
To my family,
my brother Mark and my sister Jacqueline,
my wife Gaynor,
and our children, Sebastian and Thalia,
for their continuing love, inspiration and support.
Contents

Foreword xiii

Preface xv

Section I An Introduction to Python

Chapter 1 ! The IDLE Integrated Development Learning Environment 3

1.1 INTRODUCTION 4
1.1.1 Tutorial One: Using Python as a Powerful Calculator (30
Minutes) 5
1.1.2 Tutorial Two: Lists (20 Minutes) 7
1.2 SIMPLE PROGRAMMING IN PYTHON 8
1.2.1 Tutorial Three: Defining Functions (30 Minutes) 9
1.2.2 Tutorial Four: For and While Loops (20 Minutes) 11
1.2.3 Tutorial Five: If, elif, else constructs (10 Minutes) 11
1.3 THE TURTLE MODULE AND FRACTALS 11

Chapter 2 ! Anaconda, Spyder and the Libraries NumPy, Matplotlib and


SymPy 21

2.1 A TUTORIAL INTRODUCTION TO NUMPY 23


2.1.1 Tutorial One: An Introduction to NumPy and Arrays (30
Minutes) 23
2.2 A TUTORIAL INTRODUCTION TO MATPLOTLIB 25
2.2.1 Tutorial Two: Simple Plots using the Spyder Editor Window
(30 minutes) 25
2.3 A TUTORIAL INTRODUCTION TO SYMPY 28
2.3.1 Tutorial Three: An Introduction to SymPy (30 Minutes) 28

vii
viii ! Contents

Chapter 3 ! Jupyter Notebooks and Google Colab 33

3.1 JUPYTER NOTEBOOKS, CELLS, CODE AND MARKDOWN 33


3.2 ANIMATIONS AND INTERACTIVE PLOTS 37
3.3 GOOGLE COLAB AND GITHUB 41

Chapter 4 ! Python for AS-Level (High School) Mathematics 45

4.1 AS-LEVEL MATHEMATICS (PART 1) 46


4.2 AS-LEVEL MATHEMATICS (PART 2) 50

Chapter 5 ! Python for A-Level (High School) Mathematics 61

5.1 A-LEVEL MATHEMATICS (PART 1) 62


5.2 A-LEVEL MATHEMATICS (PART 2) 68

Section II Python for Scientific Computing

Chapter 6 ! Biology 81

6.1 A SIMPLE POPULATION MODEL 81


6.2 A PREDATOR-PREY MODEL 84
6.3 A SIMPLE EPIDEMIC MODEL 87
6.4 HYSTERESIS IN SINGLE FIBER MUSCLE 89

Chapter 7 ! Chemistry 95

7.1 BALANCING CHEMICAL-REACTION EQUATIONS 95


7.2 CHEMICAL KINETICS 97
7.3 THE BELOUSOV-ZHABOTINSKI REACTION 99
7.4 COMMON-ION EFFECT IN SOLUBILITY 101

Chapter 8 ! Data Science 107

8.1 INTRODUCTION TO PANDAS 107


8.2 LINEAR PROGRAMMING 110
8.3 K-MEANS CLUSTERING 115
8.4 DECISION TREES 119
Contents ! ix

Chapter 9 ! Economics 125

9.1 THE COBB-DOUGLAS QUANTITY OF PRODUCTION MODEL 126


9.2 THE SOLOW-SWAN MODEL OF ECONOMIC GROWTH 128
9.3 MODERN PORTFOLIO THEORY (MPT) 130
9.4 THE BLACK-SCHOLES MODEL 133

Chapter 10 ! Engineering 139

10.1 LINEAR ELECTRICAL CIRCUITS AND THE MEMRISTOR 139


10.2 CHUA’S NONLINEAR ELECTRICAL CIRCUIT 142
10.3 COUPLED OSCILLATORS: MASS-SPRING MECHANICAL
SYSTEMS 144
10.4 PERIODICALLY FORCED MECHANICAL SYSTEMS 146

Chapter 11 ! Fractals and Multifractals 153

11.1 PLOTTING FRACTALS WITH MATPLOTLIB 153


11.2 BOX-COUNTING BINARY IMAGES 158
11.3 THE MULTIFRACTAL CANTOR SET 160
11.4 THE MANDELBROT SET 162

Chapter 12 ! Image Processing 167

12.1 IMAGE PROCESSING, ARRAYS AND MATRICES 168


12.2 COLOR IMAGES 169
12.3 STATISTICAL ANALYSIS ON AN IMAGE 170
12.4 IMAGE PROCESSING ON MEDICAL IMAGES 172

Chapter 13 ! Numerical Methods for Ordinary and Partial Differential


Equations 177

13.1 EULER’S METHOD TO SOLVE IVPS 178


13.2 RUNGE KUTTA METHOD (RK4) 179
13.3 FINITE DIFFERENCE METHOD: THE HEAT EQUATION 181
13.4 FINITE DIFFERENCE METHOD: THE WAVE EQUATION 184
x ! Contents

Chapter 14 ! Physics 191

14.1 THE FAST FOURIER TRANSFORM 192


14.2 THE SIMPLE FIBER RING (SFR) RESONATOR 194
14.3 THE JOSEPHSON JUNCTION 196
14.4 MOTION OF PLANETARY BODIES 198

Chapter 15 ! Statistics 203

15.1 LINEAR REGRESSION 203


15.2 MARKOV CHAINS 207
15.3 THE STUDENT T-TEST 210
15.4 MONTE-CARLO SIMULATION 214

Section III Artificial Intelligence

Chapter 16 ! Brain Inspired Computing 223

16.1 THE HODGKIN-HUXLEY MODEL 224


16.2 THE BINARY OSCILLATOR HALF-ADDER 227
16.3 THE BINARY OSCILLATOR SET RESET FLIP-FLOP 231
16.4 REAL-WORLD APPLICATIONS AND FUTURE WORK 234

Chapter 17 ! Neural Networks and Neurodynamics 241

17.1 HISTORY AND THEORY OF NEURAL NETWORKS 241


17.2 THE BACKPROPAGATION ALGORITHM 245
17.3 MACHINE LEARNING ON BOSTON HOUSING DATA 247
17.4 NEURODYNAMICS 250

Chapter 18 ! TensorFlow and Keras 255

18.1 ARTIFICIAL INTELLIGENCE 256


18.2 LINEAR REGRESSION IN TENSORFLOW 257
18.3 XOR LOGIC GATE IN TENSORFLOW 259
18.4 BOSTON HOUSING DATA IN TENSORFLOW AND KERAS 261
Contents ! xi

Chapter 19 ! Recurrent Neural Networks 267

19.1 THE DISCRETE HOPFIELD RNN 267


19.2 THE CONTINUOUS HOPFIELD RNN 270
19.3 LSTM RNN TO PREDICT CHAOTIC TIME SERIES 273
19.4 LSTM RNN TO PREDICT FINANCIAL TIME SERIES 278

Chapter 20 ! Convolutional Neural Networks, TensorBoard and Further


Reading 285

20.1 CONVOLVING AND POOLING 285


20.2 CNN ON THE MNIST DATASET 288
20.3 TENSORBOARD 290
20.4 FURTHER READING 292

Chapter 21 ! Answers and Hints to Exercises 299

21.1 SECTION 1 SOLUTIONS 299


21.2 SECTION 2 SOLUTIONS 303
21.3 SECTION 3 SOLUTIONS 306

Index 309
Foreword

I came to know about the author through his previous text: Dynamical Systems with
Applications using Python, Springer International Publishing (2018). I simply loved
this book as it helped me to enhance my knowledge in both Python and Dynamical
Systems. I was so pleased with this book that in collaboration with a senior colleague
in Dynamical Systems, we developed a set of two-semester courses with the same
title as the book. We also invited Stephen to offer for us a short course on Python for
Scientific Computing and TensorFlow for Artificial Intelligence (AI). We were very
pleased that he accepted our offer, as he has been in demand to offer these courses
all over the world. For Pitt, he first offered it online (due to Covid) in summer 2021,
and then in person on Pitt campus in summer 2022. Both of these courses were huge
successes with several hundreds of participants each time. These delegates came from
a wide variety of backgrounds: professors, scientists and engineers from a wide range
of disciplines in industry & government labs, and both graduate and undergraduate
students in a broad range of disciplines. The excellent textbook plus Stephen’s natural
teaching talents made the course very popular. We plan to offer it on a regular basis
as long as Stephen is up for it.
Based on these experiences, I was naturally very eager and excited to read
Stephen’s newest (present) text, and I am not surprised to see that this volume
is also a masterpiece! The topics and approach taken in this book are indeed like no
other! The first section provides an introduction to Python for complete novices and
goes on to show how it can be used for teaching High School mathematics. Readers
are shown how to access Python through the IDLE platform, Spyder and jupyter
notebooks through Anaconda, and cloud computing using Google Colab. The second
section covers a vast array of examples from the scientific fields of biology, chemistry,
data science, economics, engineering, fractals & multifractals, image processing, nu-
merical methods for ordinary and partial differential equations, physics and statistics.
Each chapter covers several examples from the field and there are exercises with even
more examples at the end of each chapter. The final section covers AI, with brain-
inspired computing and neurodynamics and neural networks following on nicely from
Section 2. The final chapters introduce TensorFlow and Keras, and recurrent and con-
volutional neural networks, respectively. There is further reading for those interested
in cyber security, ethics in AI, the internet of things, natural language processing and
reinforcement learning.
All Python and TensorFlow programs can be downloaded through GitHub and full
worked solutions for all of the exercises are available to view on the Web. Overall, the
book will appeal to a very wide range of readers, from high school, to undergraduate
and postgraduate students in science & engineering, to academics and researchers

xiii
xiv ! Foreword

in industry. Novices to programming and even experienced programmers will all get
something from this book.
Professor Lynch has recently been named
name aa National Teaching Fellow in the
UK. I am not very familiar with UK’s system. But as I understand, this fellowship is
“...to recognize and celebrate individuals who have made an outstanding impact on
student outcomes and the teaching profession...” Based on his other books, and his
courses, I can easily see that Stephen is surely deserving of this recognition. He is an
excellent writer, with an exceptional knack for teaching.
I hope that the reader is as excited as I was to get started on this book.

Professor Givi Peyman, Distinguished Professor of Mechanical Engineering and the


James T. MacLeod Professor in the Swanson School of Engineering at the University
of Pittsburgh., USA.
Preface

This book has developed from a series of university, national and international work-
shops that I have been delivering for over twenty years. It is based on the program-
ming language Python, which is a high-level programming language and supports
functional, imperative, object-oriented and procedural styles of programming. Prob-
ably the most important aspect of Python is that it is open source and completely
free to the user. Python is currently the most popular programming language in the
world from over 600 programming languages, and it is a relatively user-friendly lan-
guage. The book is split into three sections. In Section 1, the reader is introduced to
the Python programming language and it is shown how Python can aid in the under-
standing of A-Level (or High School) Mathematics. In Section 2, the reader is shown
how Python can be used to solve real-world problems from a broad range of scientific
disciplines and in Section 3, the reader is introduced to neural networks and shown
how TensorFlow (written in Python) can be used to solve a large array of problems
in Artificial Intelligence (AI). As well as learning the most popular programming lan-
guage in the world, the reader will also be shown how Python can be used to perform
computational mathematics to solve real-world problems. By the end of the book,
the reader will be able to solve a great many mathematical problems without having
to learn any of the theory of mathematics. The reader should note that the emphasis
of the book is on programming and computational modeling and not on the math-
ematical theory. For those readers who want to know all of the ins-and-outs of the
mathematics—for High School Mathematics, there are recommended books listed in
Section 1, and for Sections 2 and 3, I recommend my other book, Dynamical Systems
with Applications using Python [1], which covers the mathematical theory in some
detail. To keep the book relatively brief, explanations of the syntax of the code are
not included. Instead, the reader is given full working programs and explanations for
syntax are easily discovered on the Web.
Chapter 1 starts with the Python Integrated Development Learning Environment
(IDLE) which can be downloaded at:

https://www.python.org.

In Section 1.1, the reader is introduced to the IPython Console Window, where
users can use Python as a powerful calculator. As every reader is already familiar
with a calculator, I have found this method of teaching the best way to introduce
Python. The Math library (or module) is introduced and the functions available can
be found using the Help features in IDLE. Next, the reader is shown how to define
their own functions in Python—think of adding buttons to your calculator. For and

xv
xvi ! Preface

while loops are introduced by means of simple examples and then if, elif, else con-
structs are discussed. With these three programming structures, one can write any of
the programs listed throughout the book! To further aid in the understanding of how
programs work, the turtle module is introduced in Section 1.3. Readers may have
heard of the Scratch programming language:

https://scratch.mit.edu

developed at MIT, which is the largest free coding community for kids in the world.
Well, the turtle module is similar to Scratch and provides a pictorial representation to
coding and that is why it is so popular with children. In this book, I use turtle to plot
simple colorful fractals using iteration and readers can figure out how the programs
work by slowing down the turtle, plotting early stage fractals, and following the code
by drawing little pictures—this helps greatly when learning to code. Chapter 1 ends
with some exercises and a short bibliography, where interested readers can find more
introductory texts for Python. It is highly recommended that the reader attempts the
exercises at the end of Chapter 1 before moving on to Chapter 2. Note that, solutions
to the exercises, as in all other chapters can be viewed on the Web, the corresponding
URL will appear at the end of each chapter. IDLE is not suitable for scientific com-
putation, for that you will need Anaconda or Google Colab. In Chapter 2, the reader
is shown how to download Anaconda, the world’s most popular data science platform:

https://www.anaconda.com/products/individual,

simply download Anaconda installers for Linux, Mac or Windows. Within this frame-
work, we can launch Spyder (Scientific Python Development Environment) or a
jupyter notebook. Those of you familiar with MATLAB!, R will see similarities with
Spyder and if any readers have ever used Mathematica!, R you will notice a similarity
with both sets of notebooks. Note that within Anaconda there are other tools available
including the R studio for statistics and Orange 3 and Glueviv for data visualization
and data analysis. In this book, I will only be using Spyder and jupyter notebooks
from Anaconda. Note that although Anaconda is well known for Data Science, it
is much more flexible than that, and is ideal for both scientific computing and AI.
Section 2.1 is concerned with numerical Python using the numpy library (or module):

https://numpy.org,

where lists, arrays, vectors, matrices and tensors are introduced. These concepts are
vitally important in neural networks, machine learning and deep learning, which will
all be covered in the third section of the book. The chapter ends with a tutorial
introduction to the matrix plotting library matplotlib:

https://matplotlib.org,
Preface ! xvii

used for creating static, animated and interactive visualizations in Python. As well
as producing colorful plots, readers will be shown how to save their graphs to file in
any format they like (eps, jpeg, png, tiff etc.) and change the resolution of figures
using dpi (dots per inch). They will also be shown how to label axes and figures with
LaTeX symbols:

https://www.caam.rice.edu/˜heinken/latex/symbols.pdf.

In Section 2.2, the symbolic library (or module) sympy is introduced:

https://www.sympy.org/,

by means of example. Sympy is a powerful symbolic computation library for Python


and it enables the reader to perform some complex mathematics. Here the reader
will be shown how to differentiate and integrate without needing to know all
of the mathematical rules. Matrices and simple matrix algebra will be covered
here.
Chapter 3 provides an introduction to both jupyter notebooks, accessed through
Anaconda, and Google Colab jupyter notebooks, which are accessed through cloud
computing. When you download Anaconda onto your machine you take up memory
on your computer. The major benefit with this method is that you do not need access
to the Web to run your Python code when using Spyder. Google Colab, on the other
hand, is cloud computing, where your computations are performed through Google
on the cloud:

https://colab.research.google.com/.

Note that you need a Google account in order to have access to Google Colab. The
major benefit with this method is that you do not use up memory on your own
computer when you run the Python code. Section 3.1 shows the jupyter notebook
interface and readers are shown how to use Markdown to insert titles, text, html code
and LaTeX mathematical equations into a notebook. Code cells are used to insert
Python code. Cells are compiled by hitting SHIFT + ENTER in the cell or by running
the cell using the small play button. In Section 3.2, the reader will be shown how
to load files and figures into a notebook. Examples demonstrating animations and
interactive plots will be listed here. The notebook can be saved as a Python notebook
(filename.ipynb), a Python file (filename.py) or as a html file (filename.html) so the
notebook can be published on the web. In Section 3.3, Google Colab and GitHub will
be introduced and this again will be useful material for what is to come in the third
section of the book. Chapters 4–5 show how Python can be used to give a deeper
understanding of the new A-Level Mathematics syllabus which was introduced to
the UK in 2017. Most of the topics covered here are covered by many high schools
around the world and once more it is demonstrated how Python can be used to solve
mathematical problems without understanding all of the mathematical theorems and
xviii ! Preface

rules. A-Level (and High School) pupils will find these chapters extremely useful for
providing a deeper understanding of the mathematics.
Section 2 of the book covers scientific computation and follows on nicely from
Section 1. Chapter 6 presents examples from the field of Biology starting with a sim-
ple single population model of insects using the logistic model and iteration. Fixed
points, stability and bifurcation diagrams are introduced. Phase plane portraits are
plotted in the next section and the behavior of interacting species is discussed. The
spread of flu in a school using a compartmental model is covered next. In Section 6.4,
the reader can reproduce results from a recent journal paper where a spring-damper
model is used to simulate hysteresis (the system is history dependent) in single fiber
muscle. Chapter 7 covers Chemistry, balancing chemical-reaction equations, solving
differential equations to model chemical kinetics, and investigating the solubility of
silver chloride in a potassium chloride solution. Chapter 8 covers some topics from
the popular field of data science. The Pandas package is introduced for creating data
frames for data manipulation and analysis. Linear programming is introduced to solve
optimization problems, including the simplex method. Unsupervised learning is used
to cluster data in the third section, and the final section covers decision trees. The field
of data science is huge, and readers should note that statistics, machine learning, and
deep learning, covered later in the book, are also encompassed within data science.
In Chapter 9, the reader is shown a number of examples from the field of Economics,
including a microeconomic model of quantity of production, a macroeconomic model
of economic growth, the Markowitz model—a portfolio optimization model and the
Black-Scholes model for computing the fair price or theoretical value for a call or a
put option based on a function of four variables. In the field of engineering, linear
and nonlinear electric circuits (including Chua’s circuit) are covered in the first half
of Chapter 10, and then mechanical systems of coupled oscillators and nonlinear pen-
dula are modeled. Chapter 11 is concerned with fractals, multifractals and real-world
applications. Image processing is covered in Chapter 12, both grayscale and color
images are investigated, and methods for finding features, edge detection and statis-
tical analysis of images are covered. The chapter ends with examples from medical
imaging, plotting the vascular architecture from a human retinal image using a ridge
filter, and identifying a tumor in a brain image using masking and segmentation. In
the first half of Chapter 13, numerical methods for Ordinary Differential Equations
(ODEs), including Euler’s methods, the fourth order Runga-Kutta method and the
Adams-Bashforth method, are all introduced. Numerical methods for Partial Differ-
ential Equations (PDEs), and in particular finite difference methods, are covered in
the second half of Chapter 13. The standard examples of heat diffusion in a rod
and a flat plate, advection along a one-dimensional channel, and the the vibration
of a string are all included. Chapter 14 is concerned with the field of Physics, and
examples are taken from signal processing, using Fast Fourier Transforms (FFTs)
and nonlinear optics, where the Ikeda map is used to model the propagation of light
waves through a Simple Fiber Ring (SFR) resonator. Josephson junctions, super-
conducting devices that act like biological neurones but oscillate 100,000,000 times
faster are discussed, and the motion of planetary bodies (earth-moon-comet system)
Preface ! xix

is also covered. Chapter 15 shows how Python can be used for statistics, including
linear regression, the student t-test, Markov chains and Monte-Carlo simulation, for
example.
The final section of the book is concerned with neural networks and AI. To follow
Section 2, scientific computation is applied to modeling neurones and simple neural
networks in some detail. Machine learning, deep learning and TensorFlow (written
in Python) are covered later. Chapter 16 covers an invention by the author (and one
of his colleagues) labeled brain inspired computing. The first section deals with the
Hodgkin-Huxley model from the 1950s before moving on to later models that have
been developed over the decades. Using threshold oscillator logic, a binary oscillator
half-adder for logic computation is simulated in Section 16.2, and in the next section
a memory component is modeled using an oscillator-based set reset flip-flop. The
chapter ends with applications and future work. Chapter 17 covers neural networks
and neurodynamics. Simple neural networks are used to model logic AND, OR and
XOR gates. Section 17.2 covers the backpropagation algorithm, with a simple expla-
nation of how this works, and then applied to a neural network model for the Boston
Housing data for valuing homes. The final section presents a simple model of a two-
neuron module. A stability analysis is carried out to determine parameter borders
where the system displays hysteresis, unstable behavior and quasiperiodic (almost pe-
riodic) behavior. Chapter 18 provides an introduction to TensorFlow and the Keras
Application Programming Interface (API). TensorFlow is then used to build networks
for linear regression, the XOR gate and finally the Boston Housing data valuations.
Recurrent Neural Networks (RNNs) are covered in Chapter 19, starting with discrete
and continuous Hopfield models used as associative memory RNNs. The long short-
term memory RNN network is applied to predicting chaotic and financial time series
in sections three and four. Chapter 20 presents an example of a convolutional neu-
ral network (CNN). A simple example illustrating convolving and pooling starts the
chapter, and then a CNN is used on the MNIST handwritten dataset for predicting
the digits 0-9. In the next section, TensorBoard, which is TensorFlow’s visualization
toolkit, is used to see how well your neural network is performing. The final section
covers further reading, natural language processing (NLP), reinforcement learning,
ethics in AI, cyber security and the internet of things (IoT) are briefly discussed.
Chapter 21 presents answers or hints to all of the exercises in the book.

Full solutions to the Exercises in Sections 1, 2 and 3, may be found in the URLs below:

https://drstephenlynch.github.io/webpages/Solutions_Section_1.html.

https://drstephenlynch.github.io/webpages/Solutions_Section_2.html.

https://drstephenlynch.github.io/webpages/Solutions_Section_3.html.
xx ! Preface

Finally, all of the IDLE, Python and jupyter notebooks can be dowloaded through
GitHub:

https://github.com/proflynch/CRC-Press/.

[1] Lynch, S. (2018). Dynamical Systems with Applications using Python. Springer
International Publishing, New York.
Index

A-Level Mathematics, 62 bifurcation


activation function flip, 251
leaky ReLU, 260 fold, 251
ReLU, 259 Neimark-Sacker, 251
sigmoid, 260 bifurcation diagram
softmax, 288 Duffing, 148
tanh, 260 Ikeda map, 195
Adam optimizer, 257 logistic map, 85
Adams-Bashforth method, 187 neuromodule, 252
addressable memory, 268 binomial
AgCl, 101 distribution, 52
AI, 256 expansion, 49
Alan Turing, 241 biploar, 243
Anaconda, 21 Black-Scholes Model, 133
analysis of variance, 217 blood cell population model, 92
AND gate, 228 Boston housing data, 247, 261
animation, 37 box and whisker plot, 212
ANOVA, 217 box-counting, 158
API, 255 brain
application programming interface, image, 174
255 tumour, 174
arithmetic sequence, 63 bull market, 217
array, 24
artificial intelligence, 256 calculus, 29
AS-Level Mathematics, 46 Canny edge detector, 170
assay for neuronal degradation, 235 Cantor fractal, 12
asynchronous updating, 247, 268 Cantor set
autocatalysis, 99 multifractal, 161
axon, 224 carbon dioxide emissions, 205
catalyst, 104
Baby computer, 234 ceiling, 6
backpropagation, 245 centroid, 170
ballistic propagation, 237 chain rule, 66
Barnsley’s fern, 157 chaos, 83
bear market, 217 prediction, 273
Belousov-Zhabotinski Reaction, 99 chaos game, 155

309
310 ! Index

Chapman cycle, 105 epoch, 247


ChatGPT, 294 ethics in AI, 292
chemical kinetics, 97 Euler
chemical-reaction equations, 95 backward method, 186
chlorargyrite, 101 explicit method, 178, 198
Chua circuit, 142 implicit method, 186
CNN, 288 expansion path, 126
Cobb-Douglas model, 126
color images, 169 factorial, 6
common-ion effect, 101 Fahrenheit to Kelvin program, 10
concentric contraction, 91 fast Fourier transform, 192
confusion matrix, 122 feasibility region, 111
Console Window, 21 fft, 192
continuation lines, 8 Fibonacci sequence, 11
convolving, 285 figure resolution, 26
finite difference method, 181
coupled oscillators, 144
Fitzhugh-Nagumo equations, 227
COVID-19, 87
floating point arithmetic, 10
critical point, 85
floor, 6
cumulative distribution function, 53
flu model, 88
cyber security, 292
for loop, 11
data collection, 51 forces, 55, 73
decision tree Fourier
classification, 119 spectrum, 192
regression, 123 transform, 192
decision-trees, 119 fractal, 11, 154
dimension, 154
def, 9
friction, 75
delta, the Greeks, 134
full-adder, 237
dendrite, 224
functions, 63
differential equation, 35, 97
fundamental memory, 267
differentiation, 49
directed graph, 207 gamma, the Greeks, 134
discrete Fourier transform, 192 geometric series, 63
dpi (dots per inch), 26 ggplot grid plot, 140
drug efficacy, 108 gini index, 120
Duffing equation, 146 GitHub, 41
Durbin-Watson test, 206 Google Colab, 41
Greeks, 134
eccentric contraction, 91 guess number game, 18
economics, 125
edit cell, 35 half-adder, 227
Editor Window, 21 handcrafted patterns, 282
efficient frontier, 131 Hebb’s postulate of learning, 268
engineering, 139 histogram, 170, 212
epidemic model, 87 Hodgkin-Huxley model, 224
Index ! 311

Holling-Tanner model, 85 latch, 231


Hopfield RNN LaTeX, 26, 36
continuous, 270 Le Chatelier’s pronciple, 101
discrete, 267 learning rate, 246
html, 35 Levene’s test, 212
hypothesis test, 54, 71 limit cycle, 87
hysteresis, 89, 148, 195 linear algebra, 29
JJ, 201 linear electric circuit, 139
pinched, 141 linear programming, 110
linear regression, 203, 257
IDE, 21 list, 7
IDLE, 4 logistic map function, 9
if, elif, else, 11 long short-term memory, 273
Ikeda map, 194 Lorenz system, 283
image processing, 167, 168 LSTM
image statistical analysis, 170 predicting chaos, 273
indentation, 8 predicting financial time series,
inequality, 47 278
initial value problem, 68 Lyapunov function
integration, 50 Hopfield network, 271
by parts, 67 lynx-snowshoe hare, 87
by substitution, 66
interactive plot, 37 machine learning, 247
Internet of Things (IoT), 292 macroeconomics, 125
inverse discrete Fourier transform, 192 Mandelbrot set, 162
iris data set, 119 Markdown, 33
isocost, 126 Markov chain, 207
isometric contraction, 91 hidden, 207
isoquant, 126 Mask, 174
mass-spring system, 144
Jacobian matrix, 85 matplotlib, 25
Jarque-Bera test, 207 matrix, 29
JJ hysteresis, 201 mean-variance analysis, 130
Josephson junction (JJ), 196 medical images, 172
jupyter notebook, 33 memory RNN, 267
memristor, 141
k-means clustering, 115 microbes, 170
Keras, 261 microeconomics, 125
kernel, 33 mixed fundamental memories, 269
kinematics, 54 MNIST data set, 288
Koch curve, 14 Modern Portfolio Theory (MPT), 130
multifractal, 164 moments, 73
Koch sowflake, 154 Monte-Carlo simulation, 214
Koch square fractal, 18 Morris-Lecar model of neuron, 237
kurtosis, 206 multifractal, 160, 168, 171
312 ! Index

muscle model, 89 preallocate arrays, 8


predator-prey model, 84
natural language processing (NLP), probability, 52
293 density function, 71
neocognitron, 286 probability distribution curve, 210
neural network, 241 probe vector, 268
Neurodynamics, 250 product rule, 66
neuromodule, 250 products, 96
neuron, 224 projectile, 74
Newton’s equations, 198 proof, 56
Newton-Raphson method, 69 Pythagorean triples, 18
nonlinear optics, 194
normal distribution, 71, 210 quantile-quantile plot, 212
notebook, 33 quasiperiodic, 251
np.pad, 158 quotient rule, 66
null-hypothesis, 54, 72
numerical methods, 69, 177 randint, 18
numpy, 23 random module, 18
range, 7
objective function, 111 Raspberry Pi, 293
ODE, 35 reactants, 96
drug in body, 76 recurrent neural network, 267
mass-spring system, 144 reinforcement learning (RL), 294
population model, 68 retina image, 173
odeint, 98 reversed fundamental memories, 269
omnibus test, 206 RGB values, 169
OpenCV, 167 RLC circuit, 140
optical fiber, 195 RNN, 267
overfitting, 261 roulette, 214
Overleaf, 36 Runge Kutta method (RK4), 179
ozone production, 105
savefig, 26
p-value, 213 sci-kit learn, 115
Pandas, 107 scikit-image, 167
parametric equations, 67 segmentation, 174
pendulum, 147 set reset flip-flop, 231
double, 149 SFR resonator, 194
peppers image, 169 Shakespeare (NLP), 294
periodically forced systems, 146 Sharpe-ratio, 131
physics, 191 Sierpinski triangle, 15, 156
Pillow, 167 signal processing, 194
pinched hysteresis, 141 silver chloride, 101
planetary bodies, 198 simplex method, 112
pooling, 285 singlet, 105
population model, 68, 81 skewness, 206
power spectrum, 200 SLIC algorithm, 174
Index ! 313

slice, 24 theta, the Greeks, 134


softmax, 288 threshold voltage, 224
Solow-Swan model, 128 travelling salesman problem, 270
solubility curve, 103 tree fractal, 14
soma, 224 trigonometry, 62
spin-glass states, 269 truncate, 6
spurious steady-state, 269 turtle, 11
stability analysis, 250 Turtle Demo, 12
stability diagram, 250 two-neuron RNN, 271
stable critical point, 85
stagnant market, 217 undock window, 21
standard normal distribution, 71 uniploar, 242
statistical tests, 211 unstable critical point, 85
statistics packages, 205 unsupervised learning, 115, 248
steady state vector, 208
vacuum tube oscillator, 234
stiff system, 105
vector, 23, 50
student t-test, 210
vectorize, 25, 182, 188
subplot, 26
vectors in 3D, 68
supervised learning, 248
vega, the Greeks, 134
Surds, 46
Venn diagram, 52
suvat equations, 72
vollib, 136
sympy, 28
synchronous updating, 247, 269 wave equation, 184
while loop, 11
t-distribution, 210
word length, 18
t-test, 210
Taylor series, 28, 64, 178, 182 XOR gate, 228, 259
tensor, 23
TensorBoard, 290 z-distribution, 71
TensorFlow, 261 zero-based indexing, 7
View publication stats

You might also like