January 21
Topics
Floating-point numbers.
- Introduction to the course.
- Sources of approximation in numerical simulations.
- Absolute and relative error.
- Data error and computational error.
- Floating Point numbers
- Truncation and Rounding errors.
- Examples of catastrophic cancellations
- Floating Point Arithmetic.
- Propagation of errors in floating point arithmetic.
Readings
Links
Code
- em.m, a simple example of counter-intuitive floating point result.
- discrderiv.m, an example of truncation and roundoff errors: dependence on h of the evaluation of f'(x) through (f(x+h)-f(x))/h.
- catCanc1.m, first simple example of catastrophic cancelation.
- catCanc2.m, second simple example of catastrophic cancelation.
- float.m, visualization of three digits floating point systems via MatLab.
Homework
Due date: Feb. 5th.
- Consider a Floating point system with base 10 and 3 digits mantissa (namely significant digits) and evaluate 3(4/3-1)-1 in that system.
- Do the same for single and double precision standard floating point numbers.
- Consider the problem of computing the Euclidean norm $\sqrt{a^2+b^2}$ of the vector $(a,b)$. What happens when $a$ and $b$ become "too big"? And what does it mean exactly "too big" in this context assuming to use 64bit floating point numbers? Suggest a way to re-write the formula so that the problem disappears. Write a program in MatLab or Python to evaluate this number in the "naif" way and in the way you suggest and verify that your formula work even when the first does not.
- When is the expression $\sqrt{x^4+1}-x^2$ subject to catastrophic cancellation? Find a way to re-write the expression that avoids this problem. Write a program in MatLab or Python to evaluate this expression and your own one in a case that shows catastrophic cancellation and verify that your one gives, instead, the correct result.
- Using either Python or MatLab or Octave (or all of the above!) do the following: define a variable $a$ to be equal to 2.6 and print it out to 15 digits after the dot. Then add to it 0.2 and print it again to verify the correctedness of the operation. After adding 0.2 to $a$ three times you will see the appearance of an error, explain why. What happend if, instead, you add directly 0.6 to 2.6? Why?
- Consider the quadratic equation $x^2+54.32x+0.1=0$ and evaluate its roots to 11 digits via the standard solution formula with your favorite language. Now, evaluate the same roots using a four digits decimal floating point (namely, after every operation you perform, truncate the result to the first four significant digits) and evaluate the relative error for the two roots in this approximation. Find the reason for the result you find. Suggest an alternate formula to use that produces better results.
- Write for the second derivative a code using the approximation $f''(x)\simeq(f(x+h)-2f(x)+f(x-h))/h^2$. Find theoretically for which order of magnitude in h the approximation
will be the best and compare your result with the numerical data obtained by your code.
January 28
Topics
Coding in MATLAB and Python.
- A first example of program on MATLAB: em.m
- Basics of the MATLAB language -- arrays, matrices, loops
- A second example of program on MATLAB: allocation.m
- Using MATLAB interactively from the CLI.
- Conditionals in MATLAB.
- Basics of the python language, cycles in python.
- Two examples of python coding:
em.py,
emd.py.
-
Plotting in MATLAB and Python.
-
Conditionals in Python. Example: the Eratosthene's Sieve in python (eratosthenes.py) and in matlab (eratosthenes.m).
Readings
MATLAB/Octave:
Python:
Links
Just look at the
languages page!
Problems
- Using either Python or MatLab or Octave (or all of the above!) do the following: define a variable $a$ to be equal to 2.6 and print it out to 15 digits after the dot. Then add to it 0.2 and print it again to verify the correctedness of the operation. After adding 0.2 to $a$ three times you will see the appearance of an error, explain why. What happend if, instead, you add directly 0.6 to 2.6? Why?
- Write code in MatLab or Python (or both!) to compute $r=p^2-2q^2$ with $p=665857$ and $q=470832$
in single and in double precision. Explain the reason for the unexpected result you get. Is any of those two numbers correct?
Remark: in MatLab you can force a variable to represent a single-precision floating point number using
the instruction single, e.g. p=single(665857).
In Python there is no such thing and you will have to call NumPy, e.g. with import numpy.
After that, you can use instrutions like p=numpy.float32(665857) to force a number to
be stored in 32 bits format.
- Repeat the previous exercise with $p=10864$, $q=18817$ and $r=9p^4-q^4+2q^2$.
- Repeat the previous exercise with $p=10^{34}$, $q=-2$ and $r=p+q-p$.
- Use Python to evaluate $e^{-5.5}$ using the Taylor series for $e^x$ in a floating point system
with base 10 and 2 significant digits. You will have to consider many terms before the sum stabilizes
but it will help you getting a good feeling of how a floating point system works.
Then repeat the calculation in case of $\frac{1}{e^{5.5}}$ and compare the two results with each other and
with the exact value.
Remark: an easy way to avoid doing these calculations completely by hand is using Python's
'decimal' package (many thanks to Bishwa
Silwal for pointing out this to me!).
Here is an example of how to use it:
>>> from decimal import * # Load the decimal package
>>> getcontext().prec = 2 # Set the number of significant digits to 2
>>> x=Decimal(-5.5) # Declare x as a 2-digits decimal fp and set it to -5.5
>>> x*x
Decimal('30')
>>> 1+x+x*x/2+x*x*x/6
Decimal('-17')
February 4
Topics
Embedding MATLAB/Python in a Web page.
Use
SageMathCell! You can embed both Octave/MATLAB and
Python code.
Coding in MATLAB and Python.
- Using MATLAB and Python to find prime numbers with
Eratosthenes' sieve:
-
Plotting in MATLAB and Python.
Elementary Methods to solve ODE's IVP.
- One step methods:
- Local and global error of the methods above.
Readings
MATLAB/Octave:
Python:
ODEs:
Links
February 11
Topics
- Writing Python and MATLAB code in class to solve ODEs
- Verifying numerically the order of convergence of the explicit Euler method
Readings
ODE slides
Links
Order and stability of explicit and implicit Euler methods
Problems
February 18
Topics
Elementary Methods to solve ODE's IVP.
-
Heun method (explicit trapezoidal rule):
- Local and global error of the Heun method.
- Methods for stiff ODEs:
- Rounding errors and Compensated sums
Readings
Links
Examples of Python code for the Heun method
Problems
February 25
Topics
Euler methods in $\Bbb R^n$.
- Explicit Euler
- Implicit Euler
- Symplectic Euler
March 3
Topics
Runge Kutta Methods.
- Midpoint method
- Trapezoidal method
- RK4
Variable step size.
March 10
Postponed to after the break
March 17
Spring break
March 24
Topics
Boundary Value Problems.
- Shooting Method
- Finite Difference Method
March 31
Topics
Boundary Value Problems.
- Shooting Method
- Finite Difference Method
April 7
Topics
Hyperbolic PDEs.
- The Advection PDE
Readings
- The shooting method for solving BVPs by T. Lakoba (U. of Vermont).
- The Finite Difference method for solving BVPs by G. Solderling (Lund University).
- Boundary Value Problems by J. Blanchard (U. of Winsconsin)
- A nice worked out example of the shooting method in python
- A nice worked out example of the shooting method in MATLAB
References
- Programming for Computations - A Gentle Introduction to Numerical Simulations with Python by H.P. Langtangen & S. Linge (Oslo U., Norway)
Code
- shooting-eEuler.py
- shooting-eEuler-improved.py
- finiteDifferences.py
April 14
Topics
Hyperbolic PDEs.
- The Advection PDE
- The Method of Characteristics
- The FTBS method
- The FTCS method
- The Lax method
Convergence, Stability and Consistence.
- The CFL Condition
- The Von Neumann Condition
- The Lax Theorem
Readings
- Solving the Advection PDE with Finite Differences in MATLAB (see Chap. 2 of the pdf) by A. Bergara (University of the Basque Country, Spain)
- The Advection PDE by S. Gurevich (U. of Muenster, Germany).
- The Wave PDE by S. Gurevich (U. of Muenster, Germany).
- Consistency, stability and convergence by R. Fedkiw (Stanford U.)
April 21
Topics
- Animations in MATLAB and Python
- Solving Parabolic and Elliptic PDEs with the Finite Differences Method
- Compiling python code with cython
- The theory behind the Finite Elements Method.
- Elementary implementation of the Finite Elements Method for ODEs.
Readings
- A nice discussion on the computational complexity of the Finite Differences method applied to the Heat equation by J. Demmel (Berkeley).
- A nice discussion on the computational complexity of the Finite Differences method applied to the Poisson equation by J. Demmel (Berkeley).
- From Finite Differences to Finite Elements by G. Solderling (Lund University).
- A Finite Elements primer, a theoretical introduction to the FEM by D. Silvester (U. of Manchester).
- Remarks around 50 lines of Matlab: short finite element implementation.
- Finite Elements Method.
April 28
Topics
- Elementary implementation of the Finite Elements Method for PDEs.
- Elementary Spectral Methods in PDE
Readings
- Finite Elements in 1D, an elementary and insightful implementation of the FEM for ODEs, by Niall Madden, NUI Galway (Ireland)
- Finite Elements in 2D, an elementary and insightful implementation of the FEM for Poisson PDEs, by Niall Madden, NUI Galway (Ireland)
- FEM, a detailed discussion on how to implement the FEM for the Poisson PDE by O. Mali (U. of Jyväskylä)
- FEM2, implementation in MATLAB of the algorithms discussed above by O. Mali (U. of Jyväskylä)
- Fourier Transforms and Fast Fourier Transforms
- Numerical solution of KdV
Code
The following files are FEM's MATLABB implementation by O. Mali -- I modified the main so that it accepts meshes produced by
distmesh.
- main.m
- basis_linear.m
- RefElemQuad.m
- AssembleGlobalMatrices.m
- fe2d_Madden.m
- fe2d_Persson.m