Python For Control Engineering

Download as pdf or txt
Download as pdf or txt
You are on page 1of 284

Python for Control

Engineering
Hans-Petter Halvorsen

https://www.halvorsen.blog
Python for Control Engineering
Python for Control Engineering

Hans-Petter Halvorsen

2020
Python for Control Engineering
Hans-Petter
c Halvorsen

April 25, 2022

1
Preface

Python is a popular programming language, and it is one of the most used pro-
gramming languages today.

Python works on all the main platforms and operating systems used today, such
Windows, macOS, and Linux.

Python is a multi-purpose programming language, which can be use for simu-


lation, creating web pages, communicate with database systems, etc.

My Blog/Web Site [1]:


https://www.halvorsen.blog

Here you find lots of technical resources about Technology, Programming, Soft-
ware Engineering, Automation and Control, Industrial IT, etc.

Here you find my Web page with Python resources:

https://www.halvorsen.blog/documents/programming/python/

These resources are a supplement to this textbook. Here you can download the
software, download code examples, etc.

This Textbook is written in LATEXusing Overleaf.

LATEXis a document preparation system used for the communication and publi-
cation of scientific documents.

2
For more information about LATEX:
https://www.latex-project.org

Overleaf is a web-bases LATEXsystem, meaning you can write your LATEXdocuments


in your web browser, you co-work and share documents with others.

For more information about Overleaf:


https://www.overleaf.com

Python Books

You find other Python textbooks within different domains on my Python Web
page:
https://www.halvorsen.blog/documents/programming/python/

Python Books:

• Python Programming - This is a textbook in Python Programming


with lots of Practical Examples and Exercises. You will learn the necessary
foundation for basic programming with focus on Python.

• Python for Science and Engineering - This is a textbook in Python


Programming with lots of Examples, Exercises, and Practical Applications
within Mathematics, Simulations, etc. The focus is on numerical calcu-
lations in mathematics and engineering. Necessary theory is presented in
addition to many practical examples.

• Python for Control Engineering - This is a textbook in Python Pro-


gramming with lots of Examples, Exercises, and Practical Applications
within Mathematics, Simulations, Control Systems, DAQ, Database Sys-
tems, etc. The focus is on the use of Python within measurements, data
collection (DAQ), control technology, both analysis of control systems
(stability analysis, frequency response, ...) and implementation of control
systems (PID, etc.). Required theory is presented in addition to many
practical examples and exercises in Python.
• Python for Software Development - This is a textbook in Python Pro-
gramming with lots of Examples, Exercises, and Practical Applications
within Software Systems, Software Development, Software Engineering,
Database Systems, Web Application Desktop Applications, GUI Applica-
tions, etc. The focus is on the use of Python for creating modern Software
Systems. Required theory is presented in addition to many practical ex-
amples and exercises in Python.

3
Video Resources
In addition to the textbooks mentioned, lots of videos explaining and comple-
ments the different Python topics and examples within the textbook have been
made. These are both available on my website and on YouTube.

Blog:
https://www.halvorsen.blog

Python Resources:
https://www.halvorsen.blog/documents/programming/python/

Python Programming Videos:


https://www.youtube.com/playlist?list=PLdb-TcK6Aqj2lH 1mtP qlOo−Y ki5U P zp4

Python for Science and Engineering Videos:


https://www.youtube.com/playlist?list=PLdb-TcK6Aqj2hlH55Bn5oxFIvyoVbXxQS

Python for Control Engineering Videos:


https://www.youtube.com/playlist?list=PLdb-TcK6Aqj1Kg6pV3zlrpUnPIRwG2O x

Python for Software Development Videos:


https://www.youtube.com/playlist?list=PLdb-TcK6Aqj0E01L69fySfBSemVTCil L

Raspberry Pi and Python:


https://www.youtube.com/playlist?list=PLdb-TcK6Aqj3Sf5omYT-MLmxckclHi2i7

Internet of Things with Python:


https://www.youtube.com/playlist?list=PLdb-TcK6Aqj2zP8b1JjzUcFGzTVG13BPh

YouTube Channel @Industrial IT and Automation


https://www.youtube.com/IndustrialITandAutomation

Programming
The way we create software today has changed dramatically the last 30 years,
from the childhood of personal computers in the early 80s to today’s powerful
devices such as Smartphones, Tablets and PCs.

The Internet has also changed the way we use devices and software. We still
have traditional desktop applications, but Web Sites, Web Applications and so-
called Apps for Smartphones, etc. are dominating the software market today.

We need to find and learn Programming Languages that are suitable for the
New Age of Programming.

We have today several thousand different Programming Languages today. I


guess you will need to learn more than one Programming Language to survive

4
in today’s software market.

You find lots of Programming Resources here:


https://www.halvorsen.blog/documents/programming/

Software Engineering
Software Engineering is the discipline for creating software applications. A
systematic approach to the design, development, testing, and maintenance of
software.

The main parts or phases in the Software Engineering process are:

• Planning
• Requirements Analysis

• Design
• Implementation
• Testing

• Deployment and Maintenance

You find lots of Software Engineering Resources here:


https://www.halvorsen.blog/documents/programming/softwaree ngineering/

5
6
Contents

I Getting Started with Python 14


1 Introduction 15
1.1 The New Age of Programming . . . . . . . . . . . . . . . . . . . 15
1.2 MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2 What is Python? 21
2.1 Introduction to Python . . . . . . . . . . . . . . . . . . . . . . . 21
2.1.1 Interpreted vs. Compiled . . . . . . . . . . . . . . . . . . 22
2.2 Python Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.1 Python Packages for Science and Numerical Computations 24
2.3 Anaconda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4 Python Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.4.1 Python IDLE . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.4.2 Visual Studio Code . . . . . . . . . . . . . . . . . . . . . . 26
2.4.3 Thonny . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.4.4 Spyder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.4.5 Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.4.6 PyCharm . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.4.7 Wing Python IDE . . . . . . . . . . . . . . . . . . . . . . 27
2.4.8 Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . 27
2.5 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.6 Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.6.1 Python Windows 10 Store App . . . . . . . . . . . . . . . 28
2.6.2 Installing Anaconda . . . . . . . . . . . . . . . . . . . . . 29
2.6.3 Installing Visual Studio Code . . . . . . . . . . . . . . . . 29

3 Start using Python 30


3.1 Python IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.2 My first Python program . . . . . . . . . . . . . . . . . . . . . . 30
3.3 Python Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.4 Running Python from the Console . . . . . . . . . . . . . . . . . 31
3.4.1 Opening the Console on macOS . . . . . . . . . . . . . . . 32
3.4.2 Opening the Console on Windows . . . . . . . . . . . . . 33
3.4.3 Add Python to Path . . . . . . . . . . . . . . . . . . . . . 33
3.5 Scripting Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5.1 Run Python Scripts from the Python IDLE . . . . . . . . 35
3.5.2 Run Python Scripts from the Console (Terminal) macOS 36

7
3.5.3 Run Python Scripts from the Command Prompt in Win-
dows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.5.4 Run Python Scripts from Spyder . . . . . . . . . . . . . . 37

4 Basic Python Programming 40


4.1 Basic Python Program . . . . . . . . . . . . . . . . . . . . . . . . 40
4.1.1 Get Help . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.2.1 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.2.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.2.3 String Input . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.3 Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.4 Python Standard Library . . . . . . . . . . . . . . . . . . . . . . 45
4.5 Using Python Libraries, Packages and Modules . . . . . . . . . . 46
4.5.1 Python Packages . . . . . . . . . . . . . . . . . . . . . . . 48
4.6 Plotting in Python . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.6.1 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.6.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

II Python Programming 54
5 Python Programming 55
5.1 If ... Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.2 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.3 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.1 Nested For Loops . . . . . . . . . . . . . . . . . . . . . . . 61
5.4 While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

6 Creating Functions in Python 64


6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.2 Functions with multiple return values . . . . . . . . . . . . . . . 66
6.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

7 Creating Classes in Python 70


7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
7.2 The init () Function . . . . . . . . . . . . . . . . . . . . . . . . 71
7.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

8 Creating Python Modules 75


8.1 Python Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

9 File Handling in Python 78


9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.2 Write Data to a File . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.3 Read Data from a File . . . . . . . . . . . . . . . . . . . . . . . . 79
9.4 Logging Data to File . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.5 Web Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

8
10 Error Handling in Python 83
10.1 Introduction to Error Handling . . . . . . . . . . . . . . . . . . . 83
10.1.1 Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . 83
10.1.2 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 83
10.2 Exceptions Handling . . . . . . . . . . . . . . . . . . . . . . . . . 84

11 Debugging in Python 86

12 Installing and using Python Packages 87


12.1 What is PIP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

III Python Environments and Distributions 88


13 Introduction to Python Environments and Distributions 89
13.1 Package and Environment Managers . . . . . . . . . . . . . . . . 90
13.1.1 PIP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.1.2 Conda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.2 Python Virtual Environments . . . . . . . . . . . . . . . . . . . . 91

14 Anaconda 92
14.1 Anaconda Navigator . . . . . . . . . . . . . . . . . . . . . . . . . 92
14.2 Anaconda Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . 92

IV Python Editors 95
15 Python Editors 96

16 Spyder 98
16.1 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

17 Visual Studio Code 101


17.1 Introduction to Visual Studio Code . . . . . . . . . . . . . . . . . 101
17.2 Python in Visual Studio Code . . . . . . . . . . . . . . . . . . . . 102

V Python for Mathematics Applications 103


18 Mathematics in Python 104
18.1 Basic Math Functions . . . . . . . . . . . . . . . . . . . . . . . . 104
18.1.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
18.2 Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
18.2.1 Introduction to Statistics . . . . . . . . . . . . . . . . . . 108
18.2.2 Statistics functions in Python . . . . . . . . . . . . . . . . 109
18.3 Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . 111
18.4 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

9
19 Linear Algebra in Python 118
19.1 Introduction to Linear Algebra . . . . . . . . . . . . . . . . . . . 118
19.2 Linear Algebra with Python . . . . . . . . . . . . . . . . . . . . . 119
19.2.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
19.2.2 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
19.2.3 Linear Algebra (numpy.linalg) . . . . . . . . . . . . . . . . 121
19.2.4 Matrix Addition . . . . . . . . . . . . . . . . . . . . . . . 121
19.2.5 Matrix Subtraction . . . . . . . . . . . . . . . . . . . . . . 122
19.2.6 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . 124
19.2.7 Transpose of a Matrix . . . . . . . . . . . . . . . . . . . . 126
19.2.8 Determinant . . . . . . . . . . . . . . . . . . . . . . . . . 127
19.2.9 Inverse Matrix . . . . . . . . . . . . . . . . . . . . . . . . 128
19.3 Solving Linear Equations . . . . . . . . . . . . . . . . . . . . . . 129
19.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

20 Complex Numbers in Python 133


20.1 Introduction to Complex Numbers . . . . . . . . . . . . . . . . . 133
20.2 Complex Numbers with Python . . . . . . . . . . . . . . . . . . . 135

21 Differential Equations 137


21.1 Introduction to Differential Equations . . . . . . . . . . . . . . . 137
21.2 ODE Solvers in Python . . . . . . . . . . . . . . . . . . . . . . . 140
21.3 Solving Multiple 1. order Differential Equations . . . . . . . . . . 143
21.4 Solving Higher order Differential Equations . . . . . . . . . . . . 146
21.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

22 Optimization 153

VI Using Python for Simulations 157


23 Introduction to Simulations 158

24 Differential Equations 159


24.1 Introduction to Differential Equations . . . . . . . . . . . . . . . 159

25 Discrete Systems 161


25.1 Discretization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
25.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

26 Real-Time Simulations 167


26.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
26.2 Introduction to Real-Time Plotting . . . . . . . . . . . . . . . . . 169
26.3 Real-Time Plotting with Animation . . . . . . . . . . . . . . . . 174
26.3.1 Speeding Up the Plot Animation . . . . . . . . . . . . . . 178

VII Data Acquisition (DAQ) with Python 183


27 Plotting Sensor Data 184
27.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

10
27.2 Introduction to Real-Time Plotting . . . . . . . . . . . . . . . . . 184
27.3 Real-Time Plotting with Animation . . . . . . . . . . . . . . . . 186
27.3.1 Speeding Up the Plot Animation . . . . . . . . . . . . . . 188

28 Data Acquisition (DAQ) with Python 191


28.1 Introduction to DAQ . . . . . . . . . . . . . . . . . . . . . . . . . 191
28.2 Data Acquisition using NI DAQ Devices . . . . . . . . . . . . . . 191
28.2.1 NI-DAQmx . . . . . . . . . . . . . . . . . . . . . . . . . . 193
28.2.2 Measurement Automation Explorer (MAX) . . . . . . . . 194
28.3 NI-DAQmx Python API . . . . . . . . . . . . . . . . . . . . . . . 194
28.3.1 Analog Write . . . . . . . . . . . . . . . . . . . . . . . . . 195
28.3.2 Analog Read . . . . . . . . . . . . . . . . . . . . . . . . . 196
28.3.3 Digital Write . . . . . . . . . . . . . . . . . . . . . . . . . 197
28.3.4 Digital Read . . . . . . . . . . . . . . . . . . . . . . . . . 197
28.4 Controlling LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
28.5 Read Data from Temperature Sensors . . . . . . . . . . . . . . . 200
28.5.1 Read Data from TMP36 Temperature Sensor . . . . . . . 200
28.5.2 Read Data from Thermistor . . . . . . . . . . . . . . . . . 204
28.5.3 Read Data NI TC-01 Thermocouple Device . . . . . . . . 208
28.6 Data Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

VIII Control Systems 210


29 Python used for Control Applications 211
29.1 Python Control Systems Library . . . . . . . . . . . . . . . . . . 211
29.1.1 Python Control Systems Library Functions . . . . . . . . 211
29.1.2 MATLAB compatibility module . . . . . . . . . . . . . . 213
29.2 Control Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
29.2.1 Transfer functions . . . . . . . . . . . . . . . . . . . . . . 214
29.2.2 State-space Models . . . . . . . . . . . . . . . . . . . . . . 215
29.3 PID Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
29.3.1 PI Control . . . . . . . . . . . . . . . . . . . . . . . . . . 217

30 Transfer Functions 218


30.1 1.order Transfer Functions . . . . . . . . . . . . . . . . . . . . . . 219
30.1.1 Step Response . . . . . . . . . . . . . . . . . . . . . . . . 221
30.2 1.order Transfer Functions with Time Delay . . . . . . . . . . . . 224
30.3 Integrator Transfer Functions . . . . . . . . . . . . . . . . . . . . 226
30.4 2.order Transfer Functions . . . . . . . . . . . . . . . . . . . . . . 229
30.5 Block Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
30.5.1 Serial Block Diagrams . . . . . . . . . . . . . . . . . . . . 230
30.5.2 Parallel Block Diagrams . . . . . . . . . . . . . . . . . . . 231
30.5.3 Feedback Block Diagrams . . . . . . . . . . . . . . . . . . 233

31 State Space Models 235


31.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
31.2 Discrete State-space Models . . . . . . . . . . . . . . . . . . . . . 238

11
32 Frequency Response 239
32.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
32.1.1 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
32.2 Bode Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

IX Python Database Development 246


33 Database Applications with Python 247
33.1 Structured Query Language (SQL) . . . . . . . . . . . . . . . . . 247
33.2 SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
33.3 MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
33.4 MongoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

34 SQL Server with Python 249


34.1 Introduction to SQL Server . . . . . . . . . . . . . . . . . . . . . 249
34.2 SQL Server drivers for Python . . . . . . . . . . . . . . . . . . . 249
34.3 pyodbc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
34.3.1 Installation of pyodbc . . . . . . . . . . . . . . . . . . . . 249
34.3.2 ODBC Drivers . . . . . . . . . . . . . . . . . . . . . . . . 249
34.4 SQL Server Python Examples . . . . . . . . . . . . . . . . . . . . 250
34.5 Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . 251
34.6 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
34.7 pymssql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
34.8 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

X Python Application Development 252


35 OPC Communication with Python 253
35.1 Introduction to OPC . . . . . . . . . . . . . . . . . . . . . . . . . 253
35.2 OPC Classic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
35.3 OPC UA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
35.4 OPC Examples with Python . . . . . . . . . . . . . . . . . . . . 256

36 Python Integration with LabVIEW 257


36.1 What is LabVIEW? . . . . . . . . . . . . . . . . . . . . . . . . . 257
36.2 Using Python in LabVIEW . . . . . . . . . . . . . . . . . . . . . 257

37 Raspberry Pi and Python 262


37.1 What is Raspberry Pi? . . . . . . . . . . . . . . . . . . . . . . . . 262

XI Resources 263
38 Python for MATLAB Users 264
38.1 Use Python inside MATLAB . . . . . . . . . . . . . . . . . . . . 265
38.2 Calling MATLAB from Python . . . . . . . . . . . . . . . . . . . 266

12
39 Python Resources 268
39.1 Python Distributions . . . . . . . . . . . . . . . . . . . . . . . . . 268
39.2 Python Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
39.3 Python Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
39.4 Python Tutorials . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
39.5 Python in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . 269

XII Solutions to Exercises 271

13
Part I

Getting Started with


Python

14
Chapter 1

Introduction

With this textbook you will learn basic Python programming. The textbook
contains lots of examples and self-paced tasks that the users should go through
and solve in their own pace.

You will find additional resources on my blog/web site [1].


https://www.halvorsen.blog

My Web Site about Python is:


https://www.halvorsen.blog/documents/programming/python/

See Figure 1.1

1.1 The New Age of Programming


The way we create software today has changed dramatically the last 30 years,
from the childhood of personal computers in the early 80s to today’s powerful
devices such as Smartphones, Tablets and PCs.

The Internet has also changed the way we use devices and software. We still
have traditional desktop applications, but Web Sites, Web Applications and so-
called Apps for Smartphones, etc. are dominating the software market today.

We need to find and learn Programming Languages that are suitable for the
New Age of Programming.

We have today several thousand different Programming Languages, so why


should we learn Python? I guess you will need to learn more than one Pro-
gramming Language to survive in today’s software market. Python is easy to
learn, so it it a good starting point for new programmers.

Python is an interpreted, high-level, general-purpose programming language.


Created by Guido van Rossum and first released in 1991 [2].

15
Figure 1.1: Web Site - Python

Python is a fairly old Programming Language (1991) compared to many other


Programming Languages like C# (2000), Swift (2014), Java (1995), PHP (1995).

Python has during the last 10 years become more and more popular. Today,
Python has become one of the most popular Programming Languages.

There are many different rankings regarding which programming language which
is most popular. In most of these ranking, Python is in top 10.

One of these rankings is the IEEE Spectrum’s ranking of the top programming
languages [3].

From this ranking we see that Python is the most popular Programming Lan-
guage in 2018. See Figure 1.2
As we see in Figure 1.2 they categorize the different Programming Languages
into the following categories:

• Web

16
Figure 1.2: The Most Popular Programming Languages

• Mobile
• Enterprise

• Embedded
According to Figure 1.2 we see that Python can be used to program Web Ap-
plications, Enterprise Applications and Embedded Applications.

So far Python is not used or not optimized for creating Mobile Applications. We
have today 2 major Mobile platforms; iOS Applications are mainly programmed
with the Swift Programming language, while Android Applications are mainly
programmed with either Java or Kotlin.

Another survey is the ”Stack Overflow Developer Survey 2018” [4]. See Figure
1.3.

As we can see from [5] and Figure 1.4, Python becomes more and more popular
year by year.

Based on Figure 1.4, the source [5] try to predict the future of Python, see
Figure 1.5.

Based on the surveys and statistics mention above, obviously Python is a pro-
gramming language that you should learn.

Lets summarize:

• Python is fun to learn and use and it is also named after the British
comedy group called Monty Python.

• Python has a simple and flexible code structure and the code is easy to
read.

17
Figure 1.3: The Top Programming Languages - Stack Overflow Survey

• Python is highly extendable due to its high number of free available


Python Packaged and Libraries
• Python can be used on all platforms (Windows, macOS and Linux).
• Python is multi-purpose and can be used for to program Web Applications,
Enterprise Applications and Embedded Applications, and within Data
Science and Engineering Applications.

• The popularity of Python is growing fast.


• Python is open source and free to use
• The growing Python community makes it easy to find documentation,
code examples and get help when needed

In general, Python is a multipurpose programming language that can be used


in many situations. But there is not one programming language which is best
in all kind of situations, so it is important that you know about and have skills
in different languages.

My list of recommendations (one of many):


• Visual Studio and C

• LabVIEW - a graphical programming language well suited for hardware


integration, taking measurements and data logging
• MATLAB - Numerical calculations and Scientific computing
• Python - Numerical calculations, and Scientific computing, etc.

• Web Programming, such as HTML, CSS, JavaScript and a Server-side


framework/programming language like PHP, ASP.NET (C or VB.NET),
Django (Python based)

18
Figure 1.4: The Incredible Growth of Python

• Databases (such as SQL Server and MySQL) and using the Structured
Query Language (SQL) or the upcoming NoSQL databases
• App Development for the 2 main platforms iOS (XCode using the Swift
Programming Language) and Android (Android Studio using the Java
Programming Language or Kotlin Programming language)
If you have skills in most of the tools, programming languages and frameworks
mention above, you are well suited for working as a full-time programmer or
software engineer.

1.2 MATLAB
If you are looking for MATLAB, please see the following:
https://www.halvorsen.blog/documents/programming/matlab/

19
Figure 1.5: The Future of Python

20
Chapter 2

What is Python?

2.1 Introduction to Python


Python is an open source and cross-platform programming language, that has
become increasingly popular over the last ten years. It was first released in
1991. Latest version is 3.7.0. CPython is the reference implementation of the
Python programming language. Written in C, CPython is the default and most
widely-used implementation of the language.

Python is a multi-purpose programming languages (due to its many extensions),


examples are scientific computing and calculations, simulations, web develop-
ment (using, e.g., the Django Web framework), etc.

Python Home Page [6]:


https://www.python.org

The programming language is maintained and available from (Python Software


Foundation): https://www.python.org Here you can download the basic Python
features in one package, which includes the Python programming language in-
terpreter, and a basic code editor, or an integrated development environment,
called IDLE. See Figure 2.1

But this is just the Python core, i.e. the interpreter a very basic editor, and the
minimum needed to create basic Python programs.

Typically you will need more features for solving your tasks. Then you can in-
stall and use separate Python packages created by third parties. These packages
need to be downloaded and installed separately (typically you use something
called PIP), or you choose to use, e.g., a distribution package like Anaconda.

Python is an object-oriented programming language (OOP), but you can use


Python in basic application without the need to know about or use the object-
oriented features in Python.

Python is an interpreted programming language, this means that as a developer

21
Figure 2.1: IDLE - Basic Python Editor

you write Python (.py) files in a text editor and then put those files into the
python interpreter to be executed. Depending on the Editor you are using, this
is either done automatically, or you need to do it manually.

Here are some important Python sources: [6], [7], [8].

2.1.1 Interpreted vs. Compiled


What are the differences between Interpreted programming languages and Com-
piled programming languages? What kind should you choose, and why should
you bother?

Programming languages generally fall into one of two categories: Compiled or


Interpreted. With a compiled language, code you enter is reduced to a set of
machine-specific instructions before being saved as an executable file.
Both approaches have their advantages and disadvantages.

22
With interpreted languages, the code is saved in the same format that you en-
tered. Compiled programs generally run faster than interpreted ones because
interpreted programs must be reduced to machine instructions at run-time. It
is usually easier to develop applications in an interpreted environment because
you don’t have to recompile your application each time you want to test a small
section.

Python is an interpreted programming language, while e.g., C/C++ are trans-


lated by running the source code through a compiler, i.e., C/C++ are compiled
languages.

Interpreted languages, in contrast, must be parsed, interpreted, and executed


each time the program is run.

Another example of an interpreted programming language is PHP, which is


mainly used to create dynamic web pages and web applications.

Compiled languages are all translated by running the source code through a
compiler. This results in very efficient code that can be executed any number of
times. The overhead for the translation is incurred just once, when the source
is compiled; thereafter, it need only be loaded and executed.

During the design of an application, you might need to decide whether to use a
compiled language or an interpreted language for the application source code.

Interpreted languages, in contrast, must be parsed, interpreted, and executed


each time the program is run

Thus, an interpreted language is generally more suited for doing ”ad hoc” cal-
culations or simulations, while compiled languages are better for permanent
applications where speed is in focus.

2.2 Python Packages


With Python you don’t get so much out of the box. Instead of having all of
its functionality built into its core, you need to install different packages for
different topics.

This approach has advantages and disadvantages. An disadvantage is that you


need to install these packages separately and then later import these modules
in your code.

This is also typical approach for open source software, because everybody can
create their own Python packages and distribute them. In that way you also
find Python packages for almost everything, from Scientific Computing to Web
Development.

23
These packages need to be downloaded and installed separately, or you choose
to use, e.g., a distribution package like Anaconda, where you typically get the
packages you need for scientific computing. With Anaconda you typically get
the same features as with MATLAB.

Lots of Python packages exists, depending on what you are going to solve.
We have Python packages for Desktop GUI Development, Database Develop-
ment, Web Development, Software Development, etc.

See an overview of Applications for Python:


https://www.python.org/about/apps/

See also the Python Package Index (PyPI) web site:


https://pypi.org

Here you can search for, download and install many hundreds Python Packages
within different topics and applications. You can also make your own Python
Packages and distribute them here.

2.2.1 Python Packages for Science and Numerical Com-


putations
Some important Python Packages for Science and Numerical Computations are:
• NumPy - NumPy is the fundamental package for scientific computing
with Python [9]
• SciPy - SciPy is a free and open-source Python library used for scientific
computing and technical computing. SciPy contains modules for optimiza-
tion, linear algebra, integration, interpolation, special functions, FFT, sig-
nal and image processing, ODE solvers and other tasks common in science
and engineering. [9]
• Matplotlib - Matplotlib is a Python 2D plotting library. [10]

• Pandas - Pandas Python Data Analysis Library [11]


These packages need to be downloaded and installed separately, or you choose
to use, e.g., a distribution package like Anaconda, where you typically get the
packages you need for scientific computing. With Anaconda you typically get
the same features as with MATLAB.

2.3 Anaconda
Anaconda is a distribution package, where you get Python compiler, Python
packages and the Spyder editor, all in one package.

Anaconda includes Python, the Jupyter Notebook, and other commonly used
packages for scientific computing and data science.

24
They offer a free version (Anaconda Distribution) and a paid version (Enter-
prise) Anaconda is available for Windows, macOS, and Linux

Web:
https://www.anaconda.com

Wikipedia:
https://en.wikipedia.org/wiki/Anaconda( P ythond istribution)

Spyder and the Python packages (NumPy, SciPy, Matplotlib, ...) mention above
+++ are included in the Anaconda Distribution.

2.4 Python Editors


An Editor is a program where you create your code (and where you can run
and test it). Most Editors have also features for Debugging. For simple Python
programs you can use the IDLE Editor, but for more advanced programs a bet-
ter editor is recommended.

Examples of Python Editors:


• Python IDLE
• Visual Studio Code
• Thonny
• Spyder
• Visual Studio
• PyCharm
• Wing Python IDE
• Jupyter Notebook
These editors are shortly described below and in more detail later in this text-
book.

Which editor you should use depends on your background, what kind of code
editors you have used previously, your programming skills, what your are going
to develop in Python, etc.

2.4.1 Python IDLE


The programming language is maintained and available from (Python Software
Foundation): https://www.python.org Here you can download the basic Python
features in one package, which includes the Python programming language in-
terpreter, and a basic code editor, or an integrated development environment,
called IDLE. See Figure 2.1

25
Web:
https://www.python.org

2.4.2 Visual Studio Code


Visual Studio Code is a source code editor developed by Microsoft for Windows,
Linux and macOS.

Web:
https://code.visualstudio.com

Resources: Getting Started with Python in Visual Studio Code

2.4.3 Thonny
Thonny is a Python IDE for beginners. Thonny is a very simple and ba-
sic Python editor which is highly recommended for new Python users. With
Thonny you can easily install Python Libraries/Packages using a Graphical
User Interface without the need of using more cryptic PIP commands in the
Command window/Terminal.

Thonny is also the default Python Editor on the Raspberry Pi OS.

Web: https://thonny.org
Wikipedia: https://en.wikipedia.org/wiki/Thonny

2.4.4 Spyder
Spyder is an open source cross-platform integrated development environment
(IDE) for scientific programming in the Python language.

Web:
https://www.spyder-ide.org

Wikipedia:
https://en.wikipedia.org/wiki/Spyder( sof tware)

Spyder is included in the Anaconda Distribution.

2.4.5 Visual Studio


Microsoft Visual Studio is an integrated development environment (IDE) from
Microsoft. It is used to develop computer programs, as well as websites, web
apps, web services and mobile apps. The deafult (main) programming language
in Visual studio is C, but many other programming languages are supported.

Visual studio is available for Windows and macOS.

26
Visual Studio (from 2017), has integrated support for Python, it is called
”Python Support in Visual Studio”.

Web:
https://visualstudio.microsoft.com

Wikipedia:
https://en.wikipedia.org/wiki/MicrosoftV isualS tudio

2.4.6 PyCharm
PyCharm is cross-platform, with Windows, macOS and Linux versions. The
Community Edition is free to use, while the Professional Edition (paid version)
has some extra features.

Web:
https://www.jetbrains.com/pycharm/

2.4.7 Wing Python IDE


The Wing Python IDE family of integrated development environments (IDEs)
from Wingware were created specifically for the Python programming language.

3 different version of Wing exists [12]:

• Wing 101 – a very simplified free version, for teaching beginning pro-
grammers
• Wing Personal – free version that omits some features, for students and
hobbyists
• Wing Pro – a full-featured commercial (paid) version, for professional
programmers

2.4.8 Jupyter Notebook


The Jupyter Notebook is an open-source web application that allows you to cre-
ate and share documents that contain live code, equations, visualizations and
text.

Web:
http://jupyter.org

Wikipedia:
https://en.wikipedia.org/wiki/ProjectJ upyter

27
2.5 Resources
Here are some useful Python resources:
• The official Python Tutorial
- https://docs.python.org/3.7/tutorial/index.html

• The official Python Documentation


- https://docs.python.org/3.7/index.html
• Python Tutorial (w3schools.com) [13]
- https://www.w3schools.com/python/

2.6 Installing Python


The Python programming language is maintained and available from (Python
Software Foundation):

https://www.python.org

Here you can download the basic Python features in one package, which includes
the Python programming language interpreter, and a basic code editor, or an
integrated development environment, called IDLE. See Figure 2.1

For basic Python programming this is good enough.

For more advanced Python Programming you typically need a better Code Ed-
itor and additional Packages.

For the basic Python examples in the beginning, the basic Python software
from:
https://www.python.org is good enough.

I suggest you start with the basic Python software in order to learn the basics,
then you can upgrade to a better Editor, install addition Python packages (either
manually or or install Anaconda where ”everything” is included).

2.6.1 Python Windows 10 Store App


Python 3.7 is also available in the Microsoft Store for Windows 10.

The Microsoft Store version of Python 3.7 is a simplified installer for running
scripts and packages.

Microsoft Store version of Python 3.7 is very basic but it’s good enough to run
the simple scripts.

Python 3.7 Microsoft Store edition will receive all updates automatically when
they are released and no manual action is required from your end.

28
In order to install the Microsoft Store version of Python just open Microsoft
Store in Windows 10 and search for Python.

2.6.2 Installing Anaconda


The Spyder Code Editor and the Python packages (such as NumPy, SciPy, mat-
plotlib, etc) are included in the Anaconda Distribution.

Download and install from:


https://www.anaconda.com

2.6.3 Installing Visual Studio Code


Visual Studio Code code is a simple and easy to use editor that can be used for
many different programming languages.

Download and install from:


https://code.visualstudio.com

Getting Started with Python in Visual Studio Code:


https://code.visualstudio.com/docs/python/python-tutorial

29
Chapter 3

Start using Python

In this chapter we will start to use Python in some simple examples.

3.1 Python IDE


The basic code editor, or an integrated development environment, called IDLE.
See Figure 3.1.

Other Python Editors will be discussed more in detail later. For now you can
use the basic Python IDE (IDLE) or Spyder if you have installed the Anaconda
distribution package.

Figure 3.1: Python Shell / Python IDLE Editor

3.2 My first Python program


We will start using Python and create some code examples.

30
Example 3.2.1. Plotting in Python
Lets open your Python Editor and type the following:

1 p r i n t ( ” H e l l o World ! ” )
Listing 3.1: Hello World Python Example

[End of Example]

An extremely useful command is help(), which enters a help functionality to


explore all the stuff python lets you do, right from the interpreter. Press q to
close the help window and return to the Python prompt.

You can use Python in different ways, either in ”interactive” mode or in ”Script-
ing” mode.

The python program that you have installed will by default act as something
called an interpreter. An interpreter takes text commands and runs them as
you enter them - very handy for trying things out.

