Example 3.1.1

Here are 5-year averages of the worldwide temperature anomaly as compared to the 1951-1980 average (source: NASA).

A polynomial interpolant can be used to fit the data. Here we build one using a Vandermonde matrix. First, though, we express time as decades since 1950, as it improves the condition number of the matrix.

As you can see, the interpolant does represent the data, in a sense. However it's a crazy-looking curve for the application. Trying too hard to reproduce all the data exactly is known as overfitting.

Example 3.1.2

Here are the 5-year temperature averages again.

The standard best-fit line results from using a linear polynomial that meets the least squares criterion.

If we use a global cubic polynomial, the points are fit more closely.

If we were to continue increasing the degree of the polynomial, the residual at the data points would get smaller, but overfitting would increase.

Example 3.1.3

Finding numerical approximations to $\pi$ has fascinated people for millenia. One famous formula is

$$ \frac{\pi^2}{6} = 1 + \frac{1}{2^2} + \frac{1}{3^2} + \cdots. $$

Say $s_k$ is the sum of the first terms of the series above, and $p_k = \sqrt{6s_k}$. Here is a fancy way to compute these sequences in a compact code.

This graph suggests that $p_k\to \pi$ but doesn't give much information about the rate of convergence. Let $\epsilon_k=|\pi-p_k|$ be the sequence of errors. By plotting the error sequence on a log-log scale, we can see a nearly linear relationship.

This suggests a power-law relationship where $\epsilon_k\approx a k^b$, or $\log \epsilon_k \approx b (\log k) + \log a$.

In terms of the parameters $a$ and $b$ used above, we have

It's tempting to conjecture that $b\to -1$ asymptotically. Here is how the numerical fit compares to the original convergence curve.

Example 3.2.1

Because the functions $\sin^2(t)$, $\cos^2(t)$, and $1$ are linearly dependent, we should find that the following matrix is somewhat ill-conditioned.

Now we set up an artificial linear least squares problem with a known exact solution that actually makes the residual zero.

Using backslash to find the solution, we get a relative error that is about $\kappa$ times machine epsilon.

If we formulate and solve via the normal equations, we get a much larger relative error. With $\kappa^2\approx 10^{14}$, we may not be left with more than about 2 accurate digits.

Example 3.3.1

Julia provides access to both the thin and full forms of the QR factorization.

Here is a standard call:

If you look carefully, you see that we got a full $Q$ but a thin $R$. Moreover, the $Q$ above is not a standard matrix type. If you convert it to a true matrix, then it reverts to the thin form.

We can test that $\mathbf{Q}$ is orthogonal.

The thin $Q$ cannot be an orthogonal matrix, because it is not even square, but it is still ONC.

Example 3.4.1

We will use Householder reflections to produce a QR factorization of the matrix

Our first step is to introduce zeros below the diagonal in column 1. Define the vector

Applying the Householder definitions gives us

(Julia automatically fills in an identity of the correct size for the I above.) By design we can use the reflector to get the zero structure we seek:

Now we let

We are set to put zeros into column 2. We must not use row 1 in any way, lest it destroy the zeros we just introduced. To put it another way, we can repeat the process we just did on the smaller submatrix

We now apply the reflector to the submatrix.

We need two more iterations of this process.

We have now reduced the original to an upper triangular matrix using four orthogonal Householder reflections: