Table of Contents

Open all
Close all
1 Introduction
17
1.1 Development Environments
18
1.1.1 IDLE
18
1.1.2 Thonny
19
1.1.3 Spyder
20
1.1.4 Pip
21
1.2 The Modules of Python
22
1.2.1 NumPy
22
1.2.2 Matplotlib
23
1.2.3 SymPy
24
1.2.4 SciPy
24
1.2.5 VPython
25
1.3 The Keywords of Python
26
1.4 Your Path through This Book
27
2 Program Structures
29
2.1 Linear Program Structures
29
2.1.1 Linear Programs without Function Calls
30
2.2 Functions
37
2.2.1 Built-In Functions
38
2.2.2 Functions without Parameters and without Return Values
39
2.2.3 Functions with Parameters and a Return
40
2.2.4 Functions with Multiple Return Values
42
2.2.5 Functions Call Other Functions
44
2.3 Branching Structures
45
2.3.1 Single Selection
46
2.3.2 Multiple Selection
47
2.4 Repetitive Structures
50
2.4.1 The while Loop
50
2.4.2 The for Loop
56
2.5 Data Structures
63
2.5.1 Tuples
63
2.5.2 Lists
66
2.5.3 Dictionaries
72
2.5.4 Sets
75
2.6 Functional Program Style
76
2.7 Object-Oriented Program Style
79
2.7.1 Objects and Classes
80
2.7.2 Inheritance
84
2.8 Project Task: Dimensions of a Shaft
86
2.9 Tasks
88
3 Numerical Calculations Using NumPy
91
3.1 NumPy Functions
91
3.1.1 Creating One-Dimensional Arrays Using arange() and linspace()
91
3.1.2 Creating Two-Dimensional Arrays Using array()
94
3.1.3 Slicing
96
3.1.4 Mathematical NumPy Functions
97
3.1.5 Statistical NumPy Functions
99
3.2 Vectors
101
3.2.1 Addition of Vectors
101
3.2.2 Scalar Product
103
3.2.3 Cross Product
105
3.2.4 Triple Product
107
3.2.5 Dyadic Product
108
3.3 Matrix Multiplication
109
3.3.1 Chain Shape with B Parameters
113
3.3.2 Usage Example: Calculating the Energy of a Rotating Rigid Body in Space
114
3.4 Linear Systems of Equations
116
3.4.1 Systems of Equations with Real Coefficients
116
3.4.2 Systems of Equations with Complex Coefficients
118
3.5 Project Task: Lightning Protection System
121
3.6 Tasks
124
4 Function Plots and Animations Using Matplotlib
125
4.1 2D Function Plots
125
4.1.1 Basic Structure of a Function Plot
125
4.1.2 Gridlines
132
4.1.3 Labels
134
4.1.4 Line Styles
137
4.1.5 Designing Axes
138
4.1.6 Coloring Areas
144
4.1.7 Subplots
147
4.1.8 Parameter Representation
154
4.1.9 Changing Function Parameters Interactively
158
4.1.10 Contour Plots
163
4.2 3D Function Plots
166
4.2.1 Helical Line
166
4.2.2 Circular Ring
168
4.2.3 Combining a 3D Plot with a Contour Plot
169
4.3 Vectors
171
4.3.1 Vector Addition
171
4.3.2 Vector Field
173
4.4 Displaying Figures, Lines, and Arrows
174
4.4.1 Rectangles
174
4.4.2 Circles and Lines
176
4.4.3 Arrows
178
4.4.4 Polygons
179
4.4.5 Usage Example: A Metal Rod in a Magnetic Field
181
4.5 Animations
182
4.5.1 A Simple Animation: Shifting a Sine Function
183
4.5.2 Animated Oblique Throw
186
4.5.3 Animated Planetary Orbit
188
4.6 Project Task: Stirling Cycle
190
4.7 Project Task: Animating a Thread Pendulum
197
4.8 Project Task: Animating a Transmission
201
4.9 Tasks
204
5 Symbolic Computation Using SymPy
207
5.1 Basic Mathematical Operations
211
5.1.1 Addition
211
5.1.2 Multiplication of Terms
212
5.1.3 Multiplication of Linear Factors
213
5.1.4 Division
214
5.1.5 Exponentiation
215
5.1.6 Usage Example: Analyzing an Electrical Power Transmission System
216
5.2 Multiplying Matrixes
217
5.2.1 Calculation Rule
217
5.2.2 Transmission Function of a Catenary Circuit
218
5.3 Equations
220
5.3.1 Linear Systems of Equations
221
5.3.2 Nonlinear Systems of Equations
223
5.4 Simplifications of Terms
226
5.5 Series Expansion
227
5.6 Partial Fractions
228
5.7 Continued Fractions
231
5.8 Limits
234
5.8.1 Limits of Sequences
234
5.8.2 Limits of Functions
236
5.8.3 Differential Quotient
237
5.9 Differentiation
238
5.9.1 Usage Example: Curve Sketching
239
5.10 Integrations
240
5.10.1 Indefinite Integral
241
5.10.2 Definite Integral
242
5.10.3 Usage Example: Stored Electrical Energy
243
5.11 Differential Equations
245
5.11.1 Linear First-Order Differential Equations
246
5.11.2 General Solution of a Second-Order Differential Equation
250
5.11.3 Special Solution of a Second-Order Differential Equation
252
5.12 Laplace Transform
254
5.12.1 Solving Differential Equations
255
5.12.2 Analyzing Networks with Transmission Functions
259
5.13 Project Task: Step Response of a Catenary Circuit
262
5.14 Project Task: Bending a Beam That Is Fixed at One End
265
5.14.1 Second Moment of Area
265
5.14.2 Equation of the Bending Line
267
5.15 Project Task: Reaction Kinetics
270
5.16 Project Task: Dual Mass Oscillator
274
5.17 Tasks
277
6 Numerical Computations and Simulations Using SciPy
279
6.1 Numerical Computation of Zeros
280
6.2 Optimizations
282
6.3 Interpolations
284
6.4 Numerical Differentiation
286
6.4.1 Methods of Numerical Differentiation
286
6.4.2 Drawing a Tangent Slope
287
6.4.3 Derivative of a Sine Function
289
6.4.4 Usage Example: Free Fall
291
6.5 Numerical Integration
293
6.5.1 Methods of Numerical Integration
293
6.5.2 Definite Integral
295
6.5.3 Integrating a Constant
296
6.5.4 Usage Example: Free Fall
298
6.5.5 Improper Integral
300
6.5.6 Calculating Arc Lengths
302
6.5.7 Volume and Surfaces of Rotating Bodies
303
6.5.8 Double Integrals
305
6.5.9 Triple Integrals
306
6.6 Solving Differential Equations Numerically
307
6.6.1 Numerical Solution of Differential Equations
307
6.6.2 First-Order Linear Differential Equation
312
6.6.3 Second-Order Linear Differential Equation
315
6.6.4 Nonlinear Second-Order Differential Equation
318
6.6.5 Second-Order Differential Equation System: Coupled Spring-Mass System
322
6.6.6 Nonlinear Second-Order Differential Equation System: Double Pendulum
325
6.7 Discrete Fourier Transform
328
6.7.1 Basic Use of the Fast Fourier Transform Algorithm
329
6.7.2 Frequency Spectra of Non-Sinusoidal Periodic Signals
330
6.7.3 Reconstructing a Noisy Signal
332
6.8 Writing and Reading Sound Files
334
6.8.1 Generating and Saving Signals
334
6.8.2 Reading and Displaying Signals
335
6.9 Signal Processing
337
6.9.1 Frequency Response of a Butterworth Lowpass
337
6.9.2 Frequency Response of a Crossover
339
6.9.3 Filtering Signals
341
6.10 Project Task: Simulation of a Rolling Bearing Damage
342
6.11 Project Task: Predator-Prey Model
345
6.11.1 Exponential Growth
346
6.11.2 Logistic Growth
346
6.11.3 Predator-Prey Relationship for Exponential Growth
346
6.11.4 Predator-Prey Relationship for Logistic Growth
347
6.12 Project Task: Simulation of an Epidemic
349
6.13 Tasks
352
7 3D Graphics and Animations Using VPython
355
7.1 The Coordinate System
356
7.2 Basic Shapes, Points, and Lines
358
7.2.1 Cylinder
359
7.2.2 Cuboid
361
7.2.3 Points
363
7.2.4 Lines
364
7.2.5 Sphere
366
7.2.6 Penetration
369
7.2.7 Composite Bodies
370
7.3 Bodies in Motion
371
7.3.1 Vertical Movement
372
7.3.2 Horizontal Movement
373
7.3.3 Movement in Space
374
7.3.4 Composite Motion
377
7.3.5 Rotational Motion
378
7.3.6 Random Motion
383
7.4 Animation of Oscillations
385
7.4.1 Simple Pendulum
385
7.4.2 Spring Pendulum
387
7.5 Event Processing
389
7.6 Project Task: Animation of a Coupled Spring Pendulum
392
7.7 Project Task: Animation of Two Coupled Simple Pendulums
395
7.8 Tasks
398
8 Computing with Complex Numbers
399
8.1 Mathematical Operations
400
8.2 Euler’s Formula
402
8.2.1 Symbolic Method
403
8.3 Calculating with Complex Resistors
403
8.4 Function Plots with Complex Magnitudes
406
8.4.1 Complex Frequency Response of a Series Resonant Circuit
406
8.4.2 Locus Curves
408
8.5 Project Task: Electric Power Transmission System
410
8.5.1 Task
411
8.5.2 Equivalent Circuit Diagram of a Three-Phase Power Line
413
8.6 Tasks
415
9 Statistical Computations
417
9.1 Generating, Saving, and Reading Measurement Values
419
9.1.1 Generating Measurement Values
419
9.1.2 Converting a Measurement Series into a Table
420
9.1.3 Writing Measurement Values to a File
422
9.1.4 Reading Measurement Values from a File
423
9.2 Frequency Distribution
423
9.2.1 Frequency Tables
424
9.2.2 Histograms
425
9.3 Location Parameters
426
9.3.1 Arithmetic Mean
427
9.3.2 Mode, Median, Harmonic Mean, and Geometric Mean
428
9.4 Dispersion Parameters
430
9.5 Normal Distribution
434
9.5.1 Graphical Representation of the Density Function
434
9.5.2 Probability Distribution
435
9.6 Skew
437
9.7 Regression Analysis
439
9.7.1 Computing the Regression Parameters
439
9.7.2 Representing the Scatter Plot and the Regression Line
442
9.8 Project Task: Simulation of a Quality Control Chart
443
9.9 Tasks
448
10 Boolean Algebra
449
10.1 Logical Operations
450
10.1.1 Conjunction
450
10.1.2 Disjunction
451
10.1.3 Negation
452
10.2 Laws of Boolean Algebra
453
10.2.1 Simple Postulates
453
10.2.2 De Morgan’s Laws
454
10.2.3 Distributive Law
455
10.3 Circuit Synthesis
456
10.3.1 Simplifying Logic Functions by Factoring Out
456
10.3.2 Simplification Using the Disjunctive Normal Form
457
10.3.3 Simplification Using the Conjunctive Normal Form
460
10.4 Project Task: Seven-Segment Coding
460
10.5 Tasks
463
11 Interactive Programming Using Tkinter
465
11.1 Interactions with Command Buttons, Textboxes, and Labels
466
11.1.1 Labels
467
11.1.2 Textboxes and Command Buttons
468
11.2 The Layout Manager of Tkinter
468
11.2.1 The pack Method
470
11.2.2 The grid Method
472
11.2.3 Summary
474
11.3 Selection with Radio Button
475
11.4 Slider
477
11.5 The Canvas Drawing Area
478
11.5.1 Representing Lines
478
11.5.2 Function Plots
480
11.5.3 Querying Mouse Coordinates
482
11.6 Project Task: Rotational Frequency Control of an Externally Excited DC Motor
484
11.7 Tasks
496
Appendix
497
A.1 Glossary: Basic Applied Computer Science Terminology
497
A.2 Derivatives of Elementary Functions
498
A.3 Antiderivative of Elementary Functions
499
A.4 Fourier Series of Important Electrotechnical Voltage Characteristics
500
A.5 Correspondence Table of Important Inverse Laplace Transforms
500
A.6 Bibliography
501
The Author
503
Index
505