Numerical Analysis Notes

Math 342 - Spring 2026

Jump to: Math 342 homepage, Week 1, Week 2, Week 3, Week 4, Week 5, Week 6, Week 7, Week 8, Week 9, Week 10, Week 11, Week 12, Week 13, Week 14

Week 1 Notes

Day Topic
Mon, Jan 12 Floating point arithmetic
Wed, Jan 14 Relative error, significant digits
Fri, Jan 16 Taylor’s theorem

Mon, Jan 12

We talked about how computers store floating point numbers. Most modern programming languages store floating point numbers using the IEEE 754 standard.

In the IEEE 754 standard, a 64-bit floating point number has the form

𝐯𝐚𝐥𝐮𝐞=(1)𝐬𝐢𝐠𝐧×2(𝐞𝐱𝐩𝐨𝐧𝐞𝐧𝐭1023)×(1+𝐟𝐫𝐚𝐜𝐭𝐢𝐨𝐧)\mathbf{value} = (-1)^\mathbf{sign} \, \times \, 2^{(\mathbf{exponent} - 1023)} \, \times \, (1 + \mathbf{fraction})

where

We talked about how to convert between binary numbers and decimal numbers. This system works very well, but it leads to weird outcomes like 0.1 + 0.1 + 0.1 = 0.30000000000000004.

When you convert 110=0.1\tfrac{1}{10} = 0.1 to binary, you get a infinitely repeating binary decimal: 110=(0.000110011001100)2.\tfrac{1}{10} = (0.000110011001100\ldots)_2. So any finite binary representation of 110\tfrac{1}{10} will have rounding errors.

