## Orthogonal Projections/Least Squares

Orthogonal projection problem. Use the orthonormal set of Legendre polynomials given below to obtain the orthogonal projection of the function f(x) = e2x onto the span of these polynomials. Plot both the function and the (quadratic) projection.

Solution The (real) inner product and norm are
< f, g > = ∫ −11 f(x)g(x)dx and ||f|| = (∫ −11 f(x)2dx)½.
The set of orthonormal Legendre polynomials of degree 0, 1, and 2 are

p0(x) = 2-1/2,   p1(x) = (3/2)1/2x,   and   p2(x)= (5/8)1/2(3x2-1).

The projection p, which is a quadratic, is found by applying Theorem 0.18 in the text; it has the form

p(x) = < f, p0> p0(x) + < f, p1> p1(x) + < f, p2> p2(x).

The inner products are all integrals of products of polynomials and exponentials; doing them results in these values:

< f, p0> = 8-1/2(e2 - e-2)
< f, p1> = (3/32)1/2(e2 + 3e-2)
< f, p2> = (5/128)1/2(e2 - 13e-2)

The orthogonal projection, p(x), which is called the quadratic least-squares fit for f(x), is
p(x) = (1/4)(e2 - e-2) + (3/8)(e2 + 3e-2)x + (5/32)(e2 - 13e-2)(3x2-1).

We now want to use Matlab to create the required plot. Plotting e2x is easy. To plot the polynomials, we will do the following steps.

1. Put the coefficients of each Legendre polynomial into a row vector, in descending order.
c0 = [0 0 1]; c1= [0 1 0]; c2 = [3 0 -1];
2. Calculate the coefficients for the Legendre polynomials in the polynomial p(x).
a0 = (1/4)*(exp(2) - exp(-2));
a1 = (3/8)*(exp(2) + 3*exp(-2));
a2 = (5/32)*(exp(2) - 13*exp(-2));
3. Form the coefficient vector c for p(x). This contains the coefficients of p(x) relative to the basis {x2}
c = a0*c0 + a1*c1 + a2*c2;
4. Select the values of x to be used in the plot. Find the corresponding values p(x) and e2x. Plot these.
x = linspace(-1,1,300);
yp = polyval(c,x);
yf = exp(2*x);
plot(x,yf,x,yp,'r')
The title, legend, and so on can be put in using the tools in the figure window. The resulting plot is shown below.