Numerical Analysis Notes

Math 342 - Spring 2024

Jump to: Syllabus, 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 Section Topic
Wed, Jan 17 1.2 Floating point arithmetic
Fri, Jan 19 1.2 Significant digits, relative error

Wed, Jan 17

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 x=(1)s*(1.a1a2a52)2*2e1023x = (-1)^s * (1.a_1 a_2 \ldots a_{52})_2 * 2^{e - 1023} where

To understand floating point numbers, we also reviewed binary numbers, scientific notation, and logarithmic scales.

We did the following exercises in class:

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

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

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

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

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

Fri, Jan 19

Today we talked about significant digits. Here is a quick video on how these work. Then we defined absolute and relative error:

Let x*x^* be an approximation of xx \in \mathbb{R}.

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. Keep in mind these rules:

  1. When you add/subtract numbers, the last common digit that is significant for both numbers is the last significant digit of the answer.
  2. When you multiply/divide two numbers, the result has significant digits equal to the minimum number of significant digits of the two inputs.

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 finished by looking at how you can sometimes re-write algorithms on a computer to avoid overflow/underflow issues. Stirling’s formula is an approximation for n!n! which has a relative error that gets smaller as nn increases.

We used the math library in Python to test Stirling’s formula, which is the following approximation n!2πnnnen.n! \approx \sqrt{2 \pi n} \frac{n^n}{e^n}.

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.exp(1))**n
print(f(n))

Week 2 Notes

Day Section Topic
Mon, Jan 22 Taylor’s theorem
Wed, Jan 24 Taylor’s theorem - con’d
Fri, Jan 26 The Babylonian algorithm

Mon, Jan 22

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

The we did the following workshop in class.

Wed, Jan 24

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}.

The proof of both the Mean Value Theorem and Taylor’s Theorem comes from looking at an even simpler theorem called Rolle’s theorem.

Rolle’s Theorem. Let ff be a function that is differentiable in the interval between aa and bb and suppose that f(a)=f(b)f(a) = f(b). Then there exists a cc strictly inside the interval from aa to bb such that f(c)=0.f'(c) = 0.

We briefly sketched an intuitive proof of Rolle’s theorem using the Extreme Value Theorem from calculus, but the details of that proof are not really that important.

We did the following exercises in class.

  1. Use Taylor’s theorem to estimate the error in using the 20th degree Maclaurin series to estimate sin(4π)\sin(4\pi).

  2. Use Taylor’s theorem to estimate the error in using the 20th degree Maclaurin series to estimate e6e^6.

We finished with a proof that the number ee is irrational. First we temporarily assumed that ee is a reduced fraction mn\tfrac{m}{n}. Then we calculated the worst remainder for the nnth degree Maclaurin polynomial for exe^x at x=1x = 1. We did the following exercises that lead to a contradiction:

  1. Show that n!(ePn(1))n!(e - P_n(1)) must be an integer.

  2. Use Taylor’s theorem to show that n!Rn(1)n! R_n(1) must be strictly between 0 and 1.

Fri, Jan 26

Today we did a workshop about the Babylonian algorithm which is an ancient method for finding square roots.

As part of this workshop we also covered how to define variables and functions in Python and also how to use for-loops and while-loops.


Week 3 Notes

Day Section Topic
Mon, Jan 29 2.1 Bisection method
Wed, Jan 31 2.3 Newton’s method
Fri, Feb 2 2.4 Rates of convergence

Mon, Jan 29

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. We did the following workshop.

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.\text{Absolute error} \le \frac{(b-a)}{2^n}. We saw that it takes about 10 iterations to increase the accuracy by 3 decimal places (because 2101032^{10} \approx 10^3).

We finished by comparing the bisection method for finding roots with the Babylonian algorithm for finding square roots. Why are square roots called roots? Because every square root is a root of a square function. For example, 5\sqrt{5} is a root of x25=0x^2 - 5 = 0.

Wed, Jan 31

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. The error in the nn-th iterate of Newton’s method satisfies |xnr|(M2L)2n1|x0r|2n.|x_n-r| \le \left(\frac{M}{2L}\right)^{2^n-1} |x_0 - r|^{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!

Fri, Feb 2

Today we looked at some examples of what can go wrong with Newton’s method. We did these examples:

  1. What happens if you use Newton’s method with x0=0x_0 = 0 on f(x)=x32x+2f(x) = x^3 - 2x + 2?

  2. Why doesn’t Newton’s method work for f(x)=x1/3f(x) = x^{1/3}?

We also did this workshop.


Week 4 Notes

Day Section Topic
Mon, Feb 5 2.5 Secant method
Wed, Feb 7 2.2 Fixed point iteration
Fri, Feb 9 2.4 More about rates of convergence

Mon, Feb 5

We talked about the secant method which 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})}.

We wrote the following program in class.

def Secant(f, a, b, precision = 10**(-8)):
    while abs(b-a) > precision:
        a, b = b, b - f(b)*(b-a)/(f(b)-f(a))
    return b

Sometimes a function ff might be very time consuming for a computer to compute, so you could improve this function by reducing the number of times you have to call ff. If speed is a concern, then this would be a better version of the function.

def Secant(f, a, b, precision = 10**(-8)):
    fa = f(a)
    fb = f(b)
    while abs(b-a) > precision:
        a, b = b, b - fb*(b-a)/(fb-fa) # update x-values
        fa, fb = fb, f(b) # update y-values using the new x-value
    return b

Notice how we call the function ff three times in each iteration of the while-loop in the first program, but by storing the result in the variables fa and fb, we only have to call ff once per iteration in the second version of the program.

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

We finished by talking about the convergence rate of the secant method.

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). There is a constant C>0C > 0 such that for xnx_n, xn1x_{n-1} sufficiently close to rr (say |x0r|<1/C|x_0 - r| < 1/C and |x1r|<1/C|x_1 - r| < 1/C), the next iterate of the secant method has |xn+1r|C|xnr||xn1r|.|x_{n+1} - r| \le C |x_n-r| \, |x_{n-1} - r|. In particular, |xnr||x_n - r| will converge to rr.

Note, the constant CC might be larger than the constant M2L\dfrac{M}{2L} from Newton’s method, but it is usually not much larger.

  1. Use this formula to estimate |x3r||x_3-r| in terms of |x1r||x_1-r| and |x0r||x_0 - r|. Assume that the same constant CC applies for all xn+1x_{n+1}.
  2. Do the same for |x4r||x_4 - r|.
  3. Keep going until you find a pattern.

We saw that the pattern is that the exponents of each factor is a Fibonacci number. We talked briefly about Binet’s formula for Fibonacci numbers and the golden ratio φ=1+521.618\varphi = \frac{1 + \sqrt{5}}{2} \approx 1.618. The lead to the following nice rule of thumb: The number of correct decimal places in the secant method increases by a factor of about 1.6 (the golden ratio) every step.

Wed, Feb 7

Newton’s method is a special case of a method known as fixed point iteration. A of a function f(x)f(x) is a number pp such that f(p)=pf(p) = p. Not every function has a fixed point, but we do have the following existence result:

Theorem. Let fC0[a,b]f \in C^0[a,b]. If f(x)[a,b]f(x) \in [a,b] for every x[a,b]x \in [a,b], then ff must have a fixed point in [a,b][a,b].

  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.

A fixed point pp is attracting if for all x0x_0 sufficiently close to pp, the recursive sequence defined by xn+1=f(xn)x_{n+1} = f(x_n) converges to pp. It is repelling if no (sub)sequence of xnx_n ever converges to pp. You can draw a picture of these fixed point iterates by drawing a cobweb diagram.

  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.

You can sometimes use fixed point iteration to solve equations. For example, here are two different ways to solve the equation x3+3x+6=0x^3 + 3x + 6 = 0 using fixed point iteration.

  1. Re-write the equation as 6x2+3=x\dfrac{-6}{x^2+3} = x.

  2. Replace f(x)=0f(x) = 0 with the equation x+cf(x)=xx + cf(x) = x where cc is a small constant. The constant c=110c = -\tfrac{1}{10} works well for the function above.

