Linear Methods of Applied Mathematics

Projections of vectors and functions

(c) Copyright 1994-1997 by Evans M. Harrell II and James V. Herod. 
     All rights reserved.

Notes for the instructor.

This contains calculations and examples which correlate with chapter 2 of the WWW text by Harrell and Herod.

Students can be encouraged to cut and paste from this notebook to do homework.

Instructions

This notebook uses Mathematica  to perform calculations for Harrell and Herod's hypertext book, Linear Methods of Applied Mathematics.   The student needs only a basic knowledge of Mathematica  to use the notebook, which is designed both to show how to work problems in the text and to provide a template for using Mathematica  to work other problems of the student's own design.  

Calculations will be performed when the reader presses enter in a given calculation cell (bold print).  It is best to activate the cells in order, so that Mathematica  will be able to call on operators and functions defined in earlier cells.  Red color coding is used to warn the reader when a given calculation relies on an earlier one.

Projections in ordinary 3-D space

Let us begin by defining the projection of the vector v into the direction of w, for ordinary 3-D vectors v,w.

Proj[v_,w_] := w (v.w)/(w.w)

Notice that the projection has the direction of w but the length given by |v| cos(q), where q is the angle between v and w. 

You may wish to try out the command Proj for various pairs of vectors before proceeding.

We do not always wish to project a vector onto another vector.  For instance, we may wish to find the point in a plane which is closest to a given vector.  Let us assume that a plane is specified by two orthogonal vectors (the orthogonality is important), such as:

P1 := {{1,1,1},{1,-1,0}}

The projection onto the plane is the sum of the projections onto the two orthogonal basis vectors:

Projp[v_,P_] := Proj[v,P[[1]]] + Proj[v,P[[2]]]

For example, as in the text:

Projp[{3,4,5},P1]

Model Problem II.8.  

We wish to find 
     a) the vector in the plane spanned by {1,1,1} and {1,2,3}, 
     which is the closest to v ={3, 4, 5}; and 
     b) the vector in the same plane closest to {1,-1,0}.

Since the vectors spanning the plane are not given as orthogonal, we replace them with another pair:

v1 := {1,1,1}

Solve[({1,2,3} - alpha {1,1,1}).{1,1,1} == 0, alpha]

v2 = {1,2,3} - alpha {1,1,1} /. %

P2 := {{1,1,1},{-1,0,1}}

Projp[{3,4,5},P2]

Explanation:  This vector is already in the plane, so the projection to the plane has no effect.  Not so for the second vector:

Projp[{1,-1,0},P2]

Now that it is in the plane, however, another projection won't move it:

Projp[{1/2,0,-1/2},P2]

Projections in function space

The following calculations do similar things in function space.  The point here is to replace our original function with some other, convenient function, which is as close as possible to it. 

What do we mean by "close"?  The notion of distance is given by the norm of a function:  ||f - g||, which you may recognize from your courses on laboratory statistics as the r.m.s. deviation between f and g.  The best approximation of a function by a nicer function will mean the specific nicer function with the smallest r.m.s. deviation.

Let us begin by exploring this notion of distance:

The distance between functions

We have learned that the r.m.s. distance between two real-valued functions defined on an interval a ≤ x ≤ b is:

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

Warning!  This formula does not work for complex-valued functions.  The RMS norm of a single function f could be obtained with the command
												RMSDist[f,0,{x,a,b}].

