| exports | kernelspec | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
from pylab import *
from scipy.interpolate import barycentric_interpolate
Weirstrass Theorem says that we can approximate continuous functions by polynomials.
+++
:::{prf:theorem}
Let
$$ |f(x) - p(x)| \le \epsilon, \qquad x \in [a,b] $$ :::
:::{prf:proof} See [@Davis1963], Chapter 6. :::
Hence given any error tolerance
i.e., polynomials can provide uniform approximation. This is only an existence result and does not give us a polynomial with a desired error bound. It also does not even tell us the degree of the polynomial.
The proof uses Bernstein polynomials. Take
If
If
We have written
:::{prf:example}
Consider
Then the Bernstein polynomial satisfies
The error decreases at linear rate, which is very slow convergence. A quadratic interpolation will recovery the function exactly using just three function values. The Bernstein polynomials are not useful for actual approximation, but only for the proof.
% TODO %$$ %1 = 1^n = (1-x + x)^n = \sum_{k=0}^n \binom{n}{k} x^k (1-x)^{n-k} %$$ % %$$ %p_n(x) - x^2 = \sum_{k=0}^n \binom{n}{k} ( (k/n)^2 - x^2) x^k (1-x)^{n-k} %$$ :::
+++
The Taylor expansion provides another way to approximate a function in a neighbourhood of some point at which the expansion is written. A Taylor expansion will exactly recover a polynomial function so we consider somewhat more non-trivial but still simple function. E.g.
The third degree Taylor expansion around
We can estimate the error also from the remainder term
and we get the following estimate
The maximum norm of the error is
The remainder term does not give a sharp estimate. Plotting the error,
x = linspace(-1,1,100)
f = lambda x: exp(x)
p3 = lambda x: 1 + x + x**2/2 + x**3/6
e = f(x) - p3(x)
print("Max error = ", abs(e).max())
plot(x,e)
title('Error in cubic Taylor expansion')
grid(True), xlabel('x'), ylabel('$f(x)-p_3(x)$');
we see that it is not uniformly distributed in the interval
+++
We can get much better cubic polynomial approximation than the Taylor polynomial. Let us interpolate a cubic polynomial at
The error in this approximation is shown below.
xi = linspace(-1,1,4)
yi = f(xi)
y = barycentric_interpolate(xi,yi,x)
e = f(x) - y
print("Max error = ",abs(e).max())
plot(x,e, xi, 0*xi, 'o')
title('Error in cubic interpolation')
grid(True), xlabel('x'), ylabel('$f(x)-p_3(x)$');
The maximum error is approximately