When a sequence xnx_n converges to a root rr, we say that it has a linear order of convergence if there is a constant 0<C<10 < C < 1 such that |xn+1r|C|xnr| for all n.|x_{n+1} - r| \le C |x_n - r| \text{ for all } n. We say that the sequence has a quadratic order of convergence if there is a constant C>0C > 0 such that |xn+1r|C|xnr|2 for all n.|x_{n+1} - r| \le C |x_n - r|^2 \text{ for all } n. More generally, a sequence converges with order α\alpha if there is are constants C>0C > 0 and α>1\alpha > 1 such that |xn+1r|C|xnr|α for all n.|x_{n+1} - r| \le C |x_n - r|^\alpha \text{ for all } n.

In general, a sequence that converges with order α>1\alpha > 1 will have the number of correct decimal places grow by a factor of about α\alpha each step. Newton’s method is order 2, Secant method is order φ1.618\varphi \approx 1.618, and the Bisection method is only linear order.

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.

Fri, Feb 9

We started with this question:

  1. Why is Newton’s method a special case of fixed point iteration? When we apply Newton’s method to find a root of f(x)f(x), what function N(x)N(x) are we iterating? What is the derivative of NN at the root rr?

Then we did this workshop in class.

In one step of the workshop, we used the triangle inequality which says that for any two numbers aa and bb, |a+b||a|+|b|.|a+b| \le |a| + |b|.


Week 5 Notes

Day Section Topic
Mon, Feb 12 Systems of linear equations
Wed, Feb 14 LU decomposition
Fri, Feb 16 Matrix norms

Mon, Feb 12

Today we talked about systems of linear equations and linear algebra. Before we got to that, we looked at one more cool thing about Newton’s method. It also works for to find complex number roots if you use complex numbers. We talked about the polynomial x31=(x1)(x2+x+1)x^3 - 1 = (x-1)(x^2+x+1) which has three roots: x=1x = 1 and x=1±i32x = \dfrac{-1 \pm i \sqrt{3}}{2}. We talked about which complex numbers end up converging to which root as you iterate Newton’s method. You get a beautiful fractal pattern:

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

After that we started a review of row reduction from 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 Ax=bAx = b where A3×4A \in \mathbb{R}^{3 \times 4}, x4x \in \mathbb{R}^4 is the unknown vector, and b3b \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)

Then the variables p,np, n, and dd are pivot variables, and the last variable qq is a free variable. Each pivot variable depends on the value(s) of the free variables. A solution of a system of equations is a formula for the pivot variables as functions of the free variables.

Recall the following terminology from linear algebra. For any matrix Am×nA \in \mathbb{R}^{m \times n} (i.e., that has real number entries with mm rows and nn columns):

Rank + Nullity Theorem. Let Am×nA \in \mathbb{R}^{m \times n}. Then the rank of AA plus the nullity of AA must equal nn.

A matrix equation Ax=bAx = b has a solution if and only if bb is in the column space of AA. If bb 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 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).

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

  2. Use the inverse to solve (1325)x=(21)\begin{pmatrix} 1 & 3 \\ 2 & 5 \end{pmatrix} x = \begin{pmatrix} 2 \\ 1 \end{pmatrix}.

In practice, inverse matrices are rarely used to solve systems of linear equations for a couple of reasons.

  1. Most matrices aren’t invertible.
  2. Finding the inverse is at least as hard computationally as row reduction, so you might as well just use row reduction.

Wed, Feb 14

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)

Fri, Feb 16

Today we talked about what it means for a linear system to be ill-conditioned. This is when a small change in the vector bb can produce a large change in the solution vector xx for a linear system Ax=bAx=b.

Consider the following matrix:

A=(1111.001)A = \begin{pmatrix} 1 & 1 \\ 1 & 1.001 \end{pmatrix}

Let y=(22)y = \begin{pmatrix} 2 \\ 2 \end{pmatrix} and z=(22.001)z = \begin{pmatrix} 2 \\ 2.001 \end{pmatrix}.

  1. Solve Ax=yAx = y and Ax=zAx = z. Hint: A1=(1001100010001000)A^{-1} = \begin{pmatrix} 1001 & -1000 \\ -1000 & 1000 \end{pmatrix}. Notice that even though yy and zz are very close, the two solutions are not close at all. A matrix AA with the property that solutions of Ax=bAx = b are very sensitive to small changes in bb is called ill-conditioned.

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.

  1. Use the LU-decomposition to solve Bx=(12).Bx = \begin{pmatrix} 1 \\ 2 \end{pmatrix}.
  1. The inverse of the matrix LL in the LU decomposition above is L1=(1010001).L^{-1} = \begin{pmatrix} 1 & 0 \\ -1000 & 1 \end{pmatrix}. Show that LL is ill-conditioned by finding a vector yy' close the y=(12)y = \begin{pmatrix} 1 \\ 2\end{pmatrix}, but such that the corresponding solutions xx and xx' to the matrix equations Lx=yLx = y and Lx=yLx' = y' are not close.

Norms of Vectors

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

  1. x=0\|x\| = 0 if and only if x=0x=0.
  2. cx=|c|x\|c x \| = |c| \|x\| for all xVx \in V and cc \in \mathbb{R}.
  3. 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 operator norms (also known as induced 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.

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}\| (using any induced norm).

Rule of thumb. If the entries of AA and bb 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 Ax=bAx = b will be accurate to nkn-k significant digits.


Week 6 Notes

Day Section Topic
Mon, Feb 19 Condition numbers
Wed, Feb 21 Review
Fri, Feb 23 Midterm 1

Mon, Feb 19

Theorem. If An×nA \in \mathbb{R}^{n \times n} is invertible, then the relative error in the solution of the system Ax=bA x = b is bounded by xxxκ(A)bbb.\frac{\|x-x'\|}{\|x\|} \le \kappa(A) \frac{\|b-b'\|}{\|b\|}.

Proof. Using the properties of the induced norm, b=AxAx and xx=A1(bb)A1bb,\|b\| = \|A x \| \le \|A\| \, \|x\| \text{ and } \|x-x'\| = \|A^{-1}(b-b')\| \le \|A^{-1}\| \, \|b-b'\|, so putting both together gives bxxAA1xbb.\|b\| \|x-x'\| \le \|A\| \, \|A^{-1}\| \, \|x\| \, \|b-b'\|.
This leads directly to the inequality above when you separate the factors with xx from those with bb. □

This explains why the number of significant digits in the solution to Ax=bA x = b may have up to kk fewer significant digits than the entries of AA and bb when the condition number κ(A)=10k\kappa(A) = 10^k.

  1. Let A=(1.0001.0011.0001.000)A = \begin{pmatrix} 1.000 & 1.001 \\ 1.000 & 1.000 \end{pmatrix} and b=(2.0002.001)b = \begin{pmatrix} 2.000 \\ 2.001 \end{pmatrix}. How many significant digits does the solution to Ax=bAx = b have?

Last time we saw an example of a matrix B=(0.001111)B = \begin{pmatrix} 0.001 & 1 \\ 1 & 1 \end{pmatrix} which is not ill-conditioned by itself. However, both LL and UU in its LU-decomposition were ill-conditioned. It is possible to avoid this problem using the method of partial pivoting. The idea is simple: when more than one entry could be the pivot for a column, always choose the one with the largest absolute value.

You keep track of the row swaps as you use the method of partial pivoting, always apply the same row swaps to a copy of the identity matrix. At the end, you will have a permutation matrix PP and the LU-decomposition with partial pivoting is PA=LU.PA = LU. The fixes two problems:

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.

Find the LU-decomposition with partial pivoting for these matrices

  1. A=(012111)A = \begin{pmatrix} 0 & 1 & 2 \\ 1 & 1 & 1 \end{pmatrix}

  2. A=(123456789)A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix}

Wed, Feb 21

Today we reviewed for the midterm exam. We reviewed the things you need to memorize. We also talked about the following problems.

  1. Find the LU decomposition of (103429260)\begin{pmatrix} 1 & 0 & 3 \\ 4 & 2 & 9 \\ & -2 & -6 & 0 \end{pmatrix}.

  2. Find and classify the fixed points of f(x)=x38+1f(x) = \dfrac{x^3}{8} + 1. This was a little hard to solve, because it isn’t easy to factor the polynomial x38x+8x^3 - 8x + 8. But it does have computable roots 22 and 1±51 \pm \sqrt{5}.

  3. How would you use secant method to find the one negative root of x38x+8x^3 - 8x + 8? What would make good choices for x0x_0 and x1x_1? What is x2x_2 for those choices?

  4. If a=7.911×1017a = 7.911 \times 10^{-17} and b=5.032×1015b = 5.032 \times 10^{-15}, then how many significant digits do the following have?

    1. aba - b.
    2. a/ba/b.