RMSNorm[f_,{x_,a_,b_}] := RMSDist[f,0,{x,a,b}]

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], {x,-1,1}]],
	N[RMSDist[1/4,Cos[Pi x/2], {x,-1,1}]],
		N[RMSDist[-1,Cos[Pi x/2], {x,-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.  Intuitively, this should be the average of the function.  As we learned in the text, in order to calculate the best approximation to a function, we perform a projection in function space, using virtually the same formula we use to project a vector onto another vector or onto a plane:

Projf[f_,g_, {x_,a_,b_}] := g Integrate[(f g /. x -> intvar), {intvar,a,b}]                              / Integrate[(g^2 /. x -> intvar), {intvar,a,b}]

Explanation of the syntax

•  The syntax is analogous to the Mathematica  command Integrate.  You specify two functions (with the independent variables explicitly written), the variable on which they depend, and the range of that variable.  That is, a < x < b.  
•  In the definition of the command the integration variable was named intvar because that is an unlikely variable to arise elsewhere in the formula.  With this definition, the projected function and the original functions will depend on the same independent variable, which can be called anything (other than intvar in rare cases or reserved Mathematica  words).

Indeed, we find that the projection of a function f[x] onto the direction of the constant function 1 is the average.  For our example,

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

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

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

Some more examples of projection of functions

The projection of x onto the direction of x2:

Projf[x, x^2, {x, 0, 1}]

Plot[{x, 5 x^2/4}, {x, 0, 1}]

The projection in the other direction.  This time let's call the independent variable y:

Projf[y^2, y, {y, 0, 1}]

Plot[{y^2, 3 y/4}, {y, 0, 1}]

Notice that the coefficients are different when we project in different directions between the two functions.  

A question to ponder:

What would happen if we projected x into the cirection of x2, then projected x2 back in the direction ox x, then repeated the process many times?

Model Problem II.9

Model Problem II.9.  Consider the set of functions on the interval -1 ≤ x ≤ 1.  We wish to find the function in the span of 1, x, and x2, which is the closest to f(x) =  cos(πx/2).  In other words, replace the cosine by a simple quadratic.

Solution

Solution.  First let us ask whether the three functions are orthogonal.  Actually, no.  The function x is orthogonal to each of the other two, but 1 and x2 are not orthogonal.

We should replace x2 by x2 minus its projection onto the direction of 1, that is, x2 minus its average, which is easily found to be 1/3.  Then we can project cos(πx/2) onto the span of the three functions 1, x, and x2 - 1/3:

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

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

(This is also obvious without a calculation, since the cosine is even and x is odd.)

Projf[Cos[Pi x/2], x^2 - 1/3, {x, -1,1}]

The best quadratic approximation to a function on this interval would be:

BestApprox[f_] := Projf[f,1, {x, -1,1}] +                   Projf[f,x, {x, -1,1}] +                   Projf[f,x^2 - 1/3, {x, -1,1}]

We have the sum of three projections because there are three orthogonal functions in the set we are using to approximate the function f.  Otherwise, the formula is just like the one for the plane.

BestApprox[Cos[Pi x/2]]

Simplify[%]

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

Let's compare with the Taylor approximation,

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

SeriesData[x, 0, {1, 0, -Pi^2/8}, 0, 3, 1]

Plot[{2/Pi + (45*((-2*(24 - 2*Pi^2))/(3*Pi^3) + 
 
       (2*(-24 + 2*Pi^2))/(3*Pi^3))*(-1/3 + x^2))/8,        Cos[Pi x/2],        1 - Pi^2 x^2 /8}, {x, -1,1}]

A Variant of Model Problem II.9

Model Problem II.9'.  Consider the set of functions on the interval 0 ≤ x ≤ π.  We wish to find the function in the span of 1, sin(2x) and sin(3x), which is the closest to f(x) =  x2.  In other words, find the function of the form 
	a + b sin(2x) + c sin(3x)
which is closest in the r.m.s. sense to  x2.

Solution

Solution.  First let us ask whether the three functions are orthogonal.  Actually, no.  The function sin(2 x) is orthogonal to each of the other two, but 1 and sin(3x) are not orthogonal.

We should replace sin(3x) by sin(3 x) minus its projection onto the direction of 1, that is, sin(3 x) minus its average.

f3[x_] = Sin[3 x] - Projf[Sin[3 x],1,{x,0,Pi}]

BestApprox[f_] := Projf[f,1,{x,0,Pi}] +                   Projf[f,Sin[2 x],{x,0,Pi}] +                   Projf[f,f3[x],0,Pi]

We have the sum of three projections because there are three orthogonal functions in the set we are using to approximate the function f.  Otherwise, the formula is just like the one for the plane.

BestApprox[x^2]

Simplify[%]

Plot[{%,x^2},{x,0,Pi}]

It is not a really close match (wait until the next section for something more impressive).  On the other hand, there is definitely a correlation between the two graphs.

The best polynomial (Legendre series)

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]

f[x_] := Cos[Pi x/2]

Projf[f[x], P0[x], {x,-1,1}]

Projf[f[x], P1[x], {x,-1,1}]

Projf[f[x], P2[x], {x,-1,1}]

% + %% + %%%

N[RMSDist[%, f[x],{x,-1,1}]]

Plot[{%%, f[x]}, {x,-1,1}]

How are these polynomials constructed?  By convention, they are defined on -1 ≤ x ≤ 1, as has been done here.  Essentially, we begin with the usual power functions and recombine them to find an orthogonal basis for function space.  The procedure for doing this is due to Gram and Schmidt, and is based on a construction for ordinary vectors.

Orthogonalization and the Gram-Schmidt method

Orthogonalization in ordinary 3-D or n-D space

If we have a list of orthonormal vectors v1, ...,vn, and we are given a new v, we can get Mathematica  to orthogonalize the new vector by projecting away the components along v1, ...,vn.  The inputs for the orthogonalization are a vector v, which is represented in Mathematica as a list {.....}, and a set of vectors, which is a list of lists.  Here is one way to do it:

Orth[v_,VecList_] := v - Sum[VecList[[k]] VecList[[k]].v, 
{k,1,Dimensions[VecList][[1]]}]

Explanation of the syntax

•  If there are n vectors in the list, the command 
                            Dimensions[VecList][[1]]  
gives the number n, while 
            Dimensions[VecList][[2]] 
would give the number of entries in each vector.  A list of lists is a matrix, and the 
command    Dimensions       tells the size of the matrix, 
{number of rows, number of columns}.  We need this because we want the command to 
work for any size list of vectors v1, ...,vn.

Warning:  To use the command Orth properly, it is neccessary for the vectors in VecList to be orthonormal - orthogonal and normalized.

Examples

Orth[{1,1,1},{{1,0,0},{0,1,0}}]

Orth[{1,1,0},{{1,0,0}}]

Orth[{1,1,1},{{1,0,0},{0,1,0}}]

w1 := (1/Sqrt[3]){1,1,1}

Orth[{1,1,0},{w1}]

If we wish the result of this to be orthonormalized, we need to scale its length:

w2 = %/Sqrt[%.%]

Orth[{1,0,0},{w1,w2}]

w3 = %/Sqrt[%.%]

Orthogonalization in function space and construction of the Legendre polynomials

We illustrate the procedure to find a set of orthogonal polynomials, which we might use to approximate a complicated function representing realistic data as an idealized polynomial with the smallest loss of accuracy.    The set we get is the Legendre polynomials.

The Legendre polynomials are thus just what you get by applying the Gram-Schmidt procedure to the power functions 1,x,x2, x3, 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]/RMSNorm[v0[x],{x-1,1}]

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

p1[x_] = %/RMSNorm[%,{x-1,1}]

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

p2[x_] = %/RMSNorm[%,{x-1,1}]

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

p3[x_] = %/RMSNorm[%,{x-1,1}]

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

Simplify[%]

p4[x_] = %/RMSNorm[%,{x-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 ≤ π.

One possibility would be to construct a set of orthogonal polynomials on this new interval by beginning with the functions 1, x, x2, x3, etc., and then applying the Gram-Schmidt procedure to get a set of orthogonal polynomials.  Because the interval is different, these will definitely be different from the Legendre polynomials we found above - orthogonality depends on the interval as much as it depends on the function.  In particular, the new polynomials won't be nicely even or odd like the ones given above, because the interval is not balanced around 0.

Here is a shortcut, however.  Let us change the variable x in a simple, linear way in order to shift the interval [0, π] to the one we already understand.  Our new polynomials will be:

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]

It is not hard to check directly that these shifted Legendre polynomials are orthogonal.

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