Yo can run Python interactively in different ways either using the Console which
is part of the operating system or the Python IDLE and the Python Shell which
is part of the basic Python installation from https://www.python.org.

3.3 Python Shell


In interactive Mode you use the Python Shell as seen in Figure 3.1.

Here you type one and one command at a time after the ”>>>” sign in the
Python Shell.

1 >>> p r i n t ( ” H e l l o World ! ” )

3.4 Running Python from the Console


A console (or ”terminal”, or ‘command prompt’) is a textual way to interact
with your OS (Operating System).

The python program that you have installed will by default act as something
called an interpreter. An interpreter takes text commands and runs them as
you enter them - very handy for trying things out.

Below we see how we can run Python from the Console which is part of the OS.

31
3.4.1 Opening the Console on macOS
The standard console on macOS is a program called Terminal. Open Terminal
by navigating to Applications, then Utilities, then double-click the Terminal pro-
gram. You can also easily search for it in the system search tool in the top right.

The command line Terminal is a tool for interacting with your computer. A
window will open with a command line prompt message, something like this:

L a s t l o g i n : Tue Dec 11 0 8 : 3 3 : 5 1 on c o n s o l e
computername : ˜ username

Just type python at your console, hit Enter, and you should enter Python’s
Interpreter.

1 L a s t l o g i n : Tue Dec 11 1 2 : 3 4 : 1 6 on t t y s 0 0 0
2 Hans−P e t t e r −Work−MacBook−A i r : ˜ hansha$ python
3 Python 3 . 6 . 5 | Anaconda , I n c . | ( d e f a u l t , Apr 26 2 0 1 8 , 0 8 : 4 2 : 3 7 )
4 [GCC 4 . 2 . 1 Compatible Clang 4 . 0 . 1 ( t a g s /RELEASE 401/ f i n a l ) ] on
darwin
5 Type ” h e l p ” , ” c o p y r i g h t ” , ” c r e d i t s ” o r ” l i c e n s e ” f o r more
information .
6 >>>

The prompt >>> on the last line indicates that you are now in an interactive
Python interpeter session, also called the “Python shell”. This is different from
the normal terminal command prompt!

You can now enter some code for python to run. Try:

>>> p r i n t ( ” H e l l o World ” )

Se also Figure 3.2.

Figure 3.2: Console macOS

Try other Python commands, e.g.:


1 >>> a = 5
2 >>> b = 2
3 >>> x = 5
4 >>> y = 3∗ a + b
5 >>> y

32
3.4.2 Opening the Console on Windows
Window’s console is called the Command Prompt, named cmd. An easy way to
get to it is by using the key combination Windows+R (Windows meaning the
windows logo button), which should open a Run dialog. Then type cmd and
hit Enter or click Ok.

You can also search for it from the start menu.

It should look like:


C: \ U s e r s \myusername>

Just type python in the Command Prompt, hit Enter, and you should enter
Python’s Interpreter. See Figure 3.3.

Figure 3.3: Command Prompt Windows

If you get an error message like this:

’python’ is not recognized as an internal or external command, operable program


or batch file.
Then you need to add Python to your path. See instructions below.

Note! This is also an option during the setup. While installing you can se-
lect ”Add Python.exe to path”. This option is by default set to ”Off”. To get
that option you need to select ”Customize”, not using the ”Default” installation.

3.4.3 Add Python to Path


In the Windows menu, search for “advanced system settings” and select View
advanced system settings.

In the window that appears, click Environment Variables. . . near the bottom
right. See Figure 3.4.

33
Figure 3.4: Windows System Properties

In the next window, find and select the user variable named Path and click
Edit. . . to change its value. See Figure 3.5.

Select ”New” and add the path where ”python.exe” is located. See Figure 3.6.

The Default Location is:

C: \ U s e r s \ u s e r \AppData\ L o c a l \ Programs \ Python \ Python37 −32\

Click Save and open the Command Prompt once more and enter ”python” to
verify it works. See Figure 3.3.

34
Figure 3.5: Windows System Properties

3.5 Scripting Mode


In ”Scripting” mode you can write a Python Program with multiple Python
commands and then save it as a file (.py).

3.5.1 Run Python Scripts from the Python IDLE


From the Python Shell you select File → New File, or you can open an existing
Pytho program or Python Script by selecting File → Open...

Lets create a new Script and type in the following:


1 print (” Hello ”)
2 p r i n t ( ”World” )
3 p r i n t ( ”How a r e you ? ” )

In Figure 3.7 we see how this is done. As you see we can enter many Python
commands that together makes a Python program or Python script.
From the Python Shell you select Run → Run Module or hit F5 in order to run
or execute the Python Script. See Figure 3.8.

35
Figure 3.6: Windows System Properties

The IDLE editor is very basic, for more complicated tasks you typically may
prefer to use another editor like Spyder, Visual Studio Code, etc.

3.5.2 Run Python Scripts from the Console (Terminal)


macOS
From the Console (Terminal) on macOS:

1 $ cd / U s e r s / username / Downloads
2 $ python h e l l o w o r l d . py

Note! Make sure you are at your system command prompt, which will have $
or > at the end, not in Python mode (which has >>> instead)!

See also Figure 3.9.


Then it responds with:

1 Hello
2 World
3 How a r e you ?

36
Figure 3.7: Python Script

3.5.3 Run Python Scripts from the Command Prompt in


Windows
From Command Prompt in Window:

1 > cd /
2 > cd Temp
3 > python h e l l o w o r l d . py

Note! Make sure you are at your system command prompt, which will have >
at the end, not in Python mode (which has >>> instead)!

See also Figure 3.10.


Then it responds with:

1 Hello
2 World
3 How a r e you ?

3.5.4 Run Python Scripts from Spyder


If you have installed the Anaconda distribution package you can use the Spyder
editor. See 3.11.

In the Spyder editor we have the Script Editor to the left and the interactive
Python Shell or the Console window to the right. See See 3.11.

37
Figure 3.8: Running a Python Script

Figure 3.9: Running Python Scripts from Console window on macOS

Figure 3.10: Running Python Scripts from Console window on macOS

38
Figure 3.11: Running a Python Script in Spyder

39
Chapter 4

Basic Python Programming

4.1 Basic Python Program


We will start using Python and create some code examples.

We use the basic IDLE editor (or another Python Editor)

Example 4.1.1. Hello World Example


Lets open your Python Editor and type the following:

1 p r i n t ( ” H e l l o World ! ” )
Listing 4.1: Hello World Python Example

[End of Example]

4.1.1 Get Help


An extremely useful command is help(), which enters a help functionality to
explore all the stuff python lets you do, right from the interpreter.

Press q to close the help window and return to the Python prompt.

4.2 Variables
Variables are defined with the assignment operator, “=”. Python is dynamically
typed, meaning that variables can be assigned without declaring their type, and
that their type can change. Values can come from constants, from computation
involving values of other variables, or from the output of a function.

40
Example 4.2.1. Creating and using Variables in Python
We use the basic IDLE (or another Python Editor) and type the following:
1 >>> x = 3
2 >>> x
3 3
Listing 4.2: Using Variables in Python

Here we define a variable and sets the value equal to 3 and then print the result
to the screen.

[End of Example]

You can write one command by time in the IDLE. If you quit IDLE the variables
and data are lost. Therefore, if you want to write a somewhat longer program,
you are better off using a text editor to prepare the input for the interpreter
and running it with that file as input instead. This is known as creating a script.

Python scripts or programs are save as a text file with the extension .py

Example 4.2.2. Calculations in Python


We can use variables in a calculation like this:
1 x = 3
2 y = 3∗ x
3 print (y)
Listing 4.3: Using and Printing Variables in Python

We can implement the formula y = ax + b like this:


1 a = 2
2 b = 5
3 x = 3
4
5 y = a ∗x + b
6
7 print (y)
Listing 4.4: Calculations in Python

As seen in the examples, you can use the print() command in order to show the
values on the screen.

[End of Example]

41
A variable can have a short name (like x and y) or a more descriptive name
(sum, amount, etc).

You don need to define the variables before you use them (like you need to to
in, e.g., C/C++/C).

Figure 4.1 show these examples using the basic IDLE editor.

Figure 4.1: Basic Python

Here are some basic rules for Python variables:


• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters (A-z, 0-9) and
underscores
• Variable names are case-sensitive, e.g., amount, Amount and AMOUNT
are three different variables.

4.2.1 Numbers
There are three numeric types in Python:
• int
• float
• complex

42
Variables of numeric types are created when you assign a value to them, so in
normal coding you don’t need to bother.

Example 4.2.3. Numeric Types in Python

1 x = 1 # int
2 y = 2.8 # float
3 z = 3 + 2j # complex
Listing 4.5: Numeric Types in Python

This means you just assign values to a variable without worrying about what
kind of data type it is.
1 p r i n t ( type ( x ) )
2 p r i n t ( type ( y ) )
3 p r i n t ( type ( z ) )
Listing 4.6: Check Data Types in Python

If you use the Spyder Editor, you can see the data types that a variable has
using the Variable Explorer (Figure 4.2):

Figure 4.2: Variable Editor in Spyder

[End of Example]

4.2.2 Strings
Strings in Python are surrounded by either single quotation marks, or double
quotation marks. ’Hello’ is the same as ”Hello”.
Strings can be output to screen using the print function. For example: print(”Hello”).

Example 4.2.4. Using Strings in Python


Below we see examples of using strings in Python:

1 a = ” H e l l o World ! ”
2
3 print (a)
4
5 print (a [ 1 ] )
6 print (a [ 2 : 5 ] )
7 print ( len (a) )
8 print ( a . lower () )

43
9 p r i n t ( a . upper ( ) )
10 p r i n t ( a . r e p l a c e ( ”H” , ”J” ) )
11 print (a . s p l i t (” ”) )
Listing 4.7: Strings in Python

As you see in the example, there are many built-in functions form manipulating
strings in Python. The Example shows only a few of them.

Strings in Python are arrays of bytes, and we can use index to get a specific
character within the string as shown in the example code.

[End of Example]

4.2.3 String Input


Python allows for command line input.

That means we are able to ask the user for input.

Example 4.2.5. String Input in Python


The following example asks for the user’s name, then, by using the input()
method, the program prints the name to the screen:
1 p r i n t ( ” Enter your name : ” )
2 x = input ()
3 print ( ” Hello , ” + x )
Listing 4.8: String Input

[End of Example]

4.3 Built-in Functions


Python consists of lots of built-in functions. Some examples are the print func-
tion that we already have used (perhaps without noticing it is actually a Built-in
function).

Python also consists of different Modules, Libraries or Packages. These Mod-


ules, Libraries or Packages consists of lots of predefined functions for different
topics or areas, such as mathematics, plotting, handling database systems, etc.
See Section 4.4 for more information and details regarding this.

In another chapter we will learn to create our own functions from scratch.

44
4.4 Python Standard Library
Python allows you to split your program into modules that can be reused in
other Python programs. It comes with a large collection of standard modules
that you can use as the basis of your programs.
The Python Standard Library consists of different modules for handling file
I/O, basic mathematics, etc. You don’t need to install these separately, but you
need to important them when you want to use some of these modules or some
of the functions within these modules.

The math module has all the basic math functions you need, such as: Trigono-
metric functions: sin(x), cos(x), etc. Logarithmic functions: log(), log10(), etc.
Constants like pi, e, inf, nan, etc.

Example 4.4.1. Using the math module


We create some basic examples how to use a Library, a Package or a Module:

If we need only the sin() function, we can do like this:


1 from math im por t s i n
2
3 x = 3.14
4 y = sin (x)
5
6 print (y)

If we need a few functions, we can do like this:


1 from math im por t s i n , c o s
2
3 x = 3.14
4 y = sin (x)
5 print (y)
6
7 y = cos (x)
8 print (y)

If we need many functions, we can do like this:


1 from math im por t ∗
2
3 x = 3.14
4 y = sin (x)
5 print (y)
6
7 y = cos (x)
8 print (y)

We can also use this alternative:


1 im po rt math
2
3 x = 3.14
4 y = math . s i n ( x )
5
6 print (y)

45
We can also write it like this:
1 im po rt math a s mt
2
3 x = 3.14
4 y = mt . s i n ( x )
5
6 print (y)

[End of Example]

There are advantages and disadvantages with the different approaches. In your
program you may need to use functions from many different modules or pack-
ages. If you import the whole module instead of just the function(s) you need
you use more of the computer memory.

Very often we also need to import and use multiple libraries where the different
libraries have some functions with the same name but different use.

Other useful modules in the Python Standard Library are statistics (where
you have functions like mean(), stdev(), etc.)

For more information about the functions in the Python Standard Library,
see:
https://docs.python.org/3/library/index.html

4.5 Using Python Libraries, Packages and Mod-


ules
Rather than having all of its functionality built into its core, Python was de-
signed to be highly extensible. This approach has advantages and disadvantages.
A disadvantage is that you need to install these packages separately and then
later import these modules in your code.

Some important packages are:


• NumPy - NumPy is the fundamental package for scientific computing
with Python
• SciPy - SciPy is a free and open-source Python library used for scientific
computing and technical computing. SciPy contains modules for optimiza-
tion, linear algebra, integration, interpolation, special functions, FFT, sig-
nal and image processing, ODE solvers and other tasks common in science
and engineering.
• Matplotlib - Matplotlib is a Python 2D plotting library

46
Lots of other packages exists, depending on what you are going to solve.

These packages need to be downloaded and installed separately, or you choose


to use, e.g., a distribution package like Anaconda.

Here you find an overview of the NumPy library:


https://www.numpy.org

Here you find an overview of the SciPy library:


https://www.scipy.org

Here you find an overview of the Matplotlib library:


https://matplotlib.org

You will learn the basics features in all these libraries. We will use all of the in
different examples and exercises throughout this textbook.
Example 4.5.1. Using libraries
In this example we use the NumPy library:
1 im po rt numpy a s np
2
3 x = 3
4
5 y = np . s i n ( x )
6
7 print (y)

In this example we use both the math module in the Python Standard Library
and the NumPy library:
1 im po rt math a s mt
2 im po rt numpy a s np
3
4 x = 3
5
6 y = mt . s i n ( x )
7
8 print (y)
9
10
11 y = np . s i n ( x )
12
13 print (y)

Note! As seen in this example we use a function called sin() which exists both
in the math module in the Python Standard Library and the NumPy library.
In this case they give the same results. In this case the following code is not
recommended:
1 from math im por t ∗
2 from numpy imp ort ∗
3
4 x = 3
5

47
6 y = sin (x)
7
8 print (y)
9
10
11 y = sin (x)
12
13 print (y)

In this case it works, but assume you have 2 different functions with the same
name that have different meaning in 2 different libraries.

[End of Example]

4.5.1 Python Packages


In addition to the Python Standard Library, there is a growing collection of sev-
eral thousand components (from individual programs and modules to packages
and entire application development frameworks), available from the Python
Package Index.

Python Package Index (PYPI):


https://pypi.org

Here you can download and install individual Python packages.


An easy alternative is the Anaconda Distribution, where many of the most used
Python packages are included.

Anaconda:
https://www.anaconda.com/distribution/

4.6 Plotting in Python


Typically you need to create some plots or charts. In order to make plots or
charts in Python you will need an external library. The most used library is
Matplotlib.

Matplotlib is a Python 2D plotting library

Here you find an overview of the Matplotlib library:


https://matplotlib.org

If you are familiar with MATLAB and basic plotting in MATLAB, using the
Matplotlib is very similar.

The main difference from MATLAB is that you need to import the library,
either the whole library or one or more functions.
For simplicity we import the whole library like this:
1 im po rt m a t p l o t l i b . p y p l o t a s p l t

48
Plotting functions that you will use a lot:

• plot()
• title()
• xlabel()
• ylabel()
• axis()
• grid()
• subplot()
• legend()
• show()

Lets create some basic plotting examples using the Matplotlib library:

Example 4.6.1. Plotting in Python


In this example we have two arrays with data. We want to plot x vs. y. We
can assume x is a time series and y is the corresponding temperature in degrees
Celsius.
1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2
3 x = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10]
4
5 y = [ 5 , 2 ,4 , 4 , 8 , 7 , 4 , 8 , 10 , 9 ]
6
7 plt . plot (x , y)
8 plt . x l a b e l ( ’ Time ( s ) ’ )
9 plt . y l a b e l ( ’ Temperature ( degC ) ’ )
10 plt . show ( )

We get the plot as shown in Figure 4.3.

We can also write like this:


1 from m a t p l o t l i b . p y p l o t i mp ort ∗
2
3 x = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10]
4 y = [ 5 , 2 ,4 , 4 , 8 , 7 , 4 , 8 , 10 , 9 ]
5
6 plot (x , y)
7 x l a b e l ( ’ Time ( s ) ’ )
8 y l a b e l ( ’ Temperature ( degC ) ’ )
9 show ( )

This makes the code simpler to read. one problem with this approach appears
assuming we import and use multiple libraries and the different libraries have
some functions with the same name but different use.

49
Figure 4.3: Plotting in Python

[End of Example]

We have used 4 basic plotting function in the Matplotlib library:

• plot()
• xlabel()
• ylabel()
• show()

Example 4.6.2. Plotting a Sine Curve

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3
4 x = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7]
5
6 y = np . s i n ( x )
7
8 plt . plot (x , y)
9 plt . xlabel ( ’x ’ )
10 plt . ylabel ( ’y ’ )
11 plt . show ( )

This gives the following plot (see Figure 4.4):

A better solution will then be:

50
Figure 4.4: Plotting a Sine function in Python

1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2 im po rt numpy a s np
3
4 xstart = 0
5 x s t o p = 2∗ np . p i
6 increment = 0.1
7
8 x = np . a r a n g e ( x s t a r t , xstop , i n c r e m e n t )
9
10 y = np . s i n ( x )
11
12 plt . plot (x , y)
13 plt . xlabel ( ’x ’ )
14 plt . ylabel ( ’y ’ )
15 plt . show ( )

This gives the following plot (see Figure 4.5):


If you want grids you can use the grid() function.

[End of Example]

4.6.1 Subplots
The subplot command enables you to display multiple plots in the same window.
Typing ”subplot(m,n,p)” partitions the figure window into an m-by-n matrix
of small subplots and selects the subplot for the current plot. The plots are
numbered along the first row of the figure window, then the second row, and so
on. See Figure 4.6.

Example 4.6.3. Creating Subplots

51
Figure 4.5: Plotting a Sine function in Python - Better Implementation

We will create and plot sin() and cos() in 2 different subplots.


1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2 im po rt numpy a s np
3
4 xstart = 0
5 x s t o p = 2∗ np . p i
6 increment = 0.1
7
8 x = np . a r a n g e ( x s t a r t , xstop , i n c r e m e n t )
9
10 y = np . s i n ( x )
11
12 z = np . c o s ( x )
13
14
15 plt . subplot (2 ,1 ,1)
16 plt . plot (x , y , ’g ’ )
17 plt . t i t l e ( ’ sin ’ )
18 plt . xlabel ( ’x ’ )
19 plt . ylabel ( ’ sin (x) ’ )
20 plt . grid ()
21 plt . show ( )
22
23
24 plt . subplot (2 ,1 ,2)
25 plt . plot (x , z , ’ r ’ )
26 plt . t i t l e ( ’ cos ’ )
27 plt . xlabel ( ’x ’ )
28 plt . ylabel ( ’ cos (x) ’ )
29 plt . grid ()
30 plt . show ( )

[End of Example]

52
Figure 4.6: Creating Subplots in Python

4.6.2 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 4.6.1. Create sin(x) and cos(x) in 2 different plots


Create sin(x) and cos(x) in 2 different plots.

You should use all the Plotting functions listed below in your code:

• plot()
• title()
• xlabel()
• ylabel()
• axis()
• grid()
• legend()
• show()

[End of Exercise]

53
Part II

Python Programming

54
Chapter 5

Python Programming

We have been through the basics in Python, such as variables, using some basic
built-in functions, basic plotting, etc.

You may come far only using these thins, but to create real applications, you
need to know about and use features like:
• If ... Else
• For Loops
• While Loops

• Arrays ...
If you are familiar with one or more other programming language, these fea-
tures should be familiar and known to you. All programming languages have
these features built-in, but the syntax is slightly different from one language to
another.

5.1 If ... Else


An ”if statement” is written by using the if keyword.

Here are some Examples how you use a If sentences in Python:


Example 5.1.1. Using If ... Else in Python
Using If :
1 a = 5
2 b = 8
3
4 if a > b:
5 p r i n t ( ” a i s g r e a t e r than b” )
6
7 if b > a:
8 p r i n t ( ”b i s g r e a t e r than a ” )
9
10 i f a == b :

55
11 p r i n t ( ” a i s e q u a l t o b” )
Listing 5.1: If

Try to change the values for a and b.

Using If - Else:
1 a = 5
2 b = 8
3
4 if a > b:
5 p r i n t ( ” a i s g r e a t e r than b” )
6 else :
7 p r i n t ( ”b i s g r e a t e r than a o r a and b a r e e q u a l ” )
Listing 5.2: If - Else

Using Elif :
1 a = 5
2 b = 8
3
4 if a > b:
5 p r i n t ( ” a i s g r e a t e r than b” )
6 elif b > a:
7 p r i n t ( ”b i s g r e a t e r than a ” )
8 e l i f a == b :
9 p r i n t ( ” a i s e q u a l t o b” )
Listing 5.3: Elif

Note! Python uses ”elif” not ”elseif” like many other programming languages
do.

[End of Example]

5.2 Arrays
An array is a special variable, which can hold more than one value at a time.

Here are some Examples how you can create and use Arrays in Python:
Example 5.2.1. Arrays in Python

1 data = [ 1 . 6 , 3 . 4 , 5 . 5 , 9 . 4 ]
2
3 N = l e n ( data )
4
5 p r i n t (N)
6
7 p r i n t ( data [ 2 ] )
8
9 data [ 2 ] = 7 . 3
10
11 p r i n t ( data [ 2 ] )

56
12
13
14 f o r x i n data :
15 print (x)
16
17
18 data . append ( 1 1 . 4 )
19
20
21 N = l e n ( data )
22
23 p r i n t (N)
24
25
26 f o r x i n data :
27 print (x)
Listing 5.4: Using Arrays in Python

You define an array like this:


1 data = [ 1 . 6 , 3 . 4 , 5 . 5 , 9 . 4 ]

You can also use text like this:


1 c a r l i s t = [ ” Volvo ” , ” T e s l a ” , ” Ford ” ]

You can use Arrays in Loops like this:


1 f o r x i n data :
2 print (x)

You can return the number of elements in the array like this:
1 N = l e n ( data )

You can get a specific value inside the array like this:
1 index = 2
2 x = cars [ index ]

You can use the append() method to add an element to an array:


1 data . append ( 1 1 . 4 )

[End of Example]

You have many built in methods you can use in combination with arrays, like
sort(), clear(), copy(), count(), insert(), remove(), etc.

You should look into test all these methods.

57
5.3 For Loops
A For loop is used for iterating over a sequence. I guess all your programs will
use one or more For loops. So if you have not used For loops before, make sure
to learn it now.

Below you see a basic example how you can use a For loop in Python:
1 f o r i in range (1 , 10) :
2 print ( i )

The For loop is probably one of the most useful feature in Python (or in any
kind of programming language). Below you will see different examples how you
can use a For loop in Python.

Example 5.3.1. Using For Loops in Python

1 data = [ 1 . 6 , 3 . 4 , 5 . 5 , 9 . 4 ]
2
3 f o r x i n data :
4 print (x)
5
6
7 c a r l i s t = [ ” Volvo ” , ” T e s l a ” , ” Ford ” ]
8
9 for car in c a r l i s t :
10 print ( car )
Listing 5.5: Using For Loops in Python

The range() function is handy to use in For Loops:


1 N = 10
2
3 f o r x i n r a n g e (N) :
4 print (x)

The range() function returns a sequence of numbers, starting from 0 by default,


and increments by 1 (by default), and ends at a specified number.

You can also use the range() function like this:


1 start = 4
2 s t o p= 12 #but not i n c l u d i n g
3
4 f o r x in range ( s t a r t , stop ) :
5 print (x)

Finally, you can also use the range() function like this:
1 start = 4
2 s t o p = 12 #but not i n c l u d i n g
3 step = 2
4
5 f o r x i n r a n g e ( s t a r t , s top , s t e p ) :
6 print (x)

58
You should try all these examples in order to learn the basic structure of a For
loop.

[End of Example]

Example 5.3.2. Using For Loops for Summation of Data


You typically want to use a For loop for find the sum of a given data set.
1 data = [ 1 , 5 , 6 , 3 , 1 2 , 3 ]
2
3 sum = 0
4
5 #Find t h e Sum o f a l l t h e numbers
6 f o r x i n data :
7 sum = sum + x
8
9 p r i n t ( sum )
10
11 #Find t h e Mean o r Average o f a l l t h e numbers
12
13 N = l e n ( data )
14
15 mean = sum/N
16
17 p r i n t ( mean )

This gives the following results:


1 30
2 5.0

[End of Example]

Example 5.3.3. Implementing Fibonacci Numbers Using a For Loop in Python


Fibonacci numbers are used in the analysis of financial markets, in strategies
such as Fibonacci retracement, and are used in computer algorithms such as the
Fibonacci search technique and the Fibonacci heap data structure.
They also appear in biological settings, such as branching in trees, arrangement
of leaves on a stem, the fruitlets of a pineapple, the flowering of artichoke, an
uncurling fern and the arrangement of a pine cone.

In mathematics, Fibonacci numbers are the numbers in the following sequence:


0, 1, 1, 2 ,3, 5, 8, 13, 21, 34, 55, 89, 144, . . .

By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent
number is the sum of the previous two.

Some sources omit the initial 0, instead beginning the sequence with two 1s.

59
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the
recurrence relation

fn = fn−1 + fn−2 (5.1)

with seed values:

f0 = 0, f1 = 1

We will write a Python script that calculates the N first Fibonacci numbers.
The Python Script becomes like this:
1 N = 10
2
3 fib1 = 0
4 fib2 = 1
5
6 print ( fib1 )
7 print ( fib2 )
8
9 f o r k i n r a n g e (N−2) :
10 f i b n e x t = f i b 2 +f i b 1
11 fib1 = fib2
12 fib2 = fib next
13 print ( fib next )
Listing 5.6: Fibonacci Numbers Using a For Loop in Python

Alternative solution:
1 N = 10
2
3 fib = [0 , 1]
4
5
6 f o r k i n r a n g e (N−2) :
7 f i b n e x t = f i b [ k +1] +f i b [ k ]
8 f i b . append ( f i b n e x t )
9
10 print ( fib )
Listing 5.7: Fibonacci Numbers Using a For Loop in Python - Alt2

Another alternative solution:


1 N = 10
2
3 fib = [ ]
4
5 f o r k i n r a n g e (N) :
6 f i b . append ( 0 )
7
8 fib [0] = 0
9 fib [1] = 1
10

60
11 f o r k i n r a n g e (N−2) :
12 f i b [ k +2] = f i b [ k +1] +f i b [ k ]
13
14
15 print ( fib )
Listing 5.8: Fibonacci Numbers Using a For Loop in Python - Alt3

Another alternative solution:


1 im po rt numpy a s np
2
3
4 N = 10
5
6 f i b = np . z e r o s (N)
7
8 fib [0] = 0
9 fib [1] = 1
10
11 f o r k i n r a n g e (N−2) :
12 f i b [ k +2] = f i b [ k +1] +f i b [ k ]
13
14
15 print ( fib )
Listing 5.9: Fibonacci Numbers Using a For Loop in Python - Alt4

[End of Example]

5.3.1 Nested For Loops


In Python and other programming languages you can use one loop inside an-
other loop.

Syntax for nested For loops in Python:


1 f o r i t e r a t i n g v a r in sequence :
2 f o r i t e r a t i n g v a r in sequence :
3 statements ( s )
4 statements ( s )

Simple example:
1 f o r i in range (1 , 10) :
2 f o r k in range (1 , 10) :
3 print ( i , k)

Exercise 5.3.1. Prime Numbers


The first 25 prime numbers (all the prime numbers less than 100) are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97

61
By definition a prime number has both 1 and itself as a divisor. If it has any
other divisor, it cannot be prime.

A natural number (1, 2, 3, 4, 5, 6, etc.) is called a prime number (or a prime) if


it is greater than 1 and cannot be written as a product of two natural numbers
that are both smaller than it.

Create a Python Script where you find all prime numbers between 1 and 200.

Tip! I guess this can be done in many different ways, but one way is to use 2
nested For Loops.

[End of Exercise]

5.4 While Loops


The while loop repeats a group of statements an indefinite number of times
under control of a logical condition.

Example 5.4.1. Using While Loops in Python

1 m = 8
2
3 while m > 2:
4 p r i n t (m)
5 m = m− 1
Listing 5.10: Using While Loops in Python

[End of Example]

5.5 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 5.5.1. Plot of Dynamic System

Given the autonomous system:


ẋ = ax (5.2)
Where:
1
a=−
T

62
where T is the time constant.

The solution for the differential equation is:

x(t) = eat x0 (5.3)

Set T=5 and the initial condition x(0)=1.

Create a Script in Python (.py file) where you plot the solution x(t) in the time
interval:
0 ≤ t ≤ 25

Add Grid, and proper Title and Axis Labels to the plot.

[End of Exercise]

63
Chapter 6

Creating Functions in
Python

6.1 Introduction
A function is a block of code which only runs when it is called. You can pass
data, known as parameters, into a function. A function can return data as a
result.

Previously we have been using many of the built-in functions in Python

If you are familiar with one or more other programming language, creating and
using functions should be familiar and known to you. All programming lan-
guages has the possibility to create functions, but the syntax is slightly different
from one language to another.

Some programming languages uses the term Method instead of a Function.


Functions and Methods behave in the same manner, but you could say that
Methods are functions that belongs to a Class. We will learn more about Classes
in Chapter 7.

Scripts vs. Functions

It is important to know the difference between a Script and a Function.

Scripts:
• A collection of commands that you would execute in the Editor
• Used for automating repetitive tasks

Functions:
• Operate on information (inputs) fed into them and return outputs
• Have a separate workspace and internal variables that is only valid inside
the function

64
• Your own user-defined functions work the same way as the built-in func-
tions you use all the time, such as plot(), rand(), mean(), std(), etc.
Python have lots of built-in functions, but very often we need to create our own
functions (we could refer to these functions as user-defined functions)
In Python a function is defined using the def keyword:

1 d e f FunctionName :
2 <s t a t e m e n t −1>
3 .
4 .
5 <s t a t e m e n t −N>
6 return . . .

Example 6.1.1. Basic Function


Below you see a simple function created in Python:
1 d e f add ( x , y ) :
2
3 return x + y
Listing 6.1: Basic Python Function

The function adds 2 numbers. The name of the function is add, and it returns
the answer using the return statement.

The statement return [expression] exits a function, optionally passing back an


expression to the caller. A return statement with no arguments is the same as
return None.

Note that you need to use a colon ”:” at the end of line where you define the
function.

Note also the indention used.

1 d e f add ( x , y ) :

Here you see a Python script where we use the function:


1 d e f add ( x , y ) :
2
3 return x + y
4
5
6 x = 2
7 y = 5
8
9 z = add ( x , y )
10
11 print ( z )
Listing 6.2: Creating and Using a Python Function

65
[End of Example]

Example 6.1.2. Create a Function in a separate File


We start by creating a separate Python File (myfunctions.py) for the function:
1 def average (x , y) :
2
3 r e t u r n ( x + y ) /2
Listing 6.3: Function calculating the Average

Next, we create a new Python File (e.g., testaverage.py) where we use the
function we created:
1 from m y f u n c t i o n s im po rt a v e r a g e
2
3 a = 2
4 b = 3
5
6 c = average (a , b)
7
8 print ( c )
Listing 6.4: Test of Average function

[End of Example]

6.2 Functions with multiple return values


Typically we want to return more than one value from a function.

Example 6.2.1. Create a Function Function with multiple return values


Create the following example:
1 def stat (x) :
2
3 totalsum = 0
4
5 #Find t h e Sum o f a l l t h e numbers
6 f o r x i n data :
7 totalsum = totalsum + x
8
9
10 #Find t h e Mean o r Average o f a l l t h e numbers
11
12 N = l e n ( data )
13
14 mean = t o t a l s u m /N
15
16
17 r e t u r n t o t a l s u m , mean
18
19
20

66
21 data = [ 1 , 5 , 6 , 3 , 1 2 , 3 ]
22
23
24 t o t a l s u m , mean = s t a t ( data )
25
26 p r i n t ( t o t a l s u m , mean )
Listing 6.5: Function with multiple return values

[End of Example]

6.3 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 6.3.1. Create Python Function

Create a function calcaverage that finds the average of two numbers.

[End of Exercise]

Exercise 6.3.2. Create Python functions for converting between radians and
degrees
Since most of the trigonometric functions require that the angle is expressed in
radians, we will create our own functions in order to convert between radians
and degrees.

It is quite easy to convert from radians to degrees or from degrees to radians.

We have that:

2π[radians] = 360[degrees] (6.1)


This gives:
180
d[degrees] = r[radians] × ( ) (6.2)
π
and
π
r[radians] = d[degrees] × (
) (6.3)
180
Create two functions that convert from radians to degrees (r2d(x)) and from
degrees to radians (d2r(x)) respectively.

These functions should be saved in one Python file .py.

Test the functions to make sure that they work as expected.

67
[End of Exercise]

Exercise 6.3.3. Create a Function that Implementing Fibonacci Numbers


Fibonacci numbers are used in the analysis of financial markets, in strategies
such as Fibonacci retracement, and are used in computer algorithms such as the
Fibonacci search technique and the Fibonacci heap data structure.
They also appear in biological settings, such as branching in trees, arrangement
of leaves on a stem, the fruitlets of a pineapple, the flowering of artichoke, an
uncurling fern and the arrangement of a pine cone.

In mathematics, Fibonacci numbers are the numbers in the following sequence:


0, 1, 1, 2 ,3, 5, 8, 13, 21, 34, 55, 89, 144, . . .

By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent
number is the sum of the previous two.

Some sources omit the initial 0, instead beginning the sequence with two 1s.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the


recurrence relation

fn = fn−1 + fn−2 (6.4)

with seed values:

f0 = 0, f1 = 1

Create a Function that Implementing the N first Fibonacci Numbers

[End of Exercise]

Exercise 6.3.4. Prime Numbers


The first 25 prime numbers (all the prime numbers less than 100) are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97

By definition a prime number has both 1 and itself as a divisor. If it has any
other divisor, it cannot be prime.

A natural number (1, 2, 3, 4, 5, 6, etc.) is called a prime number (or a prime) if


it is greater than 1 and cannot be written as a product of two natural numbers
that are both smaller than it.

Tip! I guess this can be implemented in many different ways, but one way is to
use 2 nested For Loops.

68
Create a Python function where you check if a given number is a prime number
or not.

You can check the function in the Command Window like this:
1 number = 4
2 c h e c k i f p r i m e ( number )

Then Python respond with True or False.

[End of Exercise]

69
Chapter 7

Creating Classes in Python

7.1 Introduction
Python is an object oriented programming (OOP) language. Almost everything
in Python is an object, with its properties and methods.

The foundation for all object oriented programming (OOP) languages are Classes.

To create a class, use the keyword class:

1 c l a s s ClassName :
2 <s t a t e m e n t −1>
3 .
4 .
5 .
6 <s t a t e m e n t −N>

Example 7.1.1. Simple Class Example


We will create a simple Class in Python.

1 c l a s s Car :
2 model = ” Volvo ”
3 c o l o r = ” Blue ”
4
5
6 c a r = Car ( )
7
8
9 p r i n t ( c a r . model )
10 print ( car . color )
Listing 7.1: Simple Python Class

The results will be in this case:


1 Volvo
2 Blue

70
This example don’t illustrate the good things with classes so we will create some
more examples.

[End of Example]

Example 7.1.2. Python Class


Lets create the following Python Code:
1 c l a s s Car :
2 model = ” ”
3 c o l o r = ””
4
5 c a r = Car ( )
6
7 c a r . model = ” Volvo ”
8 c a r . c o l o r = ” Blue ”
9
10 p r i n t ( c a r . c o l o r + ” ” + c a r . model )
11
12 c a r . model = ” Ford ”
13 c a r . c o l o r = ” Green ”
14
15 p r i n t ( c a r . c o l o r + ” ” + c a r . model )
Listing 7.2: Python Class example

You should try these examples.

[End of Example]

7.2 The init () Function


In Python all classes have a built-in function called init (), which is always
executed when the class is being initiated.
In many other OOP languages we call this the Constructor.
Exercise 7.2.1. The init () Function
We will create a simple example where we use the init () function to illustrate
the principle.

We change our previous Car example like this:


1 c l a s s Car :
2 def init ( s e l f , model , c o l o r ) :
3 s e l f . model = model
4 s e l f . color = color
5
6 c a r 1 = Car ( ” Ford ” , ” Green ” )
7
8 p r i n t ( c a r 1 . model )
9 print ( car1 . c o l o r )
10
11

71
12 c a r 2 = Car ( ” Volvo ” , ” Blue ” )
13
14 p r i n t ( c a r 2 . model )
15 print ( car2 . c o l o r )
Listing 7.3: Python Class Constructor Example

Lets extend the Class by defining a Function as well:


1 # Defining the Class Car
2 c l a s s Car :
3 def init ( self , model , c o l o r ) :
4 s e l f . model = model
5 s e l f . color = color
6
7 def displayCar ( s e l f ) :
8 p r i n t ( s e l f . model )
9 print ( s e l f . color )
10
11
12 # Lets s t a r t using the Class
13
14 c a r 1 = Car ( ” T e s l a ” , ”Red” )
15
16 car1 . displayCar ()
17
18
19 c a r 2 = Car ( ” Ford ” , ” Green ” )
20
21 p r i n t ( c a r 2 . model )
22 print ( car2 . c o l o r )
23
24
25 c a r 3 = Car ( ” Volvo ” , ” Blue ” )
26
27 p r i n t ( c a r 3 . model )
28 print ( car3 . c o l o r )
29
30 c a r 3 . c o l o r=” Black ”
31
32 car3 . displayCar ()
Listing 7.4: Python Class with Function

As you see from the code we have now defined a Class ”Car” that has 2 Class
variables called ”model” and ”color”, and in addition we have defined a Func-
tion (or Method) called ”displayCar()”.

Its normal to use the term ”Method” for Functions that are defined within a
Class.

You declare class methods like normal functions with the exception that the
first argument to each method is self.

To create instances of a class, you call the class using class name and pass in
whatever arguments its init () method accepts.

For example:

72
1 c a r 1 = Car ( ” T e s l a ” , ”Red” )
Listing 7.5: Import Class

[End of Example]

Exercise 7.2.2. Create the Class in a separate Python file


We start by creating the Class and then we save the code in ”Car.py”:
1 # Defining the Class Car
2 c l a s s Car :
3 def init ( self , model , c o l o r ) :
4 s e l f . model = model
5 s e l f . color = color
6
7 def displayCar ( s e l f ) :
8 p r i n t ( s e l f . model )
9 print ( s e l f . color )
Listing 7.6: Define Python Class in separate File

Then we create a Python Script (testCar.py) where we are using the Class:
1 # I m p o r t i n g t h e Car C l a s s
2 from Car im por t Car
3
4 # Lets s t a r t using the Class
5
6 c a r 1 = Car ( ” T e s l a ” , ”Red” )
7
8 car1 . displayCar ()
9
10
11 c a r 2 = Car ( ” Ford ” , ” Green ” )
12
13 p r i n t ( c a r 2 . model )
14 print ( car2 . c o l o r )
15
16
17 c a r 3 = Car ( ” Volvo ” , ” Blue ” )
18
19 p r i n t ( c a r 3 . model )
20 print ( car3 . c o l o r )
21
22 c a r 3 . c o l o r=” Black ”
23
24 car3 . displayCar ()
Listing 7.7: Script that is using the Class

Notice the following line at the top:


1 from Car im por t Car

[language=Python]

[End of Example]

73
7.3 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 7.3.1. Create Python Class


Create a Python Class where you calculate the degrees in Fahrenheit based on
the temperature in Celsius and vice versa.

The formula for converting from Celsius to Fahrenheit is:

Tf = (Tc × 9/5) + 32 (7.1)


The formula for converting from Fahrenheit to Celsius is:

Tc = (Tf − 32) × (5/9) (7.2)

[End of Exercise]

74
Chapter 8

Creating Python Modules

As your program gets longer, you may want to split it into several files for easier
maintenance. You may also want to use a handy function that you have written
in several programs without copying its definition into each program.

To support this, Python has a way to put definitions in a file and use them
in a script or in an interactive instance of the interpreter (the Python Console
window).

8.1 Python Modules


A module is a file containing Python definitions and statements. The file name
is the module name with the suffix .py appended.

Python allows you to split your program into modules that can be reused in
other Python programs. It comes with a large collection of standard modules
that you can use as the basis of your programs as we have seen examples of in
previous chapters. Not it is time to make your own modules from scratch.

Consider a module to be the same as a code library. A file containing a set of


functions you want to include in your application.

Previously you have been using different modules, libraries or packages created
by the Python organization or by others. Here you will create your own modules
from scratch.

Example 8.1.1. Create your first Python Module


We will create a Python module with 2 functions. The first function should
convert from Celsius to Fahrenheit and the other function should convert from
Fahrenheit to Celsius.

The formula for converting from Celsius to Fahrenheit is:


Tf = (Tc × 9/5) + 32 (8.1)

75
The formula for converting from Fahrenheit to Celsius is:

Tc = (Tf − 32) × (5/9) (8.2)

First, we create a Python module with the following functions (fahrenheit.py):


1 d e f c 2 f ( Tc ) :
2
3 Tf = ( Tc ∗ 9 / 5 ) + 32
4 r e t u r n Tf
5
6
7 d e f f 2 c ( Tf ) :
8
9 Tc = ( Tf − 3 2 ) ∗ ( 5 / 9 )
10 r e t u r n Tc
Listing 8.1: Fahrenheit Functions

Then, we create a Python script for testing the functions (testfahrenheit.py):


1 from f a h r e n h e i t i mp ort c 2 f , f 2 c
2
3 Tc = 0
4
5 Tf = c 2 f ( Tc )
6
7 p r i n t ( ” F a h r e n h e i t : ” + s t r ( Tf ) )
8
9
10 Tf = 32
11
12 Tc = f 2 c ( Tf )
13
14 p r i n t ( ” C e l s i u s : ” + s t r ( Tc ) )
Listing 8.2: Python Script testing the functions

The results becomes:


1 Fahrenheit : 32.0
2 Celsius : 0.0

8.2 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 8.2.1. Create Python Module for converting between radians and
degrees
Since most of the trigonometric functions require that the angle is expressed in
radians, we will create our own functions in order to convert between radians

76
and degrees.

It is quite easy to convert from radians to degrees or from degrees to radians.


We have that:

2π[radians] = 360[degrees] (8.3)


This gives:
180
d[degrees] = r[radians] × ( ) (8.4)
π
and
π
r[radians] = d[degrees] × ( ) (8.5)
180

Create two functions that convert from radians to degrees (r2d(x)) and from
degrees to radians (d2r(x)) respectively.

These functions should be saved in one Python file .py.

Test the functions to make sure that they work as expected. You can choose to
make a new .py file to test these functions or you can use the Console window.

[End of Exercise]

77
Chapter 9

File Handling in Python

9.1 Introduction
Python has several functions for creating, reading, updating, and deleting files.
The key function for working with files in Python is the open() function.

The open() function takes two parameters; Filename, and Mode.

There are four different methods (modes) for opening a file:

• ”x” - Create - Creates the specified file, returns an error if the file exists
• ”w” - Write - Opens a file for writing, creates the file if it does not exist

• ”r” - Read - Default value. Opens a file for reading, error if the file does
not exist
• ”a” - Append - Opens a file for appending, creates the file if it does not
exist

In addition you can specify if the file should be handled as binary or text mode

• ”t” - Text - Default value. Text mode


• ”b” - Binary - Binary mode (e.g. images)

9.2 Write Data to a File


To create a New file in Python, use the open() method, with one of the following
parameters:

• ”x” - Create - Creates the specified file, returns an error if the file exists
• ”w” - Write - Opens a file for writing, creates the file if it does not exist
• ”a” - Append - Opens a file for appending, creates the file if it does not
exist

78
To write to an Existing file, you must add a parameter to the open() function:

• ”w” - Write - Opens a file for writing, creates the file if it does not exist
• ”a” - Append - Opens a file for appending, creates the file if it does not
exist

Example 9.2.1. Write Data to a File

1 f = open ( ” m y f i l e . t x t ” , ”x” )
2
3 data = ” H e l l o World”
4
5 f . w r i t e ( data )
6
7 f . close ()
Listing 9.1: Write Data to a File

[End of Example]

9.3 Read Data from a File


To read to an existing file, you must add the following parameter to the open()
function:

• ”r” - Read - Default value. Opens a file for reading, error if the file does
not exist

Example 9.3.1. Read Data from a File

1 f = open ( ” m y f i l e . t x t ” , ” r ” )
2
3 data = f . r e a d ( )
4
5 p r i n t ( data )
6
7 f . close ()
Listing 9.2: Read Data from a File

[End of Example]

9.4 Logging Data to File


Typically you want to write multiple data to the, e.g., assume you read some
temperature data at regular intervals and then you want to save the temperature
values to a File.
Example 9.4.1. Logging Data to File

79
1 data = [ 1 . 6 , 3 . 4 , 5 . 5 , 9 . 4 ]
2
3 f = open ( ” m y f i l e . t x t ” , ”x” )
4
5 f o r v a l u e i n data :
6 record = s t r ( value )
7 f . write ( record )
8 f . w r i t e ( ” \n” )
9
10 f . close ()
Listing 9.3: Logging Data to File

[End of Example]

Example 9.4.2. Read Logged Data from File

1 f = open ( ” m y f i l e . t x t ” , ” r ” )
2
3 for record in f :
4 r e c o r d = r e c o r d . r e p l a c e ( ” \n” , ” ” )
5 print ( record )
6
7 f . close ()
Listing 9.4: Read Logged Data from File

[End of Example]

9.5 Web Resources


Below you find different useful resources for File Handling.

Python File Handling - w3school:


https://www.w3schools.com/python/pythonf ileh andling.asp

Reading and Writing Files - python.org:


https://docs.python.org/3/tutorial/inputoutput.htmlreading-and-writing-files

9.6 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 9.6.1. Data Logging


Assume you have the following data you want to log to a File as shown in Table
9.1.
Log these data to a File.

Create another Python Script that reads the same data.

80
[End of Exercise]

Exercise 9.6.2. Data Logging 2


Assume you read data from a Temperature sensor every 10 seconds for a period
of let say 5 minutes.

Log the data to a File.

You can use the Random Generator in Python. An example of how to use the
Random Generator is shown below:

1 im po rt random
2 f o r x in range (10) :
3 data = random . r a n d i n t ( 1 , 3 1 )
4 p r i n t ( data )
Listing 9.5: Read Data from a File

Make sure to log both the time and the temperature value

Create another Python Script that reads the same data.

You should also plot the data you read from the File.

[End of Exercise]

81
Table 9.1: Logged Data
Time Value
1 22
2 25
3 28
... ...

82
Chapter 10

Error Handling in Python

10.1 Introduction to Error Handling


So far error messages haven’t been discussed. You could say that we have 2
kinds of errors: syntax errors and exceptions.

10.1.1 Syntax Errors


Below we see an example of syntax errors:
1 >>> p r i n t ( H e l l o World )
2 F i l e ”<ipython −i n p u t −1−10cb182148e3>” , l i n e 1
3 p r i n t ( H e l l o World )
4 ˆ
5 SyntaxError : i n v a l i d syntax

In the example we have written print(Hello World) instead of print(”Hello


World”) and then the Python Interpreter gives us an error message.

10.1.2 Exceptions
Even if a statement or expression is syntactically correct, it may cause an error
when an attempt is made to execute it. Errors detected during execution are
called exceptions and are not unconditionally fatal: you will soon learn how to
handle them in Python programs. Most exceptions are not handled by programs,
however, and result in error messages as shown here:
1 >>> 10 ∗ ( 1 / 0 )
2 Traceback ( most r e c e n t c a l l last ) :
3
4 F i l e ”<ipython −i n p u t −2−0b 2 8 0 f 3 6 8 3 5 c >” , l i n e 1 , i n <module>
5 10 ∗ ( 1 / 0 )
6
7 Z e r o D i v i s i o n E r r o r : d i v i s i o n by z e r o

or:
1 >>> ’ 2 ’ + 2
2 Traceback ( most r e c e n t c a l l last ) :
3

83
4 F i l e ”<ipython −i n p u t −3−d2b23a1db757>” , l i n e 1 , i n <module>
5 ’2 ’ + 2
6
7 TypeError : must be s t r , not i n t

10.2 Exceptions Handling


It is possible to write programs that handle selected exceptions.

In Python we can use the following built-in Exceptions Handling features:

• The try block lets you test a block of code for errors.

• The except block lets you handle the error.


• The finally block lets you execute code, regardless of the result of the try-
and except blocks.

When an error occurs, or exception as we call it, Python will normally stop and
generate an error message.

These exceptions can be handled using the try - except statements.

Some basic example:

1 try :
2 10 ∗ ( 1 / 0 )
3 except :
4 p r i n t ( ”The c a l c u l a t i o n f a i l e d ” )

or:
1 try :
2 print (x)
3 except :
4 p r i n t ( ”x i s not d e f i n e d ” )

You can also use multiple exceptions:


1 try :
2 print (x)
3 e x c e p t NameError :
4 p r i n t ( ”x i s not d e f i n e d ” )
5 except :
6 p r i n t ( ” Something i s wrong ” )

The finally block, if specified, will be executed regardless if the try block raises
an error or not.

Example:

84
1 x=2
2
3 try :
4 print (x)
5 e x c e p t NameError :
6 p r i n t ( ”x i s not d e f i n e d ” )
7 except :
8 p r i n t ( ” Something i s wrong ” )
9 finally :
10 p r i n t ( ”The Program i s f i n i s h e d ” )

In general you should use try - except - finally when you try to open a File, read
or write to Files, connect to a Database, etc.

Example:
1 try :
2 f = open ( ” m y f i l e . t x t ” )
3 f . w r i t e ( ”Lorum Ipsum ” )
4 except :
5 p r i n t ( ” Something went wrong when w r i t i n g t o t h e f i l e ” )
6 finally :
7 f . close ()

85
Chapter 11

Debugging in Python

Debugging is the process of finding and resolving defects or problems within


a computer program that prevent correct operation of computer software or a
system [14].

Debuggers are software tools which enable the programmer to monitor the ex-
ecution of a program, stop it, restart it, set breakpoints, and change values in
memory. The term debugger can also refer to the person who is doing the de-
bugging.

As a programmer, one of the first things that you need for serious program
development is a debugger.

Python has a built-in debugger that can be used if you are coding Python with
a basic text editor and running your Python programs from the command line.

A better option is to use the Debugging features integrated in your Python Ed-
itor. Debugging is typically integrated with the Python Editor you are using.

See the specific chapter for the different Python Editors.

86
Chapter 12

Installing and using Python


Packages

A package contains all the files you need for a module. Modules are Python
code libraries you can include in your project.

Since Python is open source you can find thousands of Python Packages that
you can install and use in your Python programs.

You can use a Python Distribution like Anaconda Distribution (or similar
Python Distributions) to download and install many common Python Pack-
ages as mentioned previously.

12.1 What is PIP?


PIP is a package manager for Python packages, or modules if you like. PIP is
a tool for installing Python packages.

If you do not have PIP installed, you can download and install it from this page:
https://pypi.org/project/pip/

PIP is typically used from the Command Prompt (Windows) or Terminal win-
dow (macOS).

Installing Python Packages:

1 p i p i n s t a l l packagename

Uninstalling Python Packages:


1 p i p u n i n s t a l l packagename

Some Python Editors also have a graphical way of installing Python Packages,
like, e.g., Visual Studio.

87
Part III

Python Environments and


Distributions

88
Chapter 13

Introduction to Python
Environments and
Distributions

Python comes with many flavours and version.

Python is open source and everybody can bundle and distribute Python and
different Python Packages.

A Python environment is a context in which you run Python code and includes
Python Packages.

An environment consists of an interpreter, a library (typically the Python Stan-


dard Library), and a set of installed packages.

These components together determine which language constructs and syntax


are valid, what operating-system functionality you can access, and which pack-
ages you can use.

You can have multiple Python Environments on your Computer.

Some of them are:

• CPython distribution available from python.org

• Anaconda
• Enthought Canopy
• WinPython

• etc.

It is easy to start using Python by installing one of these Python Distributions.

89
But you can also install the core Python from:
https://www.python.org

Then install the additional Python Packages you need by using PIP.
https://pypi.org/project/pip/

13.1 Package and Environment Managers


The two most popular tools for installing Python Packages and setting up
Python environments are:

• PIP - a Python Package Manager


• Conda - a Package and Environment Manager (for Python and other lan-
guages)

13.1.1 PIP
Web:
https://pypi.org

PIP is typically used from the Command Prompt (Windows) or Terminal win-
dow (macOS).

Installing Python Packages:

1 p i p i n s t a l l packagename

Uninstalling Python Packages:


1 p i p u n i n s t a l l packagename

13.1.2 Conda
Conda is an open source package management system and environment man-
agement system that runs on Windows, macOS and Linux. Conda installs, runs
and updates packages and their dependencies.

The Conda package and environment manager is included in all versions of Ana-
conda.

Conda was created for Python programs, but it can package and distribute soft-
ware for any language.

Conda allows you to to also create separate environments containing files, pack-
ages and their dependencies that will not interact with other environments.

90
Web:
https://conda.io/

Conda is part of or integrated with the Anaconda Python Distribution.

Web:
https://www.anaconda.com

13.2 Python Virtual Environments


Python ”Virtual Environments” allow Python packages to be installed in an
isolated location for a particular application, rather than being installed glob-
ally.

You can have multiple Python Environments on your computer.

Python Virtual Environments have their own installation directories and they
don’t share libraries with other virtual environments.

Python ”Virtual Environments” is handy when you have different Python appli-
cations that needs different versions of Python or different version of the Python
Packages you are using.

91
Chapter 14

Anaconda

Anaconda is not an Editor, but a Python Distribution package. Spyder is in-


cluded in the Python Distribution package. You can also use Anaconda to install
other Editors or Python packages.

It is available for Windows, macOS and Linux.

Web:
https://www.anaconda.com

Wikipedia:
https://en.wikipedia.org/wiki/Anaconda( P ythond istribution)

14.1 Anaconda Navigator


Anaconda Navigator is a desktop graphical user interface (GUI) included in
Anaconda distribution that allows users to launch applications and manage
Python packages. The Anaconda Navigator can search for packages and install
them on your computer, run the packages and update them.

Figure 14.1 shows the Anaconda Navigator.

14.2 Anaconda Prompt


You can use the Anaconda Prompt if you need to install extra Python packages,
etc.

Let say you want to install the Python Control Systems Library package. Just
enter the following in the Anaconda Prompt:

pip i n s t a l l c o n t r o l

92
Figure 14.1: Anaconda Navigator

Python Package Index, or just pip, is a tool used to handle and install Python
packages.

For for information about pip and different packages you can install, see the
following:

https://pypi.org
Figure 14.2 shows where you can find the Anaconda Prompt. Windows: Search
for Anaconda Prompt in the Search field in the start menu.

93
Figure 14.2: Anaconda Navigator

94
Part IV

Python Editors

95
Chapter 15

Python Editors

An Editor is a program where you create your code (and where you can run
and test it). Most Editors have also features for Debugging and IntelliSense.

In theory, you can use Windows Notepad for creating Python programs, but
in practice it is impossible to create programs without having an editor with
Debugging, IntelliSense, color formatting, etc.

For simple Python programs you can use the IDLE Editor, but for more ad-
vanced programs a better editor is recommended.

Examples of Python Editors:

• Spyder
• Visual Studio Code
• Thonny
• Visual Studio

• PyCharm
• Wing
• JupyterNotebook

We will give an overview of these Code Editors in the next chapters.

I guess hundreds of different editors can be used for Python Programming, ei-
ther out of the box or if you install an additional Extension that makes sure
you can use Python in that editor.

If you already have a favorite Code Editor, it is a good change you can use that
one for Python programming.

Which editor you should use depends on your background, what kind of code
editors you have used previously, your programming skills, what your are going

96
to develop in Python, etc.

If you are familiar with MATLAB, Spyder is recommended. Also, if you want
to use Python for numerical calculations and computations, Spyder is a good
choice.

If you want to create Web Applications or other kinds of Applications, other


Editors are probably better to use.

For Internet of Things Applications, simple editors like Visual Studio Code or
Thonny may be good choices. Thonny is also the default editor on the Rasp-
berry Pi OS, which is popular Internet of Things platform.

For a list of ”Best Python Editors”, see [15].

97
Chapter 16

Spyder

Spyder - short for ”Scientific PYthon Development EnviRonment”.

Spyder is an open source cross-platform integrated development environment(IDE)


for scientific programming in the Python language.

Figure 16.1: Spyder Editor

The Spyder editor consists of the following parts or windows:


• Code Editor window
• iPython Console window

98
• Variable Explorer
• etc.

Web:
https://www.spyder-ide.org

If you have used MATLAB previously or want to use Python for scientific use,
Spyder is a good choice. it is easy to install using the Anaconda Distribution.

Web:
https://www.anaconda.com

16.1 Configuration
Typically you want to show figures and plots in separate windows.

Select Tools-Preferences as shown in Figure 16.2.

Figure 16.2: Python Tools-Preferences

Then select ”Automatic” as shown in Figure 16.3.

99
Figure 16.3: Python Preferences window

100
Chapter 17

Visual Studio Code

17.1 Introduction to Visual Studio Code


Visual Studio Code is a simple and easy to use editor that can be used for many
different programming languages.

Figure 17.1: Using Visual Studio Code as Python Editor

Right-Click and select ”Run Python File in Terminal”

Web:
https://code.visualstudio.com

Wikipedia:
https://en.wikipedia.org/wiki/VisualS tudioC ode

101
17.2 Python in Visual Studio Code
In addition to Visual Studio Code you need to install the Python extension for
Visual Studio Code.

You must install a Python interpreter yourself separately from the extension.
For a quick install, use Python from python.org.

https://www.python.org

Python is an interpreted language, and in order to run Python code and get
Python IntelliSense within Visual Studio Code, you must tell Visual Studio
Code which interpreter to use.

Web:
https://code.visualstudio.com/docs/languages/python

102
Part V

Python for Mathematics


Applications

103
Chapter 18

Mathematics in Python

Python is a powerful tool for mathematical calculations.

If you are looking for similar using MATLAB, please take a look at these re-
sources:
https://www.halvorsen.blog/documents/programming/matlab/

18.1 Basic Math Functions


The Python Standard Library consists of different modules for handling file
I/O, basic mathematics, etc. You don’t need to install these separately, but you
need to important them when you want to use some of these modules or some
of the functions within these modules.

In this chapter we will focus on the math module that is part of the Python
Standard Library.

The math module has all the basic math functions you need, such as: Trigono-
metric functions: sin(x), cos(x), etc. Logarithmic functions: log(), log10(), etc.
Constants like pi, e, inf, nan, etc. etc.

Example 18.1.1. Using the math module


We create some basic examples how to use a Library, a Package or a Module:

If we need only the sin() function we can do like this:


1 from math im por t s i n
2
3 x = 3.14
4 y = sin (x)
5
6 print (y)

If we need a few functions we can do like this

104
1 from math im por t s i n , c o s
2
3 x = 3.14
4 y = sin (x)
5 print (y)
6
7 y = cos (x)
8 print (y)

If we need many functions we can do like this:


1 from math im por t ∗
2
3 x = 3.14
4 y = sin (x)
5 print (y)
6
7 y = cos (x)
8 print (y)

We can also use this alternative:


1 im po rt math
2
3 x = 3.14
4 y = math . s i n ( x )
5
6 print (y)

We can also write it like this:


1 im po rt math a s mt
2
3 x = 3.14
4 y = mt . s i n ( x )
5
6 print (y)

[End of Example]

There are advantages and disadvantages with the different approaches. In your
program you may need to use functions from many different modules or pack-
ages. If you import the whole module instead of just the function(s) you need
you use more of the computer memory.

Very often we also need to import and use multiple libraries where the different
libraries have some functions with the same name but different use.

Other useful modules in the Python Standard Library are statistics (where
you have functions like mean(), stdev(), etc.)

For more information about the functions in the Python Standard Library,
see:
https://docs.python.org/3/library/

105
18.1.1 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 18.1.1. Create Mathematical Expressions in Python


Create a function that calculates the following mathematical expression:
p
z = 3x2 + x2 + y 2 + eln (x) (18.1)
Test with different values for x and y.

[End of Exercise]

Exercise 18.1.2. Create advanced Mathematical Expressions in Python

Create the following expression in Python:

ln (ax2 + bx + c) − sin(ax2 + bx + c)
f (x) = (18.2)
4πx2 + cos(x − 2)(ax2 + bx + c)

Given a = 1, b = 3, c = 5 Find f (9)


(The answer should be f (9) = 0.0044)

Tip! You should split the expressions into different parts, such as:

poly = ax2 + bx + c

num = . . .
den = . . .
f = ...

This makes the expression simpler to read and understand, and you minimize
the risk of making an error while typing the expression in Python.

When you got the correct answer try to change to, e.g., a = 2, b = 8, c = 6

Find f (9)

[End of Exercise]

Exercise 18.1.3. Pythagoras

106
Figure 18.1: Right-angled triangle

Pythagoras theorem is as follows:

c2 = a2 + b2 (18.3)

Create a function that uses Pythagoras to calculate the hypotenuse of a right-


angled triangle (Figure 18.1), e.g.:

1 def pythagoras (a , b)
2 ...
3 ...
4 return c

[End of Exercise]

Exercise 18.1.4. Albert Einstein


Given the famous equation from Albert Einstein:

E = mc2 (18.4)

The sun radiates 385x1024 J/s of energy.

Calculate how much of the mass on the sun is used to create this energy per day.

How many years will it take to convert all the mass of the sun completely? Do
we need to worry if the sun will be used up in our generation or the next? justify
the answer.

The mass of the sun is 2x1030 kg.

107
[End of Exercise]

Exercise 18.1.5. Cylinder Surface Area


Create a function that finds the surface area of a cylinder based on the height
(h) and the radius (r) of the cylinder. See Figure ??.

Figure 18.2: cylinder

[End of Exercise]

18.2 Statistics
18.2.1 Introduction to Statistics
Mean or average:
The mean is the sum of the data divided by the number of data points. It is
commonly called ”the average”.

Formula for mean:


N
x1 + x2 + x3 + ... + xN 1 X
x̄ = = xi (18.5)
N N i=1

Example 18.2.1. Mean


Given the following dataset: 2.2, 4.5, 6.2, 3.6, 2.6

Mean:
N
1 X 2.2 + 4.5 + 6.2 + 3.6 + 2.6 19.1
x̄ = xi = = = 3.82 (18.6)
N i=1 5 5

108
[End of Example]

Variance:

Variance is a measure of the variation in a data set.

N
1 X
var(x) = (xi − x̄)2 (18.7)
N i=1
Standard deviation:
The standard deviation is a measure of the spread of the values in a dataset
or the value of a random variable. It is defined as the square root of the variance.

v
u N
√ u1 X
std(x) = σ = var = t (xi − x̄)2 (18.8)
N i=1

We typically use the symbol σ for standard deviation.

We have that σ 2 = var(x)

18.2.2 Statistics functions in Python


Mathematical statistics functions in Python:
https://docs.python.org/3/library/statistics.html

statistics is part of the The Python Standard Library.

For more information about the functions in the Python Standard Library,
see:
https://docs.python.org/3/library/

Example 18.2.2. Statistics using the statistics module in Python Standard


Library
Below you find some examples how to use some of the statistics functions in the
statistics module in Python Standard Library:

1 im po rt s t a t i s t i c s a s s t
2
3 data = [ − 1 . 0 , 2 . 5 , 3 . 2 5 , 5 . 7 5 ]
4
5 #Mean o r Average
6 m = s t . mean ( data )
7 p r i n t (m)
8
9 # Standard D e v i a t i o n
10 s t d e v = s t . s t d e v ( data )

109
11 print ( st dev )
12
13 # Median
14 med = s t . median ( data )
15 p r i n t ( med )
16
17 # Variance
18 v a r = s t . v a r i a n c e ( data )
19 p r i n t ( var )
Listing 18.1: Statistics functions in Python

[End of Example]

IMPORTANT: Do not name your file ”statistics.py” since the import will be
confused and throw the errors of the library not existing and the mean function
not existing.

You can also use the NumPy Library. NumPy is the fundamental package for
scientific computing with Python.

Here you find an overview of the NumPy library:


http://www.numpy.org

Example 18.2.3. Statistics using the NumPy Library

Below you find some examples how to use some of the statistics functions in
NumPy:

1 im po rt numpy a s np
2
3 data = [ − 1 . 0 , 2 . 5 , 3 . 2 5 , 5 . 7 5 ]
4
5 #Mean o r Average
6 m = np . mean ( data )
7 p r i n t (m)
8
9 # Standard D e v i a t i o n
10 s t d e v = np . s t d ( data )
11 print ( st dev )
12
13 # Median
14 med = np . median ( data )
15 p r i n t ( med )
16
17 # Minimum Value
18 minv = np . min ( data )
19 p r i n t ( minv )
20
21 # Maxumum Value
22 maxv = np . max( data )
23 p r i n t ( maxv )
Listing 18.2: Statistics using the NumPy Library

110
[End of Example]

Exercise 18.2.1. Create your own Statistics Module in Python


