Notebook 17 – Math 2121, Fall 2020

In today's class we discussed how to compute a line of best fit by finding a least-squares solution to a certain linear system.

Here we explore the problem of finding a polynomial curve of best fit that passes through some given datapoints, which imagine as having the form (x,f(x)) for some unknown function f.

15.5 μs
68.8 s

Suppose xvalues = [x1, x2, ..., xn]. The following method constructs the matrix

A=[1x1x12x1m11x2x22x2m11xnxn2xnm1].

This is sometimes called a Vandermonde matrix.

If f:RR is a function and we have A[c0c1cm1]=[f(x1)f(x2)f(xn)]Rn then

p(x)=c0+c1x+c2x2++cm1xm1

is a polynomial satisfying p(xi)=f(xi) for each i=1,2,,n.

13.8 μs
vandermonde (generic function with 2 methods)
51.0 μs
4×4 Array{Float64,2}:
 1.0  1.0   1.0    1.0
 1.0  3.0   9.0   27.0
 1.0  4.0  16.0   64.0
 1.0  5.0  25.0  125.0
21.5 ms

Any square Vandermonde matrix is invertible. Hence, there is a unique polynomial of degree d+1 passing through any given d datapoints (xi,yi) with x1<x2<<xd.

7.0 μs
equally_spaced_xvalues (generic function with 1 method)
28.6 μs
115 ms

The following method returns the polynomial of best fit (in the sense of least squares) that agrees in the input function fn at the given xvalues.

If not given, the degree of the returned polynomial is the length of xvalues.

15.0 μs
fit_polynomial (generic function with 2 methods)
86.8 μs

Below are some methods to plot our interpolations.

6.7 μs
compare_plots (generic function with 2 methods)
48.8 μs
polynomial_iterpolation_anim (generic function with 2 methods)
77.4 μs

Here are methods to measure the error of our interpolations.

6.6 μs
error (generic function with 1 method)
54.6 μs
plot_log_error (generic function with 2 methods)
82.3 μs

Consider the function f(x)=11+25x2.

6.4 μs
f
#11 (generic function with 1 method)
31.5 μs
7.1 s

A major problem involved in polynomial interpolation is overfitting.

We see this for the preceding function when we sample evenly spaced points in the interval [1,1].

As we add more datapoints and compute higher degree polynomials that fit the data, the 'fit' of these polynomials actually becomes worse!

On other intervals, say [1,0], this problem is less dramatic.

11.7 μs
3.2 s
693 ms
1.3 s
77.7 ms

One solution to the problem of overfitting is to fix the degree of our polynomial interpolation. This gives a better error that won't explode as we add more datapoints.

On the other hand, the error may have a nontrivial lower bound and may not tend to zero as we add more data.

If the number of datapoints n is greater than our fixed degree m, then it may be impossible to construct a polynomial that passes through every datapoint. In this case the polynomial of best fit corresponds to a least-squares (approximate) solution to the linear system

A[c0c1cm]=[f(x1)f(x2)f(xn)]

where A is a Vandermonde matrix as defined above.

9.6 μs
fixed_degree
20
4.4 μs
5.3 s
372 ms

Another solution to the overfitting problem is to sample our input function at different x-values.

6.8 μs
chebyshev_spaced_xvalues (generic function with 1 method)
172 μs

The x-values returned by this method are not equally spaced between a and b.

However, if we sample our function at these points, the resulting polynomial interpolation is much more accurate, and we avoid overfitting as the degree of our interpolation increases:

8.2 μs
3.1 s
217 ms

The error here is better than what we get by fixing the degree of our interpolation and using least-squares.

8.6 μs

What are the numbers chebyshev_spaced_xvalues(a, b, n)?

7.1 μs

The Chebyshev polynomials Tn(x) of the first kind are defined by the recurrence

T0(x)=1,

T1(x)=x,

Tn+1(x)=2xTn(x)Tn1(x).

These are the unique polynomials that satisfy Tn(cos(θ))=cos(nθ).

8.3 μs
chebyshev (generic function with 1 method)
62.5 μs
2.3 s

The values returned by chebyshev_spaced_xvalues(-1, 1, n) are the roots of Tn(x).

5.3 μs
plot_chebyshev (generic function with 1 method)
42.1 μs
706 ms

It can be shown that using chebyshev_spaced_xvalues(a, b, n) to sample our input function yields the best possible exact polynomial interpolation of degree n.

See https://en.wikipedia.org/wiki/Chebyshev_nodes for more information.

1.8 ms