Week 7 Notes

Day Section Topic
Mon, Feb 26 3.1 Polynomial interpolation, Vandermonde matrices
Wed, Feb 28 3.1 Polynomial bases, Lagrange & Newton polynomials
Fri, Mar 1 3.2 Newton’s divided differences

Mon, Feb 26

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 order to find the interpolating polynomial, we introduced Vandermonde matrices. For any set of fixed xx-values, x0,x1,,xnx_0, x_1, \ldots, x_n, the Vandermonde matrix for those values 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 polynomial p(x)=a0+a1x+a2x2++anxnp(x) = a_0 + a_1 x + a_2 x^2 + \ldots + a_n x^n that passes through the points (x0,y0),(x1,y1),(xn,yn)(x_0,y_0), (x_1,y_1), \ldots (x_n,y_n) by solving the system Va=yVa = y where a=(a0,a1,,an)a = (a_0, a_1, \ldots, a_n) is the vector of coefficients and y=(y0,y1,yn)y = (y_0, y_1, \ldots y_n) is the vector with yy-values corresponding to each xix_i. That is, we want to solve the following system of linear equations: (1x0x02x0n1x1x12x1n1x2x22x2n1xnxn2xnn)(a0a1an)=(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} a_0 \\ a_1 \\ \vdots \\ a_n \end{pmatrix} = \begin{pmatrix} y_0 \\ y_1 \\ \vdots \\ y_n \end{pmatrix}.

In Python with the Numpy library, you can enter a Vandermonde matrix using a list comprehension inside another list comprehension. For example, the Vandermonde matrix for xx-values 1,0,1,5-1, 0, 1, 5 can be entered as follows.

import numpy as np
V = np.array([[x**k for k in range(4)] for x in [-1,0,1,5]])

Then the function np.linalg.solve(V,y) will solve the system Va=yV a = y. For example, after entering VV, we would solve the first example as follows.

y = np.array([-4, 3, 0 8])
a = np.linalg.solve(V,y)
print(a) # prints [ 3.  1. -5.  1.]

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).

Wed, Feb 28

