Orthogonal series and boundary value problems 

Approximating functions with polynomials

(c) Copyright 1994,1995,1996 by Evans M. Harrell, II.  All rights reserved

This is an executed Mathematica  notebook, which can be read with MathReader.  If you are reading it with Mathematica, you may find it instructive to modify the inputs and see what results.

In this notebook we take a graphical look at approximating functions.  We have learned that the r.m.s. distance between two functions defined on an interval a ≤ x ≤ b is

RMSDist[f_,g_,a_,b_]    := Sqrt[Integrate[(f - g)^2,     {x,a,b}]]

The RMS norm of a single function f could be obtained with the command
RMSDist[f,0,a,b].

RMSDist[1, Cos[Pi x/2], -1,1]

N[%]

What is the constant which is most like the cosine?  1/2?  1/4? Let's first check some values?

{N[RMSDist[1/2,Cos[Pi x/2], -1,1]],
	N[RMSDist[1/4,Cos[Pi x/2], -1,1]],
		N[RMSDist[-1,Cos[Pi x/2],  -1,1]]}

Plot[{Cos[Pi x/2], 1,1/2,-1}, {x, -1,1}]

The closest constant  to Cos[Pi x/2] would be, geometrically speaking, the projection of the sine onto the direction of the constant functions, or, as we have defined it in the notes,

Projf[f_,g_,a_,b_]     := g[x] Integrate[f[t] g[t] , {t,a,b}]          / Integrate[g[t]^2, {t,a,b}]

f[x_] := Cos[Pi x/2]
One[x_] := 1
Projf[f,One,-1,1]

N[RMSDist[2/Pi,Cos[Pi x/2], -1,1]]

Plot[{2/Pi,Cos[Pi x/2]}, {x,-1,1}]

We ought to do a better job of approximating Cos[Pi x/2] with a polynomial of 3rd or 4th degree.  You may initially think of the Taylor series at x = 0, and wish regard Sin[x] as close to 0 + x + 0 - x^3/6.  Or you might try the Taylor series at x= Pi, which is built into Mathematica in the Series command.

Series[Cos[Pi x/2], {x, 0, 3}]

Plot[{1-(Pi x)^2 /8, Cos[Pi x/2]}, {x,-1,1}]

N[RMSDist[1-(Pi x)^2 /8, Cos[Pi x/2], -1,1]]

Perhaps it is surprising to learn that there are other polynomial expressions which do a much better job of approximating a function such as the sine than does the Taylor series about any point.  They are called the Legendre polynomials

P0[x_] := LegendreP[0,x]
P1[x_] := LegendreP[1,x]
P2[x_] := LegendreP[2,x]
P3[x_] := LegendreP[3,x]

Projf[f, P0, -1,1]

Projf[f, P2, -1,1]

% + %% + %%%

N[RMSDist[%, Cos[Pi x/2],-1,1]]

Plot[{%%,Cos[Pi x/2]}, {x,-1,1}]

How are these polynomials constructed?  By convention, they are defined on -1 ≤ x ≤ 1, as has been done here.

The Legendre polynomials are just what you get by applying the Gram-Schmidt procedure to the power functions 1,x,x^2, x^3, etc., except that Legendre chose a convention which makes them orthogonal but not normalized.  The normalized Legendre polynomials can be constructed as follows:

v0[x_] := 1
v1[x_] := x
v2[x_] := x^2
v3[x_] := x^3
v4[x_] := x^4

p0[x_] = v0[x]/RMSDist[v0[x],0,-1,1]

v1[x] - Projf[v1,p0,-1,1]

p1[x_] = %/RMSDist[%,0,-1,1]

v2[x]  - Projf[v2,p0,-1,1]  - Projf[v2,p1,-1,1]

p2[x_] = %/RMSDist[%,0,-1,1]

v3[x]  - Projf[v3,p0,-1,1]      - Projf[v3,p1,-1,1]     - Projf[v3,p2,-1,1]

p3[x_] = %/RMSDist[%,0,-1,1]

v4[x] - Projf[v4,p0,-1,1]  	- Projf[v4,p1,-1,1]     - Projf[v4,p2,-1,1]     - Projf[v4,p3,-1,1]

Simplify[%]

p4[x_] = %/RMSDist[%,0,-1,1]

It may be interesting to Plot the Legendre polynomials and try to visualize their orthogonality:

Plot[{LegendreP[0,x], LegendreP[1,x], LegendreP[2,x],      LegendreP[3,x], LegendreP[4,x]}, {x,-1,1}]

What happens if the interval is not -1 ≤ x ≤ 1?  Let's examine a similar example on another 
interval.  Specifically, let's try to approximate the sin(x) on the interval 0 ≤ x ≤ Pi.  (The following calculations have not been performed ahead of time.  If you carry them out, remember to define Projf, etc as above by going to the appropriate cell and pressing enter; otherwise Mathematica  won't know what these commands mean.)

The closest constant  to Sin[x] would be, geometrically speaking, the projection of the sine onto the direction of the constant functions:

f[x_] := Sin[x]
One[x_] := 1
Projf[f,One,0, Pi]

N[RMSDist[2/Pi,Sin[x], 0, Pi]]

Plot[{2/Pi,Sin[x]}, {x,0, Pi}]

We ought to do a better job of approximating Sin[x] with a polynomial of 3rd or 4th degree.  You may initially think of the Taylor series at x = 0, and wish regard Sin[x] as close to 0 + x + 0 - x^3/6.  Or you might try the Taylor series at x= Pi,

{Series[Sin[x], {x, 0, 3}], Series[Sin[x], {x, Pi, 3}]}

Plot[{x-x^3/6, -(-Pi+x) + (-Pi+x)^3/6, Sin[x]}, {x,0,Pi}]

N[RMSDist[x-x^3/6,Sin[x], 0, Pi]]

Series[Sin[x], {x, Pi/2, 3}]

Plot[{1 - (-Pi/2 + x)^2/2, Sin[x]}, {x,0,Pi}]

N[RMSDist[1 - (-Pi/2 + x)^2/2, Sin[x],0,Pi]]

Perhaps it is surprising to learn that there are other polynomial expressions which do a much better job of approximating a function such as the sine than does the Taylor series about any point.  Let

P0[x_] = LegendreP[0,2 (x-Pi/2)/Pi]
P1[x_] = LegendreP[1,2 (x-Pi/2)/Pi]
P2[x_] = LegendreP[2,2 (x-Pi/2)/Pi]
P3[x_] = LegendreP[3,2 (x-Pi/2)/Pi]
P4[x_] = LegendreP[4,2 (x-Pi/2)/Pi]

Projf[f, P0,0,Pi]

Projf[f, P1,0,Pi]

Projf[f, P2,0,Pi]

% + %% + %%%

N[RMSDist[%, Sin[x],0,Pi]]

Plot[{%%,Sin[x]}, {x,0,Pi}]

Above, the input variable was written as 2 (x-Pi/2)/Pi in order to shift this interval to 
0 ≤ x ≤ Pi.  

Study question.  What is the general rule to shift to an interval a ≤ x ≤ b?