Application Center - Maplesoft

App Preview:

Taylor, Legendre, and Bernstein polynomials

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

polyApprox.mws

Taylor, Legendre, or Bernstein Polynomials: an Example

Jim Herod

School of Mathematics

Georgia Tech (Retired)

jherod@mail.tds.net

Given a funtion f on an interval [a, b], we are interested in making a polynomial approximation for f. We are faced with two question: how large do we choose the degree of the polynomial and how do we choose the coefficients for the polynomial. Three choices of classical polynomials come to mind; there are others. If the function is sufficiently differentiable, we can try the well known Taylor's polynomials. Alternatively, if the function is integrable, we can choose the coefficients so that the integral of the difference in the functions and the polynomial approximation is as small as possible. This scheme will give rise to an approximation with the Legendre polynomials. Finally, for continuous functions, we can use the Bernstein polynomials. We illustrate these three methods in this worksheet.

Introduction

Taylor Series for a function f is a series of polynomials that arise from the differentiability of the function f at a single point. Effort is made in the undergraduate calculus to understand the convergence of series, and of Taylor series in particular. There is another classical series of polynomial functions that arise from the integrability of a function, instead of the differentiability. The polynomials in this class are called Legendre polynomials. A less familiar sequence of polynomials to approximate a function f uniformly over a closed interval are Bernstein polynomials. These polynomials are defined from the values of f over a uniform grid on an interval. This latter class of polynomials are simplier for their construction uses neither the techniques of differentiation nor of integration.

Students usually know about Taylor's Series. The Taylor's Series would likely be the first choice for most students to make in getting a polynomial approximation. This worksheet will serve as a warning that some thought should be given before quickly choosing Taylor Polynomials. Less known are the Legendre Polynomials. Perhaps this worksheet will correct that also. Finally, Bernstein Polynomials are hardly known by undergraduate science and engineering students. This worksheet suggest a good reason: while the technique for making the approximations with Bernstein Polynomials is simple, the rate of convergence can be slow. The Bernstein Polynomials are constructed as follows:

