Notes

Notes, etc. of Samuel Flint

Numerical Analysis

Lecture 0: Introduction

  • Material delivered by both zoom and online.

  • Fridays are lab assignments, homework also due. May have midterms

  • Final project exists, will be described later, due last week of class

Lecture 1: Mathematical Preliminaries & Error Analysis

  • Found in Chapter 1 of text book

  • Numerical analysis – study of algorithms used for numerical approx. for problems of mathematical analysis

  • Mathematical analysis – limits, differentiation, integration, infinite series, etc

  • Approximation is particularly important, and numbers represented as 32(64) bits, a sign, exponent, mantissa

    • This approximation is innaccurate for many numbers

  • Representations of numbers can be inaccurate – there may be round-off error

    • Numbers between two representations may only be approximated!

    • Round – use the nearest representable value

    • Chop – use the representable part of the value

  • Function approximation by series

    • Some functions are representable by series, $e^{x}$, $\sin x$, etc.

    • These may be represented as Taylor polynomials, which are rather generally useful.

Lecture 2: The Method of Bisection

  • Basic method in Numerical Analysis

  • Builds on Intermediate value theorem

    • Given function continuous from $[a, b]$, and $K$ in $[a, b]$, then there exists $c$ such that $f(c) = K$

    • When $f(a)$ and $f(b)$ have opposite signs, then there exists $p$ between $(a,b)$, $f(p) = 0$, that is, it has a root in the interval

  • Repeated Application of above

    • Assume continuity, and $f(a)$, $f(b)$ have opposite signs

    • f, a, b

    • $p = (a + b)/2$

    • if stopping criteria: return $p$

    • If $f(a)$ and $f(p)$ have opposite signs, then recurse with $f, a, p$

    • Else, recurse with $f, p, b$

  • Tolerance Criteria

    • $TOL$ is error tolerance value

    • Stop when one of the following:

    • $|p_{n} - p_{n - 1}| < TOL$ (absolutely tiny difference)

    • $|p_{n} - p_{n - 1}|/|p_{n}| < TOL$ (normalized difference)

    • $|f(p_{n})| < TOL$ (very close to zero)

  • Characteristics of the Method

    • Disadvantages

      • Slow to converge, as N may become large before $p_{n} - p_{n - 1}$ becomes small

      • Possible that intermediate approx may be inadvertently discarded!

  • Can also find fixed points

    • Let $g(x)$ be a function, $p$ is a fixed point if $g(p) = p$

    • Transformation requires finding root $f(x) = g(x) - x$

Lecture 3: Root Finding

Newton's Method

  • Recall Taylor Polynomials

  • The first Taylor Polynomial of $f(x)$ for $x = p$ around $p_{0}$ can be used to approximate roots.

  • $\zeta(p)$ is a value between $p$ and $p_{0}$

  • Because of the tailor polynomial, $p = p_{0} - \frac{f(p_{0})}{f^{{\prime}}(p_{0})}$ is better than $p_{0}$

  • This can be iterated to find a reasonable approximation!

  • Very quick!

  • But, has problems

    • Fails when derivative at any given point is 0!

Secant Method

  • Because Newton's method requires derivatives, the secant method estimates this

  • It's done using a simple quotient.

  • See Textbook for description

Combining Methods

  • Often Bisection & Newton's methods are combined.

  • Bisection is used to get a reasonable estimate of the root

  • And then Newton's method is used to take over, and refines this with incredible accuracy

Lecture 4: Convergence

Bisection Method

  • Have seen this before

  • Guarantees convergence

  • Can determine number of iterations required

  • Must always check sign change

  • Convergence can be calculated by using two similar functions