We did the following examples in class:

  1. Convert (10110)2 to decimal. (https://youtu.be/a2FpnU9Mm3E)

  2. Convert 35 to binary. (https://youtu.be/gGiEu7QTi68)

  3. What is the 64-bit string that represents the number 35 in the IEEE standard?

  4. What are the largest and smallest 64-bit floating point numbers that can be stored?

  5. In Python, compute 2.0**1024 and 2**1024. Why do you get different results?

  6. In Python, compare 2.0**1024 with 2.0**(-1024) and 2.0**(-1070). What do you notice?

Wed, Jan 14

Today we talked about significant digits. Here is a quick video on how these work.

Rules for Significant Digits.

  1. Addition/Subtraction. The last common digit that is significant for both numbers is the last significant digit of the answer.
  2. Multiplication/Division. Result has significant digits equal to the minimum number of significant digits of the two inputs.

Next we defined absolute and relative error:

Definition. Let x*x^* be an approximation of a real number xx.

The base-10 logarithm of the relative error is approximately the number of significant digits, so you can think of significant digits as a measure of relative error.

Intuitively, addition & subtraction “play nice” with absolute error while multiplication and division “play nice” with relative error. This can lead to problems:

  1. Catastrophic cancellation. When you subtract two numbers of roughly the same size, the relative error can get much worse. For example, both 53.76 and 53.74 have 4 significant digits, but 53.7653.74=0.0253.76 - 53.74 = 0.02 only has 1 significant digit.

  2. Useless precision. If you add two numbers with very different magnitudes, then having a very low relative error in the smaller one will not be useful.

We did these examples in class.

  1. π=3.141592...\pi = 3.141592.... What is the absolute and relative error if your round π\pi to 3.143.14?

Rounding Error. The worst case relative error from rounding to kk significant digits is 𝐫𝐞𝐥𝐚𝐭𝐢𝐯𝐞𝐞𝐫𝐫𝐨𝐫{5×10k(decimal)2k(binary).\mathbf{relative~error} \le \begin{cases} 5 \times 10^{-k} & (\text{decimal}) \\ 2^{-k} & (\text{binary}). \end{cases} Since 64-bit floating point numbers have up to 53 significant digits, they typically have a relative error of up to 2531.11×10162^{-53} \approx 1.11 \times 10^{-16}. This quantity is known as machine epsilon.

You can sometimes re-write algorithms on a computer to avoid issues with floating point numbers such as overflow/underflow and catastrophic cancellation.

  1. Consider the function f(x)=1cosxsinxf(x) = \dfrac{1 - \cos x}{\sin x}. Use Python to compute f(107)f(10^{-7}).

  2. The exact answer to previous question is 0.00000005=5×1080.00000005 = 5 \times 10^{-8} (accurate to 22 decimal places). Use this to find the relative error in your previous calculation.

  3. A better way to compute f(x)f(x) is to use a trick to avoid the catastrophic cancellation: f(x)=1cosxsinx=1cosxsinx(1+cosx1+cosx)=sinx1+cosx.f(x) = \dfrac{1-\cos x}{\sin x} = \dfrac{1 - \cos x}{\sin x} \cdot \left( \frac{1+ \cos x}{1+\cos x} \right) = \dfrac{\sin x}{1 + \cos x}. Use this new formula to compute f(107)f(10^{-7}). What is the relative error now?

Stirling’s formula is a famous approximation for the factorial function. n!2πnnnen.n! \approx \sqrt{2 \pi n} \frac{n^n}{e^n}. We approximated Stirling’s formula using the following code.

import math
n = 100
print(float(math.factorial(n)))
f = lambda n: math.sqrt(2 * math.pi * n) * n ** n / math.exp(n)
print(f(n))

Our formula worked well until n=143n=143, then we got an overflow error. The problem was that nnn^n got too big to convert to a floating point number. But you can prevent the overflow error by adjusting the formula slightly to.

n = 143
f = lambda n: math.sqrt(2 * math.pi * n) * (n / math.e) ** n
print(f(n))

Fri, Jan 26

Today we reviewed Taylor series. We recalled the following important Maclaurin series (which are Taylor series with center c=0c = 0):

We graphed Maclaurin polynomials for cosx\cos x and 11x\dfrac{1}{1-x} on Desmos to see how they converge with different radii of convergence.

We also use the Maclaurin series for sinxx\dfrac{\sin x}{x} to approximate the integral

ππsinxxdx.\displaystyle \int_{-\pi}^\pi \frac{\sin x}{x} \, dx.

Then we did the following workshop in class.


Week 2 Notes

Day Topic
Mon, Jan 19 Martin Luther King day - no class
Wed, Jan 21 Taylor’s theorem - con’d
Fri, Jan 23 Bounding error

Wed, Jan 21

Today we reviewed some theorems that we will need throughout the course. The first is probably the most important theorem in numerical analysis since it lets us estimate error when using Taylor series approximations.

Taylor’s Theorem. Let ff be a function that has (n+1)(n+1) derivatives in the interval between xx and cc. Then there exists a zz strictly inside the interval from xx to cc such that f(x)Pn(x)=f(n+1)(z)(n+1)!(xc)n+1f(x) - P_n(x) = \frac{f^{(n+1)}(z)}{(n+1)!} (x-c)^{n+1} where PnP_n is the nnth degree Taylor polynomial for ff centered at cc.

A special case of Taylor’s theorem is when n=0n = 0. Then you get the Mean Value Theorem (MVT):

Mean Value Theorem. Let ff be a function that is differentiable in the interval between aa and bb. Then there exists a cc strictly inside the interval from aa to bb such that f(c)=f(b)f(a)ba.f'(c) = \frac{f(b) - f(a)}{b-a}.

We did this example:

  1. Use Taylor’s theorem to estimate the error in using the 0th and 2nd degree Maclaurin polynomials to estimate cos(0.03)\cos(0.03) and cos(0.6)\cos(0.6).

Then we started this workshop

Fri, Jan 23

Last time we started this workshop about using Taylor’s remainder formula and the triangle inequality to find upper bounds for functions. Today we revisited that workshop, but first we talked about the following.

Taylor’s Error Formula. Let ff be a function that has (n+1)(n+1) derivatives in the interval between xx and cc. Then |f(x)Pn(x)|M|xc|n+1(n+1)!|f(x) - P_n(x)| \le \frac{M \cdot |x-c|^{n+1}}{(n+1)!} where MM is the maximum value of |f(n+1)(z)||f^{(n+1)}(z)| with zz between xx and cc.

This error formula gives a way to estimate the worst case (absolute) error when you use a Taylor polynomial approximation.

  1. The 3rd degree Taylor polynomial for cosx\cos x centered at c=πc = \pi is P3(x)=1+12(xπ)2P_3(x) = -1 + \tfrac{1}{2}(x-\pi)^2 (the coefficient on the 3rd degree term is zero). What is the worst case absolute error using this polynomial to estimate cos3\cos 3?

After that we talked about the triangle inequality.

Triangle Inequality. For any numbers aa and bb (real or complex), |a+b||a|+|b|.|a+b| \le |a| + |b|.

We talked about how you can use the triangle inequality to find upper bounds for functions. We also talked about tight upper bounds versus upper bounds that are not tight. We did this example.

  1. Use the triangle inequality to find an upper bound for |x2+3xcosx||x^2 + 3x \cos x|.

Week 3 Notes

Day Topic
Mon, Jan 26 No class (snow day)
Wed, Jan 28 Bisection method
Fri, Jan 30 Newton’s method

Wed, Jan 28

We talked about how to find the roots of a function. Recall that a root (AKA a zero) of a function f(x)f(x) is an xx-value where the function hits the xx-axis. We introduced an algorithm called the bisection method for finding roots of a continuous function that changes sign from positive to negative or negative to positive on an interval [a,b][a, b]. We wrote the following code to implement this algorithm.

def bisection(f, a, b, N):
    """
    Applies the bisection method recursively up to N times to estimate a root 
    of a continuous function f on an interval [a,b]. 
    """
    m = (a + b) / 2
    if N == 0 or f(m) == 0:
        return m
    if (f(a) > 0 and f(m) > 0) or (f(a) < 0 and f(m) < 0):
        return bisection(f, m, b, N - 1)
    else:
        return bisection(f, a, m, N - 1)

We tested this algorithm on the function f(x)=tanx1f(x) = \tan x - 1 which has a root at π4\tfrac{\pi}{4}.

One feature of the bisection method is that we can easily find the worst case absolute error in our approximation of a root. That is because every time we repeat the algorithm and cut the interval in half, the error reduces by a factor of 2, so that Absolute error(ba)2N+1.\text{Absolute error} \le \frac{(b-a)}{2^{N+1}}. We saw that it takes about 10 iterations to increase the accuracy by 3 decimal places (because 2101032^{10} \approx 10^3).

Fri, Jan 30

Today we covered Newton’s method. This is probably the most important method for finding roots of differentiable functions. The formula is xn+1=xnf(xn)f(xn). x_{n+1} = x_n - \dfrac{f(x_n)}{f'(x_n)}. This formula comes from the idea which is to start with a guess x0x_0 for a root and then repeatedly improve your guess by following the tangent line at xnx_n until it hits the xx-axis.

  1. Use Newton’s method to find roots of tanx1\tan x - 1.

  2. How can you use Newton’s method to find ee? Hint: use f(x)=lnx1f(x) = \ln x -1.

Theorem. Let fC2[a,b]f \in C^2[a,b] and suppose that ff has a root r(a,b)r \in (a,b). Suppose that there are constants L,M>0L,M >0 such that |f(x)|L|f'(x)| \ge L and |f(x)|M|f''(x)| \le M for all x[a,b]x \in [a,b]. Then |xn+1r|M2L|xnr|2|x_{n+1} - r| \le \frac{M}{2L} |x_n-r|^2 when xn[a,b]x_n \in [a,b].

Proof. Start with the first degree Taylor polynomial (centered at xnx_n) for f(r)f(r) including the remainder term and the Newton’s method iteration formula:

f(r)=f(xn)+f(xn)(rxn)+12f(z)(rxn)2=0,f(r) = f(x_n) + f'(x_n)(r-x_n) + \frac{1}{2} f''(z)(r-x_n)^2 = 0, and xn+1=xnf(xn)f(xn)f(xn)(xn+1xn)+f(xn)=0.x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} \Rightarrow f'(x_n)(x_{n+1} - x_n) + f(x_n)=0.

Subtract these two formulas to get a formula that relates (rxn+1)(r-x_{n+1}) with (rxn)(r-x_n).

f(xn)(rxn+1)+12f(z)(rxn)2=0.f'(x_n)(r-x_{n+1}) + \frac{1}{2} f''(z)(r-x_n)^2 = 0.

Use this to get an upper bound on |rxn+1||r-x_{n+1}|. □

Corollary. Let C=2LMC = \dfrac{2L}{M}. As long as the Newton method iterates xnx_n stay in [a,b][a,b], then the absolute error after nn steps will satisfy |xnrC||x0rC|2n.|\frac{x_n-r}{C}| \le \left|\frac{x_0 - r}{C} \right|^{2^n}.

This corollary explains why, if you start with a good guess in Newton’s method, the number of correct decimal places tends to double with each iteration!


Week 4 Notes

Day Topic
Mon, Feb 2 Secant method
Wed, Feb 4 Fixed point iteration
Fri, Feb 6 Newton’s method with complex numbers

Mon, Feb 2

The secant method is a variation of Newton’s method that uses secant lines instead of tangent lines. The advantage of the secant method is that it doesn’t require calculating a derivative. The disadvantage is that it is a little slower to converge than Newton’s method, but it is still much faster than the bisection method. Here is the formula:

xn+1=xnf(xn)(xnxn1)f(xn)f(xn1).x_{n+1} = x_n - \frac{f(x_n) \, (x_n - x_{n-1})}{f(x_n) - f(x_{n-1})}.

  1. Solve the equation 2x=102^x = 10 using the secant method. What would make good initial guesses x0x_0 and x1x_1?

     # This code will do one step of the secant method.
     f = lambda x: 2 ** x - 10
     a, b = 3, 3.5
     a, b = b, b - f(b) * (b - a) / (f(b) - f(a)); print(b)

Definition. A sequence xnx_n converges with order α\alpha if it converges to rr and there are constants C>0C > 0 and α1\alpha \ge 1 such that limn|xn+1r||xnr|α=C.\lim_{n \rightarrow \infty} \frac{|x_{n+1} - r|}{|x_n - r|^\alpha} = C.

Convergence of order α=1\alpha = 1 is called linear convergence and convergence of order α=2\alpha = 2 is called quadratic convergence. Newton’s method converges quadratically. It turns out that the secant method converges with order α=1+521.618\alpha = \frac{1+\sqrt{5}}{2} \approx 1.618 which is the golden ratio!

Wed, Feb 4

Newton’s method is a special case of a method known as fixed point iteration. A fixed point of a function f(x)f(x) is a number pp such that f(p)=pf(p) = p. A real-valued function f(x)f(x) has a fixed point on an interval [a,b][a, b] if and only if it intersects the graph y=xy = x on that interval.

  1. Show that cosx\cos x has a fixed point in [0,π2][0,\tfrac{\pi}{2}].

  2. Explain why f(x)=exf(x) = e^x has no fixed points.

  3. Does sinx\sin x have any fixed points?

A fixed point pp is attracting if the recursive sequence defined by xn+1=f(xn)x_{n+1} = f(x_n) converges to pp for all x0x_0 sufficiently close to pp. It is repelling if points close to pp get pushed away from pp when you apply the function ff. You can draw a picture of these fixed point iterates by drawing a cobweb diagram.

To make a cobweb diagram, repeat these steps:

  1. Show that the fixed point of cosx\cos x is attracting by repeatedly iterating.

  2. Show that g(x)=12xx5g(x) = 1 - 2x -x^5 has a fixed point, but it is not attracting.

Theorem If ff has a fixed point pp and

  1. |f(p)|<1|f'(p)| < 1, then pp is attracting,
  2. |f(p)|>1|f'(p)| > 1, then pp is repelling,
  3. |f(p)|=1|f'(p)| = 1, then no info.

Proof. Write down the first degree Taylor approximation for ff centered at pp. Use it with x=xnx = x_n, f(xn)=xn+1f(x_n) = x_{n+1} to show that if MM is an upper bound for f(z)f''(z) (near pp), then |xn+1p||xnp|(|f(p)|+M2|xnp|).|x_{n+1} - p| \le |x_n - p| \left(|f'(p)| + \frac{M}{2}|x_n-p| \right). Then as long as xnx_n is close enough to pp, the terms inside the parentheses are less than 1 which means that |xn+1p|<|xnp||x_{n+1} - p| < |x_n - p|, i.e, xn+1x_{n+1} is closer to pp than xnx_n for every nn. □

Theorem. If ff is differentiable at a fixed point pp and 0<|f(p)|<10 < |f'(p)| < 1, then for any point x0x_0 sufficiently close to pp, the fixed point iterates xnx_n defined by xn+1=f(xn)x_{n+1} = f(x_n) converge to pp with linear order. If f(p)=0f'(p) = 0, then the iterates converge to pp with order α\alpha where f(α)(p)f^{(\alpha)}(p) is the first nonzero derivative of ff at pp.

  1. For any function fC2[a,b]f \in C^2[a,b] with a root rr in (a,b)(a,b), let g(x)=xf(x)f(x)g(x) = x - \dfrac{f(x)}{f'(x)}. Show that rr is a fixed point of gg and show that g(r)=0g'(r) = 0.

Fri, Feb 6

In any root finding method, we have two ways of measuring the error when we compare an approximation xnx_n with the actual root. The forward error is the distance between the root rr and the approximation xnx_n on the xx-axis. Since we usually don’t know the true value of rr, it is hard to estimate the forward error. The backward error is just |f(xn)||f(x_n)|, which is usually easy to calculate.

  1. If xnx_n is really close to rr, then how could you use f(r)f'(r) to estimate the forward error if you know the backward error?

We used the idea of forward and backward error to automate Newton’s method. The idea is to include an optional tolerance argument. We stop iterating when the backward error is smaller than the tolerance.

def newton(f, Df, x, tolerance = 10 ** (-15)):
    """
    Applies Newton's method to a function f with derivative Df and initial guess x.  
    Stops when |f(x)| < tol. 
    """
    step = 0
    while not abs(f(x)) < tolerance:
        x = x - f(x) / Df(x)
        step = step + 1
    print("Converged in", step, "steps.")
    return x

We finished with a cool fact about Newton’s method. It also works for to find complex number roots if you use complex numbers. We did a quick review of complex numbers.

Euler’s formula.

eiθ=cosθ+isinθ.e^{i \theta} = \cos \theta + i \sin \theta.

We used the cmath library in Python to do the following experiments with Newton’s method.

  1. Cube roots of unity. Use Newton’s method to find all 3 roots of x31x^3 - 1.

  2. Euler’s identity. Use Newton’s method to solve ex+1=0e^x + 1 = 0.

We finished by talking about Newton fractals. When you use Newton’s method on a function with more than one root in the complex plane, the set of points that converge to each root (called the basins of attraction) form fractal patterns.

Basins of attraction for the roots of x31x^3-1.

Week 5 Notes

Day Topic
Mon, Feb 9 Solving nonlinear systems
Wed, Feb 11 Systems of linear equations
Fri, Feb 13 LU decomposition

Mon, Feb 9

Today we talked about how to solve systems of nonlinear equations with Newton’s method. As a motivating example, we talked about how this could be used in navigation, for example in the LORAN system.

To solve a vector-valued system 𝐅(𝐱)=𝟎\mathbf{F}(\mathbf{x}) = \mathbf{0}, you can iterate the formula 𝐱n+1=𝐱n𝐉(𝐱n)1𝐅(𝐱𝐧)\mathbf{x}_{n+1} = \mathbf{x}_n - \mathbf{J}(\mathbf{x}_n)^{-1} \mathbf{F}(\mathbf{x_n}) where 𝐉(𝐱)\mathbf{J}(\mathbf{x}) is the Jacobian matrix 𝐉(𝐱)=[F1x1F1xnFnx1Fnxn].\mathbf{J}(\mathbf{x}) = \begin{bmatrix} \frac{\partial F_1}{\partial x_1} & \ldots & \frac{\partial F_1}{\partial x_n} \\ \vdots & \ddots & \vdots \\ \frac{\partial F_n}{\partial x_1} & \ldots & \frac{\partial F_n}{\partial x_n} \end{bmatrix}.

To program this formula in Python, we’ll need to load the numpy library. We wrote the following code to solve this system:

xy+y2=1x2y2=1\begin{align*} xy + y^2 &= 1 \\ x^2 - y^2 &= 1 \end{align*}

import numpy as np

F = lambda x: np.array([
    x[0] * x[1] + x[1]**2 - 1, 
    x[0]**2 - x[1]**2 - 1
])
J = lambda x: np.array([
    [   x[1], x[0] + 2*x[1] ], 
    [ 2*x[0],       -2*x[1] ]
])

x = np.array([1,1])
for i in range(10): 
    x = x - np.linalg.inv(J(x)) @ F(x) # Use @ not * for matrix multiplication.
    print(x)

Wed, Feb 11

Today we talked about systems of linear equations and linear algebra.

  1. Suppose you have a jar full of pennies, nickles, dimes, and quarters. There are 80 coins in the jar, and the total value of the coins is $10.00. If there are twice as many dimes as quarters, then how many of each type of coin are in the jar?

We can represent this question as a system of linear equations. p+n+d+q=80p+n+d+q = 80 p+5n+10d+25q=1000p+5n+10d+25q = 1000 d=2qd = 2q where p,n,d,qp,n,d,q are the numbers of pennies, nickles, dimes, and quarters respectively. It is convenient to use matrices to simplify these equations: (11111510250012)(pndq)=(8010000).\begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & 5 & 10 & 25 \\ 0 & 0 & 1 & -2 \end{pmatrix} \, \begin{pmatrix} p \\ n \\ d \\ q \end{pmatrix} = \begin{pmatrix} 80 \\ 1000 \\ 0 \end{pmatrix}. Here we have a matrix equation of the form A𝐱=𝐛A\mathbf{x} = \mathbf{b} where A3×4A \in \mathbb{R}^{3 \times 4}, 𝐱4\mathbf{x} \in \mathbb{R}^4 is the unknown vector, and 𝐛3\mathbf{b} \in \mathbb{R}^3. Then you can solve the problem by row-reducing the augmented matrix

(111180151025100000120)\left( \begin{array}{cccc|c} 1 & 1 & 1 & 1 & 80 \\ 1 & 5 & 10 & 25 & 1000 \\ 0 & 0 & 1 & -2 & 0\end{array}\right)

which can be put into echelon form

(1111800492492000120).\left( \begin{array}{cccc|c} 1 & 1 & 1 & 1 & 80 \\ 0 & 4 & 9 & 24 & 920 \\ 0 & 0 & 1 & -2 & 0\end{array}\right).

The variables p,np, n, and dd are pivot variables, and the last variable qq is a free variable. Once you pick values for the free variable(s), you can solve for the pivot variables one at a time using back substitution. We did this using Python:

q = 20 
d = 2*q
n = (920 - 9*d - 24*q) / 4
p = 80 - n - d - q
print(p, n, q)

# Only q = 20 makes sense, otherwise either p or n will be negative.

After that we reviewed some more concepts and terminology from linear algebra. The most important thing to understand is that you can think of matrix-vector multiplication as a linear combination of the columns of the matrix:

A𝐱=(11111510250012)(pndq)=(110)p+(150)n+(1101)d+(1252)qA𝐱 is a linear combination of the columns of A.A \mathbf{x} = \begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & 5 & 10 & 25 \\ 0 & 0 & 1 & -2 \end{pmatrix} \, \begin{pmatrix} p \\ n \\ d \\ q \end{pmatrix} = \underbrace{\begin{pmatrix} 1 \\ 1 \\ 0 \end{pmatrix}p + \begin{pmatrix} 1 \\ 5 \\ 0 \end{pmatrix}n + \begin{pmatrix} 1 \\ 10 \\ 1 \end{pmatrix}d + \begin{pmatrix} 1 \\ 25 \\ -2 \end{pmatrix}q}_{A \mathbf{x} \text{ is a linear combination of the columns of } A}.

For any matrix Am×nA \in \mathbb{R}^{m \times n}:

A matrix equation A𝐱=𝐛A\mathbf{x} = \mathbf{b} has a solution if and only if 𝐛\mathbf{b} is in the column space of AA. If 𝐛\mathbf{b} is in the column space, then there will be either one unique solution if there are no free variables (i.e., the nullity of AA is zero) or there will be infinitely many solutions if there are free variables.

If An×nA \in \mathbb{R}^{n \times n} (i.e., AA is a square matrix) and the rank of AA is nn, then AA is invertible which means that there is a matrix A1A^{-1} such that AA1=A1A=IA A^{-1} = A^{-1} A = I where II is the identity matrix which is I=(100010001).I = \begin{pmatrix} 1 & 0 & \ldots & 0 \\ 0 & 1 & \ldots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \ldots & 1 \end{pmatrix}.
You can use row-reduction to find the inverse of an invertible matrix by row reducing the augmented matrix [AI]\left[ \begin{array}{c|c} A & I \end{array} \right] until you get [IA1]\left[ \begin{array}{c|c} I & A^{-1} \end{array} \right]. We did this example in class:

  1. Find the inverse of A=(120013001)A = \begin{pmatrix} 1 & 2 & 0 \\ 0 & 1 & 3 \\ 0 & 0 & 1 \end{pmatrix}.

Here is another example that we did not do in class:

  1. Use row-reduction to find the inverse of A=(1325)A = \begin{pmatrix} 1 & 3 \\ 2 & 5 \end{pmatrix}. (https://youtu.be/cJg2AuSFdjw)

Fri, Feb 13

Today we talked about LU decomposition. We defined the LU decomposition as follows. The LU decomposition of a matrix Am×nA \in \mathbb{R}^{m \times n} is a pair of matrices Lm×mL \in \mathbb{R}^{m \times m} and Um×nU \in \mathbb{R}^{m \times n} such that A=LUA = LU and UU is in echelon form and LL is a lower triangular matrix with 1’s on the main diagonal, 0’s above the main diagonal, and entries LijL_{ij} in row ii, column jj that are equal to the multiple of row ii that you subtracted from row jj as you row reduced AA to UU.

  1. Compute the LU decomposition of A=(1111225311144)A = \begin{pmatrix} 1 & 1 & 1 & 1 \\ 2 & 2 & 5 & 3 \\ -1 & -1 & 14 & 4 \end{pmatrix}.

  2. Use the LU decomposition to solve Ax=(268)Ax = \begin{pmatrix} 2 \\ 6 \\ 8 \end{pmatrix}.

We also did this workshop.

We finished with one more example.

  1. For what real numbers aa and bb does the matrix (101aaabba)\begin{pmatrix} 1 & 0 & 1 \\ a & a & a \\ b & b & a \end{pmatrix} have an LU decomposition? (https://youtu.be/-eA2D_rIcNA)

Here’s another LU decomposition example if you want more practice.

  1. Decompose A=(24354758682949214)A = \begin{pmatrix} 2 & 4 & 3 & 5 \\ -4 & -7 & -5 & -8 \\ 6 & 8 & 2 & 9 \\ 4 & 9 & -2 & 14 \end{pmatrix}. (https://youtu.be/BFYFkn-eOQk)

Week 6 Notes

Day Topic
Mon, Feb 16 Matrix norms and conditioning
Wed, Feb 18 Review
Fri, Feb 20 Midterm 1

Mon, Feb 16

  1. Let A=(1111.001)A = \begin{pmatrix} 1 & 1 \\ 1 & 1.001 \end{pmatrix}. Let 𝐲=(22)\mathbf{y} = \begin{pmatrix} 2 \\ 2 \end{pmatrix} and 𝐳=(22.001)\mathbf{z} = \begin{pmatrix} 2 \\ 2.001 \end{pmatrix}. Use A1=(1001100010001000)A^{-1} = \begin{pmatrix} 1001 & -1000 \\ -1000 & 1000 \end{pmatrix} to solve A𝐱=𝐲A\mathbf{x} = \mathbf{y} and A𝐱=𝐳A\mathbf{x} = \mathbf{z}.

Notice that even though 𝐲\mathbf{y} and 𝐳\mathbf{z} are very close, the two solutions are not close at all. When the solutions of a linear system A𝐱=𝐛A\mathbf{x} = \mathbf{b} are very sensitive to small changes in bb, we say that the matrix AA is ill-conditioned.

Norms of Vectors

Definition. A norm is a function \|\cdot\| from a vector space VV to [0,)[0,\infty) with the following properties:

  1. Definiteness. x=0\|x\| = 0 if and only if x=0x=0.
  2. Homogeneity. cx=|c|x\|c x \| = |c| \|x\| for all xVx \in V and cc \in \mathbb{R}.
  3. Triangle Inequality. x+yx+y\|x+y\| \le \|x\| + \|y\| for all x,yVx, y \in V.

Intuitively a norm measures the length of a vector. But there are different norms and they measure length in different ways. The three most important norms on the vector space n\mathbb{R}^n are:

  1. The 22-norm (also known as the Euclidean norm) is the most commonly used, and it is exactly the formula for the length of a vector using the Pythagorean theorem. x2=x12+x22++xn2.\|x\|_2 = \sqrt{x_1^2 + x_2^2 + \ldots + x_n^2}.

  2. The 11-norm (also known as the Manhattan norm) is x1=|x1|+|x2|++|xn|.\|x\|_1 = |x_1|+|x_2|+\ldots+|x_n|. This is the distance you would get if you had to navigate a city where the streets are arranged in a rectangular grid and you can’t take diagonal paths.

  3. The \infty-norm (also known as the Maximum norm) is x=max{|x1|,|x2|,,|xn|}.\|x\|_\infty = \max \{ |x_1|, |x_2|, \ldots, |x_n| \}.

These are all special cases of pp-norms which have the form xp=|x1|p+|x2|p++|xn|pp.\|x\|_p = \sqrt[p]{|x_1|^p + |x_2|^p + \ldots + |x_n|^p}.

We used Desmos to graph the set of vectors in 2\mathbb{R}^2 with pp-norm equal to one, then we could see how those norms change as pp varies between 1 and \infty.

Norms of Matrices

The set of all matrices in m×n\mathbb{R}^{m \times n} is a vector space. So it makes sense to talk about the norm of a matrix. There are many ways to define norms for matrices, but the most important for us are the induced norms (also known as operator norms). For a matrix Am×nA \in \mathbb{R}^{m \times n}, the induced pp-norm is Ap=max{Axp:xn,x=1}.\|A\|_p = \max \{\|Ax\|_p : x \in \mathbb{R}^n, \|x\|=1\}.
Two important special cases are

  1. When p=2p=2, the induced norm A2\|A\|_2 is the square root of the largest eigenvalue of ATAA^T A.
  2. When p=p=\infty, the induced norm A\|A\|_\infty is the largest 1-norm of the rows of AA.

Here is a quick exercise:

  1. Find A\|A\|_\infty for the matrix A=(1111225311144)A = \begin{pmatrix} 1 & 1 & 1 & 1 \\ 2 & 2 & 5 & 3 \\ -1 & -1 & 14 & 4 \end{pmatrix}.

Condition Number

For an invertible matrix An×nA \in \mathbb{R}^{n \times n}, the condition number of AA is κ(A)=AA1\kappa(A) = \|A\| \, \|A^{-1}\|. You can use any induced norm to define κ(A)\kappa(A), but our default will be the induced \infty-norm since it is the only one that is easy to calculate by hand.

  1. Find the condition number κ(A)\kappa(A) for the matrix A=(1111.001)A = \begin{pmatrix} 1 & 1 \\ 1 & 1.001 \end{pmatrix} with A1=(1001100010001000)A^{-1} = \begin{pmatrix} 1001 & -1000 \\ -1000 & 1000 \end{pmatrix}.

If the condition number is large, then the matrix is ill-conditioned. When we try to solve an ill-conditioned linear system, small errors in either AA or 𝐛\mathbf{b} could become large errors in our calculated solution.

Just like with root finding, we can talk about forward and backward error when we try to solve a linear system A𝐱=𝐛A \mathbf{x} = \mathbf{b}. The table below defines the absolute and relative forward and backwards errors. In the table, 𝐱\mathbf{x} is the exact solution to the system A𝐱=𝐛A \mathbf{x} = \mathbf{b}, and 𝐱a\mathbf{x}_a is an approximation of 𝐱\mathbf{x}.

Forward Error Backward Error
Absolute 𝐱a𝐱\|\mathbf{x}_a - \mathbf{x}\| A𝐱𝐚𝐛\| A \mathbf{x_a} - \mathbf{b} \|
Relative 𝐱a𝐱𝐱\dfrac{\|\mathbf{x}_a - \mathbf{x}\|}{\|\mathbf{x}\|} A𝐱a𝐛𝐛\dfrac{\|A\mathbf{x}_a - \mathbf{b}\|}{\|\mathbf{b}\|}

The following result shows how the condition number lets us estimate the relative forward error using the relative backward error.

Theorem. If AA is an invertible matrix with condition number κ(A)\kappa(A), and 𝐛0\mathbf{b} \ne 0, then 𝐱a𝐱𝐱κ(A)A𝐱a𝐛𝐛.\dfrac{\|\mathbf{x}_a - \mathbf{x}\|}{\|\mathbf{x}\|} \le \kappa(A) \dfrac{\|A\mathbf{x}_a - \mathbf{b}\|}{\|\mathbf{b}\|}.

Proof. By the definition of the induced norm, 𝐱a𝐱𝐱=A1(A𝐱a𝐛)𝐱A1A𝐱a𝐛𝐱.\dfrac{\|\mathbf{x}_a - \mathbf{x}\|}{\|\mathbf{x}\|} = \dfrac{\|A^{-1} (A\mathbf{x}_a - \mathbf{b}) \|}{\|\mathbf{x}\|} \le \|A^{-1}\| \dfrac{\|A \mathbf{x}_a - \mathbf{b} \|}{\|\mathbf{x}\|}. Since 𝐛=A𝐱\mathbf{b} = A \mathbf{x}, 𝐛A𝐱\|\mathbf{b}\| \le \|A \| \|\mathbf{x}\|, so A1A𝐱a𝐛𝐱A1AA𝐱a𝐛𝐛=κ(A)A𝐱a𝐛𝐛 \|A^{-1}\| \dfrac{\|A \mathbf{x}_a - \mathbf{b} \|}{\|\mathbf{x}\|} \le \|A^{-1}\| \|A\| \dfrac{\|A \mathbf{x}_a - \mathbf{b} \|}{\|\mathbf{b}\|} = \kappa(A) \dfrac{\|A \mathbf{x}_a - \mathbf{b} \|}{\|\mathbf{b}\|} which proves the statement. □

The following is a consequence of this theorem.

Rule of thumb. If the entries of AA and 𝐛\mathbf{b} are both accurate to nn-significant digits and the condition number of AA is κ(A)=10k\kappa(A) = 10^k, then the solution of the linear system A𝐱=𝐛A\mathbf{x} = \mathbf{b} will be accurate to nkn-k significant digits.


Week 7 Notes

Day Topic
Mon, Feb 23 LU decomposition with pivoting
Wed, Feb 25 Inner-products and orthogonality
Fri, Feb 27 Orthogonal sets & matrices

Mon, Feb 23

Consider the matrix B=(0.001111)B = \begin{pmatrix} 0.001 & 1 \\ 1 & 1 \end{pmatrix} which has LULU decomposition B=LU=(1010001)(0.00110999).B = LU = \begin{pmatrix} 1 & 0 \\ 1000 & 1 \end{pmatrix} \, \begin{pmatrix} 0.001 & 1 \\ 0 & -999 \end{pmatrix}.
Although BB is not ill-conditioned, you have to be careful using row reduction to solve equations with this matrix because both LL and UU in the LU-decomposition for BB are ill-conditioned.

In Matlab/Octave, you can use the norm(A, inf) function to find the induced \infty-norm of matrix. The function norm(A) computes the induced 2-norm by default. You can also compute the condition number of a matrix using cond(A) or cond(A, inf).

  1. Use Octave to find the condition numbers for the matrices BB, LL, and UU above.

     B  = [0.001 1; 1 1]
     L = [1 0; 1000 1]
     U = [0.001 1; 0 -999]
     cond(B)
     cond(L)
     cond(U)

It is possible to avoid this problem using the method of partial pivoting. The idea is that there is a permutation PP such that PA=LUPA = LU where PP is a permutation matrix such that PAPA has a nice LU-decomposition. The formula PA=LUPA = LU is known as the PLU-decomposition or sometimes the LUP-decomposition of AA. This method fixes two problems:

The algorithm to find the PLU-decomposition is almost the same as the LU decomposition, except you also swap rows so that the largest available entry in a column (in absolute value) becomes the pivot at each step. When you swap two rows of the echelon form matrix UU, you also have to swap the same rows of AA and the same rows in the completed columns of LL (leave the unfinished columns alone). The permutation matrix PP is the matrix you get by swapping the same rows of the identity matrix as you swapped while you found the PLU decomposition.

In Octave, you can just use the following command to get the PLU-decomposition:

[L, U, P] = lu(A)

One nice thing to know about permutation matrices is that they are always invertible and P1=PTP^{-1} = P^T where PTP^T is the transpose of PP obtained by converting every row of PP to a column of PTP^T.

  1. Show that when you use partial pivoting to row reduce B=(0.001111)B = \begin{pmatrix} 0.001 & 1 \\ 1 & 1 \end{pmatrix} to echelon form, the resulting LU matrices are not ill-conditioned.

  2. Use the PLU-decomposition to solve B𝐱=(12).B\mathbf{x} = \begin{pmatrix} 1 \\ 2 \end{pmatrix}.

Finding the PLU decomposition by hand is tedious, especially if you need to swap more than 2 rows, but here is a good video explanation of how it works:

  1. Find the PLU decomposition for A=(01212100012121512436)A = \begin{pmatrix} 0 & 1 & 2 & 1 & 2 \\ 1 & 0 & 0 & 0 & 1 \\ 2 & 1 & 2 & 1 & 5 \\ 1 & 2 & 4 & 3 & 6 \end{pmatrix}. (https://youtu.be/E3cCRcdFGmE)

Wed, Feb 25

The inner product of two vectors 𝐱,𝐲\mathbf{x}, \mathbf{y} in n\mathbb{R}^n is 𝐱T𝐲\mathbf{x}^T \mathbf{y}. We proved some important facts about inner products and got some practice with matrix algebra in the following workshop:

Fri, Feb 27

In exercise 2 from the workshop last time, we needed the property that (𝐱+𝐲)T=𝐱T+𝐲T(\mathbf{x} + \mathbf{y})^T = \mathbf{x}^T + \mathbf{y}^T. This is a special case of one of the rules for transposes:

Today we talked about two special types of matrices.

A set of vectors S={𝐯1,,𝐯d}S = \{\mathbf{v}_1, \ldots, \mathbf{v}_d\} is an orthogonal set if every vector in SS is orthogonal to every other vector in SS. An orthogonal set where every vector also has length equal to 1 is called an orthonormal set.

  1. Suppose that we have an orthonormal set in 2\mathbb{R}^2 that contains two vectors 𝐱\mathbf{x} and 𝐲\mathbf{y}. If 𝐱=(cosθsinθ)\mathbf{x} = \begin{pmatrix} \cos \theta \\ \sin \theta \end{pmatrix} for some angle θ\theta, then there are only two possible vectors that 𝐲\mathbf{y} could be. What are they? Hint: Draw a picture!

  2. If a matrix UU in m×n\mathbb{R}^{m \times n} has orthonormal columns, then UTUU^T U is the n-by-n identity matrix. Use this fact to prove that UU preserves inner-products. That is (U𝐱)T(U𝐲)=𝐱T𝐲(U\mathbf{x})^T (U \mathbf{y}) = \mathbf{x}^T \mathbf{y} for any vectors 𝐱,𝐲\mathbf{x}, \mathbf{y} in n\mathbb{R}^n.

  3. If UU has orthonormal columns, then show that UU preserves lengths, that is, U𝐱=𝐱\|U\mathbf{x}\| = \|\mathbf{x}\| for all 𝐱\mathbf{x} in n\mathbb{R}^n.

A square matrix with orthonormal columns is called an orthogonal matrix.

  1. Show that every 2-by-2 orthogonal matrix must be either (cosθsinθsinθcosθ) or (cosθsinθsinθcosθ).\begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix} \text{ or } \begin{pmatrix} \cos \theta & \sin \theta \\ \sin \theta & -\cos \theta \end{pmatrix}.

The first possibility represents all possible rotations of 2\mathbb{R}^2. The second represents all possible reflections. Those are the only length-preserving linear transformations of the plane!

We finished by talking about symmetric matrices. A matrix AA in n×n\mathbb{R}^{n \times n} is symmetric if AT=AA^T = A.

  1. Suppose that 𝐱\mathbf{x} and 𝐲\mathbf{y} are eigenvectors of a symmetric matrix AA that correspond to two different eigenvalues λ\lambda and μ\mu respectively. Prove that 𝐱\mathbf{x} is orthogonal to 𝐲\mathbf{y}.
    Hint: Observe that (A𝐱)T𝐲=𝐱T(A𝐲)(A\mathbf{x})^T \mathbf{y} = \mathbf{x}^T (A\mathbf{y}).

It turns out that symmetric matrices can only have real eigenvalues and they always have an orthonormal basis of eigenvectors.

Since we had a little extra time, we finished by talking about the conjugate transpose of a complex matrix. For any matrix AA in m×n\mathbb{C}^{m \times n}, the conjugate transpose is A*=(A)TA^* = (\bar{A})^T. It combines taking the transpose of AA with computing the complex conjugate of every entry. Recall that the complex conjugate of a complex number a+ib¯=aib\overline{a+ib} = a - ib. For matrices with real number entries, A*A^* and ATA^T are the same thing. In Matlab/Octave you use an apostrophe to get the conjugate transpose:

A = [1 2i; 3 4i]
A'

The conjugate transpose mostly has the same properties as the transpose:

One very important exception is when you work with inner-products of complex vectors. The complex inner-product of 𝐱,𝐲\mathbf{x}, \mathbf{y} in n\mathbb{C}^n is 𝐱*𝐲\mathbf{x}^* \mathbf{y}. But unlike regular inner-products, order matters for complex inner-products because 𝐲*𝐱=𝐱*𝐲¯.\mathbf{y}^* \mathbf{x} = \overline{\mathbf{x}^* \mathbf{y}}.


Week 8 Notes

Day Topic
Mon, Mar 2 Gram-Schmidt algorithm
Wed, Mar 4 QR decomposition
Fri, Mar 6 Orthogonal projections

Mon, Mar 2

Last week we introduced the orthogonal complement WW^\perp of a set WW in n\mathbb{R}^n. You should know that:

The Fundamental Theorem of Linear Algebra. Let AA be a real mm-by-nn matrix. Then

  1. We looked at the problem of finding {(111)}\left\{ \begin{pmatrix} 1 \\ 1\\ 1 \end{pmatrix} \right\}^\perp in the context of the fundamental theorem of linear algebra. The orthogonal complement is the nullspace of the matrix (111)\begin{pmatrix} 1 & 1 & 1 \end{pmatrix}.
    1. What is the dimension of the nullspace?
    2. What is a basis for the null space?

After that we talked about why orthogonal bases are better. We used this example:

Since the two components of the force of gravity are orthogonal, it is easy find the right coefficients for the force of gravity in the angled basis.

Gram-Schmidt Algorithm. Converts a basis 𝐯1,,𝐯p\mathbf{v}_1, \ldots, \mathbf{v}_p into an orthogonal basis 𝐮1,,𝐮p\mathbf{u}_1, \ldots, \mathbf{u}_p for the same subspace.

Start with 𝐮1=𝐯1\mathbf{u}_1 = \mathbf{v}_1. Then for each kk from 2 to pp, find 𝐮k\mathbf{u}_{k} using these steps:

  1. Find the orthogonal projection of 𝐯k\mathbf{v}_{k} onto the span of 𝐮1,,𝐮k1\mathbf{u}_1, \ldots, \mathbf{u}_{k-1}: Proj𝐯k=(𝐯k+1𝐮1𝐮1𝐮1)𝐮1+(𝐯k𝐮2𝐮2𝐮2)𝐮2++(𝐯k𝐮k1𝐮k1𝐮k1)𝐮k1.\operatorname{Proj} \mathbf{v}_{k} = \left(\frac{\mathbf{v}_{k+1} \cdot \mathbf{u}_1}{\mathbf{u}_1 \cdot \mathbf{u}_1}\right) \mathbf{u}_1 + \left(\frac{\mathbf{v}_{k} \cdot \mathbf{u}_2}{\mathbf{u}_2 \cdot \mathbf{u}_2}\right) \mathbf{u}_2 + \ldots + \left(\frac{\mathbf{v}_{k} \cdot \mathbf{u}_{k-1}}{\mathbf{u}_{k-1} \cdot \mathbf{u}_{k-1}}\right) \mathbf{u}_{k-1}.
  2. Subtract the orthogonal projection from 𝐯k\mathbf{v}_{k}: 𝐮k=𝐯kProj𝐯k.\mathbf{u}_{k} = \mathbf{v}_{k} - \operatorname{Proj} \mathbf{v}_{k}.

If you want an orthonormal basis, then just normalize by dividing each 𝐮i\mathbf{u}_i by its length.

  1. Apply Gram-Schmidt to 𝐱1=(360)\mathbf{x}_1 = \begin{pmatrix} 3 \\ 6 \\ 0 \end{pmatrix}, 𝐱2=(122).\mathbf{x}_2 = \begin{pmatrix} 1 \\ 2 \\ 2 \end{pmatrix}. (https://youtu.be/Rz3O6hJ9xZQ)

  2. Apply Gram-Schmidt to 𝐱1=(1111)\mathbf{x}_1 = \begin{pmatrix} 1\\ 1\\ 1\\ 1\end{pmatrix}, 𝐱2=(0111)\mathbf{x}_2 = \begin{pmatrix} 0\\ 1\\ 1\\ 1\end{pmatrix}, 𝐱3=(0011)\mathbf{x}_3 = \begin{pmatrix} 0\\ 0\\ 1\\ 1\end{pmatrix}. (https://youtu.be/Rz3O6hJ9xZQ?t=350)

Wed, Mar 4

We started with these two warm-up problems.

  1. Find the orthogonal projection of 𝐲=(123)\mathbf{y} = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} onto the line spanned by 𝐱=(101)\mathbf{x} = \begin{pmatrix} 1 \\ 0 \\ -1 \end{pmatrix}. Then find two orthogonal vectors 𝐚\mathbf{a} and 𝐛\mathbf{b} such that 𝐚\mathbf{a} lies in the span of 𝐱\mathbf{x} and 𝐚+𝐛=𝐲\mathbf{a} + \mathbf{b} = \mathbf{y}.

  2. Find the orthogonal projection of 𝐳=(630)\mathbf{z} = \begin{pmatrix} 6 \\ -3 \\ 0 \end{pmatrix} onto the plane spanned by 𝐱\mathbf{x} and 𝐲\mathbf{y}.

In order to calculate the second orthogonal projection, we used the following formula:

Orthogonal Projection onto a Subspace. If VV is a subspace of n\mathbb{R}^n with an orthogonal basis 𝐯1,,𝐯d\mathbf{v}_1, \ldots, \mathbf{v}_d, then ProjV(𝐱)=k=1d(𝐱𝐯k𝐯k𝐯k)𝐯k.\operatorname{Proj}_V(\mathbf{x}) = \sum_{k = 1}^d \left( \frac{\mathbf{x} \cdot \mathbf{v}_k }{\mathbf{v}_k \cdot \mathbf{v}_k} \right) \mathbf{v}_k.

Notice that this formula is even simpler if the basis is orthonormal. Why is that?

QR Decomposition. If AA is a matrix in m×n\mathbb{R}^{m \times n}, then there is a matrix QQ with orthonormal columns and an upper triangular matrix RR such that A=QRA = QR.

You can find the QR decomposition by applying Gram-Schmidt to the columns of AA and normalizing to get the columns of QQ. Then compute R=QTAR = Q^T A.

We did the following examples.

  1. Use Octave to find the QR decomposition for A=(116023130)A = \begin{pmatrix} 1 & 1 & 6 \\ 0 & 2 & -3 \\ -1 & 3 & 0 \end{pmatrix}.

    A = [1 1 6; 0 2 -3; -1 3 0];
    [Q, R] = qr(A)
  2. Find the QR decomposition for B=(1111)B = \begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix}.

Here is a video example with a tall-skinny matrix AA that we did not do in class.

  1. Find the QR decomposition for A=(232411)A = \begin{pmatrix} 2 & 3 \\ 2 & 4 \\ 1 & 1 \end{pmatrix}. (https://youtu.be/J41Ypt6Mftc)

Fri, Mar 6

Today we introduced the idea of floating point operations (FLOPs). Every time a computer needs to add, subtract, multiply or divide two floating point numbers, we count that as one flop. We also count every square root computation as one flop. This is a theoretical estimate of how long it takes the computer to calculate the operation.

After we got started with that workshop, we stopped to talk about computational issues with the Gram-Schmidt algorithm. We analyzed the following Octave code to perform the Gram-Schmidt algorithm.

function Q = cgs(A)
    % Returns a matrix Q with orthonormal columns by applying 
    % the classical Gram-Schmidt algorithm to the columns of A. 
    
    [m,n] = size(A);
    Q = zeros(m,n);
    for i = 1:n
        v = A(:,i);
        for k = 1:i-1
            v = v - (Q(:,k)' * A(:,i)) * Q(:,k);
        end
        Q(:,i) = v / norm(v);
    end
end

We calculated that this algorithm requires 2mn2+mn12n2+12n2mn^2 + mn - \tfrac{1}{2}n^2 + \tfrac{1}{2}n flops for a matrix with mm rows and nn columns. Typically we only focus on the leading term, so we say that asymptotically the algorithm requires 2mn22mn^2 flops.

Unfortunately the Gram-Schmidt algorithm is numerically unstable. One way to show this is to start with an large ill-conditioned matrix AA, and then compute QQ using the algorithm. The columns of QQ usually won’t be orthogonal, which is a problem. We used the Hilbert matrix with (i,j)(i,j) entry equal to 1i+j1\tfrac{1}{i+j-1} to show this.

n = 3
A = hilb(n);
Q = cgs(A);
norm(Q'*Q - eye(n))

Week 9 Notes

Day Topic
Mon, Mar 16 Least squares problems
Wed, Mar 18 Least squares problems - con’d
Fri, Mar 20 Continuous least squares

Mon, Mar 16

Class was canceled today because of the weather. But I sent everyone this workshop to try on your own.

Don’t forget that you can use the SageMathCell to do Octave calculations. Also the command to get the transpose of a matrix A in Octave is A' and the inverse is inv(A).

Since I wasn’t able to explain the technique in class, here is a video with an example similar to the ones in the workshop.

Wed, Mar 18

Today we talked some more about least squares regression. We started with this example.

  1. There has been a steady long term decline in the number of people killed by lightning every year in the United States. The data for the years 1950 to 2020 is shown in the Octave code below. Use Octave to solve for the coefficients of the best fit trend line ŷ=b0+b1x\hat{y} = b_0 + b_1 x.

     years = 1950:2020';
     deaths = [219;248;212;145;220;181;149;180;104;183;129;149;153;165;129;149;110;88;129;131;122;122;94;124;112;124;81;116;98;87;94;87;100;93;91;85;78;99;82;75;74;73;41;43;69;85;53;42;44;46;51;44;51;43;31;38;47;46;28;34;29;26;29;23;26;28;40;16;21;20;17];
     plot(years, deaths, '.')
     title("Lightning Fatalities per Year")

    SageCell link

In the last example we used the normal equations XTX𝐛=XT𝐲X^T X \mathbf{b} = X^T \mathbf{y} to find 𝐛\mathbf{b}.
Unfortunately, in regression problems, the matrix XTXX^T X tends to be very ill-conditioned, so using the normal equations to solve a least squares problem directly is usually not a good idea numerically. Instead, it is better to use the QR-decomposition X=QRX = QR where Qm×nQ \in \mathbb{R}^{m \times n} has orthonormal columns and Rn×nR \in \mathbb{R}^{n \times n} is upper triangular.

  1. Show that XTX=RTRX^T X = R^T R.

With the QR decomposition, the normal equation becomes RTR𝐛=RTQT𝐲.R^T R \mathbf{b} = R^T Q^T \mathbf{y}. We don’t actually need to solve this equation, we just need to find 𝐛\mathbf{b} so that R𝐛R\mathbf{b} equals QT𝐲Q^T \mathbf{y} in all of the rows where RR is not zero.

  1. Show that R𝐛QT𝐲R \mathbf{b} - Q^T \mathbf{y} is in the nullspace of RTR^T if and only if R𝐛R \mathbf{b} equals QT𝐲Q^T \mathbf{y} in every row where RR has a pivot.

Linear algebra packages on computers implement this approach to solve linear regression problems quickly and with very little relative error. In Matlab/Octave you can use the backslash operator to find the least squares solution to X𝐛𝐲X \mathbf{b} \approx \mathbf{y}. We used this to get the least squares solution for the problem above.

# Least squares solution using backslash operator
X = [ones(71, 1), years];
y = deaths;
b = X \ y

You can use least squares regression to find the best coefficients for any model, even nonlinear models, as long as the model is a linear function of the coefficients. For example, if you wanted to model daily high temperatures in Farmville, VA as a function of the day of the year (from 1 to 365), you could use a function like this:

ŷ=b0+b1x+b2cos(2πx365)+b3sin(2πx365).\hat{y} = b_0 + b_1 x + b_2 \cos\left( \dfrac{2\pi x}{365} \right) + b_3 \sin \left( \dfrac{2\pi x}{365} \right).

  1. Use the Octave code below to create a matrix XX with columns corresponding to the four terms of the formula above. Then use the backslash operator to solve for the coefficient vector 𝐛\mathbf{b}.

     days = 1:365';
     temps = [64;64;50;53;52;59;58;41;65;49;42;42;35;32;36;50;51;39;56;54;54;35;35;59;67;45;46;46;40;40;42;39;34;51;63;69;76;71;68;63;42;43;41;37;53;62;45;41;45;38;54;34;60;47;41;63;61;63;48;54;39;54;53;48;44;42;44;43;39;63;60;58;63;74;68;57;56;52;53;56;50;56;60;59;66;57;56;65;75;74;64;50;41;74;74;66;71;78;80;74;76;76;69;67;72;60;71;83;83;73;65;67;73;83;83;82;72;74;83;62;85;86;86;83;83;75;76;79;81;80;77;73;76;66;63;71;78;84;88;92;86;80;78;84;92;88;95;85;92;92;97;84;84;84;78;77;80;87;72;73;77;81;78;76;78;76;82;88;90;86;84;88;82;78;85;90;85;87;92;88;93;93;88;91;93;92;88;87;93;86;85;89;89;91;90;95;93;93;95;92;94;96;100;90;72;82;84;91;90;95;91;93;90;88;86;91;94;84;93;88;91;92;89;91;91;91;93;83;98;93;97;94;95;92;95;85;76;75;74;79;85;85;90;92;90;90;90;94;81;86;91;92;87;89;94;97;83;82;88;92;77;79;75;79;90;95;92;86;85;94;88;93;94;79;90;96;99;83;63;74;81;66;74;75;77;78;64;76;78;64;64;66;66;57;66;65;67;70;68;74;83;72;66;67;79;57;56;59;63;70;63;66;47;46;67;71;53;38;43;50;48;48;47;54;58;55;59;42;57;59;64;59;55;57;43;44;47;56;55;55;56;48;51;49;61;49;42;38;58;58;47;56;47;40;48;45;48;52;50;55;63;61;68;65;71];
     plot(days, temps, '.')
     title("Farmville 2019 High Temperatures")

    SageCell link

We didn’t have time to do an example of this in class, but here is another nice idea. For models that aren’t linear functions of the parameters, you can often change them into a linear model by applying a log-transform. For example, an exponential model ŷ=Cerx\hat{y} = C e^{r x} can be turned into a linear model after we take the (natural) logarithm of both sides: log(ŷ)=logC+rx.\log(\hat{y}) = \log C + r x.

  1. Find the coefficients CC and rr for an exponential model for the decline in lightning fatalities since 1950.

Fri, Mar 20

In discrete least squares problems, you want to minimize the sum of squared deviations over a finite set. In continuous least squares problems, you want to find a polynomial or other kind of function p(x)p(x) that minimizes the integral of the squared distances between p(x)p(x) and some target function f(x)f(x) on a continuous interval [a,b][a,b]: ab(p(x)f(x))2dx.\int_a^b (p(x) - f(x))^2 \, dx.

Before we derived the normal equations for continuous least squares regression, we started with a brief introduction to functional analysis (which is like linear algebra when the vectors are functions).

Definition. An inner-product space is a vector space VV with an inner-product x,y\langle x,y \rangle which is a real-valued function such that for all x,y,zVx,y, z \in V and cc \in \mathbb{R}

  1. x,y=y,x\langle {x,y} \rangle = \langle {y,x} \rangle,
  2. x,x0\langle {x,x} \rangle \ge 0 and x,x=0\langle {x,x} \rangle = 0 if and only if x=0x = 0,
  3. cx,y=cx,y\langle {cx,y} \rangle = c \langle {x,y} \rangle,
  4. x+y,z=x,z+y,z\langle {x+y,z} \rangle = \langle {x,z} \rangle + \langle {y,z} \rangle.

The norm of a vector in an inner-product space is x=x,x1/2\|x\| = \langle {x,x} \rangle^{1/2}.

Examples of inner-product spaces include

To solve a continuous least squares regression problem we need to find the orthogonal projection of a function fL2[a,b]f \in L^2[a,b] onto a subspace.

It helps if you have an orthogonal basis for the subspace. Then you can use this formula:

Orthogonal Projection Onto a Subspace with an Orthogonal Basis

Suppose that ϕ1,,ϕn\phi_1, \ldots, \phi_n is an orthogonal basis for a subspace VV in an inner-product space. Then the orthogonal projection of ff onto VV is ProjV(f)=k=1nf,ϕkϕk,ϕkϕk.\operatorname{Proj}_V(f) = \sum_{k = 1}^n \frac{\langle {f, \phi_k} \rangle}{\langle {\phi_k, \phi_k} \rangle} \phi_k.

  1. The polynomials 1,x,x21, x, x^2 are a basis for the 2nd degree polynomials in L2[1,1]L^2[-1,1]. Use the Gram-Schmidt process to find an orthogonal basis. Hint: first calculate the following inner-products in L2[1,1]L^2[-1,1]:
    1. 1,1\langle {1, 1} \rangle
    2. x,1\langle {x, 1} \rangle
    3. x2,1\langle {x^2, 1} \rangle
    4. x,x\langle {x, x} \rangle
    5. x2,x\langle {x^2, x} \rangle
    Is there a pattern for when the inner-products are zero?

The solution to the last problem is a special orthogonal basis called the (monic) Legendre polynomials. If you continue the Gram-Schmidt process, you can find Legendre polynomials of any degree.

  1. Use the Legendre polynomials to find the orthogonal projection of the function f(x)=exf(x) = e^x onto the 2nd degree polynomials in L2[1,1]L^2[-1,1].

Week 10 Notes

Day Topic
Mon, Mar 23 Orthogonal functions
Wed, Mar 25 Fourier series
Fri, Mar 27 Polynomial interpolation

Mon, Mar 23

We started with this example that we didn’t have time to finish in class last time:

  1. Use the Legendre polynomials to find the orthogonal projection of the function f(x)=exf(x) = e^x onto the 2nd degree polynomials in L2[1,1]L^2[-1,1].

Then we talked about some shortcuts that mathematicians uses when dealing with integrals.

  1. If f(x)f(x) is odd, then what is 11f(x)dx\int_{-1}^1 f(x) \, dx?

  2. When is a polynomial an odd function? When is a polynomial an even function?

  3. What happens when you multiply two even functions? What about two odd functions? What happens if you multiply an even function with an odd function?

  4. Explain why the inner-product of an odd function with an even function must be zero in L2[1,1]L^2[-1, 1].

The Legendre polynomials on the interval [1,1][-1,1] aren’t the only example of an orthogonal set of functions. Probably the most important example of an orthogonal set of functions is the Fourier basis {cos(nπx),sin(nπx):n}{12}.\{\cos(n \pi x), \sin( n \pi x) : n \in \mathbb{N}\} \cup \left\{ \frac{1}{\sqrt{2}} \right\}. on the interval [1,1][-1,1]. Any function in L2[1,1]L^2[-1,1] can be approximated by using continuous least squares with these trig functions. Since there are an infinite number of functions in this orthonormal set, we usually stop the approximation when we reach a high enough frequency nn.

  1. Look at the graphs of sin(nπx)\sin (n \pi x) and cos(nπx)\cos( n \pi x) for different values of nn. Why is 11cos(nπx)dx=11sin(nπx)dx=0\int_{-1}^1 \cos (n \pi x) \, dx = \int_{-1}^1 \sin (n \pi x) \, dx = 0 for every positive integer nn?

  2. Use the trig product formulas below to explain why the functions cos(nπx)\cos(n \pi x) and sin(nπx)\sin(n \pi x) are all orthogonal to each other.

    cos(α)cos(β)=12[cos(α+β)+cos(αβ)]cos(α)sin(β)=12[sin(α+β)+sin(βα)]sin(α)cos(β)=12[sin(α+β)+sin(αβ)]sin(α)sin(β)=12[cos(αβ)cos(α+β)]\begin{align*} \cos(\alpha) \cos(\beta) &= \tfrac{1}{2}[\cos(\alpha+\beta)+\cos(\alpha - \beta)] \\ \cos(\alpha) \sin(\beta) &= \tfrac{1}{2}[\sin(\alpha+\beta)+\sin(\beta - \alpha)] \\ \sin(\alpha) \cos(\beta) &= \tfrac{1}{2}[\sin(\alpha+\beta)+\sin(\alpha - \beta)] \\ \sin(\alpha) \sin(\beta) &= \tfrac{1}{2}[\cos(\alpha-\beta)-\cos(\alpha + \beta)] \end{align*}

  3. Use Desmos with the orthogonal projection formula Proj(f)=k=1nϕk,fϕk,ϕkϕk\operatorname{Proj}(f) = \sum_{k = 1}^n \frac{\langle {\phi_k, f} \rangle}{\langle {\phi_k, \phi_k} \rangle} \phi_k to find the projection of f(x)=exf(x) = e^x onto the span of the Fourier basis (up to a frequency of n=10n = 10).

Wed, Mar 25

Fri, Mar 27

Today we started talking about polynomial interpolation. An interpolating polynomial is a polynomial that passes through a set of points in the coordinate plane. We started with an example using these four points: (1,4)(-1,-4), (0,3)(0,3), (1,0)(1,0), and (5,8)(5,8).

Desmos link

In interpolation, the x-values are called nodes and the y-values are called values.

Theorem. For any set of n+1n+1 different nodes and n+1n+1 values, there is a unique nn-th degree interpolating polynomial p(x)p(x) that hits those values at those nodes.

In order to find the interpolating polynomial, we used Vandermonde matrices again. For any set of fixed nodes x0,x1,,xnx_0, x_1, \ldots, x_n, the Vandermonde matrix for those nodes is the matrix V(n+1)×(n+1)V \in \mathbb{R}^{(n+1) \times (n+1)} such that the entry VijV_{ij} in row ii and column jj is xij.x_i^j. In other words, VV looks like V=(1x0x02x0n1x1x12x1n1x2x22x2n1xnxn2xnn)V = \begin{pmatrix} 1 & x_0 & x_0^2 & \ldots & x_0^n \\ 1 & x_1 & x_1^2 & \ldots & x_1^n \\ 1 & x_2 & x_2^2 & \ldots & x_2^n \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_n & x_n^2 & \ldots & x_n^n \end{pmatrix} Notice that when working with Vandermonde matrices, we always start counting the rows and columns with i,j=0i,j = 0.

Using the Vandermonde matrix VV, we can find an nn-th degree interpolating polynomial p(x)=b0+b1x+b2x2++bnxnp(x) = b_0 + b_1 x + b_2 x^2 + \ldots + b_n x^n by solving the system V𝐛=𝐲V\mathbf{b} = \mathbf{y} where 𝐛=(b0,b1,,bn)\mathbf{b} = (b_0, b_1, \ldots, b_n) is the vector of coefficients and 𝐲=(y0,y1,yn)\mathbf{y} = (y_0, y_1, \ldots y_n) is the vector of yy-values corresponding to each node xix_i. That is, we want to solve the following system of linear equations: (1x0x02x0n1x1x12x1n1x2x22x2n1xnxn2xnn)(b0b1bn)=(y0y1yn).\begin{pmatrix} 1 & x_0 & x_0^2 & \ldots & x_0^n \\ 1 & x_1 & x_1^2 & \ldots & x_1^n \\ 1 & x_2 & x_2^2 & \ldots & x_2^n \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_n & x_n^2 & \ldots & x_n^n \end{pmatrix} \begin{pmatrix} b_0 \\ b_1 \\ \vdots \\ b_n \end{pmatrix} = \begin{pmatrix} y_0 \\ y_1 \\ \vdots \\ y_n \end{pmatrix}.

Here is Octave code to get the Vandermonde matrix and solve for the coefficients of the interpolating polynomial.

V = fliplr(vander([-1,0,1,5]))
y = [-4; 3; 0; 8]
b = V \ y 
SageCell link

Therefore the solution is p(x)=3+x5x2+x3p(x) = 3 + x - 5x^2 + x^3

After that example, we did the following examples in class.

  1. Find an interpolating polynomial for these points: (0,0)(0,0), (1,4)(1,4), (1,0)(-1,0), and (2,15).(2,15).

  2. Find a 4th degree polynomial that passes through (0,1)(0,1), (1,5)(1,5), (2,31)(2, 31), (3,121)(3, 121), (4,341).(4, 341).

So far, we have found interpolating polynomials that are linear combinations of the standard monomial basis {1,x,x2,,xn}\{1, x, x^2, \ldots, x^n\} for the space of all nn-th degree polynomials. There are other bases we could choose.

For any given set of nodes {x0,x1,,xn}\{x_0, x_1, \ldots, x_n\}, the Lagrange polynomials are Lk(x)=i=0,ikn(xxi)i=0,ikn(xkxi), for each k=0,,n.L_k(x) = \frac{\prod_{i = 0, i \ne k}^n (x - x_i)}{\prod_{i = 0, i \ne k}^n (x_k - x_i)}, \text{ for each } k = 0, \ldots, n.

The defining feature of the Lagrange polynomials is that Lk(xi)={1 if i=k0 otherwise.L_k(x_i) = \begin{cases} 1 & \text{ if } i = k \\ 0 & \text{ otherwise.} \end{cases}
Figure: The Lagrange polynomial L2(x)L_2(x) with nodes x=0,,5x = 0, \ldots, 5.

Lagrange Polynomial Interpolation Formula

The interpolating polynomial passing through (x0,y0),(x1,y1),,(xn,yn)(x_0,y_0), (x_1, y_1), \ldots, (x_n, y_n) is p(x)=y0L0(x)+y1L1(x)++ynLn(x).p(x) = y_0 L_0(x) + y_1 L_1(x) + \ldots + y_n L_n(x).

  1. Find the Lagrange polynomials for the nodes x0=1,x1=0,x2=1,x3=5x_0 = -1, x_1 = 0, x_2 = 1, x_3 = 5.

  2. Express the interpolating polynomial for the points (1,4),(0,3),(1,0),(5,8)(-1,-4), (0,3), (1,0), (5,8) using those Lagrange polynomials.

Here’s an example with a video that is similar to the one we did in class:

  1. Find a 2nd degree polynomial that goes through the points (1,2)(1,2), (2,3)(2,3) and (4,11)(4,11). (https://youtu.be/B67wkZ3DWc0)

Week 11 Notes

Day Topic
Mon, Mar 30 Newton polynomials
Wed, Apr 1 Divided differences
Fri, Apr 3 Interpolation error

Week 12 Notes

Day Topic
Mon, Apr 6 Interpolation error - con’d
Wed, Apr 8 Review
Fri, Apr 10 Midterm 2

Week 13 Notes

Day Topic
Mon, Apr 13 Numerical integration
Wed, Apr 15 Newton-Cotes methods
Fri, Apr 17 Error in Newton-Cotes methods

Week 14 Notes

Day Topic
Mon, Apr 20 Numerical differentiation
Wed, Apr 22 Numerical solutions of ODEs
Fri, Apr 24 Runge-Kutta methods
Mon, Apr 27 Last day, recap & review