
Programming, Modeling and Simulation in Python
I Python programming 15
1 Introduction to Python 17
1.1 Installing Python 17
1.2 Spyder development environment 17
1.3 Packages and modules 18
1.4 Interactive runs – Python as a calculator 19
1.5 Scripts and py-files 20
1.6 TAB-completion 22
1.7 Good to know commands 23
1.8 Variable explorer – read and write to file 24
1.9 LaTeX – document programs 26
1.10 Study questions 27
1.11 Exercises 28
2 Numbers and functions 31
2.1 Different types of numbers 31
2.2 Arithmetic 32
2.3 Functions of real numbers 35
2.4 Functions of complex numbers 37
2.5 Complex numbers – attributes and methods 38
2.6 Lambda functions 40
2.7 Significant digits and cancellation 41
2.8 Study questions 42
2.9 Exercises 43
3 Data types, variables, and assignments 47
3.1 Numbers, strings, lists, and tuples 47
3.2 Assignments 48
3.3 Assignments in technical terms 48
3.4 Assignments – value, type, and reference 49
3.5 Assignments – stepping 51
3.6 Tuples and multiple assignments 51
3.7 Assignments of complex expressions 52
3.8 Variable names 53
3.9 Reading and writing to the screen 54
3.10 Application – replacing windows 56
3.11 Study questions 57
3.12 Exercises 58
4 Lists, tuples and strings 63
4.1 Indexation of lists, tuples and strings 63
4.2 Slices of lists, tuples and strings 66
4.3 Slices – how to think 68
4.4 Lists – mutability 69
4.5 Lists with numbers – list comprehension 71
4.6 Classes and methods 73
4.7 Methods for lists 74
4.8 Methods for strings 76
4.9 Study questions 80
4.10 Exercises 81
5 Vectors, matrices and multidimensional arrays 85
5.1 NumPy 85
5.2 Arrays 85
5.3 Arrays – attribute 87
5.4 Vectors 87
5.5 Range and linspace 89
5.6 Subvectors 91
5.7 Vectors – removing elements 93
5.8 Vectors – mutability 93
5.9 Matrices 95
5.10 Zero and one matrices 96
5.11 Diagonal and banded matrices 97
5.12 Random matrices 98
5.13 Reshaping matrices 100
5.14 Row matrices versus vectors 103
5.15 Submatrices 104
5.16 Stacking matrices 106
5.17 Writing and reading to file 108
5.18 Arithmetic operations on arrays 112
5.19 Element-wise functions 114
5.20 Aggregation and localization functions 116
5.21 Methods for n ndarray 120
5.22 Application – temperature 121
5.23 Study questions 124
5.24 Exercises 125
6 Graphics and visualization 135
6.1 Matplotlib – pyplot 135
6.2 Example gallery 136
6.3 Create and save a figure 137
6.4 Basic plot methods 139
6.5 Axes and scaling 144
6.6 Text and legend 148
6.7 Arrows and explaining text 151
6.8 Polygons and filled areas 152
6.9 Histogram and bar charts 153
6.10 Plots of two-dimensional functions 157
6.11 Plot methods for 3D-graphics 158
6.12 Contour plots 162
6.13 Implicit functions 165
6.14 Matrices and images 167
6.15 Animations 170
6.16 Interaction with images 173
6.17 Graphics – general advice 175
6.18 Study questions 176
6.19 Exercises 177
7 Programming 185
7.1 Logical expressions 185
7.2 If-statements 188
7.3 While-loops 190
7.4 For-loops 193
7.5 Pre-allocation – element counter 197
7.6 Break commands and flags 198
7.7 Break-loops 199
7.8 Nested loops 201
7.9 Application – Madelung constant 202
7.10 Application – signal processing 204
7.11 Application – image processing 206
7.12 Application – heat flow 208
7.13 Study questions 209
7.14 Exercises 210
8 Program structure 225
8.1 Program, functions, and modules 225
8.2 Functions 226
8.3 Function calls 227
8.4 Functions – mutability 231
8.5 Functions – undefined local variables 232
8.6 Function names as input variables 233
8.7 Different number of input variables – keyword arguments 235
8.8 Functions collected in modules 237
8.9 The modules search path 238
8.10 Application – symmetries 239
8.11 Application – area on the map 242
8.12 Study questions 245
8.13 Exercises 246
9 Vectorization and efficiency 253
9.1 Measuring execution time 253
9.2 Vectorization 254
9.3 Vectorized operations under masks 256
9.4 Extracting indexes and counting 258
9.5 Application – vectorized heat flow 260
9.6 Application – digital image classification 261
9.7 Study questions 263
9.8 Exercises 264
10 Object-oriented programming 269
10.1 Classes 269
10.2 Protecting attributes – getters and setters 276
10.3 Special methods 281
10.4 Subclasses – inheritance 287
10.5 Application – functions with parameters 291
10.6 Study questions 293
10.7 Exercises 294
II Computer mathematics 297
11 Linear algebra 299
11.1 NumPy – linear algebra 299
11.2 Coordinate systems 300
11.3 Computation with vectors 302
11.4 Scalar product 304
11.5 Normalized vectors 305
11.6 Vectors in Python 305
11.7 Matrices 306
11.8 Matrix operations 307
11.9 Inverse matrix 310
11.10 Transpose and Hermitian transpose 312
11.11 Orthogonal and unitary matrices 314
11.12 Symmetric and Hermitian matrices 314
11.13 Determinants 315
11.14 Linear equation systems 317
11.15 Ill-conditioned systems 320
11.16 Eigenvalues and eigenvectors 323
11.17 Numerical method – Gaussian elimination 327
11.18 Numerical method – eigenvalues 330
11.19 Application – dynamical system 332
11.20 Application – free oscillations 337
11.21 Application – forced oscillations 341
11.22 Study questions 344
11.23 Exercises 345
12 Functions of one variable 353
12.1 Functions of one variable 353
12.2 Function graph 354
12.3 Continuity and zeros 355
12.4 Derivative 356
12.5 Derivatives of higher order 358
12.6 Taylor polynomials 359
12.7 Maximum and minimum 361
12.8 Grid – finite differences 362
12.9 Finite differences – method of undetermined coefficients 365
12.10 Integrals 368
12.11 The truncation error for the middlesum 370
12.12 Richardson extrapolation 371
12.13 Vector valued functions 375
12.14 Application – graphs of derivatives 377
12.15 Application – zeros 378
12.16 Study questions 380
12.17 Exercises 380
13 Functions of several variables 385
13.1 Functions of two variables 385
13.2 Domains in the plane 386
13.3 Function graph 387
13.4 Level curves 389
13.5 Continuity 390
13.6 Derivative 391
13.7 Gradient 392
13.8 Derivatives of higher order 394
13.9 Tangent plane and Taylor polynomials 395
13.10 Maximum and minimum 397
13.11 Grid – finite differences 399
13.12 Double integrals 401
13.13 Double integrals over general domains 403
13.14 Functions of n variables 405
13.15 Vector valued functions of n variables 413
13.16 Study questions 415
13.17 Exercises 416
14 Symbolic mathematics 421
14.1 SymPy 421
14.2 Print-out formats and interoperability 422
14.3 Computation with rational numbers 424
14.4 Computation with complex numbers 424
14.5 Symbolic variables 426
14.6 Algebraic manipulations – substitutions 427
14.7 Linear algebra 429
14.8 Functions of one variable 436
14.9 Functions of several variables 444
14.10 Study questions 448
14.11 Exercises 449
III Modeling and simulation 453
15 Introduction to modeling and simulation with SciPy 455
15.1 Modeling and simulation 455
15.2 Advantages with simulations 456
15.3 Different types of models and simulations 456
15.4 Visualizations 457
15.5 Challenges 458
15.6 SciPy 459
15.7 Import subpackages 460
15.8 Application – changing conditions 461
15.9 Application – epidemiology 462
15.10 Study questions 464
15.11 Exercises 465
16 Interpolation 467
16.1 Polynomials 467
16.2 Interpolation with polynomials 470
16.3 Interpolation with splines 475
16.4 Interpolation in two dimensions 477
16.5 Interpolation with periodic functions 482
16.6 Discrete Fourier transform 483
16.7 Nyquist frequency 485
16.8 Fast Fourier Transform (FFT) 486
16.9 Discrete Fourier transform of images 489
16.10 Study questions 495
16.11 Exercises 496
17 Non-linear equations 501
17.1 Non-lineare equations 501
17.2 Systems of non-lineare equations 502
17.3 Conditioning 504
17.4 Built-in functions in Python 504
17.5 Numerical methods 511
17.6 Application – Lagrange points 518
17.7 Study questions 520
17.8 Exercises 520
18 Optimization 527
18.1 Global and local minima 527
18.2 Univariate optimization 528
18.3 Multivariate optimization 530
18.4 Conditioning 532
18.5 Built-in functions in Python 532
18.6 Numerical methods – univariate minimization 541
18.7 Numerical methods – multivariate minimization 546
18.8 Application – reconstruction of surface 550
18.9 Study questions 553
18.10 Exercises 554
19 Modeling of data 559
19.1 Least squares fits 559
19.2 Linear least squares 560
19.3 Polynomial model functions 562
19.4 Linear model functions 564
19.5 Non-linear model functions 566
19.6 Multivariate model functions 570
19.7 Weighted least squares fits – outliers 573
19.8 Non-parametric models 576
19.9 Numerical method – Gauss-Newton 580
19.10 Application – video data 583
19.11 Application – breath of the Earth 585
19.12 Study questions 587
19.13 Exercises 588
20 Integrals 597
20.1 Interpretation of integrals 597
20.2 Curve length 598
20.3 Solids of revolution 599
20.4 Integrals in Python 600
20.5 Interpretation of multiple integrals 604
20.6 Double and triple integrals in Python 605
20.7 Numerical methods 608
20.8 Application – corrugated plates 617
20.9 Application – diffraction of a telescope 618
20.10 Application – gravitational pull 619
20.11 Study questions 621
20.12 Exercises 622
21 Differential equations 629
21.1 Ordinary differential equations 629
21.2 Systems of ordinary differential equations 630
21.3 Higher order differential equations 631
21.4 Initial value problems in Python 632
21.5 Initial value problems – step methods 638
21.6 Boundary value problems 643
21.7 Eigen value problems 647
21.8 Application – population dynamics 651
21.9 Application – wave functions for hydrogen 654
21.10 Study questions 656
21.11 Exercises 658
22 Monte Carlo methods 669
22.1 Initial example 669
22.2 Probability density functions 670
22.3 Random number generation 671
22.4 Measure of a domain 675
22.5 Multiple integrals 677
22.6 Application – Earth’s moment of inertia 679
22.7 Application – error propagation 681
22.8 Application – radiation transfer 683
22.9 Application – ideal gas in 2D 686
22.10 Study questions 689
22.11 Exercises 690
Appendix
A Installing anaconda 695
B Jupyter Notebook 697
B.1 Start Jupyter Notebook 697
B.2 Save Jupyter Notebook 701
B.3 Inserting images and links 702
B.4 Upload data files 702
B.5 Opening a Jupiter Notebook file 703
C Numeric data types 705
C.1 Built-in numeric data types in Python 705
C.2 Built-in numeric data types in NumPy 706
D Solutions to selected exercises 707
Index 711
Information
Språk:
EngelskaISBN:
9789144160467Utgivningsår:
2023Artikelnummer:
45430-01Upplaga:
FörstaSidantal:
725