Preface
The first few steps
What is a program? And what is programming?
A Python program with variables
The program
Dissection of the program
Why not just use a pocket calculator?
Why you must use a text editor to write programs
Installation of Python
Write and run your first program
A Python program with a library function
A Python program with vectorization and plotting
More basic concepts
Using Python interactively
Arithmetics, parentheses and rounding errors
Variables and objects
Integer division
Formatting text and numbers
Arrays
Plotting
Error messages and warnings
Input data
Symbolic computations
Concluding remarks
Exercises
Exercise 1: Error messages
Exercise 2: Volume of a cube
Exercise 3: Area and circumference of a circle
Exercise 4: Volumes of three cubes
Exercise 5: Average of integers
Exercise 6: Interactive computing of volume and area
Exercise 7: Peculiar results from division
Exercise 8: Update variable at command prompt
Exercise 9: Formatted print to screen
Exercise 10: Python documentation and random numbers
Basic constructions
If tests, colon and indentation
Functions
For loops
While loops
Lists and tuples - alternatives to arrays
Reading from and writing to files
Exercises
Exercise 11: Errors with colon, indent, etc.
Exercise 12: Compare integers a and b
Exercise 13: Functions for circumference and area of a circle
Exercise 14: Function for area of a rectangle
Exercise 15: Area of a polygon
Exercise 16: Average of integers
Exercise 17: While loop with errors
Exercise 18: Area of rectangle versus circle
Exercise 19: Find crossing points of two graphs
Exercise 20: Sort array with numbers
Exercise 21: Compute \( \pi \)
Exercise 22: Compute combinations of sets
Exercise 23: Frequency of random numbers
Exercise 24: Game 21
Exercise 25: Linear interpolation
Exercise 26: Test straight line requirement
Exercise 27: Fit straight line to data
Exercise 28: Fit sines to straight line
Exercise 29: Count occurrences of a string in a string
Computing integrals
Basic ideas of numerical integration
The composite trapezoidal rule
The general formula
Implementation
Making a module
Alternative flat special-purpose implementation
The composite midpoint method
The general formula
Implementation
Comparing the trapezoidal and the midpoint methods
Testing
Problems with brief testing procedures
Proper test procedures
Finite precision of floating-point numbers
Constructing unit tests and writing test functions
Vectorization
Measuring computational speed
Double and triple integrals
The midpoint rule for a double integral
The midpoint rule for a triple integral
Monte Carlo integration for complex-shaped domains
Exercises
Exercise 30: Hand calculations for the trapezoidal method
Exercise 31: Hand calculations for the midpoint method
Exercise 32: Compute a simple integral
Exercise 33: Hand-calculations with sine integrals
Exercise 34: Make test functions for the midpoint method
Exercise 35: Explore rounding errors with large numbers
Exercise 36: Write test functions for \( \int_0^4\sqrt{x}dx \)
Exercise 37: Rectangle methods
Exercise 38: Adaptive integration
Exercise 39: Integrating x raised to x
Exercise 40: Integrate products of sine functions
Exercise 41: Revisit fit of sines to a function
Exercise 42: Derive the trapezoidal rule for a double integral
Exercise 43: Compute the area of a triangle by Monte Carlo integration
Solving ordinary differential equations
Population growth
Derivation of the model
Numerical solution
Programming the Forward Euler scheme; the special case
Understanding the Forward Euler method
Programming the Forward Euler scheme; the general case
Making the population growth model more realistic
Verification: exact linear solution of the discrete equations
Spreading of diseases
Spreading of a flu
A Forward Euler method for the differential equation system
Programming the numerical method; the special case
Outbreak or not
Abstract problem and notation
Programming the numerical method; the general case
Time-restricted immunity
Incorporating vaccination
Discontinuous coefficients: a vaccination campaign
Oscillating one-dimensional systems
Derivation of a simple model
Numerical solution
Programming the numerical method; the special case
A magic fix of the numerical method
The 2nd-order Runge-Kutta method (or Heun's method)
Software for solving ODEs
The 4th-order Runge-Kutta method
More effects: damping, nonlinearity, and external forces
Illustration of linear damping
Illustration of linear damping with sinusoidal excitation
Spring-mass system with sliding friction
A finite difference method; undamped, linear case
A finite difference method; linear damping
Exercises
Exercise 44: Geometric construction of the Forward Euler method
Exercise 45: Make test functions for the Forward Euler method
Exercise 46: Implement and evaluate Heun's method
Exercise 47: Find an appropriate time step; logistic model
Exercise 48: Find an appropriate time step; SIR model
Exercise 49: Model an adaptive vaccination campaign
Exercise 50: Make a SIRV model with time-limited effect of vaccination
Exercise 51: Refactor a flat program
Exercise 52: Simulate oscillations by a general ODE solver
Exercise 53: Compute the energy in oscillations
Exercise 54: Use a Backward Euler scheme for population growth
Exercise 55: Use a Crank-Nicolson scheme for population growth
Exercise 56: Understand finite differences via Taylor series
Exercise 57: Use a Backward Euler scheme for oscillations
Exercise 58: Use Heun's method for the SIR model
Exercise 59: Use Odespy to solve a simple ODE
Exercise 60: Set up a Backward Euler scheme for oscillations
Exercise 61: Set up a Forward Euler scheme for nonlinear and damped oscillations
Exercise 62: Discretize an initial condition
Solving partial differential equations
Finite difference methods
Reduction of a PDE to a system of ODEs
Construction of a test problem with known discrete solution
Implementation: Forward Euler method
Application: heat conduction in a rod
Vectorization
Using Odespy to solve the system of ODEs
Implicit methods
Exercises
Exercise 63: Simulate a diffusion equation by hand
Exercise 64: Compute temperature variations in the ground
Exercise 65: Compare implicit methods
Exercise 66: Explore adaptive and implicit methods
Exercise 67: Investigate the \( \theta \) rule
Exercise 68: Compute the diffusion of a Gaussian peak
Exercise 69: Vectorize a function for computing the area of a polygon
Exercise 70: Explore symmetry
Exercise 71: Compute solutions as \( t\rightarrow\infty \)
Exercise 72: Solve a two-point boundary value problem
Solving nonlinear algebraic equations
Brute force methods
Brute force root finding
Brute force optimization
Model problem for algebraic equations
Newton's method
Deriving and implementing Newton's method
Making a more efficient and robust implementation
The secant method
The bisection method
Rate of convergence
Solving multiple nonlinear algebraic equations
Abstract notation
Taylor expansions for multi-variable functions
Newton's method
Implementation
Exercises
Exercise 73: Understand why Newton's method can fail
Exercise 74: See if the secant method fails
Exercise 75: Understand why the bisection method cannot fail
Exercise 76: Combine the bisection method with Newton's method
Exercise 77: Write a test function for Newton's method
Exercise 78: Solve nonlinear equation for a vibrating beam
Appendix: Getting access to Python
Required software
Anaconda and Spyder
Spyder on Mac
Installation of additional packages
How to write and run a Python program
The need for a text editor
Text editors
Terminal windows
Using a plain text editor and a terminal window
Spyder
The SageMathCloud and Wakari web services
Basic intro to SageMathCloud
Basic intro to Wakari
Installing your own Python packages
Writing IPython notebooks
A simple program in the notebook
Mixing text, mathematics, code, and graphics
References