Using the built-in functions in the Python Standard Library or the NumPy li-
brary is straightforward.

In order to get a deeper understanding of the mathematics behind these func-


tions and to learn more Python programming, you should create your own
Statistics Module in Python.

Create your own Statistics Module in Python (e.g., ”mystatistics.py) and then
create a Python Script (e.g., ”testmystatistics.py) where you test these func-
tions.

You should at least implement functions for mean, variance, standard deviation,
minimum and maximum.

[End of Exercise]

18.3 Trigonometric Functions


Python offers lots of Trigonometric functions, e.g., sin, cos, tan, etc.

Note! Most of the trigonometric functions require that the angle is expressed in
radians.
Example 18.3.1. Trigonometric Functions in Math module

1 im po rt math a s mt
2
3 x = 2∗mt . p i
4
5 y = mt . s i n ( x )
6 print (y)
7
8 y = mt . c o s ( x )
9 print (y)
10
11 y = mt . tan ( x )
12 print (y)
Listing 18.3: Trigonometric Functions in Math module

Here we have used the Math module in the Python Standard Library.

For more information about the functions in the Python Standard Library,
see:
https://docs.python.org/3/library/index.html

111
[End of Example]

Example 18.3.2. Plotting Trigonometric Functions


In the example above we used some of the trigonometric functions in basic cal-
culations.

Lets see if we are able to plot these functions.

1 im po rt math a s mt
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3
4 xdata = [ ]
5 ydata = [ ]
6
7 f o r x in range (0 , 10) :
8 xdata . append ( x )
9 y = mt . s i n ( x )
10 ydata . append ( y )
11
12 p l t . p l o t ( xdata , ydata )
13 p l t . show ( )
Listing 18.4: Plotting Trigonometric Functions

In the example we have plotted sin(x), we can easily extend the program to plot
cos(x), etc.

For more information about the functions in the Python Standard Library,
see:
https://docs.python.org/3/library/index.html

[End of Example]

Example 18.3.3. Trigonometric Functions using NumPy


The problem with using the Trigonometric functions in the the Math module
from the Python Standard Library is that they don’t handle an array as input.

We will use the NumPy library instead because they handle arrays, in addition
to all the handy functionality in the NumPy library.

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3
4 xstart = 0
5 x s t o p = 2∗ np . p i
6 increment = 0.1
7
8 x = np . a r a n g e ( x s t a r t , xstop , i n c r e m e n t )
9
10 y = np . s i n ( x )

112
11 plt . plot (x , y)
12 plt . t i t l e ( ’ y=s i n ( x ) ’ )
13 plt . xlabel ( ’x ’ )
14 plt . ylabel ( ’y ’ )
15 plt . grid ()
16 plt . a x i s ( [ 0 , 2∗ np . pi , −1, 1 ] )
17 plt . show ( )
18
19 y = np . c o s ( x )
20 plt . plot (x , y)
21 p l t . t i t l e ( ’ y=c o s ( x ) ’ )
22 plt . xlabel ( ’x ’ )
23 plt . ylabel ( ’y ’ )
24 plt . grid ()
25 p l t . a x i s ( [ 0 , 2∗ np . pi , −1, 1 ] )
26 p l t . show ( )
27
28 y = np . tan ( x )
29 plt . plot (x , y)
30 p l t . t i t l e ( ’ y=tan ( x ) ’ )
31 plt . xlabel ( ’x ’ )
32 plt . ylabel ( ’y ’ )
33 plt . grid ()
34 p l t . a x i s ( [ 0 , 2∗ np . pi , −1, 1 ] )
35 p l t . show ( )
Listing 18.5: Trigonometric Functions using NumPy

This Python script gives the plots as shown in Figure 18.3.

[End of Example]

Exercise 18.3.1. Create Python functions for converting between radians an


degrees
Since most of the trigonometric functions require that the angle is expressed in
radians, we will create our own functions in order to convert between radians
and degrees.

It is quite easy to convert from radians to degrees or from degrees to radians.

We have that:

2π[radians] = 360[degrees] (18.9)


This gives:
180
d[degrees] = r[radians] × ( ) (18.10)
π
and
π
r[radians] = d[degrees] × (
) (18.11)
180
Create two functions that convert from radians to degrees (r2d(x)) and from
degrees to radians (d2r(x)) respectively.

113
These functions should be saved in one Python file .py.

Test the functions to make sure that they work as expected.

[End of Exercise]

Exercise 18.3.2. Trigonometric functions on right triangle

Given right triangle as shown in Figure 18.4.

Create a function that finds the angle A (in degrees) based on input arguments
(a,c), (b,c) and (a,b) respectively.

Use, e.g., a third input “type” to define the different types above.

Use you previous function r2d() to make sure the output of your function is in
degrees and not in radians.

Test the function to make sure it works properly.

Tip! We have that:

a a
sin(A) = → A = arcsin( ) (18.12)
c c
b b
cos(A) = → A = arccos( ) (18.13)
c c
a a
tan(A) = → A = arctan( ) (18.14)
b b

We may also need to use the Pythagoras’ theorem:

c2 = a2 + b2 (18.15)

1 >>> a=5
2 >>> b=8
3 >>> c = s q r t ( a ∗∗2 + b ∗ ∗ 2 )
4
5 >>> A = r i g h t t r i a n g l e ( a , c , ’ s i n ’ )
6 A =
7 32.0054
8
9 >>> A = r i g h t t r i a n g l e ( b , c , ’ c o s ’ )
10 A =
11 32.0054
12 >>> A = r i g h t t r i a n g l e ( a , b , ’ tan ’ )
13 A =
14 32.0054

We also see that the answer in this case is the same, which is expected.

114
[End of Exercise]

Exercise 18.3.3. Law of Cosines

Given the triangle as shown in Figure 18.5.

Create a function where you find c using the law of cosines.

c2 = a2 + b2 − 2ab cos(C) (18.16)

Test the functions to make sure it works properly.

[End of Exercise]

Exercise 18.3.4. Plotting Trigonometric Functions

Plot sin(θ) and cos(θ) for 0 ≤ θ ≤ 2π in the same plot (both in the same plot
and in 2 different subplots).

Make sure to add labels and a legend and use different line styles and colors for
the plots.

[End of Exercise]

18.4 Polynomials
A polynomial is expressed as:

p(x) = p1 xn + p2 xn−1 + ... + pn x + pn+1 (18.17)

where p1 , p2 , p3 , ... are the coefficients of the polynomial.

We will use the Polynomial Module in the NumPy Package.

Web:
https://numpy.org/doc/stable/reference/routines.polynomials.polynomial.html

Other Resources:

Python Advanced Course Topics - Polynomials:


https://www.python-course.eu/polynomialc lassi np ython.php

115
116

Figure 18.3: Trigonometric Functions


Figure 18.4: Right Triangle

Figure 18.5: Law of Cosines

117
Chapter 19

Linear Algebra in Python

19.1 Introduction to Linear Algebra


A matrix is a two-dimensional data structure where numbers are arranged into
rows and columns.

Matrix is a special case of two dimensional array where each data element is of
strictly same size.

Matrices are very important data structures for many mathematical and scien-
tific calculations.

A general matrix is defined as:


 
a11 a12 ... a1m
 a21 a22 .. a2m 
A=  ∈ Rnxm (19.1)
 ... ... ... ... 
an1 an2 ... anm

Where n is number of rows and m is number of columns.

Example of a 3 x 3 matrix:
 
1 5 3
A= 4 6 6  (19.2)
3 8 9

Example of a 3 x 4 matrix:
 
1 5 3 4
A= 4 5 7 8  (19.3)
7 8 9 3

Example of a 4 x 2 matrix:

118
 
1 5
 4 5 
A=
 3
 (19.4)
2 
7 8

Python doesn’t have a built-in type for matrices. However, we can create list of
a list as a matrix.

Example 19.1.1. Matrix definition with Standard Python


Here is an example how we can implement a vector and a matrix in standard
Python:
1 a = [1 , 3 , 7 , 2]
2
3 p r i n t ( ” a =” , a )
4
5
6 A = [[1 , 3 , 7 , 2] ,
7 [ 5 , 8 , −9, 0 ] ,
8 [ 6 , −7, 1 1 , 1 2 ] ]
9
10 p r i n t ( ”A =” , A)
Listing 19.1: Python Arrays

This gives the following output:


1 a = [1 , 3 , 7 , 2]
2 A = [ [ 1 , 3 , 7 , 2 ] , [ 5 , 8 , −9, 0 ] , [ 6 , −7, 1 1 , 1 2 ] ]

So we can define vectors and matrices with standard Python, but standard
Python has no support for manipulation and calculation of them.

But fortunately we can use the NumPy package for creating matrices and for
matrix manipulation.

[End of Example]

19.2 Linear Algebra with Python


We will use the NumPy package for matrix manipulation.

NumPy is the fundamental package for scientific computing with Python.

Here you find an overview of the NumPy library:


http://www.numpy.org

Example 19.2.1. Matrix Manipulation using the NumPy Library

119
Below you see how we can use NumPy for creating vectors and matrices and
manipulate them using NumPy:

1 im po rt numpy a s np
2
3 a = np . a r r a y ( [ 1 , 3 , 7 , 2 ] )
4
5 p r i n t ( ” a =” , a )
6
7
8
9 A = np . a r r a y ( [ [ 1 , 3 , 7 , 2 ] ,
10 [ 5 , 8 , −9, 0 ] ,
11 [ 6 , −7, 1 1 , 1 2 ] ] )
12
13
14 p r i n t ( ”A =” , A)
15
16
17
18
19 A = np . a r r a y ( [ [ 0 , 1 ] ,
20 [ −2 , − 3 ] ] )
21
22 B = np . a r r a y ( [ [ 1 , 0 ] ,
23 [ 3 , −2]])
24
25 C = A + B
26 p r i n t (C)
27
28
29 C = A. dot (B)
30 p r i n t (C)
31
32 C = A. t r a n s p o s e ( )
33 p r i n t (C)
Listing 19.2: Matrix manipulation using NumPy

[End of Example]

19.2.1 Vectors
Use np.array() when you define vectors:
1 im po rt numpy a s np
2
3 a = np . a r r a y ( [ 1 , 3 , 7 , 2 ] )
4
5 p r i n t ( ” a =” , a )
Listing 19.3: Matrix manipulation using NumPy

120
19.2.2 Matrices
You can use np.array() when defining matrices also, but you may also use
np.matrix().

The numpy matrix object is a subclass of the numpy array object and it is
tailor-made for matrices. The numpy matrices are strictly 2-dimensional, while
numpy arrays can be of any dimension.

Example:
1 im po rt numpy a s np
2
3 A = np . m a t r i x ( [ [ 0 , 1 ] ,
4 [ −2 , − 3 ] ] )
5
6 p r i n t ( ”A =” , A)

It is no longer recommended to use this class, even for linear algebra. Instead
use regular arrays. The class may be removed in the future.

For more information, please see the following:

https://numpy.org/doc/stable/reference/generated/numpy.matrix.html

19.2.3 Linear Algebra (numpy.linalg)


For more Linear Algebra functionality in the NumPy library you need to use
the numpy.linalg module.

Here you find an overview of the numpy.linalg module:


https://docs.scipy.org/doc/numpy/reference/routines.linalg.html

We will use the numpy.linalg in different examples in this chapter.

19.2.4 Matrix Addition


Given the matrices:

A ∈ Rnxm

and

B ∈ Rnxm

Then

C = A + B ∈ Rnxm

Example:

121
 
0 1
A= (19.5)
−2 −3

 
1 0
B= (19.6)
3 −2

Then we get:

      
0 1 1 0 0+1 1+0 1 1
A+B = = = (19.7)
−2 −3 3 −2 −2 + 3 −3 − 2 1 −5

Example 19.2.2. Matrix Addition in Python

1 im po rt numpy a s np
2
3 A = np . m a t r i x ( [ [ 0 , 1 ] ,
4 [ −2 , − 3 ] ] )
5
6 B = np . m a t r i x ( [ [ 1 , 0 ] ,
7 [ 3 , −2]])
8
9 C = A + B
10 p r i n t (C)
Listing 19.4: Matrix Addition in Python

We get:
1 [ [ 1 1]
2 [ 1 −5]]

[End of Example]

19.2.5 Matrix Subtraction


Given the matrices:

A ∈ Rnxm

and

B ∈ Rnxm

Then

122
C = A − B ∈ Rnxm

Example:

 
0 1
A= (19.8)
−2 −3

 
1 0
B= (19.9)
3 −2

Then we get:

      
0 1 1 0 0−1 1−0 −1 1
A−B = = =
−2 −3 3 −2 −2 − 3 −3 − (−2) −5 −1
(19.10)

Example 19.2.3. Matrix Subration in Python

1 im po rt numpy a s np
2
3 A = np . m a t r i x ( [ [ 0 , 1 ] ,
4 [ −2 , − 3 ] ] )
5
6 B = np . m a t r i x ( [ [ 1 , 0 ] ,
7 [ 3 , −2]])
8
9 C = A − B
10 p r i n t (C)
Listing 19.5: Matrix Subraction in Python

We get:
1 [[ − 1 1]
2 [ −5 − 1 ] ]

[End of Example]

123
19.2.6 Matrix Multiplication
Given the matrices:

A ∈ Rnxm

and

B ∈ Rmxp

Then

C = AB ∈ Rnxp

Where
n
X
cjk = ajl blk
l=1

Example:

 
0 1
A= (19.11)
−2 −3

 
1 0
B= (19.12)
3 −2

Then we get:
  
10 1 0
AB =
−2
−3 3 −2
   
0·1+1·3 0 · 0 + 1 · (−2) 3 −2
= = (19.13)
−2 · 1 − 3 · 3 −2 · 0 − 3 · (−2) −11 6

We do the same in Python:

Example 19.2.4. Matrix Multiplication in Python

1 im po rt numpy a s np
2
3 A = np . m a t r i x ( [ [ 0 , 1 ] ,
4 [ −2 , − 3 ] ] )
5
6 B = np . m a t r i x ( [ [ 1 , 0 ] ,
7 [ 3 , −2]])
8
9 C = A ∗ B
10 p r i n t (C)
Listing 19.6: Matrix Multiplication in Python

124
This gives:
1 [[ 3 −2]
2 [ −11 6]]

Below you see different alternative solutions that can be used:


1 im po rt numpy a s np
2
3 A = np . a r r a y ( [ [ 0 , 1 ] ,
4 [ −2 , − 3 ] ] )
5
6 B = np . a r r a y ( [ [ 1 , 0 ] ,
7 [ 3 , −2]])
8
9 #A l t e r n a t i v e 1
10 C = A. dot (B)
11 p r i n t (C)
12
13 #A l t e r n a t i v e 2
14 C = np . dot (A, B)
15 p r i n t (C)
16
17 #A l t e r n a t i v e 3
18 C = np . mat (A) ∗ np . mat (B)
19 p r i n t (C)
Listing 19.7: Matrix Multiplication in Python - Alternative Solutions

As shown in the example you can use different syntax. The 3 alternatives in the
example give the same result. Try it.

[End of Example]

In matrix multiplication the matrices don’t need to be quadratic, but the inner
dimensions need to be the same. The size of the resulting matrix will be the
outer dimensions. See Figure 19.1.

Figure 19.1: Matrix Multiplication

We have also the following matrix rules:

AB 6= BA (19.14)

A(BC) = (AB)C (19.15)

125
(A + B)C = AC + BC (19.16)

C(A + B) = CA + CB (19.17)

Exercise 19.2.1. Matrix Rules


Create a Python Script where you verify the rules above is correct.

[End of Exercise]

19.2.7 Transpose of a Matrix


A general matrix is defined as:
 
a11 a12 ... a1m
 a21 a22 .. a2m 
A=  ∈ Rnxm (19.18)
 ... ... ... ... 
an1 an2 ... anm

Where n is number of rows and m is number of columns.

The transpose of matrix A is then:

 
a11 a21 ... an1
T
 a12 a22 .. an2 
A =  ∈ Rmxn (19.19)
 ... ... ... ... 
a1m a2m ... anm

The transform of a matrix is formed by turning all the rows of a given matrix
into columns and vice-versa.
Example 19.2.5. Transpose of a Matrix in Python

1 im po rt numpy a s np
2
3 A = np . m a t r i x ( [ [ 0 , 1 ] ,
4 [ −2 , − 3 ] ] )
5
6 At = np . t r a n s p o s e (A)
7 p r i n t ( At )
8
9
10 B = np . m a t r i x ( [ [ 1 , 0 , 4 ] ,
11 [ 3 , −2, 8 ] ] )
12
13 Bt = np . t r a n s p o s e (B)
14 p r i n t ( Bt )

126
15
16
17
18 C = np . m a t r i x ( [ [ 1 , 4 , ] ,
19 [ 2 , −3 ,] ,
20 [ −6 , − 2 ] ] )
21
22 Ct = np . t r a n s p o s e (C)
23 p r i n t ( Ct )
Listing 19.8: Transpose of a Matrix

1 [ [ 0 −2]
2 [ 1 −3]]
3
4 [ [ 1 3]
5 [ 0 −2]
6 [ 4 8]]
7
8 [ [ 1 2 −6]
9 [ 4 −3 − 2 ] ]

[End of Example]

19.2.8 Determinant
Given a matrix A the Determinant is given by:

det(A) = |A|

For a 2x2 matrix A:


 
a11 a12
A= (19.20)
a21 a22

We have:

det(A) = |A| = a11 a22 − a21 a12 (19.21)

Example:

 
1 2
A= (19.22)
3 4

We get:

det(A) = |A| = 1 · 4 − 3 · 2 = 4 − 6 = −2 (19.23)

127
Example 19.2.6. Determinant
Python Example:
1 im po rt numpy a s np
2 im po rt numpy . l i n a l g a s l a
3
4 A = np . m a t r i x ( [ [ 1 , 2 ] ,
5 [3 , 4]])
6
7 Adet = l a . d e t (A)
8
9 p r i n t ( Adet )
Listing 19.9: Determinant

This gives:
1 −2.0000000000000004
Listing 19.10: Determinant

[End of Example]

19.2.9 Inverse Matrix


The inverse of a quadratic matrix A ∈ Rnxn is defined by:

A−1

For a square matrix A, the inverse is written A−1 . When A is multiplied by


A−1 the result is the identity matrix I. Non-square matrices do not have inverses.

We have that:

AA−1 = A−1 A = I (19.24)


For a 2x2 matrix we have:
 
a11 a12
A= (19.25)
a21 a22

The inverse of A becomes:


 
−1 1 a22 −a12
A = (19.26)
det(A) −a21 a11

Example 19.2.7. Inverse Matrix

128
Python Example:
1 im po rt numpy a s np
2 im po rt numpy . l i n a l g a s l a
3
4 A = np . m a t r i x ( [ [ 1 , 2 ] ,
5 [3 , 4]])
6
7 Ainv = l a . i n v (A)
8
9 p r i n t ( Ainv )
Listing 19.11: Inverse Matrix

We get the following results:


1 [[ −2. 1. ]
2 [ 1.5 −0.5]]

[End of Example]

19.3 Solving Linear Equations


Example 19.3.1. Solving Linear Equations
Given the equations:

x1 + 2x2 = 5 (19.27)

3x1 + 4x2 = 6 (19.28)

We want to set the equations on the following form:

Ax = b (19.29)

We need to find A and b and define them in Python.

Then we can solve the equations, i.e., find x1 and x2 using Python.

It can be solved like this:

x = A−1 b (19.30)

We get:

129
 
1 2
A= (19.31)
3 4

 
5
b= (19.32)
6

 
x1
x= (19.33)
x2

Python Script:
1 im po rt numpy a s np
2 im po rt numpy . l i n a l g a s l a
3
4 A = np . a r r a y ( [ [ 1 , 2 ] ,
5 [3 , 4]])
6
7 b = np . a r r a y ( [ [ 5 ] ,
8 [6]])
9
10 Ainv = l a . i n v (A)
11
12 x = Ainv . dot ( b )
13
14 print (x)

The results becomes:


1 [[ −4. ]
2 [ 4.5]]

You can also use the following:


1 x = np . l i n a l g . s o l v e (A, b )

Python Script:
1 im po rt numpy a s np
2
3 A = np . a r r a y ( [ [ 1 , 2 ] ,
4 [3 , 4]])
5
6 b = np . a r r a y ( [ [ 5 ] ,
7 [6]])
8
9 x = np . l i n a l g . s o l v e (A, b )
10 print (x)

Note! The A matrix must be square and of full-rank, i.e. the inverse matrix
needs to exists.

130
[End of Example]

In many cases we cannot find the inverse matrix, e.g., when the matrix is not
quadratic. Finding the inverse matrix for large matrices is also time-consuming.

The numpy.linalg module can be used.

Here you find an overview of the numpy.linalg module:


https://docs.scipy.org/doc/numpy/reference/routines.linalg.html

Example 19.3.2. Solving Linear Equations when A is not quadratic


We use lstsq for the least-squares best “solution” of the system/equation.

Python Script:
1 im po rt numpy a s np
2
3 A = np . a r r a y ( [ [ 1 , 2 ] ,
4 [3 , 4] ,
5 [7 , 8]])
6
7 b = np . a r r a y ( [ [ 5 ] ,
8 [6] ,
9 [9]])
10
11 #x = np . l i n a l g . s o l v e (A, b ) #Not working b e c a u s e i n v e r s e (A) d o e s not
exists
12
13
14 x = np . l i n a l g . l s t s q (A, b , rcond=None ) [ 0 ]
15
16 print (x)

The results becomes:


1 [[ −3.5 ]
2 [ 4.17857143]]

[End of Example]

19.4 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 19.4.1. Exercise Solving Linear Equations

131
Given the following equations:

x1 + 2x2 = 5 (19.34)

3x1 + 4x2 = 6 (19.35)

7x1 + 8x2 = 9 (19.36)

Find the solutions for the given equations using Python.

[End of Exercise]

Exercise 19.4.2. Matrix Addition, Subtraction and Multiplication using nested


For Loops
Assume that that you cannot do matrix addition, subtraction and multiplica-
tion as shown in the examples above.

Create a Python Module with 3 functions (e.g., matrixaddition(), matrixsub-


traction(), matrixmultiplication()) where you implement your own version of
matrix addition, subtraction and multiplication using nested For Loops.
Make sure to test the functions that they work as expected, e.g.:
1 im po rt mymatrixmodule a s m a t r i x
2
3 A = [[1 , 3 , 7] ,
4 [ 5 , 8 , −9] ,
5 [ 6 , −7, 1 1 ] ]
6
7 B = [[2 , 3 , 5] ,
8 [ 5 , −9, −9] ,
9 [6 , 8 , 1]]
10
11 c = m a t r i x . m a t r i x a d d i t i o n (A, B)
12 p r i n t (C)
13
14 c = m a t r i x . m a t r i x s u b t r a c t i o n (A, B)
15 p r i n t (C)
16
17 c = m a t r i x . m a t r i x m u l t i p l i c a t i o n (A, B)
18 p r i n t (C)
Listing 19.12: Python Arrays

You should test your function by do the calculations by hand and by using the
the numpy functionality. Compare the results and make sure you get the same
answers.

[End of Exercise]

132
Chapter 20

Complex Numbers in
Python

20.1 Introduction to Complex Numbers


A complex number is defined like this:

z = a + jb (20.1)


Where the imaginary unit j is defined as i = −1

Where a is called the real part of z and b is called the imaginary part of z, i.e.:

Re(z) = a, Im(z) = b

Figure 20.1 shows an illustration of complex numbers.

In Python we define a complex number like this:


1 >>> z = 2 + 3 j

The complex conjugate of z is defined as:

z∗ = a − jb (20.2)

You may also imaginary numbers on exponential/polar form:

z = rejθ (20.3)

133
Where:
p
r = |z| = a2 + b2 (20.4)

and

b
θ = arctan (20.5)
a

Note that a = r cos θ and b = r sin θ

Figure 20.2 shows an illustration of complex numbers on polar form.

Figure 20.1: Complex Numbers

To add or subtract two complex numbers, we simply add (or subtract) their real
parts and their imaginary parts.

In division and multiplication, we use the polar form.

Given the complex numbers:

z1 = r1 ejθ1 (20.6)

z2 = r2 ejθ2 (20.7)
Multiplication:

z3 = z1 z2 = r1 r1 ej(θ1 +θ1 ) (20.8)

134
Figure 20.2: Complex Numbers - Polar form

Division:

z1 r1 ejθ1 r1
z3 = = = ej(θ1 −θ2 ) (20.9)
z2 r2 ejθ2 r2

20.2 Complex Numbers with Python


In Python you can use the cmath library which has mathematical functions for
complex numbers.

https://docs.python.org/3/library/cmath.html

cmath is part of the The Python Standard Library.

For more information about the functions in the Python Standard Library,
see:
https://docs.python.org/3/library/

Example 20.2.1. Basic Complex Numbers in Python


Given the following complex numbers:

a = 5 + 3j (20.10)

135
b = 1 − 1j (20.11)

In Python we can define the complex numbers and perform basic operations (+,
-, *, /) like this:

1 a = 5 + 3j
2 b = 1 − 1j
3
4 c = a + b
5 print ( c )
6
7 d = a − b
8 print (d)
9
10 e = a ∗ b
11 print ( e )
12
13 f = a / b
14 print ( f )
Listing 20.1: Basic Complex Numbers in Python

[End of Example]

Example 20.2.2. Complex Number Functions in Python

1 im po rt cmath
2
3 x = 2
4 y = −3
5
6 # c o n v e r t i n g x and y i n t o complex number u s i n g complex ( )
7 z = complex ( x , y )
8 print ( z . real )
9 p r i n t ( z . imag )
10
11 print ( z . conjugate () )
12
13 # c o n v e r t i n g complex number i n t o p o l a r u s i n g p o l a r ( )
14 w = cmath . p o l a r ( z )
15 p r i n t (w)
16
17
18 # c o n v e r t i n g complex number i n t o r e c t a n g u l a r u s i n g r e c t ( )
19 w = cmath . r e c t ( 2 , 3 )
20 p r i n t (w)
Listing 20.2: Complex Number Functions in Python

[End of Example]

136
Chapter 21

Differential Equations

21.1 Introduction to Differential Equations


A differential equation is a mathematical equation that relates some function
with its derivatives.

In applications, the functions usually represent physical quantities, the deriva-


tives represent their rates of change, and the differential equation defines a
relationship between the two.

Because such relations are extremely common, differential equations play a


prominent role in many disciplines including engineering, physics, economics,
and biology.

We typically want to solve ordinary differential equations (ODE) of the form:


dy
= f (t, y), y(t0 ) = y0 (21.1)
dt
dy
Note! Different notation is used: dt = y’= ẏ

This document will use these different notations interchangeably.

Example 21.1.1. Example of Dynamic System


Given the following differential equation:

ẋ = −ax + bu (21.2)
dx
Note! ẋ is the same as dt

We have the following:


• x - Process variable, e.g., Level, Pressure, Temperature, etc.
• u - Input variable, e.g., Control Signal from the Controller
• a, b - Constants

137
[End of Example]

With Python have we can solve these differential equations in many different
ways.

We can use so-called ODE solvers or we can make discrete version of the differ-
ential equations using discretization methods like Euler, etc.

With ODE solvers Python can solve these equations numerically. Higher order
differential equations must be reformulated into a system of first order differen-
tial equations.

In chapter 25 we will simulate (solve and plot the results) such differential equa-
tions numerically using Euler discretization.

Example 21.1.2. Differential Equation Example


Given the following differential equation:

ẋ = ax (21.3)
Where a = − T1 , where T is defined as the time constant of the system.

dx
Note! ẋ is the same as dt

The solution for the differential equation is found to be:

x(t) = eat x0 (21.4)


We shall plot the solution for this differential equation using Python.

In our system we can set T = 5 and the initial condition x0 = x(0) = 1


Python code:
1 im po rt math a s mt
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5
6 # P ar a m e t e r s
7 T = 5
8 a = −1/T
9
10 x0 = 1
11 t = 0
12
13 tstart = 0
14 t s t o p = 25
15
16 increment = 1
17
18 x = []
19 x = np . z e r o s ( t s t o p +1)
20
21 t = np . a r a n g e ( t s t a r t , t s t o p +1 , i n c r e m e n t )

138
22
23
24 # D e f i n e t h e Equation
25 f o r k in range ( tstop ) :
26 x [ k ] = mt . exp ( a ∗ t [ k ] ) ∗ x0
27
28
29 # Plot the Results
30 plt . plot (t , x)
31 p l t . t i t l e ( ’ P l o t t i n g D i f f e r e n t i a l Equation S o l u t i o n ’ )
32 plt . xlabel ( ’ t ’ )
33 plt . ylabel ( ’x( t ) ’ )
34 plt . grid ()
35 p l t . a x i s ( [ 0 , 25 , 0 , 1 ] )
36 p l t . show ( )
Listing 21.1: Differential Equation

This gives the plot shown in Figure 21.1.

Figure 21.1: Plotting Differential Equation Solution

An alternative and perhaps simpler Python code:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3
4
5 # P ar a m e t e r s
6 T = 5

139
7 a = −1/T
8
9 x0 = 1
10 t = 0
11
12 tstart = 0
13 t s t o p = 25
14 increment = 1
15 N = 25
16
17 #t = np . a r a n g e ( t s t a r t , t s t o p +1 , i n c r e m e n t ) #A l t e r n a t i v e Approach
18 t = np . l i n s p a c e ( t s t a r t , t s t o p , N)
19
20 x = np . exp ( a ∗ t ) ∗ x0
21
22
23 # Plot the Results
24 plt . plot (t , x)
25 p l t . t i t l e ( ’ P l o t t i n g D i f f e r e n t i a l Equation S o l u t i o n ’ )
26 plt . xlabel ( ’ t ’ )
27 plt . ylabel ( ’x( t ) ’ )
28 plt . grid ()
29 p l t . a x i s ( [ 0 , 25 , 0 , 1 ] )
30 p l t . show ( )
Listing 21.2: Differential Equation

This alternative Python code gives the same plot as shown in Figure 21.1.

Solving differential equations like shown in this example works fine, but the
problem is that we first have to manually (by pen and paper) find the solution
to the differential equation.

An alternative is to use solvers for Ordinary Differential Equations (ODE) in


Python.

In the examples and tasks below we will learn how we can use these built-in
ODE solvers.
Another approach is to solve such equations from ”scratch” by making a dis-
crete version of the differential equation. This approach is presented later in
this textbook (chapter 25).

[End of Example]

21.2 ODE Solvers in Python


The scipy.integrate library has two powerful powerful functions ode() and
odeint(), for numerically solving first order ordinary differential equations
(ODEs). The ode() is more flexible, while odeint() (ODE integrator) has
a simpler Python interface works fine for most problems.

For details, see the SciPy documentation:

140
https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html

https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.integrate.ode.html

Example 21.2.1. Using ODE Solver in Python


Given the following differential equation:

ẋ = ax (21.5)
Where a = − T1 , where T is defined as the time constant of the system.

dx
Note! ẋ is the same as dt

We will use the odeint() function.

The syntax is as follows:


1 x = o d e i n t ( functionname , x0 , t )

Where we have:
functioname: Function that returns derivative values at requested x and t values
as dxdt = model(x,t)

x0: Initial conditions of the differential states

t: Time points at which the solution should be reported. Additional internal


points are often calculated to maintain accuracy of the solution but are not
reported.

Where we first has to define our differential equation:


1 def functionname ( x , t ) :
2 dxdt = a ∗ x
3 r e t u r n dxdt

The Python code becomes:


1 im po rt numpy a s np
2 from s c i p y . i n t e g r a t e i mpo rt o d e i n t
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Initialization
6 tstart = 0
7 t s t o p = 25
8 increment = 1
9
10 x0 = 1
11 t = np . a r a n g e ( t s t a r t , t s t o p +1 , i n c r e m e n t )
12
13
14 # F u n c t i o n t h a t r e t u r n s dx/ dt
15 def mydiff (x , t ) :
16 T = 5
17 a = −1/T

141
18
19 dxdt = a ∗ x
20
21 r e t u r n dxdt
22
23
24 # S o l v e ODE
25 x = o d e i n t ( m y d i f f , x0 , t )
26 print (x)
27
28
29 # Plot the Results
30 plt . plot (t , x)
31 p l t . t i t l e ( ’ P l o t t i n g D i f f e r e n t i a l Equation S o l u t i o n ’ )
32 plt . xlabel ( ’ t ’ )
33 plt . ylabel ( ’x( t ) ’ )
34 plt . grid ()
35 p l t . a x i s ( [ 0 , 25 , 0 , 1 ] )
36 p l t . show ( )
Listing 21.3: Using ODE Solver in Python

This gives the same plot as shown in Figure 21.1.

Some modification to the Python code:


