Bernstein polynomials
function BernsteinPolys()The Weierstrass Approximation Theorem asserts that a continuous function on a bounded interval like can be approximated by polynomials (i.e., approximated as closely as you like in the supremum norm). Weierstrass proved this in 1885 by a diffusion argument: if diffuses however little, it becomes an entire function, which can be approximated by truncating the Taylor series. (Before the diffusion, one first extends to a continuous with compact support on the whole real line.)
Bernstein gave a proof of the Weierstrass Approximation Theorem in 1912 that is a kind of discrete version of this diffusion proof: it replaces the continuous diffusion by a random walk on an equispaced grid in . While this is perhaps a little more complicated conceptually, it is mathematically more elementary since you don't need any analysis and you don't need to truncate a series, for the polynomials emerge directly.
Specifically, for each positive integer , the degree Bernstein polynomial for is
Let's demonstrate in Chebfun. Here is a continuous function on :
LW = 'linewidth'; lw = 1.6;
s = chebfun('s',[0 1]);
f = min(abs(s-.3),2*abs(s-.7));
f = s + max(0,1-5*f);
hold off, plot(f,LW,lw)
Since is a polynomial of degree , we can construct it by evaluating it on a grid of points and then interpolating. For stability these should be Chebyshev points, not equispaced. Here is an elementary code to do this at least for small values of . Note that it isn't really stable, so it turns warnings off.
function Bn = Bn(f,n)
warning off
x = chebpts(n+1,[0 1]);
Bndata = zeros(size(x));
for k = 0:n
Bndata = Bndata + f(k/n)*nchoosek(n,k).*x.^k.*(1-x).^(n-k);
end
Bn = chebfun(Bndata,[0 1]);
warning on
endTo illustrate the behavior of Bernstein polynomials, here we see slow convergence as increases.
for n = [25 50 100]
hold off, plot(f,LW,lw)
hold on, plot(Bn(f,n),'r',LW,lw)
title(['n = ' int2str(n)],'fontsize',14)
snapnow
end


Note a signature feature of Bernstein polynomial approximations, their monotonicity in various senses. There is never any Gibbs phenomenon.
On the other hand, since these approximations depend on the central limit theorem to give accuracy as gets large, they take no advantage at all of smoothness. Here for example is a repetition of the last experiment for a far smoother function.
f = s + exp(-50*(s-.3).^2) + exp(-200*(s-.7).^2);
for n = [25 50 100]
hold off, plot(f,LW,lw)
hold on, plot(Bn(f,n),'r',LW,lw)
title(['n = ' int2str(n)],'fontsize',14)
snapnow
end


Though is now entire, the convergence is not really better than before. By contrast we know that is more than enough for Chebyshev interpolation to nail this function to machine precision:
length(f)ans =
85end