for x in [0, 1], B(n,x) = sum(f(k/n)*n!*x^k*(1-x)^(n-k)/(k!*(n-k!)),k = 0 .. ... .

Note that no differentiabiliby of f is required for f and no integrability is used. Only, f is evaluated at an evenly spaced grid across the interval.

In this worksheet, we provide an example for which the the function is infinitely differentiable at zero, the Taylor series converges, and yet the Taylor series fails to make the expected approximation for f. For this example the Legendre polynomials and the Bernstein polynomials can be used to make an approximation, but the first of these makes an approximation with a smaller degree polynomial..

We agree that a series of functions

S = Sum(u[n](x),n)

represents a function f(x) on a certain set A if and only if for every point x of the set A, the series S converges to the value of the function f(x) at that point.

Taylor Polynomials

Immediately after the question of convergence of the Taylor series of a function comes the question, "Does the Taylor series of a given function represent the function throughout the interval of convergence?" The clue to the answer lies in Taylor's formula with a remainder. To clarify the situation we recall the situation for the Taylor's series.

Let

(1) S[n](x) = f(a) + f '(a)(x-a) + ... + f^`(n)`*`(a)`/n! (x-a)^n .

Taylor's formula with a remainder now assumes the form

(2) f(x) = Sn(x) + Rn(x),

or

(3) Rn(x) = f(x) - Sn(x).

A formua for the remainder can be found by an application of the Mean Value Theorem: there is a number c between a and x such that

Rn(x) = f^`(n+1)`*`(c)`/n! (x-a)^(n+1) .

Suppose that we have an interval containing 0 on which f and all of its derivatives exist. If x is a point of the interval, then the limit of the polynomial sequence Sn(x) is f if and only if the remainder has limit zero. Techniques for showing the limit of the remainder is zero vary with the function concerned. To make any general statement specifying conditions under which a given function is represented as a limit of the Taylor polynomials is extremely difficult.

Example.

We consider the following function f(x) where the Taylor series about zero determined by f(x) does not converge to f, even though f is infinitely differentiable on any interval containing zero.

Here is the definition of f.

f(x) = 0 if 0 if 0 <= x and f(x) = exp(-1/ x^2 ) if x > 0.

We draw the graph of f on [-1, 1].

> plot(piecewise(x<=0,0,exp(-1/x^2)),x=-1..1);

[Maple Plot]

>

Is it not clear that, with the possible exception of zero, the function has derivatives of all orders? After all, for negative x's, all derivatives are zero. For positive x's, the derivative of any order can be made by successive applications of the calculus to exp(-1/ x^2 )

We establish below that f is infinitely differentiable at zero and that all derivatives at zero are zero. With this result the Taylor series determined by f about zero would be

0 + 0 x + 0 x^2 ... .
.

This series represents the function identically 0 everywhere, but the function f is not identically zero on any open interval containing zero.

Toward making a proof that all the derivatives are zero at zero, we establish this


Lemma: If n is a positive integer then lim f( x )/ x^n =0.

Here is Maple's verification of this lemma.

> exp(-1/x^2)/x^n;
limit(%,x=0);

exp(-1/(x^2))/(x^n)

0

To make proof we use l'Hospital's rule: write

exp(-1/(x^2))/(x^n) = x^(-n)/exp(1/(x^2))
Then

>

> diff(x^(-n),x);
diff(exp(1/x^2),x);
simplify(%%/%);

-x^(-n)*n/x

-2*exp(1/(x^2))/(x^3)

1/2*x^(-n+2)*n*exp(-1/(x^2))

>

Thus, limit f(x)/(x^n) = limit 1/2 n*f(x)/(x^(2-n)) ,

where the limits are taken as x approaches zero.

Inductively, since the propositon is true for n = 1 and n = 2, it is true for all integers. This establishes the Lemma.

Lemma: For each n, there is a matrix

a[n,p], p = 1 ... 2^n

such that, if x > 0,

f^`(n)`*`(x)` = sum(a[n,p]*f(x)/(x^p),p = n+2 .. 2^n) .

Moreover, a[n, n+2] = (-1)^(n+1)*(n+1)! and a[n,3n]= 2^n .

We will resist the impulse to prove this formula for the first and last coefficients of the sum for the derivatives of f. It would be preferable to have a formula for all the coefficients. These are unknown to the author.

Experimental Evidence

> f:=x->exp(-1/x^2);

f := proc (x) options operator, arrow; exp(-1/(x^2)...

> D(f)(x);

2*exp(-1/(x^2))/(x^3)

> (D@@2)(f)(x);

-6*exp(-1/(x^2))/(x^4)+4*exp(-1/(x^2))/(x^6)

> (D@@3)(f)(x);

24*exp(-1/(x^2))/(x^5)-36*exp(-1/(x^2))/(x^7)+8*exp...

> (D@@4)(f)(x);

-120*exp(-1/(x^2))/(x^6)+300*exp(-1/(x^2))/(x^8)-14...

> (D@@5)(f)(x);

720*exp(-1/(x^2))/(x^7)-2640*exp(-1/(x^2))/(x^9)+20...

Theorem: With f(x) as defined above, for each positive integer n, the n^th derivative of f is zero.

This is true if n = 0. Suppose it is true for the integer k. By the definition of the derivative at zero,

f^(k+1) (0) = limit [ f^k (x)-0]/x.

By the above two lemmas, this limit is zero.

We have established that the Taylor series for this f about 0 does not converge to f on any open interval containing zero.

Legendre Polynomials.

We now look at a different set of polynomials: the Legendre Polynomials. These polynomials { P[0](x) , P[1](x) , P[2](x) , ... } have the following properties:

(4) int(P[i](x)^2,x = -1 .. 1) is not zero, and

(5) int(P[i](x)*P[j](x),x = -1 .. 1) = 0, if i is not j,

From (4) and (5) it follows that if f is a polynomial and

f(x) = sum(a[i]*P[i](x),i = 0 .. n)

then

(6) a[i] = int(f(x)*P[i](x),x = -1 .. 1) / int(P[i](x)^2,x = -1 .. 1) .

Moreover, if f is any function with int(f(x)^2,x = -1 .. 1) finite, then the polynomial of degree n, S(x), with

int((f(x)-S(x))^2,x = -1 .. 1)

as small as possible is the polynomial

S(x) = sum(a[n]*P[n](x),n = 0 .. N)

with a[n] given by (6)

Maple knows these Legendre polynomials. Here are the first three

> with(orthopoly);

[G, H, L, P, T, U]

> P(0,x);P(1,x);P(2,x);

1

x

3/2*x^2-1/2

>

In order to get a Lengendre Polynomial approximation for the piecewise defined function f above, we compute the coefficients by the formula (6). Because the function is zero on the interval [-1, 0], it sufficies to integrate f from 0 to 1 in the formula (6). We define the degree as N, as the reader may choose to increase or decrease the degree of the polynomial with some future experiments.

> N:=5;

N := 5

> for n from 0 to N do
int(P(n,x)*exp(-1/x^2),x=0..1);
evalf(%);
int(P(n,x)^2,x=-1..1);
a[n]:=%%/%;
od;
n:='n';

exp(-1)+exp(-1)*sqrt(Pi)*erf(1)*exp(1)-sqrt(Pi)

.89073855e-1

2

a[0] := .4453692750e-1

-1/2*(-1+Ei(1,1)*exp(1))*exp(-1)

.7424775340e-1

2/3

a[1] := .1113716301

-exp(-1)-3/2*exp(-1)*sqrt(Pi)*erf(1)*exp(1)+3/2*sqr...

.50328937e-1

2/5

a[2] := .1258223425

1/8*(-6+11*Ei(1,1)*exp(1))*exp(-1)

.2574332885e-1

2/7

a[3] := .9010165098e-1

37/12*exp(-1)+97/24*exp(-1)*sqrt(Pi)*erf(1)*exp(1)-...

.7455701e-2

2/9

a[4] := .3355065450e-1

-1/32*(-72+121*Ei(1,1)*exp(1))*exp(-1)

-.1816759170e-2

2/11

a[5] := -.9992175435e-2

n := 'n'

> S:=x->sum(a[n]*P(n,x),n=0..N);

S := proc (x) options operator, arrow; sum(a[n]*P(n...

> plot([[x,S(x),x=-1..1],[x,exp(-1/x^2),x=0..1]]);

[Maple Plot]

>

We see this is a good approximation for f on [-1,1]. This is great approximation in comparison to the Taylor polynomial which failed awfully in making an approximation for f.

Bernstein Polynomials

In the Introduction, we gave the usual definition for the Bernstein Polynomials. These polynomials are defined for a function f over an interval [0, 1]. For the reader unfamiliar with these polynomials, it is perhaps instructive to look at graphs of the individual terms of the sums and to visualize how they might add to approximate f. Recall the definition:

(7) for x in [0, 1], B(n,x) = sum(f(k/n)*n!*x^k*(1-x)^(n-k)/(k!*(n-k!)),k = 0 .. ... .

For the purpose of illustration, we define a function F and draw the terms of the sum, as well as the sum and the graph of F. The reader might choose to modify F or to change the number of terms in the sum to increase intuition. In the graph which follows, we plot F in green, each term of the sum that makes the approximation in black, and the approximation in red.

> F:=x->1+x^2;

F := proc (x) options operator, arrow; x^2+1 end pr...

> N:=4;

N := 4

> TermB:=(k,x)->F(k/N)*binomial(N,k)*x^k*(1-x)^(N-k);

TermB := proc (k, x) options operator, arrow; F(k/N...

> plot([seq(TermB(k,x),k=0..N),sum(TermB(k,x),k=0..N),F(x)],x=0..1,
color=[seq(black,k=0..N),red,green]);

[Maple Plot]

>

We turn again to the function approximated poorly with Taylor's polynomials and pretty good with Legendre's polynomials. This function is defined on [-1, 1]. The first thing we must do is redefine f so as to get the graph on [0,1]. Then, we compute the Bernstein polynomials and redefine them on [-1, 1]. We repeat the definition for f here.

> f:=x->piecewise(x<0,0,x>0,exp(-1/'x^2'));
f(0):=0;

f := proc (x) options operator, arrow; piecewise(x ...

f(0) := 0

> plot(f(x),x=-1..1);

[Maple Plot]

>

We define a function g on [0,1] which will be a shift and compression of the domain of f. Look at the graph of g.

> g:=x->f(2*x-1);

g := proc (x) options operator, arrow; f(2*x-1) end...

> plot(g(x),x=0..1);

[Maple Plot]

>

We can now get the approximation for this function g on the interval [0,1]. We have two choices, use the definition for the Bernstein polynomials in equation (7) or acknowledge that Maple already knows how to form them.

> N:=5;

N := 5

> bernstein(N,g,y);

(10*y^3-20*y^4+10*y^5)*exp(-25)+(5*y^4-5*y^5)*exp(-...

This is the Bernstein polynomial approximation for g. To get the approximation for f which was given on [-1, 1], we shift the approximation for g back to the interval [-1, 1].

> gB:=unapply(%,y):

> h:=x->gB((x+1)/2):
collect(h(x),x);

(5/16*exp(-25)-5/32*exp(-25/9)+1/32*exp(-1))*x^5+(5...
(5/16*exp(-25)-5/32*exp(-25/9)+1/32*exp(-1))*x^5+(5...

> plot([h(x),f(x)],x=-1..1,color=[RED,BLACK]);

[Maple Plot]

>

We can see from this graph how close the Bernstein Polynomial of degree N fits the graph of f.

Summary

Taylor's Polynomials are better known as polynomial approximations for a function on an interval. These polynomials have the same derivatives as the function being approximated. Legendre Polynomials are polynomials that can be used to approximate a function over an interval. They are chosen to minimize the area between the polynomial approximation and the function being approximated. Bernstein Polynomials will converge uniformly to the function being approximated, if the function is continuous. While the convergence of these Bernstein Polynomials may be uniform, it may also be slow.

At least two other polynomial fits could be constructed. Choose n points on the graph of the function and determine the polynomial of degree n-1 which exactly goes through those n points. Or, choose n points on the graph of the function and find a regression polynomial of degree anything less than n determined by those points. The reader might like to construct and compare these polynomials with what has come before.