1 im po rt numpy a s np
2 from s c i p y . i n t e g r a t e i mpo rt o d e i n t
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Initialization
6 tstart = 0
7 t s t o p = 25
8 increment = 1
9
10 T = 5
11 a = −1/T
12 x0 = 1
13 t = np . a r a n g e ( t s t a r t , t s t o p +1 , i n c r e m e n t )
14
15
16 # F u n c t i o n t h a t r e t u r n s dx/ dt
17 def mydiff (x , t , a ) :
18
19 dxdt = a ∗ x
20
21 r e t u r n dxdt
22
23
24 # S o l v e ODE
25 x = o d e i n t ( m y d i f f , x0 , t , a r g s =(a , ) )
26 print (x)
27
28
29 # Plot the Results
30 plt . plot (t , x)
31 p l t . t i t l e ( ’ P l o t t i n g D i f f e r e n t i a l Equation S o l u t i o n ’ )
32 plt . xlabel ( ’ t ’ )
33 plt . ylabel ( ’x( t ) ’ )
34 plt . grid ()
35 p l t . a x i s ( [ 0 , 25 , 0 , 1 ] )

142
36 p l t . show ( )
Listing 21.4: Using ODE Solver in Python

This gives the same plot as shown in Figure 21.1.

In the modified example we have the parameters used in the differential equation
(in htis case a) as an input argument. By doing this, it is very easy to changes
values for the parameters used in the differential equation without changing the
code for the differential equation.

You can also easily run multiple simulations like this:


1 a = −0.2
2 x = o d e i n t ( m y d i f f , x0 , t , a r g s =(a , ) )
3
4 a = −0.1
5 x = o d e i n t ( m y d i f f , x0 , t , a r g s =(a , ) )

[End of Example]

21.3 Solving Multiple 1. order Differential Equa-


tions
In real life we typically have higher order differential equations, or we have a
set of 1. order differential equations that describe a given system. How can we
solve such equations in Python?

Example 21.3.1. Set of 1.order Differential Equations


Given the differential equations:
dx
= −y (21.6)
dt
dy
=x (21.7)
dt

Assume the initial conditions x(0) = 1 and y(0) = 1.

The Python code is almost similar as previous examples, but we need to do


some small trick to make it work.

Python code:

143
1 im po rt numpy a s np
2 from s c i p y . i n t e g r a t e i mpo rt o d e i n t
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Initialization
6 t s t a r t = −1
7 tstop = 1
8 increment = 0.1
9
10 # I n i t i a l condition
11 z0 = [ 1 , 1 ]
12
13
14 t = np . a r a n g e ( t s t a r t , t s t o p +1 , i n c r e m e n t )
15
16
17 # F u n c t i o n t h a t r e t u r n s dx/ dt
18 def mydiff ( z , t ) :
19 dxdt = −z [ 1 ]
20 dydt = z [ 0 ]
21
22 dzdt = [ dxdt , dydt ]
23 r e t u r n dzdt
24
25
26 # S o l v e ODE
27 z = o d e i n t ( m y d i f f , z0 , t )
28 print ( z )
29
30 x = z [: ,0]
31 y = z [: ,1]
32
33
34 # Plot the Results
35 plt . plot (t , x)
36 plt . plot (t , y)
37 p l t . t i t l e ( ’ P l o t t i n g D i f f e r e n t i a l Equations So lut ion ’ )
38 plt . xlabel ( ’ t ’ )
39 plt . ylabel ( ’z( t ) ’ )
40 plt . grid ()
41 p l t . a x i s ( [ − 1 , 1 , −1.5 , 1 . 5 ] )
42 p l t . show ( )
Listing 21.5: xxx

This gives the the plot shown in Figure 21.2.

We can also rewrite the differential equations like this (to make it easier to
understand?):
dx1
= −x2 (21.8)
dt
dx2
= x1 (21.9)
dt

The Python code then becomes:

144
Figure 21.2: Figure Name

1 im po rt numpy a s np
2 from s c i p y . i n t e g r a t e i mpo rt o d e i n t
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Initialization
6 t s t a r t = −1
7 tstop = 1
8 increment = 0.1
9
10 # I n i t i a l condition
11 x init = [1 ,1]
12
13
14 t = np . a r a n g e ( t s t a r t , t s t o p +1 , i n c r e m e n t )
15
16
17 # F u n c t i o n t h a t r e t u r n s dx/ dt
18 def mydiff (x , t ) :
19 dx1dt = −x [ 1 ]
20 dx2dt = x [ 0 ]
21
22 dxdt = [ dx1dt , dx2dt ]
23 r e t u r n dxdt
24
25
26 # S o l v e ODE
27 x = odeint ( mydiff , x i n i t , t )
28 print (x)
29
30 x1 = x [ : , 0 ]

145
31 x2 = x [ : , 1 ]
32
33
34 # Plot the Results
35 p l t . p l o t ( t , x1 )
36 p l t . p l o t ( t , x2 )
37 p l t . t i t l e ( ’ P l o t t i n g D i f f e r e n t i a l Equations So lut ion ’ )
38 plt . xlabel ( ’ t ’ )
39 plt . ylabel ( ’x( t ) ’ )
40 plt . grid ()
41 p l t . a x i s ( [ − 1 , 1 , −1.5 , 1 . 5 ] )
42 p l t . show ( )
Listing 21.6: xxx

The plot and results will be the same.

[End of Example]

21.4 Solving Higher order Differential Equations


We shall use Python to solve and plot the results of the following differential
equation:

(1 + t2 )ẅ + 2tẇ + 3w = 2 (21.10)

Note! Don’t be confused that in this example w is used and not x or y. All
these are just parameters or variable names.

dw d2 w
Note! ẇ = dt and ẅ = dt2

We will solve the differential equation in the interval [0,5s].

We will use the following initial conditions: w(t0 ) = 0 and ẇ(t0 ) = 1

First, we should rewrite the equation in order to get the highest derivative alone
on the left side of the equation:

2 − 2tẇ − 3w
ẅ = (21.11)
1 + t2

Note! Higher order differential equations must be reformulated into a system of


first order differential equations.

We do the following ”trick”:

146
w = x1 (21.12)

ẇ = x2 (21.13)
This gives a set of 1.order differential equations:

ẋ1 = x2 (21.14)

2 − 2tx2 − 3x1
ẋ2 = (21.15)
1 + t2

Now we can relatively easy implement the system in Python.

Python code:

1 im po rt numpy a s np
2 from s c i p y . i n t e g r a t e i mpo rt o d e i n t
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Initialization
6 tstart = 0
7 tstop = 5
8 increment = 0.1
9
10 # I n i t i a l condition
11 x init = [0 ,1]
12
13
14 t = np . a r a n g e ( t s t a r t , t s t o p +1 , i n c r e m e n t )
15
16
17 # F u n c t i o n t h a t r e t u r n s dx/ dt
18 def mydiff (x , t ) :
19 dx1dt = x [ 1 ]
20 dx2dt = ( 2 − t ∗x [ 1 ] − 3∗ x [ 0 ] ) / ( 1 + t ∗ ∗ 2 )
21
22 dxdt = [ dx1dt , dx2dt ]
23 r e t u r n dxdt
24
25
26 # S o l v e ODE
27 x = odeint ( mydiff , x i n i t , t )
28 print (x)
29
30 x1 = x [ : , 0 ]
31 x2 = x [ : , 1 ]
32
33
34 # Plot the Results
35 p l t . p l o t ( t , x1 )
36 p l t . p l o t ( t , x2 )
37 p l t . t i t l e ( ’ P l o t t i n g D i f f e r e n t i a l Equations So lut ion ’ )
38 plt . xlabel ( ’ t ’ )
39 plt . ylabel ( ’x( t ) ’ )

147
40 plt . grid ()
41 p l t . a x i s ( [ 0 , 5 , −1, 2 ] )
42 p l t . show ( )
Listing 21.7: xxx

This gives the the plot shown in Figure 21.3.

Figure 21.3: 2.order Differential Equation

21.5 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 21.5.1. Bacteria Population


In this task we will simulate a simple model of a bacteria population in a jar.

The model is as follows:

birth rate = bx (21.16)

148
death rate = px2 (21.17)

Then the total rate of change of bacteria population is:

ẋ = bx − px2 (21.18)

dx
Note! ẋ is the same as dt

Set b=1/hour and p=0.5 bacteria-hour

We will simulate the number of bacteria in the jar after 1 hour, assuming that
initially there are 100 bacteria present.

Use one of the ODE solvers in Python as shown in earlier examples.

[End of Exercise]

Exercise 21.5.2. Differential Equation


Given the following differential equation:

ẋ = ax + b (21.19)
Where a = − T1 , where T is defined as the time constant of the system. We can
set b = 1.

dx
Note! ẋ is the same as dt

Plot the solution for this differential equation using Python.

In our system we can set T = 5 and the initial condition x0 = x(0) = 1


When you have done that you should try with different values for a and b. Make
sure to pass these values

[End of Exercise]

Exercise 21.5.3. Simulation of Dynamic System


Given the following differential equation:

ẋ = −ax + bu (21.20)
dx
Note! ẋ is the same as dt

We have the following:


• x - Process variable, e.g., Level, Pressure, Temperature, etc.

149
• u - Input variable, e.g., Control Signal from the Controller
• a, b - Constants

Start by setting a = 0.25, b = 2 and u = 1. Plot the simulation results. Use one
of the ODE solvers in Python.

Explore with other values for a, b and u.

[End of Exercise]

Exercise 21.5.4. Mass-Spring-Damper System


Given a ”Mass-Spring-Damper” system as shown in Figure 21.4.

Figure 21.4: Mass-Spring-Damper System

The system can be described by the following equation:

F (t) − cẋ(t) − kx(t) = mẍ(t) (21.21)

Where t is the simulation time, F(t) is an external force applied to the system, c
is the damping constant of the spring, k is the stiffness of the spring, m is a mass.

x(t) is the position of the object (m).

150
ẋ(t) is the first derivative of the position, which equals the velocity of the object
(m).

ẍ(t) is the second derivative of the position, which equals the acceleration of the
object (m).

Use your skills learned from the previous examples in order to simulate this
system. Use one of the ODE solvers in Python.

You should try with different values for F , c, k and m.

[End of Exercise]

Exercise 21.5.5. ODE


Use the one of the ODE solvers in Python to solve and plot the results of the
following differential equation in the interval [t0 , tf ]:

1
3w0 + w = cos(t) (21.22)
1 + t2
Where the initial conditions are t0 = 0, tf = 5, w(t0 ) = 1

Note! w0 is the same as ẇ which is the same as dw


dt - different notations for the
same.

[End of Exercise]

Exercise 21.5.6. Pendulum model


Use the one of the ODE solvers in Python to solve and plot the results of the
following differential equations:

ẋ1 = x2 (21.23)

g b
ẋ2 = − x1 − x2 (21.24)
r mr2

The differential equations above is a simplified model of a pendulum where m


is the mass, r is the length of the arm of the pendulum, g is the gravity, and b
is a friction coefficient.

In the model, x1 is the distance from the starting point (which is when the
pendulum hangs straight down) and x2 is the velocity.

151
Figure 21.5: Pendulum

A pendulum is shown in Figure 21.5.

You may use the following values: m = 8, r = 5 and b = 10 (units are not so
important in this case). You may also explore with other values as well.

Assume you, e.g., take the pendulum away from the starting point and then drop
it, what happens then? You may, e.g., use the initial conditions x1 (0) = 0.5 and
x2 (0) = 0.

Explain the simulation results and see it in relation with the real world. Does
the simulation results make sense?

[End of Exercise]

152
Chapter 22

Optimization

Optimization is important in mathematics, control and simulation applications.


Optimization is based on finding the minimum of a given criteria function.

Example 22.0.1. Basic Optimization

Given the following function:

f (x) = x2 + 2x + 1 (22.1)
We start by plotting the function:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3
4 xmin = −5
5 xmax = 5
6 dx = 0 . 1
7
8 N = i n t ( ( xmax − xmin ) /dx )
9
10 x = np . l i n s p a c e ( xmin , xmax , N+1)
11 #x = np . a r a n g e ( xmin , xmax+dx , dx ) #A l t e r n a t i v e i m p l e m e n t a t i o n
12
13 y = x ∗∗2 + 2∗ x + 1 ;
14
15
16 plt . plot (x , y)
17 p l t . x l i m ( [ xmin , xmax ] )
Listing 22.1: Optimization in Python

This gives the the plot shown in Figure 22.1.

We will use fminbound to find the minimum of the function.

Python code:
1 from s c i p y i mpo rt o p t i m i z e
2

153
Figure 22.1: Optimization Example

3 xmin = −5
4 xmax = 5
5
6
7 def y(x) :
8 r e t u r n x ∗∗2 + 2∗ x + 1
9
10
11 x min = o p t i m i z e . fminbound ( y , xmin , xmax )
12
13 p r i n t ( x min )
Listing 22.2: xxx

The result becomes:

xmin = -1.0

We see that this is the correct answer based on our plot in Figure 22.1.

[End of Example]

Exercise 22.0.1. Make your own Minimization function


Given the following function:

f (x) = x2 + 2x + 1 (22.2)

154
Implement a basic minimization function from scratch using either a For loop
or a While loop.

Make sure to test the function and see if you get the same answer as in the
example above.

[End of Exercise]

Other optimization functions in Python:

scipy.optimize.fmin

scipy.optimize.minimize scalar

scipy.optimize.minimize

Exercise 22.0.2. Optimization Functions in Python


Given the following function:

f (x) = x2 + 2x + 1 (22.3)

Test the different optimization (finding minimum) functions:

scipy.optimize.fmin

scipy.optimize.minimize scalar

scipy.optimize.minimize

Compare the results.

[End of Exercise]

Exercise 22.0.3. Optimization


Given the following function:

f (x) = x3 − 4x (22.4)

Test the different optimization (finding minimum) functions:

scipyoptimize.fminbound

155
scipy.optimize.fmin

scipy.optimize.minimize scalar

scipy.optimize.minimize

Compare the results. You should alos plot the function.

[End of Exercise]

Exercise 22.0.4. Minimum for function with 2 variables


Given the following function:

f (x, y) = 2(x − 1)2 + x − 2 + (y − 2)2 + y (22.5)

Plot the function and find the minimum.

[End of Exercise]

Exercise 22.0.5. Optimization - Rosenbrock’s Banana Function


Given the following function:

f (x, y) = (1 − x)2 + 100(y − x2 )2 (22.6)

This function is known as Rosenbrock’s banana function.

Plot the function and find the minimum.

[End of Exercise]

156
Part VI

Using Python for


Simulations

157
Chapter 23

Introduction to Simulations

Python is very powerful for performing simulations, like simulating dynamic


systems, i.e.., solving numerical differential equations.

See the next chapters for lots of practical examples.

158
Chapter 24

Differential Equations

24.1 Introduction to Differential Equations


A differential equation is a mathematical equation that relates some function
with its derivatives.

In applications, the functions usually represent physical quantities, the deriva-


tives represent their rates of change, and the differential equation defines a
relationship between the two.

Because such relations are extremely common, differential equations play a


prominent role in many disciplines including engineering, physics, economics,
and biology.

We typically want to solve ordinary differential equations (ODE) of the form:


dy
= f (t, y), y(t0 ) = y0 (24.1)
dt
dy
Note! Different notation is used: dt = y’= ẏ

This document will use these different notations interchangeably.

Example 24.1.1. Example of Dynamic Systems


Given the following differential equation:

ẋ = −ax + bu (24.2)
dx
Note! ẋ is the same as dt

We have the following:


• x - Process variable, e.g., Level, Pressure, Temperature, etc.
• u - Input variable, e.g., Control Signal from the Controller
• a, b - Constants

159
[End of Example]

With Python have we can solve these differential equations in many different
ways.

We can use so-called ODE solvers or we can make discrete version of the differ-
ential equations using discretization methods like Euler, etc.

With ODE solvers Python can solve these equations numerically. Higher order
differential equations must be reformulated into a system of first order differen-
tial equations.

In chapter 25 we will simulate (solve and plot the results) such differential
equations numerically using Euler discretization.

160
Chapter 25

Discrete Systems

When dealing with computers we need to deal with discrete systems.

25.1 Discretization
Sometimes we want to or need to discretize a continuous system and then sim-
ulate it in Python. When dealing with computer simulation, we need to create
a discrete version of our system. This means we need to make a discrete version
of our continuous differential equations. Interpolation, Curve Fitting, etc. is
also based on a set of discrete values (data points or measurements). The same
with Numerical Differentiation and Numerical Integration, etc.

Below we see a continuous signal vs the discrete signal for a given system with
discrete time interval T s = 0.1s.

Figure 25.1: Discretization

In order to discretize a continuous model there are lots of different methods to


use.
A simple discretization method is the Euler Forward method:

x(k + 1) − x(k)
ẋ = (25.1)
Ts

161
Ts is the Sampling Time

Figure 25.2: Euler Discretization methods

Lots of other discretization methods do exists, such as “Euler backward”, Zero


Order Hold (ZOH), Tustin’s method, etc.

Example 25.1.1. Simulation of Discrete System


Given the following differential equation:

ẋ = −ax + bu (25.2)
dx
Note! ẋ is the same as dt

We have the following:


• x - Process variable, e.g., Level, Pressure, Temperature, etc.
• u - Input variable, e.g., Control Signal from the Controller

• a, b - Constants
We start with finding the discrete differential equation.

We can use e.g., the Euler Approximation:

xk+1 − xk
ẋ ≈ (25.3)
Ts
Ts - Sampling Interval

Then we get:

xk+1 − xk
= −axk + buk (25.4)
Ts

162
This gives the following discrete differential equation:

xk+1 = (1 − Ts a)xk + Ts buk (25.5)

Now we are ready to simulate the system.

We set a=0.25, b=2 and u=1 (You can explore with other values on your own)
We start creating the Python Script for the simulation of this system:

1 # S i m u l a t i o n o f d i s c r e t e model
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s
6 a = 0.25
7 b = 2
8
9 # Simulation Parameters
10 Ts = 0 . 1
11 Tstop = 30
12 uk = 1 # Step Response
13 xk = 0
14 N = i n t ( Tstop /Ts ) # S i m u l a t i o n l e n g t h
15 data = [ ]
16 data . append ( xk )
17
18
19 # Simulation
20 f o r k i n r a n g e (N) :
21 xk1 = ( 1 − a ∗Ts ) ∗ xk + Ts ∗ b ∗ uk
22 xk = xk1
23 data . append ( xk1 )
24
25
26 # Plot the Simulation Results
27 t = np . a r a n g e ( 0 , Tstop+Ts , Ts )
28
29 p l t . p l o t ( t , data )
30
31 # Formatting t h e a p p e a r a n c e o f t h e P l o t
32 p l t . t i t l e ( ’ S i m u l a t i o n o f dxdt = −ax + bu ’ )
33 plt . xlabel ( ’ t [ s ] ’ )
34 plt . ylabel ( ’x ’ )
35 plt . grid ()
36 p l t . a x i s ( [ 0 , 30 , 0 , 8 ] )
37 p l t . show ( )
Listing 25.1: Simulation of Discrete Dynamic System in Python

The simulation gives the results as shown in Figure 25.3.

Lets also take a look at the Variable Explorer as shown in Figure 25.4.

163
Figure 25.3: Simulation of Discrete System using Python

Here is an alternative solution presented:

1 # S i m u l a t i o n o f d i s c r e t e model
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s
6 a = 0.25
7 b = 2
8
9 # Simulation Parameters
10 Ts = 0 . 1 # Sampling Time
11 Tstop = 30 # End o f S i m u l a t i o n Time
12 uk = 1 # Step Response
13 N = i n t ( Tstop /Ts ) # S i m u l a t i o n l e n g t h
14 x = np . z e r o s (N+2) # I n i t i a l i z a t i o n t h e x v e c t o r
15 x[0] = 0
16
17
18 # Simulation
19 f o r k i n r a n g e (N+1) :
20 x [ k +1] = ( 1 − a ∗Ts ) ∗ x [ k ] + Ts ∗ b ∗ uk
21
22
23 # Plot the Simulation Results
24 t = np . a r a n g e ( 0 , Tstop+2∗Ts , Ts ) #C r e a t e t h e Time S e r i e s
25
26 plt . plot (t , x)
27
28 # Formatting t h e a p p e a r a n c e o f t h e P l o t
29 p l t . t i t l e ( ’ S i m u l a t i o n o f dxdt = −ax + bu ’ )
30 plt . xlabel ( ’ t [ s ] ’ )
31 plt . ylabel ( ’x ’ )

164
Figure 25.4: Variable Explorer for Discrete Simulation Example

32 plt . grid ()
33 p l t . a x i s ( [ 0 , 30 , 0 , 8 ] )
34 p l t . show ( )
Listing 25.2: Simulation of Discrete Dynamic System in Python

This gives of course the same results and the same plot.

You should try both examples, and then decide which one you prefer. I guess
there are also many other ways to do it.

[End of Example]

25.2 Exercises
Below you find different self-paced Exercises that you should go through and
solve on your own. The only way to learn Python is to do lots of Exercises!

Exercise 25.2.1. Simulation of Bacteria Population


In this task we will simulate a simple model of a bacteria population in a jar.

The model is as follows:

birth rate = bx (25.6)

death rate = px2 (25.7)

Then the total rate of change of bacteria population is:

165
ẋ = bx − px2 (25.8)

Set b=1/hour and p=0.5 bacteria-hour

We will simulate the number of bacteria in the jar after 1 hour, assuming that
initially there are 100 bacteria present.

Find the discrete model using the Euler Forward method by hand and implement
and simulate the system in Python using a For Loop.

[End of Example]

Exercise 25.2.2. Simulation with 2 variables


Given the following system:

dx1
= −x2 (25.9)
dt
dx2
= x1 (25.10)
dt

Find the discrete system and simulate the discrete system in MATLAB. Solve
the equations, e.g., in the time span [-1 1] with initial values [1, 1].

[End of Exercise]

166
Chapter 26

Real-Time Simulations

26.1 Introduction
Typically in a simulation, you run the simulation in a for loop. When you are
finished with the simulation you plot the data.
We repeat a basic simulation example from chapter 25.

Example 26.1.1. Basic Simulation of Discrete System


We will simulate the discrete system given as follows:

xk+1 = (1 − Ts a)xk + Ts buk (26.1)

We set a=0.25, b=2 and u=1 (You can explore with other values on your own)
We start creating the Python Script for the simulation of this system:

1 # S i m u l a t i o n o f d i s c r e t e model
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s
6 a = 0.25
7 b = 2
8
9 # Simulation Parameters
10 Ts = 0 . 1
11 Tstop = 30
12 uk = 1 # Step Response
13 xk = 0
14 N = i n t ( Tstop /Ts ) # S i m u l a t i o n l e n g t h
15 data = [ ]
16 data . append ( xk )
17
18
19 # Simulation
20 f o r k i n r a n g e (N) :
21 xk1 = ( 1 − a ∗Ts ) ∗ xk + Ts ∗ b ∗ uk

167
22 xk = xk1
23 data . append ( xk1 )
24
25
26 # Plot the Simulation Results
27 t = np . a r a n g e ( 0 , Tstop+Ts , Ts )
28
29 p l t . p l o t ( t , data )
30
31 # Formatting t h e a p p e a r a n c e o f t h e P l o t
32 p l t . t i t l e ( ’ S i m u l a t i o n o f dxdt = −ax + bu ’ )
33 plt . xlabel ( ’ t [ s ] ’ )
34 plt . ylabel ( ’x ’ )
35 plt . grid ()
36 p l t . a x i s ( [ 0 , 30 , 0 , 8 ] )
37 p l t . show ( )
Listing 26.1: Simulation of Discrete Dynamic System in Python

The simulation gives the results as shown in Figure 26.1.

Figure 26.1: Simulation of Discrete System using Python

As you can see from the code and when running the code, the simulation results
are plotted all in one operation after the simulation is finished, i.e, after the for
loop.

[End of Example]

Sometimes we want to plot one value at the time inside the loop, so-called
”Real-Time simulations”. That is also the case if we want to plot data from a
sensor or a real process.

168
26.2 Introduction to Real-Time Plotting
You can also use the matplotlib for real-time plotting.

Example 26.2.1. Introduction to Real-Time Plotting


Here is a basic example:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3
4 p l t . a x i s ( [ 0 , 10 , 0 , 1 ] )
5
6 d e l a y = 1 #Se co n ds
7
8 f o r i in range (10) :
9 y = np . random . random ( )
10 plt . scatter ( i , y)
11 p l t . pause ( d e l a y )
12
13 p l t . show ( )
Listing 26.2: Real-Time Plotting in Python

We get the following plot as shown in Figure 27.1.

Figure 26.2: Real-Time Plotting with Python

You cannot see the the actual behavior of the plot by watching Figure 27.1, so

169
you need to run the Python program yourself.

If you run the code you see the plot is updated with a new value every second
as specified in the code.

[End of Example]

Note! If you use Anaconda and Spider, you typically need to change the the
settings for how graphics are are displayed in Spyder.

Select Preferences from the menu, then IPython console in the list of categories
on the left, then the tab Graphics at the top, and change the Graphics back-end
from Inline to e.g. Automatic or Qt. See Figure 27.2.

Figure 26.3: Change how Graphics are displayed in the Spyder Editor

Example 26.2.2. Real-Time Simulation of Discrete System


Lets change the discrete simulation example presented in the beginning of this
chapter:
1 # Real−Time S i m u l a t i o n o f D i s c r e t e System
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s
6 a = 0.25
7 b = 2
8
9 # Simulation Parameters
10 Ts = 0 . 1
11 Tstop = 30
12 uk = 1 # Step Response

170
13 xk = 0
14 N = i n t ( Tstop /Ts ) # S i m u l a t i o n l e n g t h
15 data = [ ]
16 data . append ( xk )
17
18 p l t . a x i s ( [ 0 , N, 0 , 1 0 ] )
19
20 f o r k i n r a n g e (N) :
21 xk1 = ( 1 − a ∗Ts ) ∗ xk + Ts ∗ b ∗ uk
22 xk = xk1
23 data . append ( xk1 )
24
25 p l t . s c a t t e r ( k , xk1 )
26 p l t . pause ( Ts )
27
28 p l t . show ( )
Listing 26.3: Real-Time Simulation of Discrete Dynamic System in Python

Figure 26.4 shows the Real-Time Plot for this example.

Figure 26.4: Real-Time Simulation of Discrete System

You cannot see the the actual behavior of the plot by watching Figure 26.4, so
you need to run the Python program yourself.

If you run the code you see the plot is updated with a new value every second
as specified in the code.

In this example we have locked the scaling using the axis method. If we remove
or comment out the line ”plt.axis([0, N, 0, 10])”, we get a plot the automatically
scales the x axis and the y axis. Whats best depends on if you know the
simulation length from the beginning and know the minimum and maximum
value of the simulation results.

171
Figure 26.5 shows the Real-Time Plot with Auto-scaling after 30 iterations for
this example. You cannot see the the actual behavior of the plot by watching
Figure 26.5, so you need to run the Python program yourself.

Figure 26.5: Real-Time Simulation of Discrete System with Auto-scaling

The examples shown shows the discrete time step k on the x-axis. Typically we
want to display the continuous time t instead. Some small adjustment to the
examples make this possible. See the code below:
1 # Real−Time S i m u l a t i o n o f D i s c r e t e System
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s
6 a = 0.25
7 b = 2
8
9 # Simulation Parameters
10 Ts = 0 . 1
11 Tstop = 30
12 uk = 1 # Step Response
13 xk = 0
14 y s c a l e m a x = 10
15 N = i n t ( Tstop /Ts ) # S i m u l a t i o n l e n g t h
16 data = [ ]
17 data . append ( xk )

172
18
19 p l t . a x i s ( [ 0 , N∗Ts , 0 , y s c a l e m a x ] )
20
21 f o r k i n r a n g e (N) :
22 xk1 = ( 1 − a ∗Ts ) ∗ xk + Ts ∗ b ∗ uk
23 xk = xk1
24 data . append ( xk1 )
25
26 t = Ts∗k
27
28 p l t . s c a t t e r ( t , xk1 )
29 p l t . pause ( Ts )
30
31 p l t . show ( )
Listing 26.4: Real-Time Simulation of Discrete Dynamic System in Python

Figure 26.6 shows the final plot for this example. You cannot see the the actual
behavior of the plot by watching Figure 26.6, so you need to run the Python
program yourself.

Figure 26.6: Real-Time Simulation of Discrete System

We can also add some formatting regarding the appearance of the plot (xlabel,
ylabel, title, etc.). The final code example is shown below.

173
1 # Real−Time S i m u l a t i o n o f D i s c r e t e System
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s
6 a = 0.25
7 b = 2
8
9 # Simulation Parameters
10 Ts = 0 . 1
11 Tstop = 30
12 uk = 1 # Step Response
13 xk = 0
14 y s c a l e m a x = 10
15 N = i n t ( Tstop /Ts ) # S i m u l a t i o n l e n g t h
16 data = [ ]
17 data . append ( xk )
18
19
20 # Formatting t h e a p p e a r a n c e o f t h e P l o t
21 p l t . t i t l e ( ’ S i m u l a t i o n o f dxdt = −ax + bu ’ )
22 plt . xlabel ( ’ t [ s ] ’ )
23 plt . ylabel ( ’x ’ )
24 plt . grid ()
25
26 p l t . a x i s ( [ 0 , N∗Ts , 0 , y s c a l e m a x ] )
27
28 f o r k i n r a n g e (N) :
29 xk1 = ( 1 − a ∗Ts ) ∗ xk + Ts ∗ b ∗ uk
30 xk = xk1
31 data . append ( xk1 )
32
33 t = Ts∗k
34
35 p l t . s c a t t e r ( t , xk1 )
36 p l t . pause ( Ts )
37
38 p l t . show ( )
Listing 26.5: Real-Time Simulation of Discrete Dynamic System in Python

Run the example and notice the difference.

[End of Example]

26.3 Real-Time Plotting with Animation


For more advanced Real-Time plots we should use the animation module in the
matplotlib library (matplotlib.animation).

To create a real-time plot, we need to use the animation module in matplotlib.


We set up the figure and axes in the usual way, but we draw directly to the
axes, ax, when we want to create a new frame in the animation.

We need to use the FuncAnimation function:

174
1 a n i = a n i m a t i o n . FuncAnimation ( f i g , animate , f a r g s =(xs , ys ) ,
i n t e r v a l =1000)

FuncAnimation is a special function within the animation module that lets us


automate updating the graph. We pass the FuncAnimation() a handle to the
figure we want to draw, fig, as well as the name of a function that should be
called at regular intervals. We called this function animate() and is defined just
above our FuncAnimation() call.

Still in the FuncAnimation() parameters, we set fargs, which are the arguments
we want to pass to our animate function (since we are not calling animate()
directly from within our own code). Then, we set interval, which is how long
we should wait between calls to animate() (in milliseconds).

Note: As an argument to FuncAnimation, notice that animate does not have


any parentheses. This is passing a reference to the function and not the result
of that function. If you accidentally add parentheses to animate here, animate
will be called immediately (only once), and you’ll likely get an error

Example 26.3.1. Real-Time Plotting with Animation


Below you find the Python Code for a basic example where we use the anima-
tion module in matplotlib.

In the example we update the plot every seconds by setting the interval=1000ms
as an input argument to the FuncAnaimation function.

1 im po rt d a t e t i m e a s dt
2 im po rt numpy a s np
3 im po rt matplotlib . pyplot as p l t
4 im po rt m at pl ot l ib . animation as animation
5
6 # Create f i g u r e f o r p l o t t i n g
7 fig = plt . figure ()
8 ax = f i g . a d d s u b p l o t ( 1 , 1 , 1 )
9 xs = [ ]
10 ys = [ ]
11
12
13 # This f u n c t i o n i s c a l l e d p e r i o d i c a l l y from FuncAnimation
14 d e f animate ( i , xs , ys ) :
15
16 temp c = round ( np . random . random ( ) , 2 )
17
18 # Add x and y t o l i s t s
19 xs . append ( dt . d a t e t i m e . now ( ) . s t r f t i m e ( ’%H:%M:%S.% f ’ ) )
20 ys . append ( temp c )
21
22 # L i m i t x and y l i s t s t o 20 i t e m s
23 xs = xs [ − 2 0 : ]
24 ys = ys [ − 2 0 : ]
25
26 # Draw x and y l i s t s
27 ax . c l e a r ( )
28 ax . p l o t ( xs , ys )

175
29
30 # Format p l o t
31 p l t . x t i c k s ( r o t a t i o n =45 , ha= ’ r i g h t ’ )
32 p l t . s u b p l o t s a d j u s t ( bottom =0 .3 0)
33 p l t . t i t l e ( ’ Temperature Data ’ )
34 p l t . y l a b e l ( ’ Temperature ( deg C) ’ )
35
36 # S e t up p l o t t o c a l l animate ( ) f u n c t i o n p e r i o d i c a l l y
37 a n i = a n i m a t i o n . FuncAnimation ( f i g , animate , f a r g s =(xs , ys ) ,
i n t e r v a l =1000)
38 p l t . show ( )
Listing 26.6: Real-Time Plotting with Animation

