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

**Solution** The (real) inner product and norm are

< f, g > = ∫_{ −1}^{1} f(x)g(x)dx and
||f|| = (∫_{ −1}^{1}
f(x)^{2}dx)^{½}.

The set of orthonormal Legendre polynomials of
degree 0, 1, and 2 are

p_{0}(x) = 2^{-1/2}, p_{1}(x) =
(3/2)^{1/2}x, and p_{2}(x)=
(5/8)^{1/2}(3x^{2}-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, p_{0}> p_{0}(x) + < f,
p_{1}> p_{1}(x) + < f, p_{2}>
p_{2}(x).

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

< f, p_{0}> = 8^{-1/2}(e^{2} -
e^{-2})

< f, p_{1}> = (3/32)^{1/2}(e^{2} +
3e^{-2})

< f, p_{2}> = (5/128)^{1/2}(e^{2} -
13e^{-2})

The orthogonal projection, p(x), which is called the *quadratic
least-squares fit* for f(x), is

p(x) = (1/4)(e^{2} - e^{-2}) + (3/8)(e^{2} +
3e^{-2})x + (5/32)(e^{2} -
13e^{-2})(3x^{2}-1).

We now want to use Matlab to create the required plot. Plotting
e^{2x} is easy. To plot the polynomials, we will do the
following steps.

- 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];`

- 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)); - Form the coefficient vector
`c`for p(x). This contains the coefficients of p(x) relative to the basis {x^{2}}

`c = a0*c0 + a1*c1 + a2*c2;` - Select the values of x to be used in the plot. Find the
corresponding values p(x) and e
^{2x}. Plot these.

`x = linspace(-1,1,300);`

yp = polyval(c,x);

yf = exp(2*x);

plot(x,yf,x,yp,'r')