Numerical Methods in Physics Pat Scott Department of Physics, Imperial College November 8, 2016 Slides available from http://astro.ic.ac.uk/pscott/ course-webpage-numerical-methods-201617
Outline The problem 1 The problem 2 3
Outline The problem 1 The problem 2 3
Finite Sampling Real data are always sampled at discrete times, positions and energies (with uncertainties but that s another story) Theory is also often not practically calculable at whatever t, x and E we might like Might be expensive to compute Might rely on experimental input
Why interpolate? We may care about y at some x = t care not tabulated May need y as a continuous function of x Some other calculation depends on y at unknown x You actually need dy dx You need to calculate y(x)dx You just want a nice-looking plot for your paper/talk/mum
Requirements and Considerations The interpolating function must pass through all the points Otherwise we re doing curve fitting or data smoothing which is a whole lot shadier The function itself must be continuous (duh actually given by the above) But to how many orders? First derivative? More?
Issues of Locality Interpolation within the interval (x j, x j+1 ) may use just {y j, y j+1 }, or {y j 1, y j, y j+1, y j+2 }, or {y j 2, y j 1, y j, y j+1, y j+2, y j+3 }, etc Degree of locality of an interpolation scheme impacts continuity and stability Something must be non-continuous as we pass from using one set of points to another Less local allows for greater smoothness Less local also allows greater excursion (= insantity) Degree of interpolation scheme = number of pts used 1 Linear = degree 1, cubic = degree 3 (surprise)
Outline The problem 1 The problem 2 3
Linear interpolation the easy option Basically just connecting dots with straight lines = continuous function, no continuous derivatives Given by the formula with A x j+1 x x j+1 x j, (2) B 1 A = x x j x j+1 x j (3) y = Ay j + By j+1, (1)
Spline interpolation What if you need continuous derivatives as well? Spline interpolation: piecewise polynomials between each data point Degree n spline order n polynomial G n 1 continuous Cubic splines 4-point, 3rd degree scheme, cubic polynomials, G 2 contin. minimal way to get continuous first and second derivatives i.e. y varies linearly from x j to x j+1 = y = constant in intervals, y = 0 generated by adding corrections to linear formula: with y = Ay j + By j+1 + Cy j + Dy j+1, (4) C 1 6 (A3 A)(x j+1 x j ) 2 D 1 6 (B3 B)(x j+1 x j ) 2 (5)
Cubic Splines But what good is this?? y = Ay j + By j+1 + Cy j + Dy j+1, We don t have values of y tabulated how do we use this? Answer: We solve for y 1..n by other means (where n total num pts) How? Well, we still need to impose continuity of the first derivative...
Cubic Splines Using we get da dx = db dx = 1, (6) x j+1 x j dc dx = 1 3A2 (x j+1 x j ) 6 (7) dd dx = 3B2 1 (x j+1 x j ) 6 (8) dy dx = y j+1 y j 3A2 1 (x j+1 x j )y j + 3B2 1 (x j+1 x j )y j+1. (9) x j+1 x j 6 6
Cubic Splines Imposing the continuity of dy dx at x j (i.e. where intervals [x j 1, x j ] and [x j, x j+1 ] meet) dy dx x x j then gives the master equation x j x j 1 y 6 j 1 + x j+1 x j 1 3 valid for j = 2.. n 1 y j = dy dx x x + j (10) + x j+1 x j y j+1 = y j+1 y j y j y j 1 (11) 6 x j+1 x j x j x j 1 = system of n 2 equations in n unknowns (y 1..n) = 2D solution space need 2 more constraints to close the system
Boundary conditions Two additional constraints come from boundary conditions Need to specify either the first or second derivative at each endpoint y = 0 gives a Natural spline Other common choice is specifying first derivative at each boundary
Solution The problem x j x j 1 } {{ 6 } a j y j 1 + x j+1 x j 1 }{{ 3 } b j y j + x j+1 x j 6 } {{ } c j b 1 c 1 0 0 a 2 b 2 c 2. 0........... a j b j c j.......... 0 a n 1 b n 1 c n 1 0 0 a n b n y j+1 = y j+1 y j y j y j 1 x j+1 x j x j x j 1 }{{} F j y 1 y 2. y j. y n 1 y n = F 1 F 2. F j. F n 1 F n Endpoint equations together with master equation gives matrix equation M y = F = y = M 1 F (12) Tridiagonal system - can use tridiagonal matrix inversion tricks to do this Now we have y 1..n and can use Eq. 4 to evaluate y(x) for any x!
Outline The problem 1 The problem 2 3
Ringing - the bane of cubic splines y 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 True function Cubic spline 100 200 300 400 500 600 700 800 900 x
Ringing - the bane of cubic splines y 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 True function Cubic spline with 4 points Nodes in 4-point spline Cubic spline with 9 points Additional nodes in 9-point spline 100 200 300 400 500 600 700 800 900 x Can sometimes be solved by increasing samples Not always practical, doesn t work for many functions
Other random tips Be careful with functions possessing large higher derivatives Only need to call the setup once = important to highly optimise the evaluation, less so the setup Occasionally linear interpolation can actually be the best choice But often neither linear nor cubic splines are good enough...
Beyond basic cubic splines Higher dimensions - curves and surfaces Advanced interpolators - exponential splines, splines under tension, etc Related: curve design (Bezier curves, B splines, etc) A few (open-source) packages I like: TSPACK - 1D splines with auto tension and nice shape-preserving properties http://portal.acm.org/citation.cfm?id=151277 TENSBS - simple 2D and 3D cubic splines http://gams.nist.gov/serve.cgi/packagemodules/cmlib FITPACK - 2D splines with manual tension http://www.netlib.org/fitpack/
Housekeeping Assignment 2 now available on the homepage Covers cubic spline interpolation Tutorial/help session: 11am Fri Nov 11 Due in: Monday Nov 14 Next lecture: Random numbers (Tues Nov 15)