Figure 27.3 shows the final plot for this example. You cannot see the the actual
behavior of the plot by watching Figure 27.3, so you need to run the Python
program yourself.

Figure 26.7: Real-Time Plotting with Animation

[End of Example]

Example 26.3.2. Discrete Simulations with Animation

Lets apply this technique on our discrete system.

Python Code:

176
1 im po rt d a t e t i m e a s dt
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4 im po rt m a t p l o t l i b . a n i m a t i o n a s a n i m a t i o n
5 # Create f i g u r e f o r p l o t t i n g
6 fig = plt . figure ()
7 ax = f i g . a d d s u b p l o t ( 1 , 1 , 1 )
8 xs = [ ]
9 ys = [ ]
10
11 xk = 0 # I n i t i a l Value f o r x
12
13 # This f u n c t i o n i s c a l l e d p e r i o d i c a l l y from FuncAnimation
14 d e f s i m u l a t i o n ( i , xs , ys ) :
15
16 # Model P a r a m e t e r s
17 a = 0.25
18 b = 2
19
20 # Simulation Parameters
21 Ts = 0 . 1 # Sampling Time
22 uk = 1 # Step Response
23 g l o b a l xk # S i n c e we change x i n s i d e a f u n c t i o n we d e f i n e i t a s
a gobal v a r i a b l e
24
25 # Model o f D i s c r e t e System
26 xk1 = ( 1 − a ∗Ts ) ∗ xk + Ts ∗ b ∗ uk
27 xk = xk1
28
29 # Add x and y t o l i s t s
30 xs . append ( dt . d a t e t i m e . now ( ) . s t r f t i m e ( ’%H:%M:%S.% f ’ ) )
31 ys . append ( xk1 )
32
33 N = 60 # L i m i t x and y l i s t s t o N i t e m s
34 xs = xs [−N : ]
35 ys = ys [−N : ]
36
37 # Draw x and y l i s t s
38 ax . c l e a r ( )
39 ax . p l o t ( xs , ys )
40
41 # Format p l o t
42 p l t . x t i c k s ( r o t a t i o n =45 , ha= ’ r i g h t ’ )
43 p l t . s u b p l o t s a d j u s t ( bottom =0 .3 0)
44 p l t . t i t l e ( ’ S i m u l a t i o n o f dxdt = −ax + bu ’ )
45 plt . xlabel ( ’ t [ s ] ’ )
46 plt . ylabel ( ’x ’ )
47 plt . grid ()
48
49 # S e t up p l o t t o c a l l animate ( ) f u n c t i o n p e r i o d i c a l l y
50 a n i = a n i m a t i o n . FuncAnimation ( f i g , s i m u l a t i o n , f a r g s =(xs , ys ) ,
i n t e r v a l =100)
51 p l t . show ( )
Listing 26.7: Real-Time Simulation of Discrete Dynamic System with Animation

Figure 26.8 shows the final plot for this example. You cannot see the the actual
behavior of the plot by watching Figure 26.8, so you need to run the Python
program yourself.

177
Figure 26.8: Real-Time Simulation of Discrete Dynamic System with Animation

[End of Example]

26.3.1 Speeding Up the Plot Animation


Clearing a graph and redrawing everything can be a time-consuming process in
terms of computer time. To remedy that, we are going to use a trick known as
blitting.

Blitting is an old computer graphics technique where several graphical bitmaps


are combined into one. This way, only one needed to be updated at a time,
saving the computer from having to redraw the whole scene every time.
Matplotlib allows us to enable blitting in FuncAnimation, but it means we need
to re-write how some of the animate() function works. To reap the true benefits
of blitting, we need to set a static background, which means the axes can’t scale
and we can’t show moving timestamps anymore. This means that you have to
take the good with the bad. So you have to choose whats most important for
you in your simulations.

Example 26.3.3. Real-Time Plotting with Animation with improved Perfor-


mance

Python Code:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt m a t p l o t l i b . a n i m a t i o n a s a n i m a t i o n
4
5 # P ar a m e t e r s
6 x l e n = 200 # Number o f p o i n t s t o d i s p l a y

178
7 y range = [0 , 20] # Range o f p o s s i b l e Y v a l u e s t o d i s p l a y
8
9 # Create f i g u r e f o r p l o t t i n g
10 fig = plt . figure ()
11 ax = f i g . a d d s u b p l o t ( 1 , 1 , 1 )
12 xs = l i s t ( r a n g e ( 0 , 2 0 0 ) )
13 ys = [ 0 ] ∗ x l e n
14 ax . s e t y l i m ( y r a n g e )
15
16
17 # C r e a t e a b l a n k l i n e . We w i l l update t h e l i n e i n animate
18 l i n e , = ax . p l o t ( xs , ys )
19
20 # Add l a b e l s
21 p l t . t i t l e ( ’ Temperature Data ’ )
22 p l t . x l a b e l ( ’ Samples ’ )
23 p l t . y l a b e l ( ’ Temperature ( deg C) ’ )
24
25 # This f u n c t i o n i s c a l l e d p e r i o d i c a l l y from FuncAnimation
26 d e f animate ( i , ys ) :
27
28 r a n d v a l = np . random . random ( ) ∗20 #G e n e r a t e Random V a l u e s
between 0 and 20
29
30 temp c = round ( r a n d v a l , 2 )
31
32 #p r i n t ( temp c )
33
34 # Add y t o l i s t
35 ys . append ( temp c )
36
37 # L i m i t y l i s t t o s e t number o f i t e m s
38 ys = ys [− x l e n : ]
39
40 # Update l i n e with new Y v a l u e s
41 l i n e . s e t y d a t a ( ys )
42
43 return line ,
44
45 # S e t up p l o t t o c a l l animate ( ) f u n c t i o n p e r i o d i c a l l y
46 a n i = a n i m a t i o n . FuncAnimation ( f i g ,
47 animate ,
48 f a r g s =(ys , ) ,
49 i n t e r v a l =100 ,
50 b l i t=True )
51 p l t . show ( )
Listing 26.8: Real-Time Plotting with Animation

Figure 27.4 shows the final plot for this example. You cannot see the the actual
behavior of the plot by watching Figure 27.4, so you need to run the Python
program yourself.

[End of Example]

Example 26.3.4. Discrete Simulations with Animation with improved Perfor-


mance

179
Figure 26.9: Real-Time Plotting with Animation

Python Code:
1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt m a t p l o t l i b . a n i m a t i o n a s a n i m a t i o n
4
5 # P ar a m e t e r s
6 N = 200
7 x len = N # Number o f p o i n t s t o d i s p l a y
8 y r a n g e = [ 0 , 1 0 ] # Range o f p o s s i b l e Y v a l u e s t o d i s p l a y
9
10 # Create f i g u r e f o r p l o t t i n g
11 fig = plt . figure ()
12 ax = f i g . a d d s u b p l o t ( 1 , 1 , 1 )
13 xs = l i s t ( r a n g e ( 0 , N) )
14 ys = [ 0 ] ∗ x l e n
15 ax . s e t y l i m ( y r a n g e )
16
17 xk = 0 # I n i t i a l Value f o r x
18
19 # C r e a t e a b l a n k l i n e . We w i l l update t h e l i n e i n animate
20 l i n e , = ax . p l o t ( xs , ys )
21
22 # Add l a b e l s
23 p l t . t i t l e ( ’ S i m u l a t i o n o f dxdt = −ax + bu ’ )
24 p l t . x l a b e l ( ’ Samples ’ )
25 plt . ylabel ( ’x ’ )
26
27 # This f u n c t i o n i s c a l l e d p e r i o d i c a l l y from FuncAnimation
28 d e f s i m u l a t i o n ( i , ys ) :
29
30 # Model P a r a m e t e r s
31 a = 0.25
32 b = 2
33
34 # Simulation Parameters
35 Ts = 0 . 1 # Sampling Time
36 uk = 1 # Step Response

180
37 g l o b a l xk # S i n c e we change x i n s i d e a f u n c t i o n we d e f i n e i t a s
a gobal v a r i a b l e
38
39 # Model o f D i s c r e t e System
40 xk1 = ( 1 − a ∗Ts ) ∗ xk + Ts ∗ b ∗ uk
41 xk = xk1
42
43 # Add y t o l i s t
44 ys . append ( xk1 )
45
46 # L i m i t y l i s t t o s e t number o f i t e m s
47 ys = ys [− x l e n : ]
48
49 # Update l i n e with new Y v a l u e s
50 l i n e . s e t y d a t a ( ys )
51
52 return line ,
53
54 # S e t up p l o t t o c a l l animate ( ) f u n c t i o n p e r i o d i c a l l y
55 a n i = a n i m a t i o n . FuncAnimation ( f i g ,
56 simulation ,
57 f a r g s =(ys , ) ,
58 i n t e r v a l =100 ,
59 b l i t=True )
60 p l t . show ( )
Listing 26.9: Real-Time Simulation of Discrete Dynamic System with Animation
and increased Speed

Figure 26.10 shows the final plot for this example. You cannot see the the actual
behavior of the plot by watching Figure 26.10, so you need to run the Python
program yourself.

[End of Example]

For more information about Matplotlib:animations:

https://scipy-cookbook.readthedocs.io/items/MatplotlibA nimations.html

Other resources:

https://learn.sparkfun.com/tutorials/graph-sensor-data-with-python-and-matplotlib/allplot-
sensor-data

https://stackoverflow.com/questions/11874767/how-do-i-plot-in-real-time-in-a-while-
loop-using-matplotlib

181
Figure 26.10: Real-Time Plotting with Animation

182
Part VII

Data Acquisition (DAQ)


with Python

183
Chapter 27

Plotting Sensor Data

27.1 Introduction
Typically we want to plot the data from the sensor. We can plot save the data
in an array and then plot the data at the end of the program, but more likely
we want to plot one value at the time inside the loop, so-called ”Real-Time
plotting”.

In this chapter we only show how you can plot the data from any given sensor
using this general approach. Instead of the actual sensor data we just use the
random generator in Python.

To read the actual sensor data you typically need a DAQ (Data Acquisition)
device connected to you PC or, e.g, a Raspberry Pi device. In all cases you
will typically need to install a driver from the vendor of the DAQ device or the
sensor you are using.

27.2 Introduction to Real-Time Plotting


You can also use the matplotlib for real-time plotting.

Example 27.2.1. Introduction to Real-Time Plotting


Here is a basic example:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3
4 p l t . a x i s ( [ 0 , 10 , 0 , 1 ] )
5
6 d e l a y = 1 #Se co n ds
7
8 f o r i in range (10) :
9 y = np . random . random ( )
10 plt . scatter ( i , y)
11 p l t . pause ( d e l a y )

184
12
13 p l t . show ( )
Listing 27.1: Real-Time Plotting in Python

We get the following plot as shown in Figure 27.1.

Figure 27.1: Real-Time Plotting with Python

You cannot see the the actual behavior of the plot by watching Figure 27.1, so
you need to run the Python program yourself.

If you run the code you see the plot is updated with a new value every second
as specified in the code.

[End of Example]

Note! If you use Anaconda and Spyder, you typically need to change the the
settings for how graphics are are displayed in Spyder.

Select Preferences from the menu, then IPython console in the list of categories
on the left, then the tab Graphics at the top, and change the Graphics back-end
from Inline to e.g. Automatic or Qt. See Figure 27.2.

185
Figure 27.2: Change how Graphics are displayed in the Spyder Editor

27.3 Real-Time Plotting with Animation


For more advanced Real-Time plots we should use the animation module in the
matplotlib library (matplotlib.animation).

To create a real-time plot, we need to use the animation module in matplotlib.


We set up the figure and axes in the usual way, but we draw directly to the
axes, ax, when we want to create a new frame in the animation.

We need to use the FuncAnimation function:


1 a n i = a n i m a t i o n . FuncAnimation ( f i g , animate , f a r g s =(xs , ys ) ,
i n t e r v a l =1000)

FuncAnimation is a special function within the animation module that lets us


automate updating the graph. We pass the FuncAnimation() a handle to the
figure we want to draw, fig, as well as the name of a function that should be
called at regular intervals. We called this function animate() and is defined just
above our FuncAnimation() call.

Still in the FuncAnimation() parameters, we set fargs, which are the arguments
we want to pass to our animate function (since we are not calling animate()
directly from within our own code). Then, we set interval, which is how long
we should wait between calls to animate() (in milliseconds).

Note: As an argument to FuncAnimation, notice that animate does not have


any parentheses. This is passing a reference to the function and not the result
of that function. If you accidentally add parentheses to animate here, animate
will be called immediately (only once), and you’ll likely get an error

186
Example 27.3.1. Real-Time Plotting with Animation
Below you find the Python Code for a basic example where we use the anima-
tion module in matplotlib.

In the example we update the plot every seconds by setting the interval=1000ms
as an input argument to the FuncAnaimation function.

1 im po rt d a t e t i m e a s dt
2 im po rt numpy a s np
3 im po rt matplotlib . pyplot as p l t
4 im po rt m at pl ot l ib . animation as animation
5
6 # Create f i g u r e f o r p l o t t i n g
7 fig = plt . figure ()
8 ax = f i g . a d d s u b p l o t ( 1 , 1 , 1 )
9 xs = [ ]
10 ys = [ ]
11
12
13 # This f u n c t i o n i s c a l l e d p e r i o d i c a l l y from FuncAnimation
14 d e f animate ( i , xs , ys ) :
15
16 temp c = round ( np . random . random ( ) , 2 )
17
18 # Add x and y t o l i s t s
19 xs . append ( dt . d a t e t i m e . now ( ) . s t r f t i m e ( ’%H:%M:%S.% f ’ ) )
20 ys . append ( temp c )
21
22 # L i m i t x and y l i s t s t o 20 i t e m s
23 xs = xs [ − 2 0 : ]
24 ys = ys [ − 2 0 : ]
25
26 # Draw x and y l i s t s
27 ax . c l e a r ( )
28 ax . p l o t ( xs , ys )
29
30 # Format p l o t
31 p l t . x t i c k s ( r o t a t i o n =45 , ha= ’ r i g h t ’ )
32 p l t . s u b p l o t s a d j u s t ( bottom =0 .3 0)
33 p l t . t i t l e ( ’ Temperature Data ’ )
34 p l t . y l a b e l ( ’ Temperature ( deg C) ’ )
35
36 # S e t up p l o t t o c a l l animate ( ) f u n c t i o n p e r i o d i c a l l y
37 a n i = a n i m a t i o n . FuncAnimation ( f i g , animate , f a r g s =(xs , ys ) ,
i n t e r v a l =1000)
38 p l t . show ( )
Listing 27.2: Real-Time Plotting with Animation

Figure 27.3 shows the final plot for this example. You cannot see the the actual
behavior of the plot by watching Figure 27.3, so you need to run the Python
program yourself.

[End of Example]

187
Figure 27.3: Real-Time Plotting with Animation

27.3.1 Speeding Up the Plot Animation


Clearing a graph and redrawing everything can be a time-consuming process in
terms of computer time. To remedy that, we are going to use a trick known as
blitting.

Blitting is an old computer graphics technique where several graphical bitmaps


are combined into one. This way, only one needed to be updated at a time,
saving the computer from having to redraw the whole scene every time.
Matplotlib allows us to enable blitting in FuncAnimation, but it means we need
to re-write how some of the animate() function works. To reap the true benefits
of blitting, we need to set a static background, which means the axes can’t scale
and we can’t show moving timestamps anymore. This means that you have to
take the good with the bad. So you have to choose whats most important for
you un your simulations.

Example 27.3.2. Real-Time Plotting with Animation with improved Perfor-


mance
Python Code:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt m a t p l o t l i b . a n i m a t i o n a s a n i m a t i o n
4
5 # P ar a m e t e r s
6 x l e n = 200 # Number o f p o i n t s t o d i s p l a y
7 y range = [0 , 20] # Range o f p o s s i b l e Y v a l u e s t o d i s p l a y
8

188
9 # Create f i g u r e f o r p l o t t i n g
10 fig = plt . figure ()
11 ax = f i g . a d d s u b p l o t ( 1 , 1 , 1 )
12 xs = l i s t ( r a n g e ( 0 , 2 0 0 ) )
13 ys = [ 0 ] ∗ x l e n
14 ax . s e t y l i m ( y r a n g e )
15
16
17 # C r e a t e a b l a n k l i n e . We w i l l update t h e l i n e i n animate
18 l i n e , = ax . p l o t ( xs , ys )
19
20 # Add l a b e l s
21 p l t . t i t l e ( ’ Temperature Data ’ )
22 p l t . x l a b e l ( ’ Samples ’ )
23 p l t . y l a b e l ( ’ Temperature ( deg C) ’ )
24
25 # This f u n c t i o n i s c a l l e d p e r i o d i c a l l y from FuncAnimation
26 d e f animate ( i , ys ) :
27
28 r a n d v a l = np . random . random ( ) ∗20 #G e n e r a t e Random V a l u e s
between 0 and 20
29
30 temp c = round ( r a n d v a l , 2 )
31
32 #p r i n t ( temp c )
33
34 # Add y t o l i s t
35 ys . append ( temp c )
36
37 # L i m i t y l i s t t o s e t number o f i t e m s
38 ys = ys [− x l e n : ]
39
40 # Update l i n e with new Y v a l u e s
41 l i n e . s e t y d a t a ( ys )
42
43 return line ,
44
45 # S e t up p l o t t o c a l l animate ( ) f u n c t i o n p e r i o d i c a l l y
46 a n i = a n i m a t i o n . FuncAnimation ( f i g ,
47 animate ,
48 f a r g s =(ys , ) ,
49 i n t e r v a l =100 ,
50 b l i t=True )
51 p l t . show ( )
Listing 27.3: Real-Time Plotting with Animation

Figure 27.4 shows the final plot for this example. You cannot see the the actual
behavior of the plot by watching Figure 27.4, so you need to run the Python
program yourself.

[End of Example]

For more information about Matplotlib:animations:

https://scipy-cookbook.readthedocs.io/items/MatplotlibA nimations.html

189
Figure 27.4: Real-Time Plotting with Animation

Other resources:

https://learn.sparkfun.com/tutorials/graph-sensor-data-with-python-and-matplotlib/allplot-
sensor-data

https://stackoverflow.com/questions/11874767/how-do-i-plot-in-real-time-in-a-while-
loop-using-matplotlib

190
Chapter 28

Data Acquisition (DAQ)


with Python

Python is probably best suited for ”ad-hoc” numerical calculations, analysis,


simulations, etc., but can be used for many other purposes, even if other pro-
gramming languages are better suited.

28.1 Introduction to DAQ


To read sensor data you typically need a DAQ (Data Acquisition) device con-
nected to you PC or, e.g, a Raspberry Pi device. In all cases you will typically
need to install a driver from the vendor of the DAQ device or the sensor you
are using.

A DAQ System consists of 4 parts:


• Physical input/output signals, sensors
• DAQ device/hardware
• Driver software
• Your software application (Application software) - in this case your Python
application
Figure 28.1 shows the different steps involved in a DAQ system.

Here you find more information, resources, videos and examples regarding DAQ:
https://www.halvorsen.blog/documents/technology/daq/

28.2 Data Acquisition using NI DAQ Devices


Here we will show how we can use Python to retrieve data from the physical
world using a DAQ device or an I/O module.

191
Figure 28.1: Data Acquisition (DAQ) System

We will use a DAQ device from National Instruments (NI).

Web:
http://www.ni.com/

DAQ hardware: WE will use a NI-USB-600x DAQ device from National Instru-
ments, such as:

• NI-USB-6001
• NI-USB-6008
• NI-USB-6009

They are almost identical and the prices is not so bad either.

USB-6008:
http://www.ni.com/en-no/support/model.usb-6008.html

Figure 28.2 shows the USB-6008 DAQ device from NI.

Streaming Data from NI Data Acquisition (DAQmx) Devices into Python:


https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z000000P8o0SAC

We assume we want to do the following: - We have a USB DAQ system from


National Instruments - We want to stream data from my hardware into Python
to do data processing - We would like to log data to a file on the hard disk

The best way to do this is to use the NI-DAQmx Python API provided by Na-
tional Instruments (nidaqmx). The NI-DAQmx Python API is hosted on
GitHub.

The nidaqmx Python package is a wrapper around the NI-DAQmx C API us-
ing the ctypes Python library, and only supports the Windows operating system.

192
Figure 28.2: USB-6008

A Python API for interacting with NI-DAQmx (GitHub):


https://github.com/ni/nidaqmx-python

For more information about NI DAQ:


ni.com/daq

For more information about Python Resources for NI Hardware and Software:
ni.com/python

Another option is to use the PyDAQmx Python package.


This package allows users to use data acquisition hardware from National In-
strument with python. It makes an interface between the NIDAQmx driver
and python. It currently works only on Windows. The package is not an open
source driver from NI acquisition hardware. You first need to install the driver
provided by NI.

Web:
https://pypi.org/project/PyDAQmx/

https://pythonhosted.org/PyDAQmx/

28.2.1 NI-DAQmx
NI-DAQmx is the software you use to communicate with and control your NI
data acquisition (DAQ) device.

NI-DAQmx supports only the Windows operating system.

You can download NI-DAQmx from this location:

193
https://www.ni.com/download

28.2.2 Measurement Automation Explorer (MAX)


Measurement Automation Explorer (MAX) is a software you can use it to
configure and test the DAQ device before you use it in Python (or other pro-
gramming languages).

MAX is included with NI-DAQmx software.

Figure 28.3 shows Measurement Automation Explorer (MAX).

Figure 28.3: Measurement Automation Explorer(MAX)

With MAX you can make sure your DAQ device works as expected before you
start using it in your Python program. You can use the Test Panels to test your
analog and digital inputs and outputs channels.

You can also change name of the unit, which you need to use in your Python
code.

28.3 NI-DAQmx Python API


In this section we will use the NI-DAQmx Python API provided by National
Instruments (nidaqmx). The NI-DAQmx Python API is hosted on GitHub.

194
The nidaqmx Python package is a wrapper around the NI-DAQmx C API us-
ing the ctypes Python library, and only supports the Windows operating system.

A Python API for interacting with NI-DAQmx (GitHub):


https://github.com/ni/nidaqmx-python

Other resources:
Control NI DAQ Device with Python and NI DAQmx:
https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019Pf1SAE

Below 4 basic examples will be provided:

• Analog Write using NI DAQ Device


• Analog Read using NI DAQ Device
• Digital Write using NI DAQ Device
• Digital Read using NI DAQ Device

You can easily extend this examples to make them suit your needs. Typically
you need to include a while loop where you write and/or read from the DAQ
device inside the loop, e.g. read values from one or more sensors that are con-
nected to the DAQ device, you may want to create a control system reading
the process value and then later write the calculated control signal (e.g. using
a PID controller) back to the DAQ device and the process.

28.3.1 Analog Write


Example 28.3.1. Analog Write using NI DAQ Device
Python code:
1 im po rt nidaqmx
2
3 with nidaqmx . Task ( ) a s t a s k :
4 t a s k . a o c h a n n e l s . a d d a o v o l t a g e c h a n ( ’ Dev1/ ao0 ’ , ’ mychannel ’
,0 ,5)
5
6 value = 3
7 task . s t a r t
8 task . write ( value )
9 task . stop
Listing 28.1: Analog Write using NI DAQ Device

Note! The USB-6008 can only output a voltage signal between 0 and 5V.

[End of Example]

195
28.3.2 Analog Read
Example 28.3.2. Analog Read using NI DAQ Device
Python code:
1 im po rt nidaqmx
2
3 with nidaqmx . Task ( ) a s t a s k :
4 t a s k . a i c h a n n e l s . a d d a i v o l t a g e c h a n ( ”Dev1/ a i 1 ” )
5
6 value = task . read ( )
7 print ( value )
8 task . stop
Listing 28.2: Analog Read using NI DAQ Device

[End of Example]

Example 28.3.3. Analog Read with RSE


Python code:
1 im po rt nidaqmx
2
3 from nidaqmx . c o n s t a n t s im por t (
4 TerminalConfiguration )
5
6 with nidaqmx . Task ( ) a s t a s k :
7 t a s k . a i c h a n n e l s . a d d a i v o l t a g e c h a n ( ”Dev1/ a i 0 ” ,
t e r m i n a l c o n f i g=T e r m i n a l C o n f i g u r a t i o n . RSE)
8
9 value = task . read ( )
10 print ( value )
11 task . stop
Listing 28.3: Analog Read with RSE

[End of Example]

Example 28.3.4. Analog Read with Differential


Python code:
1 im po rt nidaqmx
2
3 from nidaqmx . c o n s t a n t s im por t (
4 TerminalConfiguration )
5
6 with nidaqmx . Task ( ) a s t a s k :
7 t a s k . a i c h a n n e l s . a d d a i v o l t a g e c h a n ( ”Dev1/ a i 0 ” ,
t e r m i n a l c o n f i g=T e r m i n a l C o n f i g u r a t i o n . DIFFERENTIAL)
8
9 value = task . read ( )
10 print ( value )
11 task . stop
Listing 28.4: Analog Read with Differential

[End of Example]

196
28.3.3 Digital Write
Example 28.3.5. Digital Write using NI DAQ Device

Python code:
1 im po rt nidaqmx
2
3 with nidaqmx . Task ( ) a s t a s k :
4 t a s k . d o c h a n n e l s . a d d d o c h a n ( ”Dev1/ p o r t 0 / l i n e 0 ” )
5
6 v a l u e = True
7 task . s t a r t
8 task . write ( value )
9 task . stop
Listing 28.5: Digital Write using NI DAQ Device

[End of Example]

28.3.4 Digital Read


Example 28.3.6. Digital Read using NI DAQ Device
Python code:
1 im po rt nidaqmx
2
3 with nidaqmx . Task ( ) a s t a s k :
4 t a s k . d i c h a n n e l s . a d d d i c h a n ( ”Dev1/ p o r t 0 / l i n e 0 ” )
5
6 task . s t a r t
7 value = task . read ( )
8 print ( value )
9 task . stop
Listing 28.6: Digital Read using NI DAQ Device

[End of Example]

You should use the ”nidaqmx.stream readers” and nidaqmx.stream writers classes
to increase the performance of your application, which accept pre-allocated
NumPy arrays.

https://nidaqmx-python.readthedocs.io/en/latest/stream readers.html#module-
nidaqmx.stream readers

https://nidaqmx-python.readthedocs.io/en/latest/stream writers.html#module-
nidaqmx.stream writers

197
28.4 Controlling LEDs
In this section we will see how we can control a LED from Python.

We will need the following equipment:


• PC with Python
• DAQ device
• Breadboard
• LED
• Resistor (e.g., 270ohm)
• Wires for connecting the components and create the circuit
Figure 28.4 shows an overview of LEDs.

Figure 28.4: LED Overview

A breadboard is used to wire electric components together. Figure 28.5 shows


how you should wire a LED using a Breadboard.

Figure 28.6 shows how you wire the LED and connect it to the DAQ device.

Python code for turning on the LED


Example 28.4.1. Controlling a LED from Python
Basic Python code:
1 im po rt nidaqmx
2
3 with nidaqmx . Task ( ) a s t a s k :
4 t a s k . d o c h a n n e l s . a d d d o c h a n ( ”Dev1/ p o r t 0 / l i n e 0 ” )
5
6 v a l u e = True
7 task . s t a r t
8 task . write ( value )
9 task . stop
Listing 28.7: Turn on a LED using Python

198
Figure 28.5: How to wire a LED on a Breadboard

Figure 28.6: Wire the LED and connect to the DAQ device

In this basic example we just turn on the LED.

Below you see an example where we turn the LED on and off inside a loop.

Python code:
1 im po rt nidaqmx
2 im po rt time
3
4
5 with nidaqmx . Task ( ) a s t a s k :
6 t a s k . d o c h a n n e l s . a d d d o c h a n ( ”Dev1/ p o r t 0 / l i n e 0 ” )
7
8 v a l u e = True
9 task . s t a r t
10
11 i = 1
12 while i < 10:
13

199
14 task . write ( value )
15 time . s l e e p ( 1 )
16 v a l u e = not v a l u e
17 task . write ( value )
18 i = i +1
19
20 task . stop
Listing 28.8: Controlling a LED using Python

[End of Example]

28.5 Read Data from Temperature Sensors


In this section we will use Python to read values from a temperature sensor.
We will also use Python to plot real-time data from the sensor.

28.5.1 Read Data from TMP36 Temperature Sensor


TMP36 is a small, low-cost temperature sensor and cost about $1 (you can buy
it “everywhere”).

We will need the following equipment:

• PC with Python
• DAQ device
• Breadboard

• TMP36 Temerature Sensor


• Wires for connecting the components and create the circuit

Figure 28.7 shows the TMP36 sensor.

We connect the TMP36 to LabVIEW using a USB DAQ Device from National
Instruments, e.g., USB-6001, USB-6008 or similar. I have used a breadboard
for the wiring.
Figure 28.8 show how we can wire the TMP36 together with the USB-6008 DAQ
device.

Figure 28.9 shows the TMP3x Datasheet.

We need to convert form Voltage (V) to degrees Celsius.

From the Datasheet (Figure 28.9) we have:

(x1 , y1 ) = (0.75V, 25◦ ) (28.1)

200
Figure 28.7: TMP36 Temperature Sensor

(x2 , y2 ) = (1V, 50◦ ) (28.2)

From the Datasheet (Figure 28.9) we see that there is a linear relationship
between Voltage and degrees Celsius (28.3):

y = ax + b (28.3)
We can find a and b using the following known formula (28.4):
y2 − y1
y − y1 = (x − x1 ) (28.4)
x2 − x1
By putting (28.1) and (28.2) into (28.4) we get:
50 − 25
y − 25 = (x − 0.75) (28.5)
1 − 0.75
Then we get the following formula we can implement in our Python program:

y = 100x − 50 (28.6)

Example 28.5.1. Read TMP36 Temperature Data

Python code:

201
Figure 28.8: TMP36 tmp36 Wiring

1 im po rt nidaqmx
2 im po rt time
3
4 from nidaqmx . c o n s t a n t s im por t (
5 TerminalConfiguration )
6
7
8 with nidaqmx . Task ( ) a s t a s k :
9 t a s k . a i c h a n n e l s . a d d a i v o l t a g e c h a n ( ”Dev1/ a i 0 ” ,
t e r m i n a l c o n f i g=T e r m i n a l C o n f i g u r a t i o n . RSE)
10
11 i = 0
12 while i < 10:
13
14 v o l t a ge = task . read ( )
15
16 d e g r e e s C = 100∗ v o l t a g e − 50
17
18 p r i n t ( ” Sample : ” , i )
19 p r i n t ( ” V o l t a g e Value : ” , round ( v o l t a g e , 2 ) )
20 p r i n t ( ” C e l s i u s Value : ” , round ( degreesC , 1 ) )
21 p r i n t ( ” \n” )
22 time . s l e e p ( 1 )
23 i = i +1
24
25 task . stop
Listing 28.9: Read TMP36 Temperature Data

In the example an ordinary while loop in combination with the sleep() function
have been used to read one new value each second.

[End of Example]

Example 28.5.2. Real-Time Plotting of Temperature Data

202
Figure 28.9: TMP3x Datasheet

In this example we will plot the data from the sensor using the Real-time plot-
ting examples shown in Chapter 27.

We want to present the value from the sensor in degrees Celsius:

1. Read Signal from DAQ Device (0-5V)


2. Convert to degrees Celsius using information from the Datasheet
3. Show/Plot Values from the Sensor

The Python code becomes as follows:


1 im po rt nidaqmx
2 im po rt time
3 im po rt d a t e t i m e a s dt
4 im po rt numpy a s np
5 im po rt matplotlib . pyplot as p l t
6 im po rt m at pl ot l ib . animation as animation
7
8 from nidaqmx . c o n s t a n t s im por t (
9 TerminalConfiguration )
10
11
12 # Create f i g u r e f o r p l o t t i n g
13 fig = plt . figure ()
14 ax = f i g . a d d s u b p l o t ( 1 , 1 , 1 )
15 xs = [ ]
16 ys = [ ]
17
18 # I n i t i a l i z e DAQ d e v i c e
19 t a s k = nidaqmx . Task ( )
20 t a s k . a i c h a n n e l s . a d d a i v o l t a g e c h a n ( ”Dev1/ a i 0 ” , t e r m i n a l c o n f i g=
T e r m i n a l C o n f i g u r a t i o n . RSE)