Last time, when we talked about polynomial interpolation, we wrote our interpolating polynomials as 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. Today we introduce two alternative bases: the Lagrange polynomials and the Newton polynomials. Both require a set of n+1n+1 distinct xx-values called nodes, x0,,xnx_0, \ldots, x_n. For any given set of nodes, the Lagrange polynomials are Lk(x)=i=0,ikn(xxi)i=0,ikn(xkxi),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)}, 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} From this we saw that 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 y0L0(x)+y1L1(x)++ynLn(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. Use those Lagrange polynomials to find the interpolating polynomial that passes through (1,4),(0,3),(1,0),(5,8)(-1,-4), (0,3), (1,0), (5,8).

  3. Express the interpolating polynomial that passes through (1,6),(1,0),(2,6)(-1,-6), (1,0), (2,6) as a linear combination of Lagrange polynomials.

We finished by describing the Newton polynomials which are

Nk(x)={1 if k=0i=0k1(xxi) if k=1,,n.N_k(x) = \begin{cases} 1 & \text{ if } k = 0 \\ \prod_{i = 0}^{k-1} (x- x_i) & \text{ if } k = 1, \ldots, n. \end{cases}

You can express an interpolating polynomial as a linear combination of Newton polynomials by solving the linear system

(N0(x0)N1(x0)N2(x0)Nn(x0)N0(x1)N1(x1)N2(x1)Nn(x1)N0(x2)N1(x2)N2(x2)Nn(x2)N0(xn)N1(xn)N2(xn)Nn(xn))(c0c1cn)=(y0y1yn)\begin{pmatrix} N_0(x_0) & N_1(x_0) & N_2(x_0) & \ldots & N_n(x_0) \\ N_0(x_1) & N_1(x_1) & N_2(x_1) & \ldots & N_n(x_1) \\ N_0(x_2) & N_1(x_2) & N_2(x_2) & \ldots & N_n(x_2) \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ N_0(x_n) & N_1(x_n) & N_2(x_n) & \ldots & N_n(x_n) \\ \end{pmatrix} \begin{pmatrix} c_0 \\ c_1 \\ \vdots \\ c_n \end{pmatrix} = \begin{pmatrix} y_0 \\ y_1 \\ \vdots \\ y_n \end{pmatrix} to find coefficients such that the interpolating polynomial c0N0(x)+c1N1(x)++cnNn(x)c_0 N_0(x) + c_1 N_1(x) + \ldots + c_n N_n(x) passes through each point (xi,yi)(x_i, y_i).

  1. Solve the linear system above to find the interpolating polynomial through (1,4),(0,3),(1,0),(5,8)(-1,-4), (0,3), (1,0), (5,8) expressed in terms of the Newton basis.

Fri, Mar 1

Today we talked about the method of divided differences, which lets us find the coefficients of an interpolating polynomial expressed using the Newton basis.

For a function ff and a set of n+1n+1 distinct xx-values, x0,,xnx_0, \ldots, x_n, the divided differences are defined recursively by the following formula

f[xj,,xk]={f[xj+1,,xk]f[xj,,xk1]xkxj if k>j,f(xj) if k=j.f[x_j, \ldots, x_k] = \begin{cases} \dfrac{f[x_{j+1}, \ldots, x_k] - f[x_j,\ldots, x_{k-1}]}{x_k - x_j} & \text{ if } k > j, \\ f(x_j) & \text{ if }k = j.\end{cases}

We did these examples.

  1. Make a divided differences table for the points (1,4),(0,3),(1,0),(5,8)(-1,-4), (0,3), (1,0), (5,8), and use it to find the interpolating polynomial (in Newton form).

  2. Use the xx-values π-\pi, π/2-\pi/2, 00, π/2\pi/2, π\pi to make an interpolating polynomial for f(x)=cosx.f(x) = \cos x.

    Solution

    The table of divided differences is:

    xx

    f(x)f(x)

    DD1

    DD2

    DD3

    DD4

    π-\pi

    1-1

    2π\frac{2}{\pi}

    π2-\frac{\pi}{2}

    00

    00

    2π\frac{2}{\pi}

    83π3-\frac{8}{3\pi^3}

    00

    11

    4π2-\frac{4}{\pi^2}

    83π4\frac{8}{3\pi^4}

    2π-\frac{2}{\pi}

    83π3\frac{8}{3\pi^3}

    π2\frac{\pi}{2}

    00

    00

    2π-\frac{2}{\pi}

    π\pi

    1-1

    So the Newton form of the interpolating polynomial is 1+2π(x+π)83π3x(x+π)(x+π2)+83π4x(x+π)(x+π2)(xπ2).-1 + \frac{2}{\pi} (x + \pi) - \frac{8}{3\pi^3} x (x + \pi) (x+ \tfrac{\pi}{2}) + \frac{8}{3\pi^4} x (x+\pi) (x + \tfrac{\pi}{2}) (x - \tfrac{\pi}{2}). Notice that the coefficients are just the numbers (in blue) at the top of each column in the divided differences table.

Here are some videos with additional examples:

After those examples, we did this workshop in class:


Week 8 Notes

Day Section Topic
Mon, Mar 4 3.3 Interpolation error
Wed, Mar 6 3.3 Interpolation error - con’d
Fri, Mar 8 3.4 Chebyshev polynomials

Mon, Mar 4

Often the interpolating polynomial PnP_n is constructed for a function ff so that Pn(xk)=f(xk)P_n(x_k) = f(x_k) for each node x0,,xnx_0, \ldots, x_n. Then we call PnP_n an interpolating polynomial for the function ff. Using interpolating polynomials is one way to approximate a function. For example, we did this last time with the function f(x)=cosxf(x) = \cos x.

  1. Find the 2nd degree interpolating polynomial for f(x)=10xf(x)=10^x with nodes x0=0,x1=1,x2=2.x_0=0, x_1=1, x_2 = 2. Desmos graph

Here are some important results about these approximations.

Mean Value Theorem for Divided Differences. Let x0,,xnx_0, \ldots, x_n be distinct nodes in [a,b][a,b] and let fCn[a,b]f \in C^{n}[a,b]. Then there exists a number ξ\xi between the values of x0,,xnx_0, \ldots, x_n such that f[x0,,xn]=f(n)(ξ)n!.f[x_0, \ldots, x_n] = \frac{ f^{(n)}(\xi) }{n!}.

Proof. Let Pn(x)P_n(x) be the nn-th degree interpolating polyomial for ff at x0,,xnx_0,\ldots, x_n. The function fPnf-P_n has n+1n+1 roots, so its derivative must have nn roots, and so on, until the n-th derivative has at least one root. Call that root ξ\xi. Then f(n)(ξ)=Pn(n)(ξ)f^{(n)}(\xi) = P_n^{(n)}(\xi). However, PnP_n is a linear combination of Newton basis polynomials and only the last Newton basis polynomial is nn-th degree. Its coefficient in the interpolating polynomial is f[x0,,xn]f[x_0, \ldots, x_n] so when you take nn derivatives of PnP_n, you get n!f[x0,,xn]n! f[x_0, \ldots, x_n] which completes the proof. □

Interpolation Error Theorem. Let x0,,xnx_0, \ldots, x_n be distinct nodes in [a,b][a,b] and let fCn+1[a,b]f \in C^{n+1}[a,b]. If Pn(x)P_n(x) is the nn-th degree interpolating polynomial for those nodes, then for each x[a,b]x \in [a,b], there exists a number ξ\xi between x0,,xnx_0, \ldots, x_n, and xx such that f(x)Pn(x)=f(n+1)(ξ)(n+1)!(xx0)(xxn).f(x)-P_n(x) = \frac{f^{(n+1)}(\xi)}{(n+1)!} (x-x_0) \cdots (x-x_n).

Proof. Add xx to the list of nodes and construct the (n+1)(n+1)-th degree interpolating polynomial Pn+1P_{n+1}. Then using the Newton form for both interpolating polynomials, Pn+1(x)Pn(x)=f[x0,,xn,x](xx0)(xxn).P_{n+1}(x) - P_n(x) = f[x_0,\ldots,x_n,x](x-x_0)\cdots (x-x_n). So by the MVT for Divided Differences, there exists ξ\xi between xx and x0,,xnx_0, \ldots, x_n such that f(x)Pn(x)=Pn+1(x)Pn(x)=f(n+1)(ξ)(n+1)!(xx0)(xxn).f(x)-P_n(x) = P_{n+1}(x)-P_n(x) = \frac{f^{(n+1)}(\xi)}{(n+1)!} (x-x_0) \cdots (x-x_n). ~ □

We finished with the following example.

  1. Estimate the error in using P2(x)=1+9x+812x(x1)P_2(x) = 1 + 9 x + \tfrac{81}{2}x(x-1) to approximate f(x)=10xf(x) = 10^x at x=0.5.x = 0.5.

Wed, Mar 6

Today we looked at some interpolation examples in more detail. We used Python to implement the Vandermonde matrix and the divided difference methods for finding interpolating polynomials. Then we use the following Python notebook to look at some of the issues that arise with interpolation.

In the notebook, we looked at the following two examples which both raise important issues.

  1. sinx\sin x on [0,10π][0, 10\pi]. This example illustrates what goes wrong when you use the Vandermonde matrix approach. As the number of nodes grows past 20, the Vandermonde matrix is ill-conditioned, so it gives an incorrect interpolating polynomial. Because large Vandermonde matrices tend to be ill-conditioned, using the method of divided differences with the Newton basis for interpolation is usually preferred.

  2. f(x)=11+25x2f(x) = \dfrac{1}{1+25x^2} on [1,1][-1,1]. This function is a version of Runge’s function (also known as the Witch of Agnesi).

When you use equally spaced nodes interpolate the function f(x)=11+25x2f(x) = \dfrac{1}{1+25x^2} on [1,1][-1,1], the error gets worse as the number of nodes increases, particularly near the endpoints of the interval. This problem is called Runge’s phenomenon.

It is possible to avoid the error from Runge’s phenomenon. The key is to use a carefully chosen set of nodes that are not equally spaced. The best nodes to use are the roots of the Chebyshev polynomials which (surprisingly!) are equal to the following trigonometric functions on the interval [1,1][-1,1]: Tn+1(x)=cos((n+1)arccosx).T_{n+1}(x) = \cos ((n+1) \arccos x). The roots of the (n+1)(n+1)th degree Chebyshev polynomials are: xk=cos((2k+1)π2(n+1)),k=0,,n.x_k = \cos\left( \frac{(2k+1) \pi}{2(n+1)} \right), ~ k = 0, \ldots, n.

Fri, Mar 8

Today we wrapped up our discussion of polynomial interpolation with this workshop.


Week 9 Notes

Day Section Topic
Mon, Mar 18 5.1 Newton-Cotes formulas
Wed, Mar 20 5.1 Newton-Cotes formulas - con’d
Fri, Mar 22 Error in Newton-Cotes formulas

Mon, Mar 18

Today we introduced numerical integration. We reviewed the Riemann sum definition of the definite integral abf(x)dx=limnk=1f(xk)Δx\int_a^b f(x) \, dx = \lim_{n \rightarrow \infty} \sum_{k = 1}^\infty f(x_k) \Delta x where nn is the number of rectangles, Δx=(ba)/n\Delta x = (b-a)/n, and xk=a+kΔxx_k = a + k \Delta x is the right endpoint of each rectangle. Then we talked about the following methods for approximating integrals:

We derived the formulas for the composite trapezoid rule

abf(x)dx=h2(f(x0)+2f(x1)+2f(x2)++2f(xn1)+f(xn)), \int_a^b f(x) \, dx = \frac{h}{2} (f(x_0) + 2 f(x_1) + 2 f(x_2) + \ldots + 2 f(x_{n-1}) + f(x_n)),

and the composite Simpson’s rule

abf(x)dxh6(f(x0)+4f(x0.5)+2f(x1)+4f(x1.5)+2f(x2)++4f(xn0.5)+f(xn)),\int_a^b f(x)\,dx \approx \frac{h}{6}(f(x_0) + 4f(x_{0.5}) + 2f(x_1) + 4 f(x_{1.5}) + 2 f(x_2) + \ldots + 4f(x_{n-0.5}) + f(x_n)),

where h=banh = \frac{b-a}{n} and xk=a+khx_k = a + k h in both formulas. Here is an example of a Python function that computes the composite Simpson’s rule:

def simpson(f, a, b, n):
  h = (b-a)/n
  total = f(a)+f(b)
  total += sum([4*f(a+(k+0.5)*h) for k in range(n)])
  total += sum([2*f(a+k*h) for k in range(1,n)])
  return total*h/6

We looked at this example in class:

  1. Estimate the area under y=sinxy = \sin x from x=0x=0 to π\pi using a Riemann sum and Simpson’s method. How much more accurate is Simpson’s method when n=100?n=100?

Wed, Mar 20

Today we did the following workshop about numerical integration.

Here are some tips for the workshop.

  1. You might want to review the Taylor series workshop we did all the way back on January 22.

  2. Because the function f(x)=sinxxf(x) = \dfrac{\sin x}{x} is undefined at x=0x=0, you will get an error if you ask Python to evaluate the function there (for example, in problem 4). To avoid that problem, you can use this code to define f(x)f(x):

from math import *

f = lambda x: sin(x)/x if x != 0 else 1
  1. You’ll have to write your own code to compute the trapezoid rule. But you can look at the code from class Monday to see how I coded Simpson’s method which is similar.

Fri, Mar 22

Today we talked about the error in Newton-Cotes integration methods. An integration method has degree of precision nn if it is perfectly accurate for all polynomials up to degree nn. It is easy to see that the trapezoid method has degree of precision 1. Surprisingly, Simpson’s method has degree of precision 3.

Theorem. Simpson’s method has degree of precision 3.

We proved this theorem in class by observing that if f(x)f(x) is a third degree polynomial and P2(x)P_2(x) is a second degree interpolating polynomial for ff at the nodes aa, bb, and m=a+b2,m = \frac{a+b}{2}, then f(x)=P2(x)+c3(xa)(xm)(xb)f(x) = P_2(x) + c_3 (x-a)(x-m)(x-b) where c3c_3 is the third divided difference f[a,m,b]f[a,m,b]. Then we used u-substitution to show that ab(xa)(xm)(xb)dx=0.\int_a^b (x-a)(x-m)(x-b) \, dx = 0.
Since Simpson’s method is just the integral of P2(x)P_2(x) and the extra term integrates to zero, it follows that Simpson’s method is exact for 3rd degree polynomials.

For most other functions, Simpson’s method will not be perfect. Instead, we can use the error formulas for polynomial interpolation to estimate the error when using the composite trapezoid and Simpson’s methods. Here are the error formulas:

We didn’t prove the Simpson’s rule error formula in class, but we did prove the error formula for the trapezoid rule and the proof for Simpson’s rule is similar. We finished by applying these rules to the following questions:

  1. How big does nn need to be in the composite trapezoid rule to estimate 121xdx\int_1^2 \dfrac{1}{x} \, dx with an error of less than 101210^{-12}?

  2. How big does nn need to be in the composite trapezoid rule to estimate 121xdx\int_1^2 \dfrac{1}{x} \, dx with an error of less than 101210^{-12}?

  3. If you double nn, how much does the error tend to decrease in the trapezoid rule? What about in the Simpon’s rule?


Week 10 Notes

Day Section Topic
Mon, Mar 25 5.4 Gaussian quadrature
Wed, Mar 27 5.6 Monte carlo integration
Fri, Mar 29 4.1 Numerical differentiation

Mon, Mar 25

Today we introduced a different numerical integration technique called Gaussian quadrature. This technique is a little more complicated than Simpson’s method, but it can potentially be much more accurate and faster to compute. The idea is that instead of using equally spaced nodes like in the composite Newton-Cotes formulas, you can use a specially chosen set of nodes that allows you to get a degree of precision of 2n12n - 1 with only nn nodes.

The simplest version of Gaussian quadrature only works on the interval [1,1][-1,1]. To apply it to any other interval, you would have to use a change of variables. The formula for Gaussian quadrature is

11f(x)dxw1f(x1)+w2f(x2)++wnf(xn)\int_{-1}^1 f(x) \, dx \approx w_1 f(x_1) + w_2 f(x_2) + \ldots + w_n f(x_n)

where x1,,xnx_1, \ldots, x_n are the roots of the nth-degree Legendre polynomial and w1,,wnw_1, \ldots, w_n are special weights that are usually pre-computed.

Here is a table with the values of xix_i and wiw_i for nn up to 5 from Wikipedia:

nn Nodes xix_i Weights wiw_i
1 0 2
2 ±0.57735… 1
3 0 0.888889…
±0.774597… 0.555556…
4 ±0.339981… 0.652145…
±0.861136… 0.347855…
5 0 0.568889…
±0.538469… 0.478629…
±0.90618… 0.236927…

We did the following exercises in class.

  1. Use Gaussian quadrature with n=3n = 3 to estimate 11exdx\int_{-1}^1 e^x \, dx.

  2. Use Gaussian quadrature with n=3n = 3 to estimate 1112πex2/2dx.\int_{-1}^1 \frac{1}{\sqrt{2\pi}}e^{-x^2/2} \, dx.

  3. Use the u-substitution u=x/2u = x/2 to convert the integral 2212πex2/2dx\int_{-2}^2 \frac{1}{\sqrt{2\pi}}e^{-x^2/2} \, dx into one where you can apply Gaussian quadrature.

  4. What u-substitution could you apply to 0πsinxdx\int_0^\pi \sin x \, dx so that you could apply Gaussian quadrature?

  5. Show that if u=xmru = \dfrac{x-m}{r} where r=ba2r = \dfrac{b-a}{2} and m=a+b2m = \dfrac{a+b}{2}, then abf(x)dx=11rf(m+ur)du.\int_a^b f(x) \,dx = \int_{-1}^1 rf( m + u r ) \, du.

Wed, Mar 27

Today we talked about Monte Carlo integration, which is when you randomly generate inputs to try to find the value of an integral. Monte Carlo integration is slow to converge and it can give very bad results if you use a poorly chosen pseudo-random number generator. But it is one of the most effective methods for calculating multivariable integrals.

For a function f:df:\mathbb{R}^d \rightarrow \mathbb{R}, the basic idea is to calculate the average value of a function in a region Ω\Omega by randomly generating points x1,,xnx_1, \ldots, x_n in Ω\Omega.
Average value1ni=1nf(xi).\text{Average value} \approx \frac{1}{n} \sum_{i = 1}^n f(x_i). If the dimension d=2d = 2, then the double integral over the region Ω\Omega is approximately Ωf(x)dAArea(Ω)(1ni=1nf(xi)),\iint_\Omega f(x) \, dA \approx \operatorname{Area}(\Omega) \cdot \left( \frac{1}{n} \sum_{i = 1}^n f(x_i) \right), and if d=3d = 3, then the triple integral over Ω\Omega would be Ωf(x)dVVolume(Ω)(1ni=1nf(xi)).\iiint_\Omega f(x) \, dV \approx \operatorname{Volume}(\Omega) \cdot \left( \frac{1}{n} \sum_{i = 1}^n f(x_i) \right). Integrals in higher dimensions are similar, just using higher dimensional analogues of volume (called the measure of Ω\Omega).

We did this example:

  1. Use Monte Carlo integration to estimate the double integral 0102sin(xy2)dxdy.\int_0^1 \int_0^2 \sin(x y^2) \, dx dy.

  2. Use Monte Carlo integration to estimate the double integral Ωsin(xy)+1dxdy\iint_\Omega \sin(xy) + 1 \, dx dy where Ω={(x,y):x2+y21}\Omega = \{ (x,y) : x^2 + y^2 \le 1 \}.

The method above assumes that we choose our points uniformly in Ω\Omega. But we can actually use any probability distribution with support equal to Ω\Omega to approximate an integral. This is called importance sampling. If we have a method to compute random vectors in Ω\Omega with a probability distribution that has density function p(x)p(x), then the importance sample formula is: Ωf(x)dA1ni=1nf(xi)p(xi).\iint_\Omega f(x) \, dA \approx \frac{1}{n} \sum_{i = 1}^n \frac{ f(x_i) }{ p(x_i) }. If we randomly generate the coordinates of xx using a probability distribution like the normal distribution that has unbounded support, then we can calculate improper integrals like this example:

  1. Use importance sample where the entries of each sample input vector are chosen with a normal distribution to estimate: sin2xx2dx.\int_{-\infty}^\infty \dfrac{\sin^2 x}{x^2} \, dx.

The actual value of this integral should be π\pi. In theory, Monte Carlo integration will give the correct answer on average. But in practice using the normal distribution doesn’t work well for this integral because it under-samples the tails. A better probability distribution would be something like the Cauchy distribution or the Pareto distribution which we used in class. It can be tricky to pick a good distribution to use.

Fri, Mar 29

Today we talked about numerical differentiation and why it is numerically unstable which means that we can’t use a single numerical technique to get better and better approximations.

Recall that computers represent floating point numbers in binary in the following form: ±1.b1b2b3b52Binary mantissa×2Exponent.\pm 1.\underbrace{b_1 \, b_2 \, b_3 \, \cdots \, b_{52}}_{\text{Binary mantissa}} \times 2^{\text{Exponent}}. When a computer computes a function, it will almost always have to round the result since there is no where to put the information after the last binary decimal point. So it will have a relative error of about 2531.11×10162^{-53} \approx 1.11 \times 10^{-16}. This number is called the machine epsilon (and denoted ϵ\epsilon). It is the reason that numerical differentiation techniques are numerically unstable.

When you compute the difference quotient f(x+h)f(x)h\frac{f(x+h) - f(x)}{h} to approximate f(x)f'(x), the error should be |f(x+h)f(x)hf(x)|=|f(ξ)2h|, \left| \frac{f(x+h) - f(x)}{h} - f'(x)\right| = \left| \frac{f''(\xi)}{2} h \right|, for some ξ\xi between xx and x+hx+h by the Taylor remainder theorem. But that error formula assumes that f(x+h)f(x+h) and f(x)f(x) are being calculated precisely. In fact, they are going to have rounding errors and so will only be accurate to approximately ϵf(x)\epsilon f(x) where ϵ\epsilon is machine epsilon. Adding that factor, you get a combined error of roughly |ϵf(x)h|+|f(ξ)h2|. \left| \frac{\epsilon f(x)}{h} \right| + \left| \frac{f''(\xi) h}{2} \right|. That explains why the error initially decreases, but then starts to increase as hh get’s smaller. We graphed the logarithm of the relative error in using the difference quotient to approximate the derivative of f(x)=10xf(x) = 10^x as a function of kk when h=10kh = 10^{-k}. To graph it, we introduced the pyplot library in Python:

import matplotlib.pyplot as plt
from math import *

f = lambda x: 10**x
rel_error = lambda h: abs((f(0+h)-f(0))/h - log(10))/log(10)

xs = [k/10 for k in range(180)]
ys = [log(rel_error(10**(-x)))/log(10) for x in xs]

plt.plot(xs,log_rel_errors)
Shows the base-10 logarithm of the relative error on y-axis and the exponent kk for h=10kh = 10^{-k} on the x-axis.

We finished by getting started on this workshop:


Week 11 Notes

Day Section Topic
Mon, Apr 1 class canceled
Wed, Apr 3 Discrete least squares regression
Fri, Apr 5 Discrete least squares - con’d

Wed, Apr 3

Today we introduced least squares regression. We took a linear algebra approach to understanding least squares regression. Given an nn-by-kk matrix XX and a vector yny \in \mathbb{R}^n, we can try to find solutions to the system of equations Xb=y. X b = y. But a solution only exists if yy is in the column space of XX (also known as the range of XX). If yy is not in the column space, then there is no solution, but you can always find a point bkb \in \mathbb{R}^k such that ŷ=Xb\hat{y} = X b is as close as possible to yy. In other words, you can find ŷ\hat{y} that minimizes ŷy\|\hat{y} - y\| where the norm is the 2\ell_2-norm: v=v12+v22++vk2.\|v\| = \sqrt{v_1^2 + v_2^2 + \ldots + v_k^2}. That’s why we say we are finding the smallest sum of squared error.
It is a geometry fact that ŷy\|\hat{y} - y\| is minimized exactly when ŷy\hat{y} - y is orthogonal to the column space of XX. And that happens exactly when XT(ŷy)=0.X^T (\hat{y}-y) = 0. This is because of the fundamental theorem of linear algebra:

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

  1. The column space of AA is the orthogonal complement of the null space of ATA^T in m\mathbb{R}^m.
  2. The row space of AA is the orthogonal complement of the null space of AA in n\mathbb{R}^n.

Substituting ŷ=Xb\hat{y} = Xb and rearranging terms, we get the normal equations. XTXb=XTy.X^T X b = X^T y. You can solve the normal equations using row reduction to find the coefficients of the vector bb that generate the ŷ\hat{y} closest to yy.

We applied this technique to the examples in this workshop.

Fri, Apr 5

Today we talked some more about least squares regression. We started with this warm-up problem.

  1. Let A=(111213)A = \begin{pmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{pmatrix}. Find the null space null(AT)\operatorname{null}(A^T).

Then we defined the orthogonal complement of a subspace VnV \subset \mathbb{R}^n to be the set of vectors in n\mathbb{R}^n that are orthogonal to every element in VV, i.e., V={wn:wTv=0 for all vn}.V^\perp = \{w \in \mathbb{R}^n : w^T v = 0 \text{ for all } v \in \mathbb{R}^n\}.

  1. Use Desmos-3d to graph null(AT)\operatorname{null}(A^T) and null(AT)\operatorname{null}(A^T)^{\perp} for the matrix AA above.

  2. Find the least squares solution to Ax=(424)Ax = \begin{pmatrix} 4 \\ -2 \\ 4 \end{pmatrix}.

Then we talked about how 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).

Here is the code we used to analyze this example.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from math import *
df = pd.read_excel("http://people.hsc.edu/faculty-staff/blins/StatsExamples/FarmvilleHighTemps2019.xlsx")
xs = np.array(df.day)
ys = np.array(df.high)
plt.plot(xs,ys,"o")
plt.show()

X = np.array([[1, x, cos(2*pi*x/365), sin(2*pi*x/365) ] for x in xs])
bs = np.linalg.solve(X.T @ X, X.T @ ys)
print(bs)

model = lambda t: bs[0]+bs[1]*t + bs[2]*cos(2*pi*t/365) + bs[3]*sin(2*pi*t/365)
plt.plot(xs,ys,"o")
plt.plot(xs, [model(x) for x in xs])
plt.show()

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.


Week 12 Notes

Day Section Topic
Mon, Apr 8   Continuous least squares
Wed, Apr 10 Orthogonal functions, Gram-Schmidt
Fri, Apr 12 Fourier series

Mon, Apr 8

Today we introduced the idea of continuous least squares regression. Unlike the discrete case where we only want the smallest sum of squared residuals at a finite number of points, here the goal is to find a polynomial p(x)p(x) that minimizes the integral of the squared differences: 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

Definition. Let V,WV, W be inner-product spaces. If T:VWT:V \rightarrow W is a linear transformation, then the adjoint of TT is a linear transformation T*:WVT^*: W \rightarrow V defined by y,Tx=T*y,x\langle {y,Tx} \rangle = \langle {T^*y,x} \rangle for every xVx \in V and yWy \in W.

Definition. Let VV be an inner-product space. Two vectors v,wVv, w \in V are orthogonal if v,w=0\langle {v,w} \rangle = 0. If WW is a subspace of VV, then orthogonal complement WW^\perp is the subspace containing all vectors in VV that are orthogonal to every element of WW. That is, W={vV:v,w=0 for all wW}.W^{\perp} = \{ v \in V : \langle {v,w} \rangle = 0 \text{ for all } w \in W \}.

The following theorem says that the orthogonal complement of the range of a linear transformation is always the null space of the adjoint.

Theorem. Let V,WV, W be inner-product spaces. If T:VWT:V \rightarrow W is a linear transformation, then range(T)=null(T*).\operatorname{range}(T)^\perp = \operatorname{null}(T^*).

We want to find the best fit polynomial of degree n.  p(x)=b0+b1x++bnxn.p(x) = b_0 + b_1 x + \ldots + b_n x^n.
This polynomial will be in L2[a,b]L^2[a,b] and it is a linear function of the coefficients bn+1b \in \mathbb{R}^{n+1}. So there is a linear transformation X:n+1L2[a,b]X: \mathbb{R}^{n+1} \rightarrow L^2[a,b] such that p(x)=Xbp(x) = Xb. We want to find the p(x)p(x) closest to f(x)f(x) in L2[a,b]L^2[a,b]. In other words, we want to minimize p(x)f(x).\|p(x)-f(x)\|. That happens when p(x)f(x)p(x) - f(x) is orthogonal to the range of XX which happens when X*(p(x)f(x))=0.X^*(p(x) - f(x)) = 0. By replacing p(x)p(x) with XbXb, we get the normal equation for continuous least squares regression: X*Xb=X*f.X^*X b = X^* f.

Since X:n+1L2[a,b]X: \mathbb{R}^{n+1} \rightarrow L^2[a,b] and X*:L2[a,b]n+1X^*: L^2[a,b] \rightarrow \mathbb{R}^{n+1}, the composition X*XX^*X is a linear transformation from n+1\mathbb{R}^{n+1} to n+1\mathbb{R}^{n+1}. So it is an (n+1)(n+1)-by-(n+1)(n+1) matrix and its (i,j)(i,j)-entry is ei,X*Xej=Xei,Xej=abxi+jdx\langle {e_i,X^*Xe_j} \rangle = \langle {Xe_i, X e_j} \rangle = \int_a^b x^{i+j} \, dx where e0,,ene_0, \ldots, e_n are the standard basis vectors for n+1\mathbb{R}^{n+1}. The entries of X*fX^*f are ei,X*f=Xei,f=abxif(x)dx.\langle {e_i, X^*f} \rangle = \langle {Xe_i, f} \rangle = \int_a^b x^i f(x) \, dx.

To summarize, the normal equation to find the coefficients bb of the continuous least squares polynomial is

Theorem (Continuous Least Squares). For fL2[a,b]f \in L^2[a,b], the coefficients of the continuous least squares polynomial approximation p(x)=b0+b1x++bnxnp(x) = b_0 + b_1 x + \ldots + b_n x^ncan be found by solving the normal equation X*Xb=X*fX^*X b = X^* f where X*XX^*X is an (n+1)(n+1)-by-(n+1)(n+1) matrix with entries (X*X)ij=abxi+jdx,(X^*X)_{ij} = \int_a^b x^{i+j} \, dx, and X*fX^*f is a vector in n+1\mathbb{R}^{n+1} with entries (X*f)i=abxif(x)dx.(X^*f)_i = \int_a^b x^i f(x) \, dx.

  1. Use continuous least squares regression to find the best fit 2nd degree polynomial approximation of f(x)=exf(x) = e^x on [0,2][0,2].

We used this Python code to solve the problem and graph the solution:

import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt

n=2
f = np.exp
left, right = 0, 2
XstarX = np.array([[quad(lambda x: x**(i+j),left,right)[0] for j in range(n+1)] for i in range(n+1)])
Xstarf = np.array([quad(lambda x: x**i*f(x),left,right)[0] for i in range(n+1)])

b = np.linalg.solve(XstarX,Xstarf)
p = lambda x: sum([b[i]*x**i for i in range(n+1)])
print(b)

xvals = np.linspace(left,right,1000) #generates 1000 equally spaced points b/w left & right
plt.plot(xvals,f(xvals))
plt.plot(xvals,p(xvals))
plt.show()

(SageCell link)

Wed, Apr 10

Last time we derived the normal equations for continuous least squares. We came up with the following result using the standard basis ϕi(x)=xi\phi_i(x) = x^i, but it would work for any linearly independent family of functions ϕ0(x),ϕ1(x),,ϕn(x)\phi_0(x), \phi_1(x), \ldots, \phi_n(x) in L2[a,b]L^2[a,b].

Theorem (Continuous Least Squares). For fL2[a,b]f \in L^2[a,b], the coefficients of the continuous least squares polynomial approximation p(x)=b0ϕ0(x)+b1ϕ1(x)++bnϕn(x)p(x) = b_0 \phi_0(x) + b_1 \phi_1(x) + \ldots + b_n \phi_n(x) can be found by solving the normal equation X*Xb=X*fX^*X b = X^* f where X*XX^*X is an (n+1)(n+1)-by-(n+1)(n+1) matrix with entries (X*X)ij=ϕi,ϕj=abϕi(x)ϕj(x)dx,(X^*X)_{ij} = \langle {\phi_i,\phi_j} \rangle = \int_a^b \phi_i(x) \phi_j(x) \, dx, and X*fX^*f is a vector in n+1\mathbb{R}^{n+1} with entries (X*f)i=ϕi,f=abϕi(x)f(x)dx.(X^*f)_i = \langle {\phi_i,f} \rangle = \int_a^b \phi_i(x) f(x) \, dx.

The functions ϕi\phi_i can be any basis of polynomials (i.e., the standard basis, the Lagrange basis, the Newton basis, etc.) But the nicest basis for this formula would be if the polynomials were all orthogonal to each other. In linear algebra there is an algorithm to take a set of vectors that aren’t all orthogonal, and create a new set of vectors with the same span that are all orthogonal to each other.

Gram-Schmidt Algorithm. Given a set of linearly independent vectors v1,,vnv_1, \ldots, v_n in an inner product space VV, this will find a set of pairwise orthogonal vectors w1,,wnw_1, \ldots, w_n that have the same span as the original vectors.

  1. Let w1=v1w_1 = v_1.

  2. For each k=2,,nk = 2,\ldots,n, let wk+1=vk+1i=1kwi,vk+1wi,wiwi.w_{k+1} = v_{k+1} - \sum_{i = 1}^k \frac{\langle {w_i, v_{k+1}} \rangle}{\langle {w_i,w_i} \rangle} w_i.

  1. Apply the Gram-Schmidt algorithm to the functions 11, xx, and x2x^2, on L2[1,1]L^2[-1,1]. These are the first three Legendre polynomials. The Legendre polynomials are the orthogonal basis of polynomials obtained by applying the Gram-Schmidt algorithm to the standard basis polynomials (in the inner product space L2[1,1]).L^2[-1,1]).

If we have an orthogonal basis of functions ϕ1(x),,ϕn(x)\phi_1(x), \ldots, \phi_n(x), such as the Legendre polynomials, then the matrix X*XX^*X from the normal equation has zero entries except on the main diagonal: X*X=[ϕ02ϕ12ϕn2].X^* X = \begin{bmatrix} \|\phi_0\|^2 & & & \\ & \|\phi_1\|^2 & & \\ & & \ddots & \\ & & & \|\phi_n\|^2 \end{bmatrix}.

Therefore the solution of the normal equations is just bi=abϕi(x)f(x)dxϕi2 for each 0in.b_i = \frac{\int_{a}^b \phi_i(x) f(x) \, dx}{\|\phi_i\|^2} \text{ for each } 0 \le i \le n.

We used Desmos to compute the 2nd degree continuous least squares regression polynomial on the interval [1,1][-1,1] using the Legendre basis polynomials for the following functions.

  1. f(x)=cos(πx)f(x) = \cos(\pi x).

  2. f(x)=exf(x) = e^x.

Fri, Apr 12

Today we talked about Fourier series. 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 set {sin(x),sin(2x),sin(3x),}{1,cos(x),cos(2x),cos(3x),}\{\sin(x), \sin(2x), \sin(3x), \ldots \} \cup \{1,\cos(x), \cos(2x), \cos(3x),\ldots \} on the interval [π,π][-\pi,\pi]. Any function in L2[π,π]L^2[-\pi,\pi] can be approximated by using continuous least squares with these trig functions. Since there are an infinite number of functions in this orthogonal set, we usually stop the approximation when we reach a high enough frequency sin(nx)\sin(nx) and cos(nx)\cos(nx).

We started by using the trig product formulas 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*} to find the norms of cos(nx)\|\cos(nx)\| and sin(nx)\|sin(nx)\| for any nn \in \mathbb{N}. We also proved that cos(mx)\cos(mx) is orthogonal to cos(nx)\cos(nx) when mnm \ne n.

Then we did this workshop.


Week 13 Notes

Day Section Topic
Mon, Apr 15 Review
Wed, Apr 17 Midterm 2
Fri, Apr 19 6.1 Euler’s method

Mon, Apr 15

Today we reviewed for midterm 2. Instead of going over the review problems, we talked about the formulas you should have memorized (most other formulas will be given with the problem, although you will have to know how to use them). Here was the list we came up with:

  1. Vandermonde matrices & the standard basis for polynomials

  2. Lagrange basis

  3. Divided differences and the Newton basis

  4. The difference quotient

  5. Inner-product and norm for L2[a,b]L^2[a,b].

  6. Degree of precision for integration methods

We did the following exercises.

  1. Use divided differences to find the interpolating polynomial for the points (2,1),(0,1),(1,2),(-2,1), (0,1), (1,-2), and (2,1)(2,1).

  2. Use the Langrange basis to find the same interpolating polynomial.

  3. What Vandermonde matrix would you use, and in what linear system, to find the coefficients of the interpolating polynomial in the standard basis.

  4. What would you get if you used Gaussian quadrature with N=10N=10 nodes to calculate 115x46x2+3dx\int_{-1}^1 5x^4 - 6x^2 + 3 \, dx?

We also mentioned a few other important concepts that you might want to review like:

Fri, Apr 19

We talked more about the Initial Value Problem (abbreviated IVP) for first order differential equations (abbreviated ODEs).

We started with this example:

  1. Find the solution of the differential equation dydt=ty\dfrac{dy}{dt} = -\dfrac{t}{y} with initial condition y(3)=4y(-3) = 4.

Then we introduced the following theorem (without proof):

Theorem (Existence & Uniqueness of Solutions). If f(t,y)f(t,y) is continuous and the partial derivatives |fy||\frac{\partial f}{\partial y}| are bounded for all t[a,b]t \in [a,b], then the initial value problem dydt=f(t,y)y=y0\frac{dy}{dt} = f(t,y) ~~~~ y = y_0 has a unique solution y(t)y(t) defined on the interval [a,b][a,b].

We looked at these examples:

  1. The differential equation dydt=ty\dfrac{dy}{dt} = \dfrac{-t}{y} has solutions that follow circular arcs centered at the origin. The IVP y(1)=0y(-1) = 0 has two solutions y=1t2y = \sqrt{1-t^2} and y=1t2y = -\sqrt{1-t^2}. Why doesn’t that contradict the theorem above? Which condition of the theorem fails for this IVP?

  2. The explosion equation is the ODE dydt=ky2.\frac{dy}{dt} = k y^2. The solution is y(t)=1Ckty(t) = \frac{1}{C-kt}. If k=1k = 1, then a solution to the IVP with y(0)=1y(0) = 1 is y(t)=11ty(t) = \dfrac{1}{1-t}. But that solution is not defined when t=1t=1 (it has a vertical asymptote). Why is that consistent with the theorem above?

  3. Show that y=2e5xy = 2e^{5x} is the unique solution of the IVP y=5yy' = 5y with y(0)=2y(0) = 2. How do you know that this is the only solution?

Unfortunately most differential equations cannot be solved analytically (the way we did for the examples above). So next week, we will talk about methods to find approximate solutions.


Week 14 Notes

Day Section Topic
Mon, Apr 22 6.1 Euler’s method error
Wed, Apr 24 Existence & uniqueness of solutions
Fri, Apr 26 6.4 Runge-Kutta methods
Mon, Apr 29 Review

Mon, Apr 22

The simplest method for finding an approximate solution to a differential equation is Euler’s method.

Euler’s Method Algorithm

To find an approximate solution to the initial value problem dydt=f(t,y),y(a)=y0\dfrac{dy}{dt} = f(t,y), ~~~~ y(a) = y_0 on the interval [a,b][a,b],

  1. Choose a step size hh and initialize t=at = a and y=y0y = y_0.
  2. While t<bt < b do
    1. Update yy by adding f(t,y)hf(t,y) \cdot h.
    2. Update tt by adding hh.

We started by doing this example by hand:

  1. dydt=yt2+1\displaystyle\dfrac{dy}{dt} = y - t^2 + 1 on [1,3][-1,3] with initial condition y(1)=0y(-1) = 0 and step size h=1h = 1.

Then we implemented the algorithm in Python to get a more accurate solution.

from math import *
import matplotlib.pyplot as plt

def EulersMethod(f,a,b,h,y0):
    # Returns two lists, one of t-values and the other of y-values. 
    t, y = a, y0
    ts, ys = [a], [y0]
    while t < b:
      y += f(t,y)*h
      t += h
      ts.append(t)
      ys.append(y)
    return ts, ys

f = lambda t,y: y-t**2 + 1

# h = 1
ts, ys = EulersMethod(f,-1,3,1,0)
plt.plot(ts,ys)
# h = 0.1
ts, ys = EulersMethod(f,-1,3,0.1,0)
plt.plot(ts,ys)
# h = 0.01
ts, ys = EulersMethod(f,-1,3,0.01,0)
plt.plot(ts,ys)
plt.show()

(SageCell link)

  1. Suppose we have a growing population, but with a seasonal limit on the population size. The model for the population yy is dydt=0.1y(10+2cos(2πt)y),y(0)=1.\frac{dy}{dt} = 0.1 y (10 + 2 \cos(2 \pi t) - y), ~~~~ y(0) = 1. Explain why the initial value problem has a unique solution with any initial condition.

To analyze the population example we used this Python code:

f = lambda t,y: 0.1*y*(10+2*cos(2*pi*t)-y)
# h = 1 
ts, ys = EulersMethod(f,0,10,1,1)
plt.plot(ts,ys)
# h = 0.1 
ts, ys = EulersMethod(f,0,10,0.1,1)
plt.plot(ts,ys)
# h = 0.01
ts, ys = EulersMethod(f,0,10,0.001,1)
plt.plot(ts,ys)
plt.show()

(SageCell link)

  1. We finished by using Euler’s method to estimate the number ee by approximating the solution of the IVP dydt=y,y(0)=1\dfrac{dy}{dt} = y, ~~~ y(0) = 1 on the interval [0,1][0,1].

Wed, Apr 24

We started with this question:

  1. If you take one step of Euler’s method starting at a point (t,y)(t,y) with a differential equation dydt=f(t,y)\dfrac{dy}{dt} = f(t,y) use the Taylor series remainder formula to estimate the difference between the Euler’s method approximation y(t)+f(t,y)y(t)hy(t) + \underbrace{f(t,y)}_{y'(t)} \cdot h and the actual value of y(t+h)y(t+h).
Here is an image from section 6.1 of the book that shows this error.

Over many steps, the error from using Euler’s method tends to grow. It is possible to prove the following upper bound for the error in Euler’s method over the whole interval [a,b][a,b]:

Error[eL(ba)1]L(Mh2+δh)\text{Error} \le \frac{[e^{L(b-a)} - 1]}{L} \left(\frac{Mh}{2} + \frac{\delta}{h}\right) where L=max|f(t,y)y|L = \max \left|\frac{\partial f(t,y)}{\partial y}\right|, M=max|y(t)|M = \max |y''(t)|, and δ\delta is machine epsilon. At first, the error decreases as hh gets smaller, but eventually the machine epsilon term (which comes from rounding errors) gets very large, so Euler’s method can be numerically unstable.

To get a method with less error, would could try to incorporate the 2nd derivative of y(t)y(t) into each step. Note that d2dt2y(t)=ddtf(t,y)=ft(t,y)+fy(t,y)f(t,y)\dfrac{d^2}{dt^2} y(t) = \dfrac{d}{dt} f(t,y) = f_t(t,y) + f_y(t,y) f(t,y) by the chain rule for multivariable functions. In practice it isn’t always easy to compute the partial derivatives of ff, so the Runge-Kutta method looks for relatively simple formulas involving f(t,y)f(t,y) that approximate this expression above without calculating the partial derivatives. The 2nd order Runge-Kutta method known as the midpoint method has this formula

y(ti+1)y(ti)+f(ti+12h,y(ti)+12hf(ti,yi))h.y(t_{i+1}) \approx y(t_i) + f(t_i + \tfrac{1}{2}h ,y(t_i) + \tfrac{1}{2}h f(t_i,y_i)) h.

Second Order Runge-Kutta Algorithm. To approximate a solution to the IVP dydt=f(t,y),y(a)=y0\dfrac{dy}{dt} = f(t,y), ~~~~ y(a) = y_0 on the interval [a,b][a,b],

  1. Choose a step size hh and initialize t=at=a and y=y0y = y_0.
  2. While t<bt < b do
    1. y=y+f(t+h/2,y+h/2f(t,y))hy = y + f(t+h/2, y + h/2 \cdot f(t,y))h
    2. t=t+ht = t+h

We implemented the algorithm for this Runge-Kutta method in Python and used it to approximate the solution of this IVP:

  1. dydt=yt2+1\dfrac{dy}{dt} = y-t^2 +1 on [1,3][-1,3] with initial condition y(1)=0y(-1)=0.

We also compared our result with the Euler’s method approximate solutions for h=1,0.1,h = 1, 0.1, and 0.010.01. We saw that the midpoint method was much more accurate. My version of the code for this algorithm is below.

def MidpointMethod(f,a,b,h,y0):
    # Return two lists, one of y-values and the other of t-values
    t, y = a, y0
    ts, ys = [a], [y0]
    while t < b:
      y += f(t+h/2,y+h/2*f(t,y))*h
      t += h
      ts.append(t)
      ys.append(y)
    return ts, ys

Fri, Apr 26

Today we introduced the 4th order Runge-Kutta (RK4) method and we did this workshop: