#!/usr/bin/env python # coding: utf-8 # # Numerical Methods 1 # ### [Gerard Gorman](http://www.imperial.ac.uk/people/g.gorman), [Matthew Piggott](http://www.imperial.ac.uk/people/m.d.piggott), [Nicolas Barral](http://www.imperial.ac.uk/people/n.barral) # # Revision exercises # ## Question 1 # # Below is a extract of shot data from a seismic survey. # # | Time (ms) | Shot | # |-----|-----| # | 0 | -0.021373 | # | 4 | -0.024578 | # | 8 | -0.023914 | # | 12 | -0.018227 | # | 16 | -0.00781 | # | 20 | 0.005602 | # | 24 | 0.019264 | # | 28 | 0.030235 | # | 32 | 0.036059 | # | 36 | 0.035334 | # # 1. Calculate the Lagrange polynomial for these points. Plot both the Lagrange polynomial and the raw data points. # 2. The full shot is available in the file [shot.txt](data/shot.txt) (in the data folder) - where the sample interval is 4ms as above. Note that the file only contains one column as you can calculate the time column yourself. Use cubic-polynomial splines to re-interpolate the data for a sample interval of 7.07ms. Plot both the original shot data and the interpolated time series. # ## Question 2 # # 1. Calculate the upper triangular form for the matrix: # \begin{align*} # A = \begin{bmatrix} # −5 & 3 & 4\\ # 10 & −8 & −9\\ # 15 & 1 & 2 # \end{bmatrix} # \end{align*} # # 2. Consider the matrix: # \begin{align*} # C = \begin{bmatrix} # −5 & 3 & 4\\ # 10 & −8 & −9\\ # 15 & 1 & 2 # \end{bmatrix} # \end{align*} # Does matrix $C$ have an inverse? If not, then why not? # ## Question 3 # # Consider the function # $$f(x) = \dfrac{1}{(x − 0.3)^2 + 0.01} - \dfrac{1}{(x − 0.8)^2 + 0.04}$$. # # # 1. Write a function that computes the second derivative of $f(x)$ using central differencing. The interface to your function should look like *central_diff2(f, x, h)* where $f$ is the function to be differentiated, $x$ is the position at which the derivative should be estimated, and $h (= \Delta x)$ is the step size. # 2. Use this function to compute the derivative at $x = 0.5$ for decreasing values of $\Delta x$. Start with $\Delta x=1.2$ and keep halving until the relative difference between solutions falls below $1.0^{-6}$ Plot the convergence of the method, i.e. plot $\Delta x$ against the absolute difference between the analytical value and the finite difference approximation. # ## Question 4 # # Consider the integral: # $$\int_0^{2\pi} x^2\cos(x) dx.$$ # # Show how the absolute error between the exact solution and the numerical solution varies with the size of the integration step for the four numerical integration methods: trapezoid rule; Simpson's rule; composite Simpson's rule; and Weddle's rule. Show your result by plotting error against integration step, $dx$.