203
21 task . s t a r t
22
23
24 # This f u n c t i o n i s c a l l e d p e r i o d i c a l l y from FuncAnimation
25 d e f readdaq ( i , xs , ys ) :
26
27 #Read Value from DAQ d e v i c e
28 v o l t a g e = task . read ( )
29
30 #Convert V o l t a g e t o d e g r e e s C e l s i u s
31 d e g r e e s C = 100∗ v o l t a g e − 50
32 temp c = round ( degreesC , 1 )
33 p r i n t ( ” C e l s i u s Value : ” , temp c )
34
35 # Add x and y t o l i s t s
36 xs . append ( dt . d a t e t i m e . now ( ) . s t r f t i m e ( ’%H:%M:%S.% f ’ ) )
37 ys . append ( temp c )
38
39 # L i m i t x and y l i s t s t o 20 i t e m s
40 xs = xs [ − 2 0 : ]
41 ys = ys [ − 2 0 : ]
42
43 # Draw x and y l i s t s
44 ax . c l e a r ( )
45 ax . p l o t ( xs , ys )
46
47 # Format p l o t
48 p l t . x t i c k s ( r o t a t i o n =45 , ha= ’ r i g h t ’ )
49 p l t . s u b p l o t s a d j u s t ( bottom =0 .3 0)
50 p l t . t i t l e ( ’ Temperature Data ’ )
51 p l t . y l a b e l ( ’ Temperature ( deg C) ’ )
52
53
54 # S e t up p l o t t o c a l l readdaq ( ) f u n c t i o n p e r i o d i c a l l y
55 a n i = a n i m a t i o n . FuncAnimation ( f i g , readdaq , f a r g s =(xs , ys ) ,
i n t e r v a l =1000)
56 p l t . show ( )
57 task . stop
Listing 28.10: Real-Time Plotting of Temperature Data

[End of Example]

28.5.2 Read Data from Thermistor


A Thermistor is an electronic component that changes resistance to tempera-
ture, a so-called Resistance Temperature Detectors (RTD). It is often used as a
temperature sensor.

Example 28.5.3. Read Thermistor Temperature Data


We will need the following equipment:

• PC with Python
• DAQ device

204
• Breadboard
• 10kohm Thermistor

• 10kohm Resistor
• Wires for connecting the components and create the circuit

Our Thermistor is a so-called NTC (Negative Temperature Coefficient). In a


NTC Thermistor, resistance decreases as the temperature rises.

There is an non-linear relationship between resistance and excitement. To find


the temperature we can use the following equation (Steinhart-Hart equation):

1
= A + B ln(R) + C(ln(R))3 (28.7)
TK
where A, B and C are constants with the following values: A = 0.001129148, B =
0.000234125, C = 8.76741E − 08

TK is the temperature in Kelvin.

We want to solve the equation regarding the Temperature:


1
TK = (28.8)
A + B ln(R) + C(ln(R))3

The Temperature in degrees Celsius will then be:

TC = TK − 273.15 (28.9)

Wiring...
Figure 28.10 shows how we wire the components together.

Figure 28.10: Thermistor Wiring

205
Figure 28.11: Voltage Divider

The wiring is called a ”Voltage divider”.

Figure 28.11 shows a general Voltage Divider.

A general Voltage Divider has the following equation:


R2
Vout = Vin (28.10)
R1 + R2
The Voltage Divider for our system becomes as shown in Figure 28.12.

Figure 28.12: Voltage Divider for our System

We then have the following equation:


Rt
Vout = Vin (28.11)
R0 + Rt

206
where Rt is our 10kohm Thermistor and R0 is an ordinary 10kohm Resistor.

Vin in our case will be +5V which we get from the USB-6008 DAQ device as
shown in the wiring diagram.

Vout is the voltage we read using the DAQ device.

Since we need to find the Resistance Rt , which is used in the Steinhart-Hart


equation, we reformulate the formula:
Vout R0
Rt = (28.12)
Vin − Vout
We har no ready to start making the Python program for this example.

The program include the following necessary steps:

1. We wire the circuit on the Breadboard and connect it to the DAQ device
2. We measure Vout using the DAQ
3. We calculate Rt using the Voltage Divider equation
Vout R0
Rt = Vin −Vout

4. We use Steinhart-Hart equation for finding the Temperature


1
TK = A+B ln(Rt )+C(ln(Rt ))3

5. Finally we convert to degrees Celsius


TC = TK − 273.15

The Python code then becomes:


1 im po rt nidaqmx
2 im po rt numpy a s np
3 im po rt time
4
5 from nidaqmx . c o n s t a n t s im por t (
6 TerminalConfiguration )
7
8
9 Vin = 5
10 Ro = 10000 # %10k R e s i s t o r
11
12
13 with nidaqmx . Task ( ) a s t a s k :
14 t a s k . a i c h a n n e l s . a d d a i v o l t a g e c h a n ( ”Dev1/ a i 0 ” ,
t e r m i n a l c o n f i g=T e r m i n a l C o n f i g u r a t i o n . RSE)
15
16 i = 0
17 while i < 10:
18
19 Vout = t a s k . r e a d ( )
20
21 Rt = ( Vout∗Ro ) / ( Vin−Vout ) # V o l t a g e D i v i d e r Equation
22 # Rt =10000; Used f o r T e s t i n g . S e t t i n g Rt=10k s h o u l d g i v e
TempC=25
23

207
24 # Steinhart constants
25 A = 0.001129148
26 B = 0.000234125
27 C = 0.0000000876741
28
29 # S t e i n h a r t −Hart Equation
30 TempK = 1 / (A + (B ∗ np . l o g ( Rt ) ) + (C ∗ pow ( np . l o g ( Rt ) , 3 ) )
)
31
32 # Convert from K e l v i n t o C e l s i u s
33 TempC = TempK − 2 7 3 . 1 5
34
35 p r i n t ( ” Sample : ” , i )
36 p r i n t ( ” V o l t a g e Value : ” , round ( Vout , 2 ) )
37 p r i n t ( ” C e l s i u s Value : ” , round (TempC, 1 ) )
38 p r i n t ( ” \n” )
39 time . s l e e p ( 1 )
40 i = i +1
41
42 task . stop
Listing 28.11: Read Thermistor Temperature Data

[End of Example]

Example 28.5.4. Real-Time Plotting of Thermistor Temperature Data


Python code:
1 See p r e v i o u s examples
Listing 28.12: Real-Time Plotting of Thermistor Temperature Data

[End of Example]

28.5.3 Read Data NI TC-01 Thermocouple Device


In this chapter several examples have been shown using a DAQ device combined
with different sensors and components.

Here some examples will be shown using a preset temperature sensor from Na-
tional Instruments called NI USB-TC01. This is a USB based temperature
without need for any kind of wiring, you just plug it in and make your Python
program. Since the NI USB-TC01 is compatible with NI-DAQmx, you can pro-
gram it in the same way as other DAQ devices from NI.

Figure 28.13 shows the TC-01 Thermocouple Device from NI.

Example 28.5.5. Real-Time Plotting of Thermistor Temperature Data


Python code:

208
Figure 28.13: TC-01 Thermocouple Device

1 im po rt nidaqmx
2
3 t a s k = nidaqmx . Task ( )
4
5 t a s k . a i c h a n n e l s . a d d a i t h r m c p l c h a n ( ”TC01/ a i 0 ” )
6
7 task . s t a r t ()
8
9 value = task . read ( )
10 p r i n t ( round ( v a l u e , 1 ) )
11
12 task . stop ()
13 task . c l o s e ()
Listing 28.13: TC-01 Thermocouple Python Example

This is just a basic example, which you can easily extend using a while loop or
using some kind of plotting, etc..

[End of Example]

28.6 Data Logging


Python has several functions for creating, reading, updating, and deleting files.

209
Part VIII

Control Systems

210
Chapter 29

Python used for Control


Applications

The Python Control Systems Library (python-control) is a Python package that


implements basic operations for analysis and design of feedback control systems.

29.1 Python Control Systems Library


The python-control package is a set of python classes and functions that imple-
ment common operations for the analysis and design of feedback control systems.

The python-control package makes use of NumPy and SciPy.

A MATLAB compatibility package (control.matlab) is available that provides


many of the common functions corresponding to commands available in the
MATLAB Control Systems Toolbox.

Python Control Systems Library Homepage:

https://pypi.org/project/control

Python Control Systems Library Documentation:

https://python-control.readthedocs.io

The python-control package may be installed using pip:

pip i n s t a l l c o n t r o l

29.1.1 Python Control Systems Library Functions


Here are some of the most used functions in the Python Control Systems Library:

211
Functions for Model Creation and Manipulation:

• tf() - Create a transfer function system


• ss() - Create a state space system
• c2d() - Return a discrete-time system
• tf2ss() - Transform a transfer function to a state space system
• ss2tf() - Transform a state space system to a transfer function.
• series() - Return the series of 2 or more subsystems
• parallel() - Return the parallel of 2 or more subsystems
• feedback() - Return the feedback of system
• pade() - Creates a Pade Aproxomation, which is a Transfer function rep-
resentation of a Time Delay

Functions for Model Simulations:

• step response() - Step response of a linear system


• lsim() - Simulate the output of a linear system

Functions for Stability Analysis:

• step response() - Step response of a linear system


• lsim() - Simulate the output of a linear system
• pole() - Compute system poles
• zero() - Compute system zeros
• pzmap() - Plot a pole/zero map for a linear system
• margin() - Calculate gain and phase margins and associated crossover
frequencies
• stability margins() - Calculate stability margins and associated crossover
frequencies

Functions for Frequency Response:

• bode plot() - Create a Bode plot for a system


• mag2db() - Convert a magnitude to decibels (dB)
• db2mag() - Convert a gain in decibels (dB) to a magnitude

There are lots of other functions. They are documented in the Python Control
Systems Library Documentation:

https://python-control.readthedocs.io

212
29.1.2 MATLAB compatibility module
The control.matlab module contains a number of functions that emulate some
of the functionality of MATLAB. The intent of these functions is to provide
a simple interface to the python control systems library (python-control) for
people who are familiar with the MATLAB Control Systems Toolbox.

Example 29.1.1. MATLAB compatibility module Example


In this example we will use the MATLAB compatibility module for simulating
and showing the frequency response for a 2.order mass-spring-damper system.

1 im po rt m a t p l o t l i b . p y p l o t a s p l t # MATLAB p l o t t i n g f u n c t i o n s
2 from c o n t r o l . matlab im po rt ∗ # MATLAB−l i k e f u n c t i o n s
3
4 # P ar a m e t e r s d e f i n i n g t h e system
5 m = 250.0 # system mass
6 k = 40.0 # spring constant
7 b = 60.0 # damping c o n s t a n t
8
9 # System m a t r i c e s
10 A = [ [ 0 , 1 . ] , [−k/m, −b/m ] ]
11 B = [ [ 0 ] , [ 1 /m ] ]
12 C = [[1. , 0]]
13 s y s = s s (A, B, C, 0 )
14
15 # Step r e s p o n s e f o r t h e system
16 plt . figure (1)
17 yout , T = s t e p ( s y s )
18 p l t . p l o t (T . T, yout . T)
19
20 # Bode p l o t f o r t h e system
21 plt . figure (2)
22 mag , phase , om = bode ( s y s )
Listing 29.1: MATLAB compatibility module Example

As you see, the syntax is very similar to MATLAB.


The code gives the the results shown in Figure 29.1.

[End of Example]

29.2 Control Systems


Figure 29.2 shows a basic control system. Typically you use a PID controller to
control the processes.

The following topics will be discussed in this part:

• Transfer Functions
• State-space Models

213
Figure 29.1: MATLAB compatibility module Example

Figure 29.2: Control System

• PID Control
• Frequency Response

29.2.1 Transfer functions


Transfer functions are a model form based on the Laplace transform. Transfer
functions are very useful in analysis and design of linear dynamic systems.

A general transfer function is on the form:

y(s)
H(s) = (29.1)
u(s)

Example 29.2.1. Basic Transfer Function in Python


Lets define the following transfer function in Python:
3
H(s) = (29.2)
4s + 1
The Python code becomes:

214
1 im po rt numpy a s np
2 im po rt c o n t r o l
3
4 num = np . a r r a y ( [ 3 ] )
5 den = np . a r r a y ( [ 4 , 1 ] )
6
7 H = c o n t r o l . t f (num , den )
8
9 p r i n t ( ’H( s ) = ’ , H)
Listing 29.2: Basic Transfer Function in Python

[End of Example]

29.2.2 State-space Models


A state-space model is a structured form or representation of a set of differen-
tial equations. State-space models are very useful in Control theory and design.
The differential equations are converted in matrices and vectors.

A general linear State-space model can then be written on this compact form:

ẋ = Ax + Bu (29.3)
y = Cx + Du (29.4)

Where A, B, C and D are matrices.

Example 29.2.2. State Space Model


Given the following system):

x˙1 = x2 (29.5)

2x˙1 = −2x1 − 6x2 + 4u1 + 8u2 (29.6)

y = 5x1 + 6x2 + 7u1 (29.7)

This gives the following state space model:

       
x˙1 0 1 x1 0 0 u1
= + (29.8)
x˙2 −1 −3 x2 2 4 u2

215
   
  x1   u1
y= 5 6 7 0 (29.9)
x2 u2

We can define this state space model in Python:

1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2 im po rt c o n t r o l
3
4 # System m a t r i c e s
5 A = [ [ 0 , 1 ] , [ −1 , − 3 ] ]
6 B = [[0 , 0] , [2 , 4]]
7 C = [[5 , 6]]
8 D = [[7 , 0]]
9 s s m o d e l = c o n t r o l . s s (A, B, C, D)
Listing 29.3: State space model

[End of Example]

29.3 PID Control


Figure 29.3 shows a basic control system. Typically you use a PID controller to
control the processes.

Figure 29.3: Control System using PID Controller

The PID Algorithm (29.10):

Z t
Kp
u(t) = Kp e + edτ + Kp Td ė (29.10)
Ti 0

216
Where u is the controller output and e is the control error:

e(t) = r(t) − y(t) (29.11)

Where
r is the Reference Signal or Set-point
y is the Process value, i.e., the Measured value

PID Tuning Parameters:


Kp Proportional Gain
Ti Integral Time [sec]
Td Derivative Time [sec]

The transfer function for the PID controller is (we use Laplace on 29.10):

u(s) Kp
Hpid (s) = = Kp + + Kp Td s (29.12)
e(s) Ti s

Or like this:

u(s) Kp (Td Ti s2 + Ti s + 1)
Hpid (s) = = (29.13)
e(s) Ti s

29.3.1 PI Control
Very often we only need to use the PI Algorithm (29.14):

Z t
Kp
u(t) = Kp e + edτ (29.14)
Ti 0

The transfer function for the PI controller is:

u(s) Kp (Ti s + 1)
Hpi (s) = = (29.15)
e(s) Ti s

217
Chapter 30

Transfer Functions

Transfer functions are a model form based on the Laplace transform. Transfer
functions are very useful in analysis and design of linear dynamic systems.

A general transfer function is on the form:

y(s)
H(s) = (30.1)
u(s)
Where y is the output and u is the input. A general transfer function can be
written on the following general form:

numerator(s)
H(s) = (30.2)
denominator(s)
or to be more specific:

bm sm + bm−1 sm−1 + ... + b1 s + b0


H(s) = (30.3)
an sn + an−1 sn−1 + ... + a1 s + a0
The numerators of transfer function models describe the locations of the zeros
of the system, while the denominators of transfer function models describe the
locations of the poles of the system.

Below we will learn more about some important special cases of this general
form, namely the 1.order transfer function and the 2.order transfer function.

We will use the Python Control Systems Library. The Python Control Systems
Library (python-control) is a Python package that implements basic operations
for analysis and design of feedback control systems.

Python Control Systems Library Documentation:

https://python-control.readthedocs.io

Example 30.0.1. Create Transfer Function in Python

218
Given the following transfer function:

3
H(s) = (30.4)
4s + 1
The Python code becomes:
1 im po rt numpy a s np
2 im po rt c o n t r o l
3
4 num = np . a r r a y ( [ 3 ] )
5 den = np . a r r a y ( [ 4 , 1 ] )
6
7 H = c o n t r o l . t f (num , den )
8
9 p r i n t ( ’H( s ) = ’ , H)
Listing 30.1: Create Transfer Function in Python

[End of Example]

Example 30.0.2. Create more advanced Transfer Function in Python


Given the following transfer function:

3s + 3
H(s) = (30.5)
4s2 + 5s + 6
The Python code becomes:
1 im po rt numpy a s np
2 im po rt c o n t r o l
3
4 num = np . a r r a y ( [ 3 , 2 ] )
5 den = np . a r r a y ( [ 4 , 5 , 6 ] )
6
7 H = c o n t r o l . t f (num , den )
8
9 p r i n t ( ’H( s ) = ’ , H)
Listing 30.2: Create Transfer Function in Python

[End of Example]

30.1 1.order Transfer Functions


A 1.order transfer function is given by:

x(s) K
H(s) = = (30.6)
u(s) Ts + 1

219
Where K is the Gain and T is the Time constant. u is the input signal, while x
is the output signal.

In the time domain we get the following differential equation (using Inverse
Laplace):

1
ẋ = (−x + Ku) (30.7)
T

dx
Note that ẋ is the same as dt .

We can draw the following block diagram of the system as shown in Figure 30.1.

Figure 30.1: Block Diagram 1.order System

Poles: A 1.order system has a pole p = − T1 . In Python you may use the pole()
function in order to find the poles. You can also use the pzmap() function.

Step Response: In Python you may use the step response() function in order
to find the step response.

Example 30.1.1. 1.order Transfer Function Python Example


Given the following transfer function:

3
H(s) = (30.8)
4s + 1
The Python code becomes:
1 im po rt numpy a s np
2 im po rt c o n t r o l
3
4 K = 3
5 T = 4
6
7 num = np . a r r a y ( [K] )
8 den = np . a r r a y ( [ T , 1 ] )

220
9
10 H = c o n t r o l . t f (num , den )
11
12 p r i n t ( ’H( s ) = ’ , H)
Listing 30.3: Basic Transfer Function in Python

[End of Example]

30.1.1 Step Response


A step response of such a transfer function has the characteristics as seen in
Figure 30.2.

Figure 30.2: 1.order Process

Given the 1.order transfer function:

y(s) K
H(s) = = (30.9)
u(s) Ts + 1
Here we will start by finding the mathematical expression for the step response
(y(t)):

The Laplace Transformation pair for a unit step is as follows:

1
⇔1 (30.10)
s

221
Or more general:

U
⇔U (30.11)
s

Where U is the step size.

The step response of an integrator then becomes:

y(s) = H(s)u(s) (30.12)

Where

U
u(s) = (30.13)
s

This gives:

U K U
y(s) = H(s) = (30.14)
s Ts + 1 s

We use the following Laplace Transformation pair in order to find y(t):


k
⇔ k(1 − e−t/T ) (30.15)
(T s + 1)s

This gives the following:

y(t) = KU (1 − e−t/T ) (30.16)

In Python we can use the step response function from the Python Control Sys-
tems Library.

Python code:
1 >>> t , y = s t e p r e s p o n s e ( s y s , t , x0 )

222
Where sys is either a transfer function or a state-space model, x0 is the start/ini-
tial condition and t is the time vector. Note that t is auto-computed if not given.

Example 30.1.2. Basic Step Response Example


Given the following transfer function:

3
H(s) = (30.17)
4s + 1

The Python code for plotting the step response:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt c o n t r o l
4
5 K = 3
6 T = 4
7
8 num = np . a r r a y ( [K] )
9 den = np . a r r a y ( [ T , 1 ] )
10
11 H = c o n t r o l . t f (num , den )
12 p r i n t ( ’H( s ) = ’ , H)
13
14 t , y = c o n t r o l . s t e p r e s p o n s e (H)
15 plt . plot (t , y)
Listing 30.4: Basic Step Response Example

This gives the the plot shown in Figure 30.3.

You should of course use different functions for creating grid, axis, title, xlabel,
ylabel, etc. These are exemplified in other chapters.
[End of Example]

Example 30.1.3. An Alternative Definition of the Transfer Function


Given the same transfer function:

3
H(s) = (30.18)
4s + 1

Here we will use the TransferFunction class to define the Laplace operator s
as a constant. This can then be used to create variables that allow algebraic
creation of transfer functions

The Python code for defining the transfer function in an alternative way and
plotting the step response:

223
Figure 30.3: Step Response Example

1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2 im po rt c o n t r o l
3
4 s = control . TransferFunction . s
5
6 H = (3) /(4∗ s + 1)
7
8 p r i n t ( ’H( s ) = ’ , H)
9
10 t , y = c o n t r o l . s t e p r e s p o n s e (H)
11
12 plt . plot (t , y)
Listing 30.5: Alternative definition of the Transfer Function

This gives the the same plot shown in Figure 30.3.

[End of Example]

30.2 1.order Transfer Functions with Time De-


lay
A 1.order transfer function with time-delay may be written as:

224
K
H(s) = e−τ s (30.19)
Ts + 1

In the time domain we get the following differential equation (using Inverse
Laplace):

1
ẋ = (−x + Ku(t − τ )) (30.20)
T

We can draw the following block diagram of the system (30.4).

Figure 30.4: Block Diagram 1.order with Time Delay

Step response for a 1.order process with time delay, see Figure 30.5.

Figure 30.5: 1.order Process with Time delay

Example 30.2.1. 1.order Transfer Function with Time Delay using Pade Ap-
proximation

225
Given the following transfer function with time delay:

3
H(s) = e−2s (30.21)
4s + 1

The Python code for plotting the step response:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt c o n t r o l
4
5 K = 3
6 T = 4
7
8 num = np . a r r a y ( [K] )
9 den = np . a r r a y ( [ T , 1 ] )
10
11 H1 = c o n t r o l . t f (num , den )
12 p r i n t ( ’H1( s ) = ’ , H1)
13
14
15 Tau = 2
16 N = 10 # Order o f t h e Approximation
17
18
19 [ num pade , den pade ] = c o n t r o l . pade ( Tau ,N)
20 Hpade = c o n t r o l . t f ( num pade , den pade )
21 p r i n t ( ’ Hpade ( s ) = ’ , Hpade )
22
23 H = c o n t r o l . s e r i e s (H1 , Hpade )
24 p r i n t ( ’H( s ) = ’ , H)
25
26 t , y = c o n t r o l . s t e p r e s p o n s e (H)
27
28 plt . plot (t , y)
Listing 30.6: Basic Step Response Example

This gives the the plot shown in Figure 30.6.

You should try to change the variable N in the code, which is the order of the
Pade approximation.

You should of course also use different functions for creating grid, axis, title,
xlabel, ylabel, etc. These are exemplified in other chapters.

[End of Example]

30.3 Integrator Transfer Functions


Given the following (30.22):

226
Figure 30.6: Step Response 1.order with Time Delay using Pade

y(s) K
H(s) = = (30.22)
u(s) s

Poles: The Integrator has a pole in origo: p=0. In Python you may use the
pole() function in order to find the poles. You can also use the pzmap() func-
tion.

Step Response: In Python you may use the step response() function in order
to find the step response.

Here we will start by finding the mathematical expression for the step response
(y(t)):

The Laplace Transformation pair for a step is as follows:

1
⇔1 (30.23)
s

The step response of an integrator then becomes:

227
KU 1
y(s) = H(s)u(s) = = KU 2 (30.24)
s s s

We use the following Laplace Transformation pair in order to find y(t):


1
⇔t (30.25)
s2

Then we get the following:

y(t) = KU t (30.26)

We see that the step response of the integrator is a Ramp.

Conclusion: A bigger K will give a bigger slope (In Norwegian: “stigningstall”)


and the integration will go faster. Simulations in Python will also show this.

Example 30.3.1. Integrator


Given the following (30.27):

y(s) K
H(s) = = (30.27)
u(s) s

We will find poles, perform step response, etc. in Python for this system.

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt c o n t r o l
4
5 K = 3
6 num = np . a r r a y ( [K] )
7 den = np . a r r a y ( [ 1 , 0 ] )
8 H = c o n t r o l . t f (num , den )
9 p r i n t ( ’H( s ) = ’ , H)
10
11 p = c o n t r o l . p o l e (H)
12
13
14 t , y = c o n t r o l . s t e p r e s p o n s e (H)
15 plt . plot (t , y)
16 p l t . t i t l e ( ’ Step Response ’ )
17 plt . xlabel ( ’ t [ s ] ’ )
18 plt . ylabel ( ’y( t ) ’ )
19 plt . grid ()
20 plt . axis ([0 , 7 , 0 , 22])
21 p l t . show ( )

228
22
23 c o n t r o l . pzmap (H)
24 p l t . show ( )
Listing 30.7: Integrator

In the simulation we have set K = 3. You should explore with other values as
well and observe the results.

Instead of using the step response() function, we can implement and plot (30.26):

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3
4 #Model P a r a m e t e r s
5 K = 3
6
7 # Simulation Parameters
8 U = 1 # A s t e p i n t h e c o n t r o l s i g n a l a t t=0
9 t = 0
10
11 tstart = 0
12 t s t o p = 10
13
14 increment = 1
15
16 y = []
17 y = np . z e r o s ( t s t o p )
18
19 t = np . a r a n g e ( t s t a r t , t s t o p , i n c r e m e n t )
20
21
22 # Define the Function
23 f o r k in range ( tstop ) :
24 y[k] = K ∗ U ∗ t [k]
25
26
27 # Plot the Simulation Results
28 plt . plot (t , y)
29 p l t . t i t l e ( ’ Step Response ’ )
30 plt . xlabel ( ’ t [ s ] ’ )
31 plt . ylabel ( ’y( t ) ’ )
32 p l t . show ( )
Listing 30.8: Integrator from scratch

This should give the same results.

[End of Example]

30.4 2.order Transfer Functions


Given the following (30.31):

229
K
H(s) = (30.28)
as2 + bs + c

We can also define a 2.order transer function like this (30.29):

K
H(s) = (30.29)
( ωs0 )2 + 2ζ ωs0 + 1

Where
K is the gain
ζ is the relative damping factor
ω0 [rad/s] is the undamped resonance frequency

2.order system - special case:

K
H(s) = (30.30)
(T1 s + 1)(T2 s + 1)

We see that this system can be considered as two 1.order systems in series:

K 1 K
H(s) = = (30.31)
(T1 s + 1) (T2 s + 1) (T1 s + 1)(T2 s + 1)

30.5 Block Diagrams


Python Control Systems Library has functions for manipulating block diagrams
and transfer functions.

30.5.1 Serial Block Diagrams


Serial:

Figure 30.7 shows a serial block diagram.

Example 30.5.1. Serial Block Diagram


Given the following transfer functions:

3
H(s) = (30.32)
4s + 1

230
Figure 30.7: Serial Block Diagram

5
H(s) = (30.33)
2s + 1

Python code:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt c o n t r o l
4
5 K = 3
6 T = 4
7 num = np . a r r a y ( [K] )
8 den = np . a r r a y ( [ T , 1 ] )
9 H1 = c o n t r o l . t f (num , den )
10
11 K = 5
12 T = 2
13 num = np . a r r a y ( [K] )
14 den = np . a r r a y ( [ T , 1 ] )
15 H2 = c o n t r o l . t f (num , den )
16
17 H = c o n t r o l . s e r i e s (H1 , H2)
18
19 p r i n t ( ’H( s ) = ’ , H)
20
21 t , y = c o n t r o l . s t e p r e s p o n s e (H)
22 plt . plot (t , y)
Listing 30.9: Serial Block Diagram Example

[End of Example]

30.5.2 Parallel Block Diagrams


Parallel:

Figure 30.7 shows a parallel block diagram.

Example 30.5.2. Parallel Block Diagram

231
Figure 30.8: Parallel Block Diagram

Given the following transfer functions:

3
H(s) = (30.34)
4s + 1

5
H(s) = (30.35)
2s + 1

Python code:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt c o n t r o l
4
5 K = 3
6 T = 4
7 num = np . a r r a y ( [K] )
8 den = np . a r r a y ( [ T , 1 ] )
9 H1 = c o n t r o l . t f (num , den )
10
11 K = 5
12 T = 2
13 num = np . a r r a y ( [K] )
14 den = np . a r r a y ( [ T , 1 ] )
15 H2 = c o n t r o l . t f (num , den )
16
17 H = c o n t r o l . p a r a l l e l (H1 , H2)
18
19 p r i n t ( ’H( s ) = ’ , H)
20
21 t , y = c o n t r o l . s t e p r e s p o n s e (H)
22 plt . plot (t , y)
Listing 30.10: Parallel Block Diagram Example

[End of Example]

232
30.5.3 Feedback Block Diagrams
Feedback:

Figure 30.7 shows a feedback block diagram.

Figure 30.9: Feedback Block Diagram

Example 30.5.3. Feedback Block Diagram


Given the following transfer functions:

3
H(s) = (30.36)
4s + 1

5
H(s) = (30.37)
2s + 1

Python code:

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt c o n t r o l
4
5 K = 3
6 T = 4
7 num = np . a r r a y ( [K] )
8 den = np . a r r a y ( [ T , 1 ] )
9 H1 = c o n t r o l . t f (num , den )
10
11 K = 5
12 T = 2
13 num = np . a r r a y ( [K] )
14 den = np . a r r a y ( [ T , 1 ] )
15 H2 = c o n t r o l . t f (num , den )
16
17 H = c o n t r o l . f e e d b a c k (H1 , H2)
18
19 p r i n t ( ’H( s ) = ’ , H)
20
21 t , y = c o n t r o l . s t e p r e s p o n s e (H)

233
22 plt . plot (t , y)
Listing 30.11: Feedback Block Diagram Example

[End of Example]

234
Chapter 31

State Space Models

31.1 Introduction
A state-space model is a structured form or representation of a set of differen-
tial equations. State-space models are very useful in Control theory and design.
The differential equations are converted in matrices and vectors.

Assume we have the following linear equations:

ẋ1 = a1 1x1 + a2 1x2 + +an 1xn + b1 1u1 + b2 1u2 + +bn 1un

...
ẋn = a1 mx1 + a2 mx2 + +an mxn + b1 mu1 + b2 mu2 + +bn 1un

These equations can be formatted and be put on a standard form. A general


linear State-space model can then be written on this compact form:

ẋ = Ax + Bu (31.1)
y = Cx + Du (31.2)

Where A, B, C and D are matrices.

dx
Note that ẋ is the same as dt .

Example 31.1.1. State Space Model


Given the following system):

x˙1 = x2 (31.3)

235
2x˙1 = −2x1 − 6x2 + 4u1 + 8u2 (31.4)

y = 5x1 + 6x2 + 7u1 (31.5)

This gives the following state space model:

       
x˙1 1 0 x1 0 0 u1
= + (31.6)
x˙2 −3−1 x2 2 4 u2
   
  x1   u1
y= 5 6 7 0 (31.7)
x2 u2

We can define this state space model in Python:

1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2 im po rt c o n t r o l
3
4 # System m a t r i c e s
5 A = [ [ 0 , 1 ] , [ −1 , − 3 ] ]
6 B = [[0 , 0] , [2 , 4]]
7 C = [[5 , 6]]
8 D = [[7 , 0]]
9 s s m o d e l = c o n t r o l . s s (A, B, C, D)
Listing 31.1: State space model

[End of Example]

Example 31.1.2. Mass Spring Damper System


Given a ”Mass-Spring-Damper” system as shown in Figure 31.1.

The system can be described by the following equation:

F (t) − cẋ(t) − kx(t) = mẍ(t) (31.8)

Where t is the simulation time, F(t) is an external force applied to the system, c
is the damping constant of the spring, k is the stiffness of the spring, m is a mass.

x(t) is the position of the object (m).

ẋ(t) is the first derivative of the position, which equals the velocity of the object
(m).

236
Figure 31.1: Mass-Spring-Damper System

ẍ(t) is the second derivative of the position, which equals the acceleration of the
object (m).

The state space model can be stated like this:

      
x˙1 0 1 x1 0
= k c + 1 u (31.9)
x˙2 −m −m x2 m
 
  x1
y= 1 0 (31.10)
x2

Python code:

1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2 im po rt c o n t r o l
3
4 # P ar a m e t e r s d e f i n i n g t h e system
5 m = 250.0 # system mass
6 k = 40.0 # spring constant
7 c = 60.0 # damping c o n s t a n t
8
9 # System m a t r i c e s
10 A = [ [ 0 , 1 . ] , [−k/m, −c /m ] ]
11 B = [ [ 0 ] , [ 1 /m ] ]
12 C = [[1. , 0]]
13 s y s = c o n t r o l . s s (A, B, C, 0 )
14

237
15 # Step r e s p o n s e f o r t h e system
16 t , y = control . step response ( sys )
17 plt . plot (t , y)
Listing 31.2: Mass Spring Damper System

You should explore with different values for m, c and k.

[End of Example]

31.2 Discrete State-space Models

238
Chapter 32

Frequency Response

32.1 Introduction
The frequency response of a system is a frequency dependent function which
expresses how a sinusoidal signal of a given frequency on the system input is
transferred through the system. Each frequency component is a sinusoidal sig-
nal having certain amplitude and a certain frequency.

The frequency response is an important tool for analysis and design of signal
filters and for analysis and design of control systems.The frequency response
can be found experimentally or from a transfer function model.

The Python Control Systems Library (python-control) is a Python package that


implements basic operations for analysis and design of feedback control systems.

Python Control Systems Library Documentation:

https://python-control.readthedocs.io

32.1.1 Theory
The frequency response of a system is defined as the steady-state response of
the system to a sinusoidal input signal. When the system is in steady-state, it
differs from the input signal only in amplitude/gain (A) and phase lag (φ).

This is illustrated in Figure 32.1.

The theory behind frequency response is also based on complex numbers, so


make sure to take a look at the chapter about complex numbers.

If we have the input signal:

u(t) = U sin(ωt) (32.1)

239
Figure 32.1: Frequency Response Definition

The steady-state output signal will be:

y(t) = U Asin(ωt + φ) (32.2)

Where A = YU is the ratio between the amplitudes of the output signal and the
input signal (in steady-state).

A and φ is a function of the frequency ω so we may write A = A(ω) and φ = φ(ω)

For a transfer function:

y(s)
H(S) = (32.3)
u(s)

We have that:

6 H(jω)
H(jω) = |H(jω)|ej (32.4)

Where H(jω) is the frequency response of the system, i.e., we may find the
frequency response by setting s = jω in the transfer function.

32.2 Bode Diagram


Bode diagrams are useful in frequency response analysis. The Bode diagram
consists of 2 diagrams, the Bode magnitude diagram, A(ω) and the Bode phase

240
diagram, φ(ω).

This is illustrated in Figure 32.2.

Figure 32.2: Bode Diagram

You can find the Bode diagram from experiments on the physical process or
from the transfer function (the model of the system).

With Python you can easily create Bode diagram from the Transfer function
model using the bode() function in the Python Control Systems Library.

The Gain function:

A(ω) = |H(jω)| (32.5)

The Phase function:

φ(ω) = 6 H(jω) (32.6)

241
The A(ω) axis is in decibel (dB), where the decibel value of x is calculated as:

x[dB] = 20log10 (x) (32.7)

The φ(ω) axis is in degrees (not radians!)

Example 32.2.1. Frequency Response - 1.order


Given the following transfer function:

3
H(s) = (32.8)
4s + 1
The Python code becomes:
1 im po rt numpy a s np
2 im po rt c o n t r o l
3
4 num = np . a r r a y ( [ 3 ] )
5 den = np . a r r a y ( [ 4 , 1 ] )
6
7 H = c o n t r o l . t f (num , den )
8 p r i n t ( ’H( s ) = ’ , H)
9
10 c o n t r o l . bode (H, dB=True )
Listing 32.1: Frequency Response - 1.order

This gives the the plot shown in Figure 32.3.

Instead of Plotting the Bode Diagram we can also use the bode function for
calculation and showing the data as well. We can also specify which frequencies
we want to include.

Python Code:

1 im po rt numpy a s np
2 im po rt c o n t r o l
3
4 num = np . a r r a y ( [ 3 ] )
5 den = np . a r r a y ( [ 4 , 1 ] )
6
7 H = c o n t r o l . t f (num , den )
8 p r i n t ( ’H( s ) = ’ , H)
9
10 w l i s t = [ 0 . 0 1 , 0 . 1 , 1 , 2 ,3 ,5 ,10 , 100]
11 [ mag , phase , w ] = c o n t r o l . bode (H, w l i s t , dB=True )
12
13 # Convert t o D e c i b e l
14 magdb = 20 ∗ np . l o g 1 0 ( mag )
15 #magdb = c o n t r o l . mag2db ( mag ) A l t e r n a t i v e s o l u t i o n : This i s a
premade f u n c t i o n implmenting t h e f o r m u l a above
16 p r i n t ( magdb )

242
Figure 32.3: Frequency Response 1.order

17
18 # Convert t o D e g r e e s
19 p h a s e d e g = phase ∗ 180/ np . p i ;
20 p r i n t ( phasedeg )
Listing 32.2: Frequency Response - 1.order

[End of Example]

Example 32.2.2. Frequency Response - 2.order


Given the following transfer function:

3
H(s) = (32.9)
4s2 + 5s + 6
The Python code becomes:
1 im po rt numpy a s np
2 im po rt c o n t r o l
3
4 num = np . a r r a y ( [ 3 ] )
5 den = np . a r r a y ( [ 4 , 5 , 6 ] )
6
7 H = c o n t r o l . t f (num , den )
8 p r i n t ( ’H( s ) = ’ , H)

243
9
10 c o n t r o l . bode (H, dB=True )
Listing 32.3: Frequency Response - 2.order

This gives the the plot shown in Figure 32.4.

Figure 32.4: Frequency Response 2.order

[End of Example]

Example 32.2.3. Frequency Response Example


Given the following transfer function:

3s + 3
H(s) = (32.10)
4s2 + 5s + 6
The Python code becomes:
1 im po rt numpy a s np
2 im po rt c o n t r o l
3
4 num = np . a r r a y ( [ 3 , 2 ] )
5 den = np . a r r a y ( [ 4 , 5 , 6 ] )
6
7 H = c o n t r o l . t f (num , den )
8 p r i n t ( ’H( s ) = ’ , H)

244
9
10 c o n t r o l . bode (H, dB=True )
Listing 32.4: Frequency Response Example

This gives the the plot shown in Figure 32.5.

Figure 32.5: Frequency Response

[End of Example]

245
Part IX

Python Database
Development

246
Chapter 33

Database Applications with


Python

Here we will learn how we can use Python for communication with a Database
system such as SQL Server or MySQL. We will learn how we connect to a
database, how we can insert data into the database and retrieve data from the
database.

A Database is a structured way to store lots of information. The information


is stored in different tables. Some of the most popular Database Systems today
are:
• SQL Server
• MySQL
• MariaDB
• MongoDB
• etc.
ER Diagram (Entity-Relationship Diagram) is used for design and modeling of
databases. It specifies tables and relationship between them (Primary Keys and
Foreign Keys)

Figure 33.1 shows an example of an ER diagram consisting of two database


tables.

Here you can learn more about Database Systems, download examples and get
additional resources, see videos, etc.:
https://www.halvorsen.blog/documents/technology/database/

33.1 Structured Query Language (SQL)


Structured Query Language (SQL) is a database language supported by most of
the existing database systems today. You use SQL to interact with the database

247
Figure 33.1: ER Diagram Example

system, like insert data into the database and retrieve data from the database.

Here you can learn more about SQL, download examples and get additional
resources, see videos, etc.:
https://www.halvorsen.blog/documents/technology/database/

33.2 SQL Server


Here we will see how we can communicate with a SQL Server database from
Python.

33.3 MySQL
Here we will see how we can communicate with a MySQL database from Python.

33.4 MongoDB
Here we will see how we can communicate with a MongoDB database from
Python.

MongoDB is a so-called NoSQL database. One of the most popular NoSQL


database systems is MongoDB.

You can download a free MongoDB database from:


https://www.mongodb.com

248
Chapter 34

SQL Server with Python

34.1 Introduction to SQL Server


Here we will see how we can communicate with a SQL Server from Microsoft
using the Python programming language.

34.2 SQL Server drivers for Python


There are several python SQL drivers available. However, Microsoft places its
testing efforts and its confidence in pyodbc driver. Another driver is pymssql.

pyodbc is an open source Python module that can be used to accessing ODBC
databases.

34.3 pyodbc
pyODBC uses the Microsoft ODBC driver for SQL Server.

34.3.1 Installation of pyodbc


Install pyodbc using pip - Python package manager.

pip install pyodbc

34.3.2 ODBC Drivers


Microsoft have written and distributed multiple ODBC drivers for SQL Server:
You can use the ”ODBC Driver 17 for SQL Server” driver.

This driver supports SQL Server 2008 through 2019.

249
Note that the ”SQL Server Native Client ...” and earlier drivers are deprecated
and should not be used for new development.
The connection string can be written like this:

DRIVER=ODBC Driver 17 for SQL Server;SERVER=test;DATABASE=test;UID=user;PWD=password

34.4 SQL Server Python Examples


The cursor.execute function can be used to retrieve a result set from a query
against the SQL Database. This function accepts a query and returns a result
set.

Example 34.4.1. Basic SQL Server Example


The cursor.execute function is used to retrieve a result set from a query against
the SQL Server Database. This function accepts a query and returns a result
set, which can be iterated over with the use of cursor.fetchone().

1 im po rt pyodbc
2
3 s e r v e r = ”NUCHPH\\SQLEXPRESS”
4 d a t a b a s e = ”BOOKS”
5 username = ” s a ”
6 password = ” xxx ”
7 conn = pyodbc . c o n n e c t ( ”DRIVER={ODBC D r i v e r 17 f o r SQL S e r v e r } ;
SERVER=” + s e r v e r + ” ;DATABASE=” + d a t a b a s e + ” ; UID=” +
username + ” ;PWD=” + password )
8
9 c u r s o r = conn . c u r s o r ( )
10
11 c u r s o r . e x e c u t e ( ”SELECT @@version ; ” )
12 row = c u r s o r . f e t c h o n e ( )
13 w h i l e row :
14 p r i n t ( row [ 0 ] )
15 row = c u r s o r . f e t c h o n e ( )
Listing 34.1: Basic SQL Server Example

Example 34.4.2. Getting Data from a Table in SQL Server

Below you find a basic example where data are retrieved from the SQL Server.

1 im po rt pyodbc
2
3 s e r v e r = ”NUCHPH\\SQLEXPRESS”
4 d a t a b a s e = ”BOOKS”
5 username = ” s a ”
6 password = ” xxx ”
7 conn = pyodbc . c o n n e c t ( ”DRIVER={ODBC D r i v e r 17 f o r SQL S e r v e r } ;
SERVER=” + s e r v e r + ” ;DATABASE=” + d a t a b a s e + ” ; UID=” +
username + ” ;PWD=” + password )
8

250
9 c u r s o r = conn . c u r s o r ( )
10
11
12 f o r row i n c u r s o r . e x e c u t e ( ” s e l e c t BookId , T i t l e , I s b n from BOOK” ) :
13 p r i n t ( row . BookId , row . T i t l e , row . I s b n )
Listing 34.2: Getting Data from SQL Server

[End of Example]

34.5 Stored Procedures


34.6 Resources
https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-
Windows

34.7 pymssql
Resources:

https://pypi.org/project/pymssql/
http://www.pymssql.org/

34.8 Resources
https://docs.microsoft.com/en-us/sql/connect/python/python-driver-for-sql-server

251
Part X

Python Application
Development

252
Chapter 35

OPC Communication with


Python

35.1 Introduction to OPC


OPC is a standard that defines the communication of data between devices from
different manufactures. OPC requires an OPC server that communicates with
the OPC clients.

Figure 35.1 shows the basic concepts of OPC.

Figure 35.1: OPC

OPC allows “plug-and-play”, gives benefits as reduces installation time and the
opportunity to choose products from different manufactures.

253
We have different OPC standards: “Real-time” data (OPC DA), Historical data
(OPC HDA), Alarm Event data (OPC AE), etc.

In Figure 35.2 we see a typical OPC scenario.

Figure 35.2: Typical OPC Scenario

We can divide the OPC in 2 categories: OPC Classic and OPC UA, which is
the next generation OPC.

Classic OPC requires a Microsoft Windows operating system to implement


COM/DCOM server functionality.

By utilizing SOA and Web Services, OPC UA is a platform-independent system


that eliminates the previous dependency on a Windows operating system. By
utilizing SOAP/XML over HTTP, OPC UA can deploy on a variety of embed-
ded systems regardless of whether the system is a general purpose operating
system, such as Windows, or a deterministic real-time operating system.

In Figure 35.3 we get an overview of the main differences between OPC Classic
and OPC UA.

Here you find more information, resources, videos and examples regarding OPC:
https://www.halvorsen.blog/documents/technology/opc/

35.2 OPC Classic


The most used ”classic” OPC standards are the following:

254
Figure 35.3: Typical OPC Scenario

• OPC DA (Data Access). The most common OPC specification is OPC


DA, which is used to read and write “real-time” data. When vendors refer
to OPC generically, they typically mean OPC DA.
• OPC HDA (Historical Data Access)

• OPC A E (Alarms Events)


• ... (many others)

These OPC specification are based on the OLE, COM, and DCOM technologies
developed by Microsoft for the Microsoft Windows operating system family.
This makes it complicated to make it work in a modern Network! Typically
you need a Tunneller Software in order to share the OPC data in a network
(between OPC Servers and Clients)

35.3 OPC UA
OPC UA (Unified Architecture).

OPC UA solves problems with standard/classic OPC:

• Works only on Windows

• Cumbersome to use OPC in a network due to COM/DCOM


• OPC UA eliminating the need to use a Microsoft Windows based platform
of earlier OPC versions.
• OPC UA combines the functionality of the existing OPC interfaces with
new technologies such as XML and Web Services (HTTP, SOAP)

• Cross-platform
• No dedicated OPC Server is no longer necessary because the server can
run on an embedded system

255
Figure 35.4: OPC Firewall Challenges

In Figure 35.4 we get an overview of the main differences between OPC Classic
and OPC UA regarding communication over a network.

OPC UA supports two protocols:


• “UA Binary” protocol opc.tcp://ServerThis uses a simple binary protocol
• “UA XML” protocol http://ServerThis used open standards like XML,
SOAP (-¿ Web Service)
This is visible to application programmers only via changes to the URL. Oth-
erwise OPC UA works completely transparent to the API.

35.4 OPC Examples with Python

256
Chapter 36

Python Integration with


LabVIEW

36.1 What is LabVIEW?


LabVIEW is a graphical programming language well suited for hardware inte-
gration, taking measurements and data logging.
Go to my web site in order to learn more about LabVIEW:
https://www.halvorsen.blog

https://www.halvorsen.blog/documents/programming/labview/

Her you find information about LabVIEW, you find lots of resources like training
material, videos, code examples, etc.

36.2 Using Python in LabVIEW


LabVIEW is a fully functional programming language which you can use to cre-
ate many different kinds of applications. In addition it can also integrate with
many other programming languages like MATLAB and Python.

Web:
https://zone.ni.com/reference/en-XX/help/371361R-01/glang/pythonp al/

Use the Python functions to call Python code from LabVIEW. See Figure 36.1

Note! LabVIEW supports calling Python version 2.7 and 3.6. Although un-
supported versions might work with the LabVIEW Python functions, NI rec-
ommends using supported versions of Python only.

Ensure that the bitness of Python corresponds to the bitness of LabVIEW in-
stalled on the machine. This means if you have LabVIEW 32 bit, you should
use Python 32 bit and if you have LabVIEW 64 bit, you should use Python 64

257
bit.

To run the Python code, LabVIEW requires the Python shared libraries (DLLs)
in the system path.

For Windows: If you install Python 3.6, add the directory containing python36.dll
to the system path. If you install Python 2.7, add the directory containing
python27.dll to the system path.

For detailed instructions regarding Installing Python for Calling Python Code:
http://www.ni.com/product-documentation/54295/en/

LabVIEW functions for dealing with Python: Open Python Session Python
Node Close Python Session

Figure 36.1: Python Integration in LabVIEW

The ”Python Node” calls a Python function directly.

Here I will present some examples how we can integrate an existing Python
script or Python function.
Example 36.2.1. Python Integration in LabVIEW
We want to use Python to covert between Ceslius and Fahrenheit (and vice
versa).

The formula for converting from Celsius to Fahrenheit is:

Tf = (Tc × 9/5) + 32 (36.1)

The formula for converting from Fahrenheit to Celsius is:

Tc = (Tf − 32) × (5/9) (36.2)


First, we create a Python module with the following functions (fahrenheit.py):
1 d e f c 2 f ( Tc ) :
2

258
3 Tf = ( Tc ∗ 9 / 5 ) + 32
4 r e t u r n Tf
5
6
7 d e f f 2 c ( Tf ) :
8
9 Tc = ( Tf − 3 2 ) ∗ ( 5 / 9 )
10 r e t u r n Tc
Listing 36.1: Python Functions

Then, we create a Python script for testing the functions (test fahrenheit.py):
1 from f a h r e n h e i t i mp ort c 2 f , f 2 c
2
3 Tc = 0
4
5 Tf = c 2 f ( Tc )
6
7 p r i n t ( ” F a h r e n h e i t : ” + s t r ( Tf ) )
8
9
10 Tf = 32
11
12 Tc = f 2 c ( Tf )
13
14 p r i n t ( ” C e l s i u s : ” + s t r ( Tc ) )
Listing 36.2: Testing the Functions

The results becomes:


1 Fahrenheit : 32.0
2 Celsius : 0.0

Lets make the LabVIEW program that call these Python functions:

In Figure 36.2 we see the Front Panel.

In Figure 36.3 we see the Block Diagram.

In Figure 36.4 we see LabVIEW Code for calling both Python functions (c2f
and f2c) from LabVIEW.

[End of Example]

259
Figure 36.2: Python Integration in LabVIEW

Figure 36.3: Python Integration in LabVIEW

260
Figure 36.4: Python Integration in LabVIEW

261
Chapter 37

Raspberry Pi and Python

37.1 What is Raspberry Pi?


The Raspberry Pi is a credit-card-sized computer that plugs into your TV and
a keyboard. It is a capable little computer which can be used in electronics
projects, and for many of the things that your desktop PC does.

Raspberry Pi is very popular in IoT projects and applications.

For more information and resources regarding Raspberry Pi:

https://www.halvorsen.blog/documents/technology/iot/raspberry pi.php

Other Resources:

https://learn.sparkfun.com/tutorials/python-programming-tutorial-getting-started-
with-the-raspberry-pi/programming-in-python

First, before you start programming Python on a Raspberry Pi device, you need
to install an operating system like Raspbian. Raspbian is a Linux distribution
tailor made for Raspberry Pi.

Raspbian comes also pre-installed Python.

For more information about Raspbian:

https://www.raspberrypi.org/downloads/raspbian/

262
Part XI

Resources

263
Chapter 38

Python for MATLAB Users

If you are familiar with MATLAB, you can relatively easily switch to Python.
Most of the functionality and the functions in different toolboxes have similar
functions in Python.

If you are looking for MATLAB resources, please see the following:
https://www.halvorsen.blog/documents/programming/matlab/

I have made lots of MATLAB resources, including the following textbooks:

• Introduction to MATLAB
• Modelling, Simulation and Control in MATLAB

• Simulink and Advanced Topics in MATLAB

Figure 38.1 shows the book covers.

Figure 38.1: MATLAB Textbooks

264
38.1 Use Python inside MATLAB
To call Python libraries from MATLAB, you need to install the reference im-
plementation for Python (CPython).
For more information, see the following:

https://se.mathworks.com/help/matlab/getting-started-with-python.html

MATLAB and Python must also be the same architecture/bit version. If you
have MATLAB 64 bits version, you need Python 64 bits. If you have MATLAB
32 bits version, you need Python 32 bits.

In MATLAB start by finding information about the default Python Environ-


ment used by MATLAB:
1 >> p y v e r s i o n

This gives the following on my Windows10 computer:


1 > pyversion
2
3 version : ’ 3.7 ’
4 e x e c u t a b l e : ’C: \ U s e r s \ hansha \AppData\ L o c a l \ Programs \ Python \
Python37 64 \ pythonw . e x e ’
5 l i b r a r y : ’C: \ U s e r s \ hansha \AppData\ L o c a l \ Programs \ Python \
Python37 64 \ python37 . d l l ’
6 home : ’C: \ U s e r s \ hansha \AppData\ L o c a l \ Programs \ Python \
Python37 64 ’
7 isloaded : 1

If MATLAB and Python don’t the same architecture/bit version, you need to
either install the proper version of either MATLAB or Python. You may also
need to ”Add Python to Path” as described in Chapter 3.

Example 38.1.1. MATLAB - Python Example

Earlier we have created a Python module with 2 functions. The first function
should convert from Celsius to Fahrenheit and the other function should convert
from Fahrenheit to Celsius.

The formula for converting from Celsius to Fahrenheit is:

Tf = (Tc × 9/5) + 32 (38.1)

The formula for converting from Fahrenheit to Celsius is:

Tc = (Tf − 32) × (5/9) (38.2)

First, we created a Python module with the following functions (fahrenheit.py):

265
1 d e f c 2 f ( Tc ) :
2
3 Tf = ( Tc ∗ 9 / 5 ) + 32
4 r e t u r n Tf
5
6
7 d e f f 2 c ( Tf ) :
8
9 Tc = ( Tf − 3 2 ) ∗ ( 5 / 9 )
10 r e t u r n Tc
Listing 38.1: Fahrenheit Functions

Then, we created a Python script for testing the functions (testfahrenheit.py):


1 from f a h r e n h e i t i mp ort c 2 f , f 2 c
2
3 Tc = 0
4
5 Tf = c 2 f ( Tc )
6
7 p r i n t ( ” F a h r e n h e i t : ” + s t r ( Tf ) )
8
9
10 Tf = 32
11
12 Tc = f 2 c ( Tf )
13
14 p r i n t ( ” C e l s i u s : ” + s t r ( Tc ) )
Listing 38.2: Python Script testing the functions

The results becomes:


1 Fahrenheit : 32.0
2 Celsius : 0.0

Now we want to use these functions inside the MATLAB environment.

The MATLAB code for this becomes:

1 clc
2 Tc = 0
3 Tf = py . f a h r e n h e i t . c 2 f ( Tc )
4 Tc = py . f a h r e n h e i t . f 2 c ( Tf )
Listing 38.3: Calling Python Library and Functions from MATLAB

[End of Example]

38.2 Calling MATLAB from Python


The MATLAB Engine API for Python provides a package for Python to call
MATLAB as a computational engine. The engine supports the reference imple-
mentation (CPython).

266
For more information, see the following:

https://se.mathworks.com/help/matlab/matlab-engine-for-python.html

267
Chapter 39

Python Resources

Here you find my Web page with Python resources [1]:


https://www.halvorsen.blog/documents/programming/python/

Python Home Page [6]:


https://www.python.org

Python Standard Library [16]:


https://docs.python.org/3/library/index.html

39.1 Python Distributions


Anaconda:
https://www.anaconda.com

39.2 Python Libraries


NumPy Library:
http://www.numpy.org

SciPy Library:
https://www.scipy.org

Matplotlib Library:
https://matplotlib.org

39.3 Python Editors


Spyder:
https://www.spyder-ide.org

268
Visual studio Code:
https://code.visualstudio.com

Visual Studio:
https://visualstudio.microsoft.com

PyCharm:
https://www.jetbrains.com/pycharm/

Wing:
https://wingware.com

Jupyter Notebook:
http://jupyter.org

39.4 Python Tutorials


Python Tutorial - w3schools.com [13]:
https://www.w3schools.com/python/

The Python Guru [17]:


https://thepythonguru.com

Wikibooks - A Beginner’s Python Tutorial:


https://en.wikibooks.org/wiki/AB eginner

TutorialsPoints - Python Tutorial:


https://www.tutorialspoint.com/python/

The Hitchhiker’s Guide to Python:


https://docs.python-guide.org

Google’s Python Class:


https://developers.google.com/edu/python/

39.5 Python in Visual Studio


Work with Python in Visual Studio
https://docs.microsoft.com/visualstudio/python/

269
Bibliography

[1] H.-P. Halvorsen, “Technology blog - https://www.halvorsen.blog,” 2018.


[2] H.-P. Halvorsen, “Technology blog - https://en.wikipedia.org/wiki/Python( programmingl anguage),00 201
[3] T. . T. P. Languages, “The 2018 top programming languages
- https://spectrum.ieee.org/at-work/innovation/the-2018-top-
programming-languages,” 2018.
[4] S. Overflow, “Stack overflow developer survey 2018 -
https://insights.stackoverflow.com/survey/2018/,” 2018.
[5] stackoverflow.blog, “The incredible growth of python -
https://stackoverflow.blog/2017/09/06/incredible-growth-python/,”
2018.
[6] python.org, “python.org - https://www.python.org,” 2018.
[7] python.org, “The python tutorial - https://docs.python.org/3.7/tutorial/,”
2018.
[8] python.org, “Python 3.7.1 documentation - https://docs.python.org/3.7/,”
2018.
[9] scipy.org, “Scipy - https://www.scipy.org,” 2018.
[10] matplotlib.org, “Matplotlib - https://matplotlib.org,” 2018.
[11] pandas, “pandas - http://pandas.pydata.org,” 2018.
[12] Wingware, “Wingware python ide - https://wingware.com,” 2018.
[13] w3schools.com, “Python tutorial - https://www.w3schools.com/python/,”
2018.
[14] Wikipedia, “Debugging - https://en.wikipedia.org/wiki/Debugging,” 2018.
[15] TechBeamers, “Get the best python ide -
https://www.techbeamers.com/best-python-ide-python-programming/,”
2018.
[16] python.org, “The python standard library -
https://docs.python.org/3/library/,” 2018.
[17] T. P. Guru, “The python guru - https://thepythonguru.com,” 2018.

270
Part XII

Solutions to Exercises

271
Start using Python

Simulation and Plotting of Dynamic System


Given the autonomous system:
ẋ = ax (1)
Where:
1
a=−
T

where T is the time constant.

The solution for the differential equation is:

x(t) = eat x0 (2)

Set T=5 and the initial condition x(0)=1.

Create a Script in Python (.py file) where you plot the solution x(t) in the time
interval:
0 ≤ t ≤ 25

Add Grid, and proper Title and Axis Labels to the plot.

Python Script:
1 im po rt math a s mt
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5
6 # Model P a r a m e t e r s
7 T = 5
8 a = −1/T
9
10 # Simulation Parameters
11 x0 = 1
12 t = 0
13
14 tstart = 0

272
15 t s t o p = 25
16
17 increment = 1
18
19 x = []
20 x = np . z e r o s ( t s t o p +1)
21
22 t = np . a r a n g e ( t s t a r t , t s t o p +1 , i n c r e m e n t )
23
24
25 # Define the Function
26 f o r k in range ( tstop ) :
27 x [ k ] = mt . exp ( a ∗ t [ k ] ) ∗ x0
28
29
30 # Plot the Simulation Results
31 plt . plot (t , x)
32 p l t . t i t l e ( ’ S i m u l a t i o n o f Dynamic System ’ )
33 plt . xlabel ( ’ t ’ )
34 plt . ylabel ( ’x ’ )
35 plt . grid ()
36 p l t . a x i s ( [ 0 , 25 , 0 , 1 ] )
37 p l t . show ( )

The simulation gives the results as shown in Figure 1.

Figure 1: Simulation of Dynamic System

[End of Exercise]

273
Mathematics in Python

Create Mathematical Expressions in Python


Create a function that calculates the following mathematical expression:
p
z = 3x2 + x2 + y 2 + eln (x) (3)
Test with different values for x and y.

We create a Python Module with a Python Function (mymathfunctions.py):


1 im po rt math a s mt
2
3 def calcexpression (x , y) :
4
5 z = 3∗ x ∗∗2 + mt . s q r t ( x ∗∗2 + y ∗ ∗ 2 ) + mt . exp ( mt . l o g ( x ) )
6 return z

Then we can create a Python Script in order to test the function:


1 im po rt mymathfunctions a s mymath
2
3 x = 2
4 y = 2
5
6 z = mymath . c a l c e x p r e s s i o n ( x , y )
7
8 print ( z )

The results become:


1 16.82842712474619

[End of Solution]

Create advanced Mathematical Expressions in Python


Create the following expression in Python:

ln (ax2 + bx + c) − sin(ax2 + bx + c)
f (x) = (4)
4πx2 + cos(x − 2)(ax2 + bx + c)

274
Given a = 1, b = 3, c = 5 Find f (9)
(The answer should be f (9) = 0.0044)

Tip! You should split the expressions into different parts, such as:

poly = ax2 + bx + c

num = . . .
den = . . .
f = ...

This makes the expression simpler to read and understand, and you minimize
the risk of making an error while typing the expression in Python.

When you got the correct answer try to change to, e.g., a = 2, b = 8, c = 6

Find f (9)

Python Script:
1 ...

[End of Solution]

275
Discrete Systems

Bacteria Population
In this task we will simulate a simple model of a bacteria population in a jar.

The model is as follows:

birth rate = bx (5)

death rate = px2 (6)

Then the total rate of change of bacteria population is:

ẋ = bx − px2 (7)

Set b=1/hour and p=0.5 bacteria-hour

We will simulate the number of bacteria in the jar after 1 hour, assuming that
initially there are 100 bacteria present.

Find the discrete model using the Euler Forward method by hand and imple-
ment and simulate the system in Python using a For Loop.

We can use e.g., the Euler Approximation:

xk+1 − xk
ẋ ≈ (8)
Ts
Ts - Sampling Interval

Then we get:

276
xk+1 − xk
= bxk − px2k (9)
Ts

This gives the following discrete differential equation:

xk+1 = xk + Ts (bxk − px2k ) (10)

Now we are ready to simulate the system.

Python Script:
1 # Simulation of Bacteria Population
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s
6 b = 1
7 p = 0.5
8
9 # Simulation Parameters
10 Ts = 0 . 0 1
11 Tstop = 1
12 xk = 100
13 N = i n t ( Tstop /Ts ) # S i m u l a t i o n l e n g t h
14 data = [ ]
15 data . append ( xk )
16
17
18 # Simulation
19 f o r k i n r a n g e (N) :
20 xk1 = xk + Ts∗ ( b ∗ xk − p ∗ xk ∗ ∗ 2 ) ;
21 xk = xk1
22 data . append ( xk1 )
23
24 # Plot the Simulation Results
25 t = np . a r a n g e ( 0 , Tstop+Ts , Ts )
26
27 plt . p l o t ( t , data )
28 plt . t i t l e ( ’ Simulation of Bacteria Population ’ )
29 plt . xlabel ( ’ t [ s ] ’ )
30 plt . ylabel ( ’x ’ )
31 plt . grid ()
32 plt . axis ([0 , 1 , 0 , 100])
33 plt . show ( )

The simulation gives the results as shown in Figure 2.

[End of Solution]

277
Figure 2: Simulation of Bacteria Population

Simulation with 2 variables


Given the following system:

dx1
= −x2 (11)
dt
dx2
= x1 (12)
dt

Find the discrete system and simulate the discrete system in Python. Solve the
equations, e.g., in the time span [-1 1] with initial values [1, 1].

Python Script:
1 # S i m u l a t i o n with 2 V a r i a b l e s
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s
6 b = 1
7 p = 0.5
8
9 # Simulation Parameters
10 Ts = 0 . 1
11 T s t a r t = −1
12 Tstop = 1
13 x1k = 1
14 x2k = 1
15 N = i n t ( ( Tstop−T s t a r t ) /Ts ) # S i m u l a t i o n l e n g t h
16 datax1 = [ ]

278
17 datax2 = [ ]
18 datax1 . append ( x1k )
19 datax2 . append ( x2k )
20
21
22 # Simulation
23 f o r k i n r a n g e (N) :
24 x1k1 = x1k − Ts ∗ x2k
25 x2k1 = x2k + Ts ∗ x1k
26
27 x1k = x1k1
28 x2k = x2k1
29 datax1 . append ( x1k1 )
30 datax2 . append ( x2k1 )
31
32 # Plot the Simulation Results
33 t = np . a r a n g e ( T s t a r t , Tstop+Ts , Ts )
34
35 plt . p l o t ( t , datax1 , t , datax2 )
36 plt . t i t l e ( ’ S i m u l a t i o n with 2 V a r i a b l e s ’ )
37 plt . xlabel ( ’ t [ s ] ’ )
38 plt . ylabel ( ’x ’ )
39 plt . grid ()
40 plt . a x i s ( [ − 1 , 1 , −1.5 , 1 . 5 ] )
41 plt . show ( )

The simulation gives the results as shown in Figure 2.

Figure 3: Simulation Example with 2 Variables

Alternative Solution:
1 # S i m u l a t i o n with 2 V a r i a b l e s
2 im po rt numpy a s np
3 im po rt m a t p l o t l i b . p y p l o t a s p l t
4
5 # Model P a r a m e t e r s

279
6 b = 1
7 p = 0.5
8
9 # Simulation Parameters
10 Ts = 0 . 1
11 T s t a r t = −1
12 Tstop = 1
13 N = i n t ( ( Tstop−T s t a r t ) /Ts ) # S i m u l a t i o n l e n g t h
14 x1 = np . z e r o s (N+2)
15 x2 = np . z e r o s (N+2)
16 x1 [ 0 ] = 1
17 x2 [ 0 ] = 1
18
19
20 # Simulation
21 f o r k i n r a n g e (N+1) :
22 x1 [ k +1] = x1 [ k ] − Ts ∗ x2 [ k ]
23 x2 [ k +1] = x2 [ k ] + Ts ∗ x1 [ k ]
24
25
26 # Plot the Simulation Results
27 t = np . a r a n g e ( T s t a r t , Tstop+2∗Ts , Ts )
28
29 plt . p l o t ( t , x1 , t , x2 )
30 plt . t i t l e ( ’ S i m u l a t i o n with 2 V a r i a b l e s ’ )
31 plt . xlabel ( ’ t [ s ] ’ )
32 plt . ylabel ( ’x ’ )
33 plt . grid ()
34 plt . a x i s ( [ − 1 , 1 , −1.5 , 1 . 5 ] )
35 plt . show ( )

Choose the approach that fits you. You should also check the time that the
simulation take. For larger simulations, this second alternative may be better.

[End of Solution]

280
Python for Control Engineering

You might also like