Linear vs quadratic Convergence

  • Linear convergence if $\alpha = 1$, quadratic if $\alpha = 2$ \[\left\vert p - p_{{n + 1}} \right\vert \leq c\left\vert p - p_{n} \right\vert^{{\alpha}}\]

  • Quadratic convergence implies exponential error reduction

  • Suppose that $p_{n}$ approximates $p$ with a small difference, then from quadratic difference, we can see speed.

  • Secant method converges quickly, as shown by limit

  • Newton's method, assuming continuity in neighborhood of root, and derivative isn't zero, then there's a positive $\delta$ if the initial point satisfies $|p - p_{0}| \leq \delta$, then all points converge quadratically

    • Call $e_{n} = p - p_{n}$

      • $e_{{n + 1}} = p - p_{n+1 }= e_{n} + f(p_{n})/f^{\prime}(p_{n})$

      • Note, $f(p)$ can be expanded using Taylor's theorem

      • $f(p) = f(p_{n} + e_{n}) = f(p_{n}) + e_{n}f^{\prime}(p_{n}) + 1/2 e^{2}_{n} f^{\prime\prime}(\zeta_{n}) = 0$

      • Or $e_{n + 1} = -1/2 (f^{\prime\prime}(\zeta_{n})/f^{\prime}(x_{n})) e^{2}_{n}$

  • Sometimes, however, Newton's method doesn't converge

    • Flat spots (derivative = 0)

    • Can run away from the root

    • Or may cycle because of the initial estimate!

      • Use bisection on the interval with very high error tolerance

Lecture 5: Lagrange Polynomials

  • Interpolation, used for estimation and prediction

    • Census data, pretty well known, overall population of US

    • But 10-year cycle, what about 1975, 2005, 2015?

    • Requires interpolation

    • This estimates/predicts, by finding a function that fits the data

  • Can be done in higher dimensions!

    • Interpolating from, for instance, a set of elevation points, provides a smoother, more realistic curve

Weierstrass Approximation Theorem

  • 19th Century

  • Suppose $f$ is defined and continuous on $[a, b]$, for each $\epsilon > 0$, there exists a polynomial such that $|f(x) - P(x)| < \epsilon$ for all $x$ in $[a, b]$

Lagrange Polynomial Interpolation

  • Two-point case

    • Find polynomial that passes through $(x_{0}, y_{0})$ and $(x_{1}, y_{1})$ – $y = ax + b$. Thus $y_{0} = ax_{0} + b$ and $y_{1} = ax_{1 }+ b$

      • $y = \frac{y_{0} - y_{1}}{x_{0} + x_{1}}x + \frac{x_{0} y_{1} - x_{1} y_{0}}{x_{0} - x_{1}}$

      • Call this $L_{0}(x) = \frac{x - x_{1}}{x_{0} - x_{1}}$, $L_{1}(x) = \frac{x - x_{0}}{x_{1} - x_{0}}$, $L_{i} = \begin{cases} 1 & x = x_{i} \\ 0 & \text{otherwise}\end{cases}$

      • Then, $P(x) = L_{0}(x)y_{0} + L_{1}(x)y_{1}$

      • Generalizes well

  • The General Case!

    • $P_{n}(x) = \sum_{{i=0}}^{{n}}L_{i}(x)f(x_{i})$, and $f(x_{i}) = y_{i}$

    • $L_{i}(x) = \prod_{{j=0,j \neq i}}^{{n}}\left(\frac{x - x_{j}}{x_{i} - x_{j}}\right)$

    • A polynomial of degree $n$

  • But why not use Taylor Polynomials?

    • Expansions are gross, really long

Lecture 6: Neville's Method & Newton's Divided Difference Method

  • Other interpolation methods

Neville's Method

  • British Mathematician, early half of 20th century

  • Given $n + 1$ distinct points and values, evaluate polynomial at certain points $x \neq x_{i}$

  • Polynomial built in steps based on: \[ p_{0,\ldots, n} = \left(\frac{x - x_{J}}{x_{i} - x_{j}}\right)p_{0\ldots, j-1, j+1, \ldots, n} + \ldots (i) \]

  • Ordered from $u$ to $v$

  • More general, clean way to create what are effectively Lagrange polynomials

  • Can you get $f(x)$ without $p(x)$

  • Iterated algorithm surprisingly simple.

    • Initialize tableau to the start

    • Then, from j from 1 to n,

      • i from j to n

        • fill in tableau at i,j with $\frac{x - x_{i-j} Q_{i,j-1}}{x_{i} - x_{i-j}} + \frac{xi-x}{x?}$… (fill in later)

Newton's Interpolating Polynomial

  • $p_{k}$ comes from $p_{{k - 1}}$ and is extensible by adding another term without altering coefficients already existing

  • This uses divided difference, and the previously computed values

  • This is used to form $a_{i}$, built from prior items

  • Divided differences are used to quickly build the polynomial

  • Simple pattern, overall

  • Recursive definition is fairly simple

  • Note: Divided difference is invariant under all permutations

  • Can, presumably, be converted to a tableau method.

  • Computational Complexity

    • $\mathcal{O}(k^{2})$

  • Lagrange & Newton

    • Equivalent, but different method of calculation

