Programming for Computations (Python version)¶
Contents:
- Programming for Computations (Python version)
- Preface
- The first few steps
- What is a program? And what is programming?
- A Python program with variables
- A Python program with a library function
- A Python program with vectorization and plotting
- More basic concepts
- Exercises
- Exercise 1.1: Error messages
- Exercise 1.2: Volume of a cube
- Exercise 1.3: Area and circumference of a circle
- Exercise 1.4: Volumes of three cubes
- Exercise 1.5: Average of integers
- Exercise 1.6: Interactive computing of volume and area
- Exercise 1.7: Peculiar results from division
- Exercise 1.8: Update variable at command prompt
- Exercise 1.9: Formatted print to screen
- Exercise 1.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 2.1: Errors with colon, indent, etc.
- Exercise 2.2: Compare integers a and b
- Exercise 2.3: Functions for circumference and area of a circle
- Exercise 2.4: Function for area of a rectangle
- Exercise 2.5: Area of a polygon
- Exercise 2.6: Average of integers
- Exercise 2.7: While loop with errors
- Exercise 2.8: Area of rectangle versus circle
- Exercise 2.9: Find crossing points of two graphs
- Exercise 2.10: Sort array with numbers
- Exercise 2.11: Compute \(\pi\)
- Exercise 2.12: Compute combinations of sets
- Exercise 2.13: Frequency of random numbers
- Exercise 2.14: Game 21
- Exercise 2.15: Linear interpolation
- Exercise 2.16: Test straight line requirement
- Exercise 2.17: Fit straight line to data
- Exercise 2.18: Fit sines to straight line
- Exercise 2.19: Count occurrences of a string in a string
- Computing integrals
- Basic ideas of numerical integration
- The composite trapezoidal rule
- The composite midpoint method
- Testing
- Vectorization
- Measuring computational speed
- Double and triple integrals
- Derivation via one-dimensional integrals
- Direct derivation
- Programming a double sum
- Reusing code for one-dimensional integrals
- Verification via test functions
- Theory
- Implementation
- The Monte Carlo integration algorithm
- Implementation
- Verification
- Test function for function with random numbers
- Integral over a circle
- Exercises
- 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 4.1: Geometric construction of the Forward Euler method
- Exercise 4.2: Make test functions for the Forward Euler method
- Exercise 4.3: Implement and evaluate Heun’s method
- Exercise 4.4: Find an appropriate time step; logistic model
- Exercise 4.5: Find an appropriate time step; SIR model
- Exercise 4.6: Model an adaptive vaccination campaign
- Exercise 4.7: Make a SIRV model with time-limited effect of vaccination
- Exercise 4.8: Refactor a flat program
- Exercise 4.9: Simulate oscillations by a general ODE solver
- Exercise 4.10: Compute the energy in oscillations
- Exercise 4.11: Use a Backward Euler scheme for population growth
- Exercise 4.12: Use a Crank-Nicolson scheme for population growth
- Exercise 4.13: Understand finite differences via Taylor series
- Exercise 4.14: Use a Backward Euler scheme for oscillations
- Exercise 4.15: Use Heun’s method for the SIR model
- Exercise 4.16: Use Odespy to solve a simple ODE
- Exercise 4.17: Set up a Backward Euler scheme for oscillations
- Exercise 4.18: Set up a Forward Euler scheme for nonlinear and damped oscillations
- Exercise 4.19: Discretize an initial condition
- Population growth
- Solving partial differential equations
- Finite difference methods
- Exercises
- Exercise 5.1: Simulate a diffusion equation by hand
- Exercise 5.2: Compute temperature variations in the ground
- Exercise 5.3: Compare implicit methods
- Exercise 5.4: Explore adaptive and implicit methods
- Exercise 5.5: Investigate the \(\theta\) rule
- Exercise 5.6: Compute the diffusion of a Gaussian peak
- Exercise 5.7: Vectorize a function for computing the area of a polygon
- Exercise 5.8: Explore symmetry
- Exercise 5.9: Compute solutions as \(t\rightarrow\infty\)
- Exercise 5.10: Solve a two-point boundary value problem
- Solving nonlinear algebraic equations
- Brute force methods
- Newton’s method
- The secant method
- The bisection method
- Rate of convergence
- Solving multiple nonlinear algebraic equations
- Exercises
- Exercise 6.1: Understand why Newton’s method can fail
- Exercise 6.2: See if the secant method fails
- Exercise 6.3: Understand why the bisection method cannot fail
- Exercise 6.4: Combine the bisection method with Newton’s method
- Exercise 6.5: Write a test function for Newton’s method
- Exercise 6.6: Solve nonlinear equation for a vibrating beam
- Appendix: Getting access to Python
- References