Lecture 7: Matlab

  • Matlab!

  • Equations easily defined

  • Each script is separate function (gross)

Lecture 7: Interpolation, Continued

  • High-degree polynomials fail

    • Sometimes data doesn't fit polynomials!

    • Runge's Phenomenon – Edges of interval oscilated at polynomial interpolation with high degree

  • Splines used – spline function consists of polynomial pieces joined with smoothness conditions

  • Piecewise, works for $t_{0}$ to $t_{1}$, $t_{1}$ to $t_{2}$, \ldots, $t_{{n - 1}}$ to $t_{n}$

    • $t$ are called "knots", ordered

    • $\mathcal{S}$ is spline of degree $k$ if

      • each polynomial is degree $k$

      • and is $k - 1$ times continuously differentiable

      • Note, end points must match

      • k = 1, linear

      • k = 2, quadratic

      • k = 3, cubic (most used)

  • Quadratic:

    • Must find a, b, c

    • Can be tough, given matching condition

    • Boundary equation can also be added, $Q_{0}^{{\prime}}(t_{0}) = 0$

    • Recurrence equation:

      • $z_{0} = 0$

      • $z_{{i + 1}} = -z_{i} + 2\left(\frac{y_{i + 1} - y_{i}}{t_{i+1} - t_{i}}$

      • $Q_{i}(x) = \frac{z_{{i + 1}} - z_{i}}{2(t_{i + 1} - z_{i})} (x - t_{i})^{2} + z_{i}(x - t_{i}) + y_{i}$

Lecture 8: Cubic Spline Interpolation

  • Most complex yet, but frequently used

  • Given $n$ data points $[(x_{i}, a_{i})]$, find $b_{j}$, $c_{j}$, $d_{j}$ coefficients for $S_{j}(x)$ for $0 \leq j \leq n - 1$

    • Find intervals matching at knots, and assuming normal boundary conditions

    • Then, $h_{j}$ is the difference between two knots, $x_{j + 1 }- x_{j}$

      • Then $h_{j-1 }c_{{j - 1}} + 2(h_{{j-1}} + h_{j})c_{j} + h_{j} c_{j+1} = \frac{3}{h_{j}}(a_{j+1} - a_{j}) - \frac{3}{h_{j-1}}(a_{j} - a_{j-1})$

      • $b_{j} = \frac{1}{h_{j}}(a_{{j+1}} - a_{j}) - \frac{h_{j}}{3}(2c_{j} + c_{j+1})$

      • $d_{j} = \frac{1}{3h_{j}}(c_{{j + 1}} - c_{j})$

      • $S_{j}(x) = a_{j} + b_{j}(x - x_{j}) + c_{j} (x - x_{j})^{2} + d_{j} (x - x_{j})^{3}\ldots$

      • Can be represented as matrix, $A$ of $h$ values, $b$ as vector, and $c$ as vector

      • $\mathbf{b}$ is vector of constants (from $b_{j}$), $\mathbf{x}$ is $c$

      • Solve $A\mathbf{x} = \mathbf{b}$

  • Not particularly difficult – matrix solution possible, trivial even

Lecture 9: More Cubic Spline Interpolation

  • Cubic Spline most frequently used for single-variable functions

  • Solved using diagonal/triangular matrix-vector equation

  • $r$ cubic spline interpolation based on recurrence equations

  • $f(t)$ from $\mathbb{R} \rightarrow \mathbb{R}$

  • $S_i(t) = a_i + b_i(t - t_i) + c_i(t - t_i)^2 + d_i(t - t^i)^3$

    • $a$ are knots

    • Smoothness conditions, require shared knots

    • first derivative sharing

    • second derivative sharing

  • Equations should show this

  • $h_t = t_{{i + 1}}_{} - t_{i}$

    • $a_{i} + b_{i} h_{i} + c_{i }h_{i}^{2} + d_{i} h_{i}^{3} = a_{{i + 1}}$

    • Finally, we can solve for d, b, a

    • Can follow on and continue to solve

    • Can eventually get equations only in a, h, i (15)

  • Then, Can be found using matrix equations, and recurrence equation form can be used

  • Note, assumes equal spacing

  • Consider fairly flexible

    • Normally natural cubic spline interpolation

Lecture 10: Spatial interpolation: higher-dimensional interpolation methods

  • We'll create triangles for our sample points, using known algorithm

  • Gets a triangulated irregular network

  • Will use a number of triangles in other triangles

  • Coefficients used are shape functions, half the determinant of a matrix

Lecture 11: Spatial Interpolation

  • Project all points to plane

  • Triangulate all points, i.e., connect all points to create triangles

  • Then, use triangulation, extend to triangulated irregular network, add back dimension in Z

  • Elements of TIN are a set of functions w, which divide area of triangle

    • $w(x, y) = N_{1}(x,y)w_{1} + N_{2}(x, y)w_{2} + N_{3}(x, y)w_{3}$

    • N are based on various things, including $\mathcal{A} = \frac{1}{2} \mathrm{det}\begin{bmatrix}1 & x_{1} & y_{1} \\1 & x_{2} & y_{2} \\1 & x_{3} & y_{3}\end{bmatrix}$

  • Interpolation function must be calculated carefully

  • This presents the finite element method

    • Triangulations and shape functions are significant part of method

    • Interpolation provides a method of integrating approximations of complex functions that can't be directly integrated

  • Interpolation in Matlab

    • scatteredInterpolant uses the same kind of methods, specify as 'linear'

    • Takes x, y, z

    • Can generate a grid, and then visualise using mesh

Lecture 12: Review & Exam Prep

  • Test Overview

    • Root finding

      • Bisection, Newton's, Convergence

    • Single-variable interpolation

      • Lagrange

      • Divided Differences

      • Piecewise Interpolation: Linear, Quadratic, Cubic Spline

    • No spatial interpolation, no Matlab

  • Use textbook for quadratic/cubic spline interpolation

Lecture 13: Numerical Differentiation

  • Approximating derivatives using polynomial approximating the function

  • Limit Theorem ($f^{{\prime}}(x_{0}) = \lim_{{h \to 0}}\frac{f(x_{0} + h) - f(x_{0})}{h}$

    • $h$ should be as small as possible for achiving good accuracy

    • Can have issues like subtraction of numbers with nearly equal values

    • Errors can be amplified (division with tiny denominator)

    • Just use it by dividing by $h$

  • Taylor's theorem gives us a definition of $f(x + h)$

    • Involves an error term, $\zeta$ ($f^{\prime}(x) = \frac{1}{h}(f(x + h) - f(x)) - \frac{1}{2!}hf^{\prime\prime}(\zeta)$, $\zeta \in [x, x + h]$)

    • Looking backward adds error term, rather than subtract, linear function of $h$

  • Interpolation error theorem

    • Generally, for an interpolated function, a zeta function is given.

  • Three-point formulas used instead:

    • With an error term, long, complex equations

    • Uniform sampling, means $h$ is known

    • These have simple error terms, making defining derivatives simple

Lecture 14: Numerical Integration

  • Indefinite integral is class of functions $F$

  • Definite integral over fixed integral is number

  • F can be hard to get symbolically

  • Lower & Upper sums

  • Lower sum based on lower end of rectangles

    • Uses largest lower bound, smallest width

  • Upper sum based on upper end of rectangles

    • Uses smallest upper bound, small widths

  • Are both incorrect estimates

  • Goal is for something to be Reimann-integrable

  • Richardson's Extrapolation gives a formula for approximating M for any $h$

    • Even powers of $h$ give better extrapolation results

Lecture 15: Numerical Integration

Trapezoid Rule

  • Upper/lower bounds by rectangles

  • Uses a linear spline, and area under the trapezoid

  • LaGrange polynomial can be used

  • Integral can be directly taken

    • $\frac{1}{2}(x_{1} - x_{0})(x_{0} - x_{1})$

  • Then, we can sum over these

  • Precision requires second deravitive, and composite trapezoide, then there's $h^{2}$ error

    • Large interval (linear)

    • Fewer trapezoids (quadratic)

    • Curvature

  • To find number of subintervals given an error

    • $\epsilon = \frac{b - a}{12}h^{2} f^{{\prime\prime}}(\xi)$

    • $h^{2} = (\frac{1}{n})^{2}$

    • Then set up inequality with error max

Simpson's Rule

  • For higher-order polynomials

  • Second Lagrange polynomial

  • $h/3 (f(a) + 4f(a + h) + f(a + 2h))$ (from $a$ to $a + 2h$

  • Error theorem exists, uses third derivative

Lecture 16: Simpson's Rule, Continued

  • Estimating polynomial written in lagrange form, using 3 points (see above)

  • Finds best-fit quadratic polynomial

  • Composite form of rule:

    • Requires even integrals

    • $S(f; P) = \frac{h}{3}(f(a) + f(b) 4\sum_{{k = 1}}^{{n/2}} f(a + (2k - 1)h) + 2\sum_{k = 1}^{(n - 2)/2} f^{{(4)}}(a + 2kh))$

    • Error: $S - \frac{1}{180}(b - a)h^{4}f^{(4)}(\zeta)$

  • 3/8 rule, from cubic spline interpolation, 4 point closed rule (slide 5)

    • $(3/8)h(f(x0) + 3f(x1) + 3f(x2) + f(x3)) - (3/80)h^{5} f^{(4)}(\zeta)$

  • Boole's Rule

    • Much more complex, $h^7$ error

    • Similarly, Newton-Cotes rule has about the same error

Lecture 17: Romberg's Method

  • Romberg's Integration

  • Given a new $T$ function, trapezoid rule across the whole

Lecture 18: Monte Carlo integration

  • Random sampling

  • Monte Carlo, Monaco is the source of the name

  • Computational methods require pseudo-random number generators

  • Monte Carlo method for finding value of Pi

    • shoot a thousand darts at a board with a circle, of unit size, and divide number in over total, and multiply by 4

  • Can be used easily for integration

Lecture 19: Systems of Linear Equations

  • Current provided by battery

    • Can be solved with simple set of linear equations…

    • Can be solved using, for instance, Gaussian elimination

  • Can be over or under-determined

  • Can also be ill conditioned

  • Only going to consider linear equations

  • Na\"ive Gaussian Elimination is possible, but potentially difficult

  • $Ax = B$ also gives $x = A^{{-1}} b$

    • Inverse must be square, and $det(A) \neq 0$ ($A$ is full ranked)

    • Least Squares method gives $\hat{x} = (A^{T} A)^{{-1}} A^{T}b$

    • Must not have redundant rows

Lecture 20: Elimination, Continued

  • Scaled Partial Pivoting

    • Involves scales of $|a_{n1}|/|l_{n}|$

    • Row with largest scale value used for first elimination

    • Then from second column against all, with same maximum value

    • Residual Vector $R = A\hat{x} - b$

Lecture 21: Condition Numbers

  • Indicates how well-conditioned/ill-conditioned a matrix is

  • Ill conditioned matrices are harder to handle and give less precise results

  • Find chacateristic polynomial, and then from this eigenvalues.

  • Condition number $\kappa$ is $\frac{|\lambda_{{max}}|}{|\lambda_{{min}}|}$ (assuming matrix is normal)

    • Normally, however, $\|A\|_{2} \cdot \|A^{-1}\|_{2}$

  • Norm is the $\sqrt{\rho(A^{T} A)}$

    • $\rho$ function returns lambda max

Lecture 22: Piecewise Linear Approximation

  • Polynomial interpolation can have some problems

  • Piecewise linear approximations can have many points, difficult to evaluate

  • Can instead remove some points such that upper/lower bounds are satisfied

  • Fairly simple with time series and error threshold, start, min, max given.

    • For loop finding max and min of slope.

    • Add lines, given slopes, etc.

Lecture 23: Least Squares Analysis

  • Hooke's Law

  • Used when there's a linear relationship

  • Hooke's law derived from least-squares approximation

  • Columns for $x$ and $y$

    • Appears to be almost a line, but not quite

    • Best fit approximating all points has several definitions, minimum occurs when partial derivatives are 0

    • Square is used for error

    • Linear Approximation is fairly simple

    • Matlab calculation

Lecture 24: Review for Midterm 2

  • Three Point Formulas

  • Upper/Lower bounds

  • Trapezoidal Rule

  • Simpson's Rule

  • Composite Trapezoidal, Simpson's

  • Error Estimation – intervals bounding the error

  • Systems of Linear Equations

  • Gaussian Elimination, Na\"ive & Partial Pivoting

  • Residual Vector

  • Condition Number

  • Include error terms for composite rules